aboutsummaryrefslogtreecommitdiff
path: root/Spear/Math/Vector
diff options
context:
space:
mode:
Diffstat (limited to 'Spear/Math/Vector')
-rw-r--r--Spear/Math/Vector/Vector3.hs30
1 files changed, 19 insertions, 11 deletions
diff --git a/Spear/Math/Vector/Vector3.hs b/Spear/Math/Vector/Vector3.hs
index eeab486..8a1cfa9 100644
--- a/Spear/Math/Vector/Vector3.hs
+++ b/Spear/Math/Vector/Vector3.hs
@@ -1,6 +1,10 @@
1module Spear.Math.Vector.Vector3 1module Spear.Math.Vector.Vector3
2( 2(
3 Vector3 3 Vector3
4, Right3
5, Up3
6, Forward3
7, Position3
4 -- * Construction 8 -- * Construction
5, unitx3 9, unitx3
6, unity3 10, unity3
@@ -19,6 +23,11 @@ import Spear.Math.Vector.Class
19import Foreign.C.Types (CFloat) 23import Foreign.C.Types (CFloat)
20import Foreign.Storable 24import Foreign.Storable
21 25
26type Right3 = Vector3
27type Up3 = Vector3
28type Forward3 = Vector3
29type Position3 = Vector3
30
22 31
23-- | Represents a vector in 3D. 32-- | Represents a vector in 3D.
24data Vector3 = Vector3 33data Vector3 = Vector3
@@ -27,7 +36,6 @@ data Vector3 = Vector3
27 {-# UNPACK #-} !Float 36 {-# UNPACK #-} !Float
28 deriving (Eq, Show) 37 deriving (Eq, Show)
29 38
30
31instance Num Vector3 where 39instance Num Vector3 where
32 Vector3 ax ay az + Vector3 bx by bz = Vector3 (ax + bx) (ay + by) (az + bz) 40 Vector3 ax ay az + Vector3 bx by bz = Vector3 (ax + bx) (ay + by) (az + bz)
33 Vector3 ax ay az - Vector3 bx by bz = Vector3 (ax - bx) (ay - by) (az - bz) 41 Vector3 ax ay az - Vector3 bx by bz = Vector3 (ax - bx) (ay - by) (az - bz)
@@ -35,29 +43,29 @@ instance Num Vector3 where
35 abs (Vector3 ax ay az) = Vector3 (abs ax) (abs ay) (abs az) 43 abs (Vector3 ax ay az) = Vector3 (abs ax) (abs ay) (abs az)
36 signum (Vector3 ax ay az) = Vector3 (signum ax) (signum ay) (signum az) 44 signum (Vector3 ax ay az) = Vector3 (signum ax) (signum ay) (signum az)
37 fromInteger i = Vector3 i' i' i' where i' = fromInteger i 45 fromInteger i = Vector3 i' i' i' where i' = fromInteger i
38 46
39 47
40instance Fractional Vector3 where 48instance Fractional Vector3 where
41 Vector3 ax ay az / Vector3 bx by bz = Vector3 (ax / bx) (ay / by) (az / bz) 49 Vector3 ax ay az / Vector3 bx by bz = Vector3 (ax / bx) (ay / by) (az / bz)
42 fromRational r = Vector3 r' r' r' where r' = fromRational r 50 fromRational r = Vector3 r' r' r' where r' = fromRational r
43 51
44 52
45instance Ord Vector3 where 53instance Ord Vector3 where
46 Vector3 ax ay az <= Vector3 bx by bz 54 Vector3 ax ay az <= Vector3 bx by bz
47 = (ax <= bx) 55 = (ax <= bx)
48 || (az == bx && ay <= by) 56 || (az == bx && ay <= by)
49 || (ax == bx && ay == by && az <= bz) 57 || (ax == bx && ay == by && az <= bz)
50 58
51 Vector3 ax ay az >= Vector3 bx by bz 59 Vector3 ax ay az >= Vector3 bx by bz
52 = (ax >= bx) 60 = (ax >= bx)
53 || (ax == bx && ay >= by) 61 || (ax == bx && ay >= by)
54 || (ax == bx && ay == by && az >= bz) 62 || (ax == bx && ay == by && az >= bz)
55 63
56 Vector3 ax ay az < Vector3 bx by bz 64 Vector3 ax ay az < Vector3 bx by bz
57 = (ax < bx) 65 = (ax < bx)
58 || (az == bx && ay < by) 66 || (az == bx && ay < by)
59 || (ax == bx && ay == by && az < bz) 67 || (ax == bx && ay == by && az < bz)
60 68
61 Vector3 ax ay az > Vector3 bx by bz 69 Vector3 ax ay az > Vector3 bx by bz
62 = (ax > bx) 70 = (ax > bx)
63 || (ax == bx && ay > by) 71 || (ax == bx && ay > by)
@@ -80,7 +88,7 @@ instance VectorClass Vector3 where
80 88
81 {-# INLINABLE z #-} 89 {-# INLINABLE z #-}
82 z (Vector3 _ _ az) = az 90 z (Vector3 _ _ az) = az
83 91
84 {-# INLINABLE (!) #-} 92 {-# INLINABLE (!) #-}
85 (Vector3 ax _ _) ! 0 = ax 93 (Vector3 ax _ _) ! 0 = ax
86 (Vector3 _ ay _) ! 1 = ay 94 (Vector3 _ ay _) ! 1 = ay
@@ -115,13 +123,13 @@ sizeFloat = sizeOf (undefined :: CFloat)
115instance Storable Vector3 where 123instance Storable Vector3 where
116 sizeOf _ = 3*sizeFloat 124 sizeOf _ = 3*sizeFloat
117 alignment _ = alignment (undefined :: CFloat) 125 alignment _ = alignment (undefined :: CFloat)
118 126
119 peek ptr = do 127 peek ptr = do
120 ax <- peekByteOff ptr 0 128 ax <- peekByteOff ptr 0
121 ay <- peekByteOff ptr $ 1*sizeFloat 129 ay <- peekByteOff ptr $ 1*sizeFloat
122 az <- peekByteOff ptr $ 2*sizeFloat 130 az <- peekByteOff ptr $ 2*sizeFloat
123 return (Vector3 ax ay az) 131 return (Vector3 ax ay az)
124 132
125 poke ptr (Vector3 ax ay az) = do 133 poke ptr (Vector3 ax ay az) = do
126 pokeByteOff ptr 0 ax 134 pokeByteOff ptr 0 ax
127 pokeByteOff ptr (1*sizeFloat) ay 135 pokeByteOff ptr (1*sizeFloat) ay