diff options
Diffstat (limited to 'Spear/Math/Vector/Vector3.hs')
-rw-r--r-- | Spear/Math/Vector/Vector3.hs | 150 |
1 files changed, 94 insertions, 56 deletions
diff --git a/Spear/Math/Vector/Vector3.hs b/Spear/Math/Vector/Vector3.hs index 82deba2..9d44c8b 100644 --- a/Spear/Math/Vector/Vector3.hs +++ b/Spear/Math/Vector/Vector3.hs | |||
@@ -1,3 +1,7 @@ | |||
1 | {-# LANGUAGE MultiParamTypeClasses #-} | ||
2 | {-# LANGUAGE NoImplicitPrelude #-} | ||
3 | {-# LANGUAGE TypeFamilies #-} | ||
4 | |||
1 | module Spear.Math.Vector.Vector3 | 5 | module Spear.Math.Vector.Vector3 |
2 | ( | 6 | ( |
3 | Vector3(..) | 7 | Vector3(..) |
@@ -5,6 +9,7 @@ module Spear.Math.Vector.Vector3 | |||
5 | , Up3 | 9 | , Up3 |
6 | , Forward3 | 10 | , Forward3 |
7 | , Position3 | 11 | , Position3 |
12 | , sizeVector3 | ||
8 | -- * Construction | 13 | -- * Construction |
9 | , unitx3 | 14 | , unitx3 |
10 | , unity3 | 15 | , unity3 |
@@ -17,15 +22,17 @@ module Spear.Math.Vector.Vector3 | |||
17 | ) | 22 | ) |
18 | where | 23 | where |
19 | 24 | ||
20 | 25 | import Spear.Math.Algebra | |
21 | import Spear.Math.Vector.Vector | 26 | import Spear.Math.Vector.Vector |
27 | import Spear.Prelude | ||
22 | 28 | ||
23 | import Foreign.C.Types (CFloat) | 29 | import Foreign.C.Types (CFloat) |
24 | import Foreign.Storable | 30 | import Foreign.Storable |
31 | import qualified Prelude as P | ||
25 | 32 | ||
26 | type Right3 = Vector3 | 33 | type Right3 = Vector3 |
27 | type Up3 = Vector3 | 34 | type Up3 = Vector3 |
28 | type Forward3 = Vector3 | 35 | type Forward3 = Vector3 |
29 | type Position3 = Vector3 | 36 | type Position3 = Vector3 |
30 | 37 | ||
31 | 38 | ||
@@ -36,17 +43,58 @@ data Vector3 = Vector3 | |||
36 | {-# UNPACK #-} !Float | 43 | {-# UNPACK #-} !Float |
37 | deriving (Eq, Show) | 44 | deriving (Eq, Show) |
38 | 45 | ||
39 | instance Num Vector3 where | 46 | |
47 | sizeVector3 = (3::Int) * sizeOf (undefined :: CFloat) | ||
48 | |||
49 | |||
50 | instance Addition Vector3 Vector3 where | ||
51 | {-# INLINABLE (+) #-} | ||
40 | Vector3 ax ay az + Vector3 bx by bz = Vector3 (ax + bx) (ay + by) (az + bz) | 52 | Vector3 ax ay az + Vector3 bx by bz = Vector3 (ax + bx) (ay + by) (az + bz) |
53 | |||
54 | |||
55 | instance Subtraction Vector3 Vector3 where | ||
56 | {-# INLINABLE (-) #-} | ||
41 | Vector3 ax ay az - Vector3 bx by bz = Vector3 (ax - bx) (ay - by) (az - bz) | 57 | Vector3 ax ay az - Vector3 bx by bz = Vector3 (ax - bx) (ay - by) (az - bz) |
58 | |||
59 | |||
60 | instance Product Vector3 Vector3 Vector3 where | ||
61 | {-# INLINABLE (*) #-} | ||
42 | Vector3 ax ay az * Vector3 bx by bz = Vector3 (ax * bx) (ay * by) (az * bz) | 62 | Vector3 ax ay az * Vector3 bx by bz = Vector3 (ax * bx) (ay * by) (az * bz) |
63 | |||
64 | |||
65 | instance Quotient Vector3 Vector3 where | ||
66 | {-# INLINABLE (/) #-} | ||
67 | Vector3 ax ay az / Vector3 bx by bz = Vector3 (ax / bx) (ay / by) (az / bz) | ||
68 | |||
69 | |||
70 | -- Scalar product. | ||
71 | instance Product Vector3 Float Vector3 where | ||
72 | {-# INLINABLE (*) #-} | ||
73 | (Vector3 x y z) * s = Vector3 (s * x) (s * y) (s * z) | ||
74 | |||
75 | |||
76 | instance Product Float Vector3 Vector3 where | ||
77 | {-# INLINABLE (*) #-} | ||
78 | s * (Vector3 x y z) = Vector3 (s * x) (s * y) (s * z) | ||
79 | |||
80 | |||
81 | -- Scalar division. | ||
82 | instance Quotient Vector3 Float where | ||
83 | {-# INLINABLE (/) #-} | ||
84 | (Vector3 x y z) / s = Vector3 (x / s) (y / s) (y / s) | ||
85 | |||
86 | |||
87 | instance Num Vector3 where | ||
88 | (+) = add | ||
89 | (-) = sub | ||
90 | (*) = mul | ||
43 | abs (Vector3 ax ay az) = Vector3 (abs ax) (abs ay) (abs az) | 91 | abs (Vector3 ax ay az) = Vector3 (abs ax) (abs ay) (abs az) |
44 | signum (Vector3 ax ay az) = Vector3 (signum ax) (signum ay) (signum az) | 92 | signum (Vector3 ax ay az) = Vector3 (signum ax) (signum ay) (signum az) |
45 | fromInteger i = Vector3 i' i' i' where i' = fromInteger i | 93 | fromInteger i = Vector3 i' i' i' where i' = fromInteger i |
46 | 94 | ||
47 | 95 | ||
48 | instance Fractional Vector3 where | 96 | instance Fractional Vector3 where |
49 | Vector3 ax ay az / Vector3 bx by bz = Vector3 (ax / bx) (ay / by) (az / bz) | 97 | (/) = Spear.Math.Algebra.div |
50 | fromRational r = Vector3 r' r' r' where r' = fromRational r | 98 | fromRational r = Vector3 r' r' r' where r' = fromRational r |
51 | 99 | ||
52 | 100 | ||
@@ -71,91 +119,85 @@ instance Ord Vector3 where | |||
71 | || (ax == bx && ay > by) | 119 | || (ax == bx && ay > by) |
72 | || (ax == bx && ay == by && az > bz) | 120 | || (ax == bx && ay == by && az > bz) |
73 | 121 | ||
74 | max (Vector3 ax ay az) (Vector3 bx by bz) = Vector3 (Prelude.max ax bx) (Prelude.max ay by) (Prelude.max az bz) | 122 | max (Vector3 ax ay az) (Vector3 bx by bz) = |
123 | Vector3 (max ax bx) (max ay by) (max az bz) | ||
75 | 124 | ||
76 | min (Vector3 ax ay az) (Vector3 bx by bz) = Vector3 (Prelude.min ax bx) (Prelude.min ay by) (Prelude.min az bz) | 125 | min (Vector3 ax ay az) (Vector3 bx by bz) = |
126 | Vector3 (min ax bx) (min ay by) (min az bz) | ||
77 | 127 | ||
78 | 128 | ||
79 | instance Vector Vector3 where | 129 | instance Vector Vector3 where |
80 | {-# INLINABLE fromList #-} | 130 | {-# INLINABLE fromList #-} |
81 | fromList (ax:ay:az:_) = Vector3 ax ay az | 131 | fromList (ax:ay:az:_) = Vector3 ax ay az |
82 | |||
83 | {-# INLINABLE x #-} | ||
84 | x (Vector3 ax _ _ ) = ax | ||
85 | 132 | ||
86 | {-# INLINABLE y #-} | 133 | {-# INLINABLE x #-} |
87 | y (Vector3 _ ay _ ) = ay | 134 | x (Vector3 ax _ _ ) = ax |
88 | 135 | ||
89 | {-# INLINABLE z #-} | 136 | {-# INLINABLE y #-} |
90 | z (Vector3 _ _ az) = az | 137 | y (Vector3 _ ay _ ) = ay |
91 | 138 | ||
92 | {-# INLINABLE (!) #-} | 139 | {-# INLINABLE z #-} |
93 | (Vector3 ax _ _) ! 0 = ax | 140 | z (Vector3 _ _ az) = az |
94 | (Vector3 _ ay _) ! 1 = ay | ||
95 | (Vector3 _ _ az) ! 2 = az | ||
96 | _ ! _ = 0 | ||
97 | 141 | ||
98 | {-# INLINABLE dot #-} | 142 | {-# INLINABLE (!) #-} |
99 | Vector3 ax ay az `dot` Vector3 bx by bz = ax*bx + ay*by + az*bz | 143 | (Vector3 ax _ _) ! 0 = ax |
144 | (Vector3 _ ay _) ! 1 = ay | ||
145 | (Vector3 _ _ az) ! 2 = az | ||
146 | _ ! _ = 0 | ||
100 | 147 | ||
101 | {-# INLINABLE normSq #-} | 148 | {-# INLINABLE dot #-} |
102 | normSq (Vector3 ax ay az) = ax*ax + ay*ay + az*az | 149 | Vector3 ax ay az `dot` Vector3 bx by bz = ax*bx + ay*by + az*bz |
103 | 150 | ||
104 | {-# INLINABLE norm #-} | 151 | {-# INLINABLE normSq #-} |
105 | norm = sqrt . normSq | 152 | normSq (Vector3 ax ay az) = ax*ax + ay*ay + az*az |
106 | 153 | ||
107 | {-# INLINABLE scale #-} | 154 | {-# INLINABLE norm #-} |
108 | scale s (Vector3 ax ay az) = Vector3 (s*ax) (s*ay) (s*az) | 155 | norm = sqrt . normSq |
109 | 156 | ||
110 | {-# INLINABLE neg #-} | 157 | {-# INLINABLE neg #-} |
111 | neg (Vector3 ax ay az) = Vector3 (-ax) (-ay) (-az) | 158 | neg (Vector3 ax ay az) = Vector3 (-ax) (-ay) (-az) |
112 | 159 | ||
113 | {-# INLINABLE normalise #-} | 160 | {-# INLINABLE normalise #-} |
114 | normalise v = | 161 | normalise v = |
115 | let n' = norm v | 162 | let n' = norm v |
116 | n = if n' == 0 then 1 else n' | 163 | n = if n' == 0 then 1 else n' |
117 | in scale (1.0 / n) v | 164 | in ((1.0::Float) / n) * v |
118 | 165 | ||
119 | 166 | ||
120 | sizeFloat = sizeOf (undefined :: CFloat) | 167 | sizeFloat = sizeOf (undefined :: CFloat) |
121 | 168 | ||
122 | 169 | ||
123 | instance Storable Vector3 where | 170 | instance Storable Vector3 where |
124 | sizeOf _ = 3*sizeFloat | 171 | sizeOf _ = (3::Int) * sizeFloat |
125 | alignment _ = alignment (undefined :: CFloat) | 172 | alignment _ = alignment (undefined :: CFloat) |
126 | 173 | ||
127 | peek ptr = do | 174 | peek ptr = do |
128 | ax <- peekByteOff ptr 0 | 175 | ax <- peekByteOff ptr 0 |
129 | ay <- peekByteOff ptr $ 1*sizeFloat | 176 | ay <- peekByteOff ptr $ (1::Int) * sizeFloat |
130 | az <- peekByteOff ptr $ 2*sizeFloat | 177 | az <- peekByteOff ptr $ (2::Int) * sizeFloat |
131 | return (Vector3 ax ay az) | 178 | return (Vector3 ax ay az) |
132 | 179 | ||
133 | poke ptr (Vector3 ax ay az) = do | 180 | poke ptr (Vector3 ax ay az) = do |
134 | pokeByteOff ptr 0 ax | 181 | pokeByteOff ptr 0 ax |
135 | pokeByteOff ptr (1*sizeFloat) ay | 182 | pokeByteOff ptr ((1::Int) * sizeFloat) ay |
136 | pokeByteOff ptr (2*sizeFloat) az | 183 | pokeByteOff ptr ((2::Int) * sizeFloat) az |
137 | 184 | ||
138 | 185 | ||
139 | -- | Unit vector along the X axis. | 186 | -- | Unit vector along the X axis. |
140 | unitx3 = Vector3 1 0 0 | 187 | unitx3 = Vector3 1 0 0 |
141 | 188 | ||
142 | |||
143 | -- | Unit vector along the Y axis. | 189 | -- | Unit vector along the Y axis. |
144 | unity3 = Vector3 0 1 0 | 190 | unity3 = Vector3 0 1 0 |
145 | 191 | ||
146 | |||
147 | -- | Unit vector along the Z axis. | 192 | -- | Unit vector along the Z axis. |
148 | unitz3 = Vector3 0 0 1 | 193 | unitz3 = Vector3 0 0 1 |
149 | 194 | ||
150 | |||
151 | -- | Zero vector. | 195 | -- | Zero vector. |
152 | zero3 = Vector3 0 0 0 | 196 | zero3 = Vector3 0 0 0 |
153 | 197 | ||
154 | |||
155 | -- | Create a 3D vector from the given values. | 198 | -- | Create a 3D vector from the given values. |
156 | vec3 :: Float -> Float -> Float -> Vector3 | 199 | vec3 :: Float -> Float -> Float -> Vector3 |
157 | vec3 ax ay az = Vector3 ax ay az | 200 | vec3 = Vector3 |
158 | |||
159 | 201 | ||
160 | -- | Create a 3D vector as a point on a sphere. | 202 | -- | Create a 3D vector as a point on a sphere. |
161 | orbit :: Vector3 -- ^ Sphere center. | 203 | orbit :: Vector3 -- ^ Sphere center. |
@@ -163,21 +205,17 @@ orbit :: Vector3 -- ^ Sphere center. | |||
163 | -> Float -- ^ Azimuth angle. | 205 | -> Float -- ^ Azimuth angle. |
164 | -> Float -- ^ Zenith angle. | 206 | -> Float -- ^ Zenith angle. |
165 | -> Vector3 | 207 | -> Vector3 |
166 | |||
167 | orbit center radius anglex angley = | 208 | orbit center radius anglex angley = |
168 | let ax = anglex * pi / 180 | 209 | let sx = sin anglex |
169 | ay = angley * pi / 180 | 210 | sy = sin angley |
170 | sx = sin ax | 211 | cx = cos anglex |
171 | sy = sin ay | 212 | cy = cos angley |
172 | cx = cos ax | ||
173 | cy = cos ay | ||
174 | px = x center + radius*cy*sx | 213 | px = x center + radius*cy*sx |
175 | py = y center + radius*sy | 214 | py = y center + radius*sy |
176 | pz = z center + radius*cx*cy | 215 | pz = z center + radius*cx*cy |
177 | in | 216 | in |
178 | vec3 px py pz | 217 | vec3 px py pz |
179 | 218 | ||
180 | |||
181 | -- | Compute the given vectors' cross product. | 219 | -- | Compute the given vectors' cross product. |
182 | cross :: Vector3 -> Vector3 -> Vector3 | 220 | cross :: Vector3 -> Vector3 -> Vector3 |
183 | (Vector3 ax ay az) `cross` (Vector3 bx by bz) = | 221 | (Vector3 ax ay az) `cross` (Vector3 bx by bz) = |