|
| 1 | +{-# LANGUAGE ScopedTypeVariables #-} |
| 2 | +{-# LANGUAGE GeneralizedNewtypeDeriving #-} |
| 3 | + |
| 4 | +-- 2-D, 3-D and 4-D Vectors. |
| 5 | +-- The interface is compatible with that of the 'linear' package. |
| 6 | +module SDL.Internal.Vect |
| 7 | + ( V2(..) |
| 8 | + , V3(..) |
| 9 | + , V4(..) |
| 10 | + , Point(..) |
| 11 | + ) where |
| 12 | + |
| 13 | +-- From the 'linear' package, (c) Edward Kmett. |
| 14 | + |
| 15 | +import Control.Applicative |
| 16 | +import Foreign.Storable |
| 17 | +import Foreign.Ptr (castPtr) |
| 18 | + |
| 19 | +-- | A handy wrapper to help distinguish points from vectors at the |
| 20 | +-- type level. |
| 21 | +newtype Point f a = P (f a) |
| 22 | + deriving (Show, Read, Ord, Eq, Functor, Applicative, Num, Storable) |
| 23 | + |
| 24 | +-- | A 2-dimensional vector |
| 25 | +-- |
| 26 | +-- >>> pure 1 :: V2 Int |
| 27 | +-- V2 1 1 |
| 28 | +-- |
| 29 | +-- >>> V2 1 2 + V2 3 4 |
| 30 | +-- V2 4 6 |
| 31 | +-- |
| 32 | +-- >>> V2 1 2 * V2 3 4 |
| 33 | +-- V2 3 8 |
| 34 | +-- |
| 35 | +-- >>> sum (V2 1 2) |
| 36 | +-- 3 |
| 37 | +data V2 a = V2 !a !a |
| 38 | + deriving (Show, Read, Ord, Eq) |
| 39 | + |
| 40 | +-- | A 3-dimensional vector |
| 41 | +data V3 a = V3 !a !a !a |
| 42 | + deriving (Show, Read, Ord, Eq) |
| 43 | + |
| 44 | +-- | A 4-dimensional vector |
| 45 | +data V4 a = V4 !a !a !a !a |
| 46 | + deriving (Show, Read, Ord, Eq) |
| 47 | + |
| 48 | +instance Functor V2 where |
| 49 | + fmap f (V2 a b) = V2 (f a) (f b) |
| 50 | + {-# INLINE fmap #-} |
| 51 | + a <$ _ = V2 a a |
| 52 | + {-# INLINE (<$) #-} |
| 53 | + |
| 54 | +instance Functor V3 where |
| 55 | + fmap f (V3 a b c) = V3 (f a) (f b) (f c) |
| 56 | + {-# INLINE fmap #-} |
| 57 | + a <$ _ = V3 a a a |
| 58 | + {-# INLINE (<$) #-} |
| 59 | + |
| 60 | +instance Functor V4 where |
| 61 | + fmap f (V4 a b c d) = V4 (f a) (f b) (f c) (f d) |
| 62 | + {-# INLINE fmap #-} |
| 63 | + a <$ _ = V4 a a a a |
| 64 | + {-# INLINE (<$) #-} |
| 65 | + |
| 66 | +instance Applicative V2 where |
| 67 | + pure a = V2 a a |
| 68 | + V2 a b <*> V2 d e = V2 (a d) (b e) |
| 69 | + |
| 70 | +instance Storable a => Storable (V4 a) where |
| 71 | + sizeOf _ = 4 * sizeOf (undefined::a) |
| 72 | + {-# INLINE sizeOf #-} |
| 73 | + alignment _ = alignment (undefined::a) |
| 74 | + {-# INLINE alignment #-} |
| 75 | + poke ptr (V4 x y z w) = do poke ptr' x |
| 76 | + pokeElemOff ptr' 1 y |
| 77 | + pokeElemOff ptr' 2 z |
| 78 | + pokeElemOff ptr' 3 w |
| 79 | + where ptr' = castPtr ptr |
| 80 | + {-# INLINE poke #-} |
| 81 | + peek ptr = V4 <$> peek ptr' <*> peekElemOff ptr' 1 |
| 82 | + <*> peekElemOff ptr' 2 <*> peekElemOff ptr' 3 |
| 83 | + where ptr' = castPtr ptr |
| 84 | + {-# INLINE peek #-} |
| 85 | + |
| 86 | +instance Storable a => Storable (V2 a) where |
| 87 | + sizeOf _ = 2 * sizeOf (undefined::a) |
| 88 | + {-# INLINE sizeOf #-} |
| 89 | + alignment _ = alignment (undefined::a) |
| 90 | + {-# INLINE alignment #-} |
| 91 | + poke ptr (V2 x y) = poke ptr' x >> pokeElemOff ptr' 1 y |
| 92 | + where ptr' = castPtr ptr |
| 93 | + {-# INLINE poke #-} |
| 94 | + peek ptr = V2 <$> peek ptr' <*> peekElemOff ptr' 1 |
| 95 | + where ptr' = castPtr ptr |
| 96 | + {-# INLINE peek #-} |
| 97 | + |
| 98 | +instance Num a => Num (V2 a) where |
| 99 | + (+) = liftA2 (+) |
| 100 | + (-) = liftA2 (-) |
| 101 | + (*) = liftA2 (*) |
| 102 | + negate = fmap negate |
| 103 | + abs = fmap abs |
| 104 | + signum = fmap signum |
| 105 | + fromInteger = pure . fromInteger |
0 commit comments