-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTransformSpec.hs
292 lines (173 loc) · 9.03 KB
/
TransformSpec.hs
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
module TransformSpec where
import Test.Hspec
import Matrix
import RTCTuple
import Data.Array
import Transform
spec :: Spec
spec = describe "Transform" $ do
describe "Constructing and inspecting a 4x4 Matrix" $ do
let transform = Transform.translation 5.0 (-3.0) 2.0
let p = RTCTuple.point (-3.0) 4.0 5.0
let expected = RTCTuple.point 2.0 1.0 7.0
let result = Matrix.tmult transform p
it "transform*p = point(2,1,7)" $
RTCTuple.equal result expected `shouldBe` True
describe "Multiplying by the inverse of a translation matrix" $ do
let transform = Transform.translation 5.0 (-3.0) 2.0
let inv = Matrix.inverse transform
let p = RTCTuple.point (-3.0) 4.0 5.0
let expected = RTCTuple.point (-8.0) 7.0 3.0
let result = Matrix.tmult inv p
it "inv*p = point(-8,7,3)" $
RTCTuple.equal result expected `shouldBe` True
describe "Translation does not affect vectors" $ do
let transform = Transform.translation 5.0 (-3.0) 2.0
let v = RTCTuple.vector (-3.0) 4.0 5.0
let result = Matrix.tmult transform v
it "transform*v=v" $
RTCTuple.equal result v `shouldBe` True
describe "A scaling matrix applied to a point" $ do
let transform = Transform.scaling 2.0 3.0 4.0
let p = RTCTuple.point (-4.0) 6.0 8.0
let expected = RTCTuple.point (-8.0) 18.0 32.0
let result = Matrix.tmult transform p
it "transform*p = point(2,1,7)" $
RTCTuple.equal result expected `shouldBe` True
describe "A scaling matrix applied to a vector" $ do
let transform = Transform.scaling 2.0 3.0 4.0
let p = RTCTuple.vector (-4.0) 6.0 8.0
let expected = RTCTuple.vector (-8.0) 18.0 32.0
let result = Matrix.tmult transform p
it "transform*p = point(2,1,7)" $
RTCTuple.equal result expected `shouldBe` True
describe "Multiplying by the inverse of a scaling matrix" $ do
let transform = Transform.scaling 2.0 3.0 4.0
let inv = Matrix.inverse transform
let v = RTCTuple.vector (-4.0) 6.0 8.0
let expected = RTCTuple.vector (-2.0) 2.0 2.0
let result = Matrix.tmult inv v
it "inv*v = vector(-2,2,2)" $
RTCTuple.equal result expected `shouldBe` True
describe "Reflection is scaling by a negative value" $ do
let transform = Transform.scaling (-1.0) 1.0 1.0
let p = RTCTuple.point 2.0 3.0 4.0
let expected = RTCTuple.point (-2.0) 3.0 4.0
let result = Matrix.tmult transform p
it "transform*p = point(2,1,7)" $
RTCTuple.equal result expected `shouldBe` True
describe "Rotating a point around the x axis" $ do
let half_quarter = Transform.rotation_x (pi/4.0)
let full_quarter = Transform.rotation_x (pi/2.0)
let p = RTCTuple.point 0.0 1.0 0.0
let expected_half = RTCTuple.point 0.0 (0.5*sqrt 2) (0.5*sqrt 2)
let expected_full = RTCTuple.point 0.0 0.0 1.0
let result_half = Matrix.tmult half_quarter p
let result_full = Matrix.tmult full_quarter p
it "half_quarter*p = expected_half" $
RTCTuple.equal result_half expected_half `shouldBe` True
it "full_quarter*p = expected_full" $
RTCTuple.equal result_full expected_full `shouldBe` True
describe "The inverse of an x rotation rotates in the opposite direction" $ do
let half_quarter = Transform.rotation_x (pi/4.0)
let inv = Matrix.inverse half_quarter
let p = RTCTuple.point 0.0 1.0 0.0
let expected_inv = RTCTuple.point 0.0 (0.5*sqrt 2) (-0.5*sqrt 2)
let result_inv = Matrix.tmult inv p
it "inv*p = expected_inv" $
RTCTuple.equal result_inv expected_inv `shouldBe` True
describe "Rotating a point around the y axis" $ do
let half_quarter = Transform.rotation_y (pi/4.0)
let full_quarter = Transform.rotation_y (pi/2.0)
let p = RTCTuple.point 0.0 0.0 1.0
let expected_half = RTCTuple.point (0.5*sqrt 2) 0.0 (0.5*sqrt 2)
let expected_full = RTCTuple.point 1.0 0.0 0.0
let result_half = Matrix.tmult half_quarter p
let result_full = Matrix.tmult full_quarter p
it "half_quarter*p = expected_half" $
RTCTuple.equal result_half expected_half `shouldBe` True
it "full_quarter*p = expected_full" $
RTCTuple.equal result_full expected_full `shouldBe` True
describe "Rotating a point around the z axis" $ do
let half_quarter = Transform.rotation_z (pi/4.0)
let full_quarter = Transform.rotation_z (pi/2.0)
let p = RTCTuple.point 0.0 1.0 0.0
let expected_half = RTCTuple.point (-0.5*sqrt 2) (0.5*sqrt 2) 0.0
let expected_full = RTCTuple.point (-1.0) 0.0 0.0
let result_half = Matrix.tmult half_quarter p
let result_full = Matrix.tmult full_quarter p
it "half_quarter*p = expected_half" $
RTCTuple.equal result_half expected_half `shouldBe` True
it "full_quarter*p = expected_full" $
RTCTuple.equal result_full expected_full `shouldBe` True
describe "A shearing transformation moves x in proportion to y" $ do
let transform = Transform.shearing 1.0 0.0 0.0 0.0 0.0 0.0
let p = RTCTuple.point 2.0 3.0 4.0
let expected = RTCTuple.point 5.0 3.0 4.0
let result = Matrix.tmult transform p
it "transform*p = expected" $
RTCTuple.equal result expected `shouldBe` True
describe "A shearing transformation moves x in proportion to z" $ do
let transform = Transform.shearing 0.0 1.0 0.0 0.0 0.0 0.0
let p = RTCTuple.point 2.0 3.0 4.0
let expected = RTCTuple.point 6.0 3.0 4.0
let result = Matrix.tmult transform p
it "transform*p = expected" $
RTCTuple.equal result expected `shouldBe` True
describe "A shearing transformation moves y in proportion to x" $ do
let transform = Transform.shearing 0.0 0.0 1.0 0.0 0.0 0.0
let p = RTCTuple.point 2.0 3.0 4.0
let expected = RTCTuple.point 2.0 5.0 4.0
let result = Matrix.tmult transform p
it "transform*p = expected" $
RTCTuple.equal result expected `shouldBe` True
describe "A shearing transformation moves y in proportion to z" $ do
let transform = Transform.shearing 0.0 0.0 0.0 1.0 0.0 0.0
let p = RTCTuple.point 2.0 3.0 4.0
let expected = RTCTuple.point 2.0 7.0 4.0
let result = Matrix.tmult transform p
it "transform*p = expected" $
RTCTuple.equal result expected `shouldBe` True
describe "A shearing transformation moves z in proportion to x" $ do
let transform = Transform.shearing 0.0 0.0 0.0 0.0 1.0 0.0
let p = RTCTuple.point 2.0 3.0 4.0
let expected = RTCTuple.point 2.0 3.0 6.0
let result = Matrix.tmult transform p
it "transform*p = expected" $
RTCTuple.equal result expected `shouldBe` True
describe "A shearing transformation moves z in proportion to y" $ do
let transform = Transform.shearing 0.0 0.0 0.0 0.0 0.0 1.0
let p = RTCTuple.point 2.0 3.0 4.0
let expected = RTCTuple.point 2.0 3.0 7.0
let result = Matrix.tmult transform p
it "transform*p = expected" $
RTCTuple.equal result expected `shouldBe` True
describe "A shearing transformation moves z in proportion to y" $ do
let transform = Transform.shearing 0.0 0.0 0.0 0.0 0.0 1.0
let p = RTCTuple.point 1.0 0.0 1.0
let a = Transform.rotation_x (pi/2.0)
let b = Transform.scaling 5.0 5.0 5.0
let c = Transform.translation 10.0 5.0 7.0
let p2 = Matrix.tmult a p
let p2_expected = RTCTuple.point 1.0 (-1.0) 0.0
let p3 = Matrix.tmult b p2
let p3_expected = RTCTuple.point 5.0 (-5.0) 0.0
let p4 = Matrix.tmult c p3
let p4_expected = RTCTuple.point 15.0 0.0 7.0
it "a*p = p2_expected" $
RTCTuple.equal p2 p2_expected `shouldBe` True
it "b*p2 = p3_expected" $
RTCTuple.equal p3 p3_expected `shouldBe` True
it "c*p3 = p4_expected" $
RTCTuple.equal p4 p4_expected `shouldBe` True
describe "Chained transformations must be applied in reverse order" $ do
let transform = Transform.shearing 0.0 0.0 0.0 0.0 0.0 1.0
let p = RTCTuple.point 1.0 0.0 1.0
let a = Transform.rotation_x (pi/2.0)
let b = Transform.scaling 5.0 5.0 5.0
let c = Transform.translation 10.0 5.0 7.0
let t = Matrix.matMult c (Matrix.matMult b a)
let result_expected = RTCTuple.point 15.0 0.0 7.0
let result = Matrix.tmult t p
it "a*p = p2_expected" $
RTCTuple.equal result result_expected `shouldBe` True