diff options
Diffstat (limited to 'Spear/Math/Vector')
-rw-r--r-- | Spear/Math/Vector/Vector2.hs | 28 | ||||
-rw-r--r-- | Spear/Math/Vector/Vector3.hs | 2 | ||||
-rw-r--r-- | Spear/Math/Vector/Vector4.hs | 34 |
3 files changed, 34 insertions, 30 deletions
diff --git a/Spear/Math/Vector/Vector2.hs b/Spear/Math/Vector/Vector2.hs index 0b29ec4..dfb4fb9 100644 --- a/Spear/Math/Vector/Vector2.hs +++ b/Spear/Math/Vector/Vector2.hs | |||
@@ -1,6 +1,9 @@ | |||
1 | module Spear.Math.Vector.Vector2 | 1 | module Spear.Math.Vector.Vector2 |
2 | ( | 2 | ( |
3 | Vector2 | 3 | Vector2(..) |
4 | , Right2 | ||
5 | , Up2 | ||
6 | , Position2 | ||
4 | -- * Construction | 7 | -- * Construction |
5 | , unitx2 | 8 | , unitx2 |
6 | , unity2 | 9 | , unity2 |
@@ -11,13 +14,14 @@ module Spear.Math.Vector.Vector2 | |||
11 | ) | 14 | ) |
12 | where | 15 | where |
13 | 16 | ||
14 | |||
15 | import Spear.Math.Vector.Class | 17 | import Spear.Math.Vector.Class |
16 | 18 | ||
17 | |||
18 | import Foreign.C.Types (CFloat) | 19 | import Foreign.C.Types (CFloat) |
19 | import Foreign.Storable | 20 | import Foreign.Storable |
20 | 21 | ||
22 | type Right2 = Vector2 | ||
23 | type Up2 = Vector2 | ||
24 | type Position2 = Vector2 | ||
21 | 25 | ||
22 | -- | Represents a vector in 2D. | 26 | -- | Represents a vector in 2D. |
23 | data Vector2 = Vector2 {-# UNPACK #-} !Float {-# UNPACK #-} !Float deriving (Eq, Show) | 27 | data Vector2 = Vector2 {-# UNPACK #-} !Float {-# UNPACK #-} !Float deriving (Eq, Show) |
@@ -30,13 +34,13 @@ instance Num Vector2 where | |||
30 | abs (Vector2 ax ay) = Vector2 (abs ax) (abs ay) | 34 | abs (Vector2 ax ay) = Vector2 (abs ax) (abs ay) |
31 | signum (Vector2 ax ay) = Vector2 (signum ax) (signum ay) | 35 | signum (Vector2 ax ay) = Vector2 (signum ax) (signum ay) |
32 | fromInteger i = Vector2 i' i' where i' = fromInteger i | 36 | fromInteger i = Vector2 i' i' where i' = fromInteger i |
33 | 37 | ||
34 | 38 | ||
35 | instance Fractional Vector2 where | 39 | instance Fractional Vector2 where |
36 | Vector2 ax ay / Vector2 bx by = Vector2 (ax / bx) (ay / by) | 40 | Vector2 ax ay / Vector2 bx by = Vector2 (ax / bx) (ay / by) |
37 | fromRational r = Vector2 r' r' where r' = fromRational r | 41 | fromRational r = Vector2 r' r' where r' = fromRational r |
38 | 42 | ||
39 | 43 | ||
40 | instance Ord Vector2 where | 44 | instance Ord Vector2 where |
41 | Vector2 ax ay <= Vector2 bx by = (ax <= bx) || (ax == bx && ay <= by) | 45 | Vector2 ax ay <= Vector2 bx by = (ax <= bx) || (ax == bx && ay <= by) |
42 | Vector2 ax ay >= Vector2 bx by = (ax >= bx) || (ax == bx && ay >= by) | 46 | Vector2 ax ay >= Vector2 bx by = (ax >= bx) || (ax == bx && ay >= by) |
@@ -89,18 +93,18 @@ sizeFloat = sizeOf (undefined :: CFloat) | |||
89 | instance Storable Vector2 where | 93 | instance Storable Vector2 where |
90 | sizeOf _ = 2*sizeFloat | 94 | sizeOf _ = 2*sizeFloat |
91 | alignment _ = alignment (undefined :: CFloat) | 95 | alignment _ = alignment (undefined :: CFloat) |
92 | 96 | ||
93 | peek ptr = do | 97 | peek ptr = do |
94 | ax <- peekByteOff ptr 0 | 98 | ax <- peekByteOff ptr 0 |
95 | ay <- peekByteOff ptr $ sizeFloat | 99 | ay <- peekByteOff ptr $ sizeFloat |
96 | return (Vector2 ax ay) | 100 | return (Vector2 ax ay) |
97 | 101 | ||
98 | poke ptr (Vector2 ax ay) = do | 102 | poke ptr (Vector2 ax ay) = do |
99 | pokeByteOff ptr 0 ax | 103 | pokeByteOff ptr 0 ax |
100 | pokeByteOff ptr sizeFloat ay | 104 | pokeByteOff ptr sizeFloat ay |
101 | 105 | ||
102 | 106 | ||
103 | -- | Get the vector's x coordinate. | 107 | -- | Get the vector's x coordinate. |
104 | 108 | ||
105 | 109 | ||
106 | 110 | ||
@@ -122,9 +126,9 @@ vec2 ax ay = Vector2 ax ay | |||
122 | 126 | ||
123 | 127 | ||
124 | -- | Compute a vector perpendicular to the given one, satisfying: | 128 | -- | Compute a vector perpendicular to the given one, satisfying: |
125 | -- | 129 | -- |
126 | -- perp (Vector2 0 1) = Vector2 1 0 | 130 | -- perp (Vector2 0 1) = Vector2 1 0 |
127 | -- | 131 | -- |
128 | -- perp (Vector2 1 0) = Vector2 0 (-1) | 132 | -- perp (Vector2 1 0) = Vector2 0 (-1) |
129 | perp :: Vector2 -> Vector2 | 133 | perp :: Vector2 -> Vector2 |
130 | perp (Vector2 x y) = Vector2 y (-x) | 134 | perp (Vector2 x y) = Vector2 y (-x) |
diff --git a/Spear/Math/Vector/Vector3.hs b/Spear/Math/Vector/Vector3.hs index 70bd299..429df0f 100644 --- a/Spear/Math/Vector/Vector3.hs +++ b/Spear/Math/Vector/Vector3.hs | |||
@@ -1,6 +1,6 @@ | |||
1 | module Spear.Math.Vector.Vector3 | 1 | module Spear.Math.Vector.Vector3 |
2 | ( | 2 | ( |
3 | Vector3 | 3 | Vector3(..) |
4 | , Right3 | 4 | , Right3 |
5 | , Up3 | 5 | , Up3 |
6 | , Forward3 | 6 | , Forward3 |
diff --git a/Spear/Math/Vector/Vector4.hs b/Spear/Math/Vector/Vector4.hs index 3b5ed95..4314b51 100644 --- a/Spear/Math/Vector/Vector4.hs +++ b/Spear/Math/Vector/Vector4.hs | |||
@@ -1,6 +1,6 @@ | |||
1 | module Spear.Math.Vector.Vector4 | 1 | module Spear.Math.Vector.Vector4 |
2 | ( | 2 | ( |
3 | Vector4 | 3 | Vector4(..) |
4 | -- * Construction | 4 | -- * Construction |
5 | , unitx4 | 5 | , unitx4 |
6 | , unity4 | 6 | , unity4 |
@@ -34,32 +34,32 @@ instance Num Vector4 where | |||
34 | abs (Vector4 ax ay az aw) = Vector4 (abs ax) (abs ay) (abs az) (abs aw) | 34 | abs (Vector4 ax ay az aw) = Vector4 (abs ax) (abs ay) (abs az) (abs aw) |
35 | signum (Vector4 ax ay az aw) = Vector4 (signum ax) (signum ay) (signum az) (signum aw) | 35 | signum (Vector4 ax ay az aw) = Vector4 (signum ax) (signum ay) (signum az) (signum aw) |
36 | fromInteger i = Vector4 i' i' i' i' where i' = fromInteger i | 36 | fromInteger i = Vector4 i' i' i' i' where i' = fromInteger i |
37 | 37 | ||
38 | 38 | ||
39 | instance Fractional Vector4 where | 39 | instance Fractional Vector4 where |
40 | Vector4 ax ay az aw / Vector4 bx by bz bw = Vector4 (ax / bx) (ay / by) (az / bz) (aw / bw) | 40 | Vector4 ax ay az aw / Vector4 bx by bz bw = Vector4 (ax / bx) (ay / by) (az / bz) (aw / bw) |
41 | fromRational r = Vector4 r' r' r' r' where r' = fromRational r | 41 | fromRational r = Vector4 r' r' r' r' where r' = fromRational r |
42 | 42 | ||
43 | 43 | ||
44 | instance Ord Vector4 where | 44 | instance Ord Vector4 where |
45 | Vector4 ax ay az aw <= Vector4 bx by bz bw | 45 | Vector4 ax ay az aw <= Vector4 bx by bz bw |
46 | = (ax <= bx) | 46 | = (ax <= bx) |
47 | || (az == bx && ay <= by) | 47 | || (az == bx && ay <= by) |
48 | || (ax == bx && ay == by && az <= bz) | 48 | || (ax == bx && ay == by && az <= bz) |
49 | || (ax == bx && ay == by && az == bz && aw <= bw) | 49 | || (ax == bx && ay == by && az == bz && aw <= bw) |
50 | 50 | ||
51 | Vector4 ax ay az aw >= Vector4 bx by bz bw | 51 | Vector4 ax ay az aw >= Vector4 bx by bz bw |
52 | = (ax >= bx) | 52 | = (ax >= bx) |
53 | || (ax == bx && ay >= by) | 53 | || (ax == bx && ay >= by) |
54 | || (ax == bx && ay == by && az >= bz) | 54 | || (ax == bx && ay == by && az >= bz) |
55 | || (ax == bx && ay == by && az == bz && aw >= bw) | 55 | || (ax == bx && ay == by && az == bz && aw >= bw) |
56 | 56 | ||
57 | Vector4 ax ay az aw < Vector4 bx by bz bw | 57 | Vector4 ax ay az aw < Vector4 bx by bz bw |
58 | = (ax < bx) | 58 | = (ax < bx) |
59 | || (az == bx && ay < by) | 59 | || (az == bx && ay < by) |
60 | || (ax == bx && ay == by && az < bz) | 60 | || (ax == bx && ay == by && az < bz) |
61 | || (ax == bx && ay == by && az == bz && aw < bw) | 61 | || (ax == bx && ay == by && az == bz && aw < bw) |
62 | 62 | ||
63 | Vector4 ax ay az aw > Vector4 bx by bz bw | 63 | Vector4 ax ay az aw > Vector4 bx by bz bw |
64 | = (ax > bx) | 64 | = (ax > bx) |
65 | || (ax == bx && ay > by) | 65 | || (ax == bx && ay > by) |
@@ -88,29 +88,29 @@ instance VectorClass Vector4 where | |||
88 | 88 | ||
89 | {-# INLINABLE w #-} | 89 | {-# INLINABLE w #-} |
90 | w (Vector4 _ _ _ aw) = aw | 90 | w (Vector4 _ _ _ aw) = aw |
91 | 91 | ||
92 | {-# INLINABLE (!) #-} | 92 | {-# INLINABLE (!) #-} |
93 | (Vector4 ax _ _ _) ! 0 = ax | 93 | (Vector4 ax _ _ _) ! 0 = ax |
94 | (Vector4 _ ay _ _) ! 1 = ay | 94 | (Vector4 _ ay _ _) ! 1 = ay |
95 | (Vector4 _ _ az _) ! 2 = az | 95 | (Vector4 _ _ az _) ! 2 = az |
96 | (Vector4 _ _ _ aw) ! 3 = aw | 96 | (Vector4 _ _ _ aw) ! 3 = aw |
97 | _ ! _ = 0 | 97 | _ ! _ = 0 |
98 | 98 | ||
99 | {-# INLINABLE dot #-} | 99 | {-# INLINABLE dot #-} |
100 | Vector4 ax ay az aw `dot` Vector4 bx by bz bw = ax*bx + ay*by + az*bz + aw*bw | 100 | Vector4 ax ay az aw `dot` Vector4 bx by bz bw = ax*bx + ay*by + az*bz + aw*bw |
101 | 101 | ||
102 | {-# INLINABLE normSq #-} | 102 | {-# INLINABLE normSq #-} |
103 | normSq (Vector4 ax ay az aw) = ax*ax + ay*ay + az*az + aw*aw | 103 | normSq (Vector4 ax ay az aw) = ax*ax + ay*ay + az*az + aw*aw |
104 | 104 | ||
105 | {-# INLINABLE norm #-} | 105 | {-# INLINABLE norm #-} |
106 | norm = sqrt . normSq | 106 | norm = sqrt . normSq |
107 | 107 | ||
108 | {-# INLINABLE scale #-} | 108 | {-# INLINABLE scale #-} |
109 | scale s (Vector4 ax ay az aw) = Vector4 (s*ax) (s*ay) (s*az) (s*aw) | 109 | scale s (Vector4 ax ay az aw) = Vector4 (s*ax) (s*ay) (s*az) (s*aw) |
110 | 110 | ||
111 | {-# INLINABLE neg #-} | 111 | {-# INLINABLE neg #-} |
112 | neg (Vector4 ax ay az aw) = Vector4 (-ax) (-ay) (-az) (-aw) | 112 | neg (Vector4 ax ay az aw) = Vector4 (-ax) (-ay) (-az) (-aw) |
113 | 113 | ||
114 | {-# INLINABLE normalise #-} | 114 | {-# INLINABLE normalise #-} |
115 | normalise v = | 115 | normalise v = |
116 | let n' = norm v | 116 | let n' = norm v |
@@ -124,14 +124,14 @@ sizeFloat = sizeOf (undefined :: CFloat) | |||
124 | instance Storable Vector4 where | 124 | instance Storable Vector4 where |
125 | sizeOf _ = 4*sizeFloat | 125 | sizeOf _ = 4*sizeFloat |
126 | alignment _ = alignment (undefined :: CFloat) | 126 | alignment _ = alignment (undefined :: CFloat) |
127 | 127 | ||
128 | peek ptr = do | 128 | peek ptr = do |
129 | ax <- peekByteOff ptr 0 | 129 | ax <- peekByteOff ptr 0 |
130 | ay <- peekByteOff ptr $ 1 * sizeFloat | 130 | ay <- peekByteOff ptr $ 1 * sizeFloat |
131 | az <- peekByteOff ptr $ 2 * sizeFloat | 131 | az <- peekByteOff ptr $ 2 * sizeFloat |
132 | aw <- peekByteOff ptr $ 3 * sizeFloat | 132 | aw <- peekByteOff ptr $ 3 * sizeFloat |
133 | return (Vector4 ax ay az aw) | 133 | return (Vector4 ax ay az aw) |
134 | 134 | ||
135 | poke ptr (Vector4 ax ay az aw) = do | 135 | poke ptr (Vector4 ax ay az aw) = do |
136 | pokeByteOff ptr 0 ax | 136 | pokeByteOff ptr 0 ax |
137 | pokeByteOff ptr (1 * sizeFloat) ay | 137 | pokeByteOff ptr (1 * sizeFloat) ay |