aboutsummaryrefslogtreecommitdiff
path: root/Spear/Math/Vector3.hs
blob: 0d559c39f34357a4043040b04328054da8468f02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
module Spear.Math.Vector3
(
    Vector3
    -- * Accessors
,   x
,   y
,   z
    -- * Construction
,   unitX
,   unitY
,   unitZ
,   zero
,   fromList
,   vec3
,   orbit
    -- * Operations
,   Spear.Math.Vector3.min
,   Spear.Math.Vector3.max
,   dot
,   cross
,   normSq
,   norm
,   scale
,   normalise
,   neg
)
where

import Foreign.C.Types (CFloat)
import Foreign.Storable


-- | Represents a vector in 3D.
data Vector3 = Vector3
    {-# UNPACK #-} !Float
    {-# UNPACK #-} !Float
    {-# UNPACK #-} !Float
    deriving (Eq, Show)


instance Num Vector3 where
    Vector3 ax ay az + Vector3 bx by bz = Vector3 (ax + bx) (ay + by) (az + bz)
    Vector3 ax ay az - Vector3 bx by bz = Vector3 (ax - bx) (ay - by) (az - bz)
    Vector3 ax ay az * Vector3 bx by bz = Vector3 (ax * bx) (ay * by) (az * bz)
    abs (Vector3 ax ay az) = Vector3 (abs ax) (abs ay) (abs az)
    signum (Vector3 ax ay az) = Vector3 (signum ax) (signum ay) (signum az)
    fromInteger i = Vector3 i' i' i' where i' = fromInteger i
    
    
instance Fractional Vector3 where
    Vector3 ax ay az / Vector3 bx by bz = Vector3 (ax / bx) (ay / by) (az / bz)
    fromRational r = Vector3 r' r' r' where r' = fromRational r
    
    
instance Ord Vector3 where
    Vector3 ax ay az <= Vector3 bx by bz
        =  (ax <= bx)
        || (az == bx && ay <= by)
        || (ax == bx && ay == by && az <= bz)
    
    Vector3 ax ay az >= Vector3 bx by bz
        =  (ax >= bx)
        || (ax == bx && ay >= by)
        || (ax == bx && ay == by && az >= bz)
    
    Vector3 ax ay az < Vector3 bx by bz
        =  (ax < bx)
        || (az == bx && ay < by)
        || (ax == bx && ay == by && az < bz)
    
    Vector3 ax ay az > Vector3 bx by bz
        =  (ax > bx)
        || (ax == bx && ay > by)
        || (ax == bx && ay == by && az > bz)


sizeFloat = sizeOf (undefined :: CFloat)


instance Storable Vector3 where
    sizeOf _    = 3*sizeFloat
    alignment _ = alignment (undefined :: CFloat)
    
    peek ptr = do
        ax <- peekByteOff ptr 0
        ay <- peekByteOff ptr $ 1*sizeFloat
        az <- peekByteOff ptr $ 2*sizeFloat
        return (Vector3 ax ay az)
    
    poke ptr (Vector3 ax ay az) = do
        pokeByteOff ptr 0 ax
        pokeByteOff ptr (1*sizeFloat) ay
        pokeByteOff ptr (2*sizeFloat) az


x (Vector3 ax _  _ ) = ax
y (Vector3 _  ay _ ) = ay
z (Vector3 _  _  az) = az


-- | Unit vector along the X axis.
unitX :: Vector3
unitX = Vector3 1 0 0


-- | Unit vector along the Y axis.
unitY :: Vector3
unitY = Vector3 0 1 0


-- | Unit vector along the Z axis.
unitZ :: Vector3
unitZ = Vector3 0 0 1


-- | Zero vector.
zero :: Vector3
zero = Vector3 0 0 0


-- | Create a vector from the given list.
fromList :: [Float] -> Vector3
fromList (ax:ay:az:_) = Vector3 ax ay az


-- | Create a 3D vector from the given values.
vec3 :: Float -> Float -> Float -> Vector3
vec3 ax ay az = Vector3 ax ay az


-- | Create a 3D vector as a point on a sphere.
orbit :: Vector3 -- ^ Sphere center.
      -> Float -- ^ Sphere radius
      -> Float -- ^ Azimuth angle.
      -> Float -- ^ Zenith angle.
      -> Vector3

orbit center radius anglex angley =
    let ax = anglex * pi / 180
        ay = angley * pi / 180
        sx = sin ax
        sy = sin ay
        cx = cos ax
        cy = cos ay
        px = x center + radius*cy*sx
        py = y center + radius*sy
        pz = z center + radius*cx*cy
    in
        vec3 px py pz


-- | Create a vector with components set to the minimum of each of the given vectors'.
min :: Vector3 -> Vector3 -> Vector3
min (Vector3 ax ay az) (Vector3 bx by bz) = Vector3 (Prelude.min ax bx) (Prelude.min ay by) (Prelude.min az bz)


-- | Create a vector with components set to the maximum of each of the given vectors'.
max :: Vector3 -> Vector3 -> Vector3
max (Vector3 ax ay az) (Vector3 bx by bz) = Vector3 (Prelude.max ax bx) (Prelude.max ay by) (Prelude.max az bz)


-- | Compute the given vectors' dot product.
dot :: Vector3 -> Vector3 -> Float
Vector3 ax ay az `dot` Vector3 bx by bz = ax*bx + ay*by + az*bz


-- | Compute the given vectors' cross product.
cross :: Vector3 -> Vector3 -> Vector3
(Vector3 ax ay az) `cross` (Vector3 bx by bz) =
    Vector3 (ay * bz - az * by) (az * bx - ax * bz) (ax * by - ay * bx)
    
    
-- | Compute the given vector's squared norm.
normSq :: Vector3 -> Float
normSq (Vector3 ax ay az) = ax*ax + ay*ay + az*az
    

-- | Compute the given vector's norm.
norm :: Vector3 -> Float
norm = sqrt . normSq


-- | Multiply the given vector with the given scalar.
scale :: Float -> Vector3 -> Vector3
scale s (Vector3 ax ay az) = Vector3 (s*ax) (s*ay) (s*az)


-- | Normalise the given vector.
normalise :: Vector3 -> Vector3
normalise v =
    let n' = norm v
        n = if n' == 0 then 1 else n'
    in
        scale (1.0 / n) v


-- | Negate the given vector.
neg :: Vector3 -> Vector3
neg (Vector3 ax ay az) = Vector3 (-ax) (-ay) (-az)