linear 1.7 → 1.8
raw patch · 9 files changed
+243/−1 lines, 9 files
Files
- CHANGELOG.markdown +4/−0
- linear.cabal +1/−1
- src/Linear/Plucker.hs +45/−0
- src/Linear/Quaternion.hs +40/−0
- src/Linear/V0.hs +22/−0
- src/Linear/V1.hs +24/−0
- src/Linear/V2.hs +33/−0
- src/Linear/V3.hs +36/−0
- src/Linear/V4.hs +38/−0
CHANGELOG.markdown view
@@ -1,3 +1,7 @@+1.8+---+* Added missing `Unbox` instances for working with unboxed vectors of `linear` data types.+ 1.7 --- * Fixed `axisAngle`
linear.cabal view
@@ -1,6 +1,6 @@ name: linear category: Math, Algebra-version: 1.7+version: 1.8 license: BSD3 cabal-version: >= 1.8 license-file: LICENSE
src/Linear/Plucker.hs view
@@ -45,6 +45,7 @@ ) where import Control.Applicative+import Control.Monad (liftM) import Control.Lens hiding (index, (<.>)) import Data.Distributive import Data.Foldable as Foldable@@ -61,6 +62,9 @@ #if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ >= 706 import GHC.Generics (Generic1) #endif+import qualified Data.Vector.Generic.Mutable as M+import qualified Data.Vector.Generic as G+import qualified Data.Vector.Unboxed.Base as U import Linear.Epsilon import Linear.Metric@@ -475,3 +479,44 @@ -- TODO: drag some more stuff out of my thesis +data instance U.Vector (Plucker a) = V_Plucker !Int (U.Vector a)+data instance U.MVector s (Plucker a) = MV_Plucker !Int (U.MVector s a)+instance U.Unbox a => U.Unbox (Plucker a)++instance U.Unbox a => M.MVector U.MVector (Plucker a) where+ basicLength (MV_Plucker n _) = n+ basicUnsafeSlice m n (MV_Plucker _ v) = MV_Plucker n (M.basicUnsafeSlice (6*m) (6*n) v)+ basicOverlaps (MV_Plucker _ v) (MV_Plucker _ u) = M.basicOverlaps v u+ basicUnsafeNew n = liftM (MV_Plucker n) (M.basicUnsafeNew (6*n))+ basicUnsafeRead (MV_Plucker _ a) i =+ do let o = 6*i+ x <- M.basicUnsafeRead a o+ y <- M.basicUnsafeRead a (o+1)+ z <- M.basicUnsafeRead a (o+2)+ w <- M.basicUnsafeRead a (o+3)+ v <- M.basicUnsafeRead a (o+4)+ u <- M.basicUnsafeRead a (o+5)+ return (Plucker x y z w v u)+ basicUnsafeWrite (MV_Plucker _ a) i (Plucker x y z w v u) =+ do let o = 6*i+ M.basicUnsafeWrite a o x+ M.basicUnsafeWrite a (o+1) y+ M.basicUnsafeWrite a (o+2) z+ M.basicUnsafeWrite a (o+3) w+ M.basicUnsafeWrite a (o+4) v+ M.basicUnsafeWrite a (o+5) u++instance U.Unbox a => G.Vector U.Vector (Plucker a) where+ basicUnsafeFreeze (MV_Plucker n v) = liftM ( V_Plucker n) (G.basicUnsafeFreeze v)+ basicUnsafeThaw ( V_Plucker n v) = liftM (MV_Plucker n) (G.basicUnsafeThaw v)+ basicLength ( V_Plucker n _) = n+ basicUnsafeSlice m n (V_Plucker _ v) = V_Plucker n (G.basicUnsafeSlice (6*m) (6*n) v)+ basicUnsafeIndexM (V_Plucker _ a) i =+ do let o = 6*i+ x <- G.basicUnsafeIndexM a o+ y <- G.basicUnsafeIndexM a (o+1)+ z <- G.basicUnsafeIndexM a (o+2)+ w <- G.basicUnsafeIndexM a (o+3)+ v <- G.basicUnsafeIndexM a (o+4)+ u <- G.basicUnsafeIndexM a (o+5)+ return (Plucker x y z w v u)
src/Linear/Quaternion.hs view
@@ -39,6 +39,7 @@ ) where import Control.Applicative+import Control.Monad (liftM) import Control.Lens hiding ((<.>)) import Data.Complex (Complex((:+))) import Data.Data@@ -57,6 +58,9 @@ #if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ >= 706 import GHC.Generics (Generic1) #endif+import qualified Data.Vector.Generic.Mutable as M+import qualified Data.Vector.Generic as G+import qualified Data.Vector.Unboxed.Base as U import Linear.Epsilon import Linear.Conjugate import Linear.Metric@@ -493,3 +497,39 @@ axisAngle axis theta = Quaternion (cos half) (sin half *^ normalize axis) where half = theta / 2 {-# INLINE axisAngle #-}++data instance U.Vector (Quaternion a) = V_Quaternion !Int (U.Vector a)+data instance U.MVector s (Quaternion a) = MV_Quaternion !Int (U.MVector s a)+instance U.Unbox a => U.Unbox (Quaternion a)++instance U.Unbox a => M.MVector U.MVector (Quaternion a) where+ basicLength (MV_Quaternion n _) = n+ basicUnsafeSlice m n (MV_Quaternion _ v) = MV_Quaternion n (M.basicUnsafeSlice (4*m) (4*n) v)+ basicOverlaps (MV_Quaternion _ v) (MV_Quaternion _ u) = M.basicOverlaps v u+ basicUnsafeNew n = liftM (MV_Quaternion n) (M.basicUnsafeNew (4*n))+ basicUnsafeRead (MV_Quaternion _ v) i =+ do let o = 4*i+ x <- M.basicUnsafeRead v o+ y <- M.basicUnsafeRead v (o+1)+ z <- M.basicUnsafeRead v (o+2)+ w <- M.basicUnsafeRead v (o+3)+ return (Quaternion x (V3 y z w))+ basicUnsafeWrite (MV_Quaternion _ v) i (Quaternion x (V3 y z w)) =+ do let o = 4*i+ M.basicUnsafeWrite v o x+ M.basicUnsafeWrite v (o+1) y+ M.basicUnsafeWrite v (o+2) z+ M.basicUnsafeWrite v (o+3) w++instance U.Unbox a => G.Vector U.Vector (Quaternion a) where+ basicUnsafeFreeze (MV_Quaternion n v) = liftM ( V_Quaternion n) (G.basicUnsafeFreeze v)+ basicUnsafeThaw ( V_Quaternion n v) = liftM (MV_Quaternion n) (G.basicUnsafeThaw v)+ basicLength ( V_Quaternion n _) = n+ basicUnsafeSlice m n (V_Quaternion _ v) = V_Quaternion n (G.basicUnsafeSlice (4*m) (4*n) v)+ basicUnsafeIndexM (V_Quaternion _ v) i =+ do let o = 4*i+ x <- G.basicUnsafeIndexM v o+ y <- G.basicUnsafeIndexM v (o+1)+ z <- G.basicUnsafeIndexM v (o+2)+ w <- G.basicUnsafeIndexM v (o+3)+ return (Quaternion x (V3 y z w))
src/Linear/V0.hs view
@@ -42,6 +42,9 @@ #if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ >= 706 import GHC.Generics (Generic1) #endif+import qualified Data.Vector.Generic.Mutable as M+import qualified Data.Vector.Generic as G+import qualified Data.Vector.Unboxed.Base as U import Linear.Metric import Linear.Epsilon import Linear.Vector@@ -186,3 +189,22 @@ ix i f = el i (indexed f i) {-# INLINE ix #-} #endif++newtype instance U.Vector (V0 a) = V_V0 Int+newtype instance U.MVector s (V0 a) = MV_V0 Int+instance U.Unbox (V0 a)++instance M.MVector U.MVector (V0 a) where+ basicLength (MV_V0 n) = n+ basicUnsafeSlice _ n _ = MV_V0 n+ basicOverlaps _ _ = False+ basicUnsafeNew n = return (MV_V0 n)+ basicUnsafeRead _ _ = return V0+ basicUnsafeWrite _ _ _ = return ()++instance G.Vector U.Vector (V0 a) where+ basicUnsafeFreeze (MV_V0 n) = return (V_V0 n)+ basicUnsafeThaw (V_V0 n) = return (MV_V0 n)+ basicLength (V_V0 n) = n+ basicUnsafeSlice _ n _ = V_V0 n+ basicUnsafeIndexM _ _ = return V0
src/Linear/V1.hs view
@@ -31,6 +31,7 @@ ) where import Control.Applicative+import Control.Monad (liftM) import Control.Lens import Data.Data import Data.Distributive@@ -51,6 +52,10 @@ import Linear.Vector import Prelude hiding (sum) +import qualified Data.Vector.Generic.Mutable as M+import qualified Data.Vector.Generic as G+import qualified Data.Vector.Unboxed.Base as U+ -- $setup -- >>> import Control.Lens @@ -215,3 +220,22 @@ ix i f = el i (indexed f i) {-# INLINE ix #-} #endif++newtype instance U.Vector (V1 a) = V_V1 (U.Vector a)+newtype instance U.MVector s (V1 a) = MV_V1 (U.MVector s a)+instance U.Unbox a => U.Unbox (V1 a)++instance U.Unbox a => M.MVector U.MVector (V1 a) where+ basicLength (MV_V1 v) = M.basicLength v+ basicUnsafeSlice m n (MV_V1 v) = MV_V1 (M.basicUnsafeSlice m n v)+ basicOverlaps (MV_V1 v) (MV_V1 u) = M.basicOverlaps v u+ basicUnsafeNew n = liftM MV_V1 (M.basicUnsafeNew n)+ basicUnsafeRead (MV_V1 v) i = liftM V1 (M.basicUnsafeRead v i)+ basicUnsafeWrite (MV_V1 v) i (V1 x) = M.basicUnsafeWrite v i x++instance U.Unbox a => G.Vector U.Vector (V1 a) where+ basicUnsafeFreeze (MV_V1 v) = liftM V_V1 (G.basicUnsafeFreeze v)+ basicUnsafeThaw (V_V1 v) = liftM MV_V1 (G.basicUnsafeThaw v)+ basicLength (V_V1 v) = G.basicLength v+ basicUnsafeSlice m n (V_V1 v) = V_V1 (G.basicUnsafeSlice m n v)+ basicUnsafeIndexM (V_V1 v) i = liftM V1 (G.basicUnsafeIndexM v i)
src/Linear/V2.hs view
@@ -29,6 +29,7 @@ ) where import Control.Applicative+import Control.Monad (liftM) import Control.Lens hiding ((<.>)) import Data.Data import Data.Distributive@@ -46,6 +47,9 @@ #if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ >= 706 import GHC.Generics (Generic1) #endif+import qualified Data.Vector.Generic.Mutable as M+import qualified Data.Vector.Generic as G+import qualified Data.Vector.Unboxed.Base as U import Linear.Metric import Linear.Epsilon import Linear.Vector@@ -270,3 +274,32 @@ {-# INLINE ix #-} #endif +data instance U.Vector (V2 a) = V_V2 !Int (U.Vector a)+data instance U.MVector s (V2 a) = MV_V2 !Int (U.MVector s a)+instance U.Unbox a => U.Unbox (V2 a)++instance U.Unbox a => M.MVector U.MVector (V2 a) where+ basicLength (MV_V2 n _) = n+ basicUnsafeSlice m n (MV_V2 _ v) = MV_V2 n (M.basicUnsafeSlice (2*m) (2*n) v)+ basicOverlaps (MV_V2 _ v) (MV_V2 _ u) = M.basicOverlaps v u+ basicUnsafeNew n = liftM (MV_V2 n) (M.basicUnsafeNew (2*n))+ basicUnsafeRead (MV_V2 _ v) i =+ do let o = 2*i+ x <- M.basicUnsafeRead v o+ y <- M.basicUnsafeRead v (o+1)+ return (V2 x y)+ basicUnsafeWrite (MV_V2 _ v) i (V2 x y) =+ do let o = 2*i+ M.basicUnsafeWrite v o x+ M.basicUnsafeWrite v (o+1) y++instance U.Unbox a => G.Vector U.Vector (V2 a) where+ basicUnsafeFreeze (MV_V2 n v) = liftM ( V_V2 n) (G.basicUnsafeFreeze v)+ basicUnsafeThaw ( V_V2 n v) = liftM (MV_V2 n) (G.basicUnsafeThaw v)+ basicLength ( V_V2 n _) = n+ basicUnsafeSlice m n (V_V2 _ v) = V_V2 n (G.basicUnsafeSlice (2*m) (2*n) v)+ basicUnsafeIndexM (V_V2 _ v) i =+ do let o = 2*i+ x <- G.basicUnsafeIndexM v o+ y <- G.basicUnsafeIndexM v (o+1)+ return (V2 x y)
src/Linear/V3.hs view
@@ -29,6 +29,7 @@ ) where import Control.Applicative+import Control.Monad (liftM) import Control.Lens hiding ((<.>)) import Data.Data import Data.Distributive@@ -46,6 +47,9 @@ #if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ >= 706 import GHC.Generics (Generic1) #endif+import qualified Data.Vector.Generic.Mutable as M+import qualified Data.Vector.Generic as G+import qualified Data.Vector.Unboxed.Base as U import Linear.Epsilon import Linear.Metric import Linear.V2@@ -263,3 +267,35 @@ ix i f = el i (indexed f i) #endif +data instance U.Vector (V3 a) = V_V3 !Int (U.Vector a)+data instance U.MVector s (V3 a) = MV_V3 !Int (U.MVector s a)+instance U.Unbox a => U.Unbox (V3 a)++instance U.Unbox a => M.MVector U.MVector (V3 a) where+ basicLength (MV_V3 n _) = n+ basicUnsafeSlice m n (MV_V3 _ v) = MV_V3 n (M.basicUnsafeSlice (3*m) (3*n) v)+ basicOverlaps (MV_V3 _ v) (MV_V3 _ u) = M.basicOverlaps v u+ basicUnsafeNew n = liftM (MV_V3 n) (M.basicUnsafeNew (3*n))+ basicUnsafeRead (MV_V3 _ v) i =+ do let o = 3*i+ x <- M.basicUnsafeRead v o+ y <- M.basicUnsafeRead v (o+1)+ z <- M.basicUnsafeRead v (o+2)+ return (V3 x y z)+ basicUnsafeWrite (MV_V3 _ v) i (V3 x y z) =+ do let o = 3*i+ M.basicUnsafeWrite v o x+ M.basicUnsafeWrite v (o+1) y+ M.basicUnsafeWrite v (o+2) z++instance U.Unbox a => G.Vector U.Vector (V3 a) where+ basicUnsafeFreeze (MV_V3 n v) = liftM ( V_V3 n) (G.basicUnsafeFreeze v)+ basicUnsafeThaw ( V_V3 n v) = liftM (MV_V3 n) (G.basicUnsafeThaw v)+ basicLength ( V_V3 n _) = n+ basicUnsafeSlice m n (V_V3 _ v) = V_V3 n (G.basicUnsafeSlice (3*m) (3*n) v)+ basicUnsafeIndexM (V_V3 _ v) i =+ do let o = 3*i+ x <- G.basicUnsafeIndexM v o+ y <- G.basicUnsafeIndexM v (o+1)+ z <- G.basicUnsafeIndexM v (o+2)+ return (V3 x y z)
src/Linear/V4.hs view
@@ -30,6 +30,7 @@ ) where import Control.Applicative+import Control.Monad (liftM) import Control.Lens hiding ((<.>)) import Data.Data import Data.Distributive@@ -47,6 +48,9 @@ #if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ >= 706 import GHC.Generics (Generic1) #endif+import qualified Data.Vector.Generic.Mutable as M+import qualified Data.Vector.Generic as G+import qualified Data.Vector.Unboxed.Base as U import Linear.Epsilon import Linear.Metric import Linear.V2@@ -288,4 +292,38 @@ ix i f = el i (indexed f i) #endif +data instance U.Vector (V4 a) = V_V4 !Int (U.Vector a)+data instance U.MVector s (V4 a) = MV_V4 !Int (U.MVector s a)+instance U.Unbox a => U.Unbox (V4 a) +instance U.Unbox a => M.MVector U.MVector (V4 a) where+ basicLength (MV_V4 n _) = n+ basicUnsafeSlice m n (MV_V4 _ v) = MV_V4 n (M.basicUnsafeSlice (4*m) (4*n) v)+ basicOverlaps (MV_V4 _ v) (MV_V4 _ u) = M.basicOverlaps v u+ basicUnsafeNew n = liftM (MV_V4 n) (M.basicUnsafeNew (4*n))+ basicUnsafeRead (MV_V4 _ v) i =+ do let o = 4*i+ x <- M.basicUnsafeRead v o+ y <- M.basicUnsafeRead v (o+1)+ z <- M.basicUnsafeRead v (o+2)+ w <- M.basicUnsafeRead v (o+3)+ return (V4 x y z w)+ basicUnsafeWrite (MV_V4 _ v) i (V4 x y z w) =+ do let o = 4*i+ M.basicUnsafeWrite v o x+ M.basicUnsafeWrite v (o+1) y+ M.basicUnsafeWrite v (o+2) z+ M.basicUnsafeWrite v (o+3) w++instance U.Unbox a => G.Vector U.Vector (V4 a) where+ basicUnsafeFreeze (MV_V4 n v) = liftM ( V_V4 n) (G.basicUnsafeFreeze v)+ basicUnsafeThaw ( V_V4 n v) = liftM (MV_V4 n) (G.basicUnsafeThaw v)+ basicLength ( V_V4 n _) = n+ basicUnsafeSlice m n (V_V4 _ v) = V_V4 n (G.basicUnsafeSlice (4*m) (4*n) v)+ basicUnsafeIndexM (V_V4 _ v) i =+ do let o = 4*i+ x <- G.basicUnsafeIndexM v o+ y <- G.basicUnsafeIndexM v (o+1)+ z <- G.basicUnsafeIndexM v (o+2)+ w <- G.basicUnsafeIndexM v (o+3)+ return (V4 x y z w)