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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
use std::ops;
use crate::IsFloat;
use crate::geo::Vec3h;
/// Linear Transformation Matrix for 3D Homogeneous Coordinates.
#[derive(Debug, Clone)]
pub struct Transform3h<T: IsFloat> {
data: [T; 16]
}
impl<T: IsFloat> Transform3h<T> {
/// Return reference to transform data.
pub fn get_data(&self) -> &[T] {
return &self.data
}
}
impl Transform3h<f32> {
/// Return identity matrix.
///
/// This transform keeps every vector the same.
pub fn identity() -> Transform3h<f32> {
Transform3h { data: [
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0,
] }
}
/// Return scaling matrix.
///
/// This transform scales the x, y, and z components of each vector by the provided factor.
pub fn scale(factor: f32) -> Transform3h<f32> {
Transform3h { data: [
factor, 0.0, 0.0, 0.0,
0.0, factor, 0.0, 0.0,
0.0, 0.0, factor, 0.0,
0.0, 0.0, 0.0, 1.0,
] }
}
/// Return stretching matrix.
///
/// This transform scales the x components of each vector by the provided factor but leaves the y and z components intact.
pub fn stretch_x(factor: f32) -> Transform3h<f32> {
Transform3h { data: [
factor, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0,
] }
}
/// Return stretching matrix.
///
/// This transform scales the y components of each vector by the provided factor but leaves the x and z components intact.
pub fn stretch_y(factor: f32) -> Transform3h<f32> {
Transform3h { data: [
1.0, 0.0, 0.0, 0.0,
0.0, factor, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0,
] }
}
/// Return stretching matrix.
///
/// This transform scales the z components of each vector by the provided factor but leaves the x and y components intact.
pub fn stretch_z(factor: f32) -> Transform3h<f32> {
Transform3h { data: [
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, factor, 0.0,
0.0, 0.0, 0.0, 1.0,
] }
}
/// Return skew matrix.
///
/// This transform skews the vector along the x axis,
/// changing the value proportionally to the y and z values according to the provided factors.
pub fn skew_x(y_factor: f32, z_factor: f32) -> Transform3h<f32> {
Transform3h { data: [
1.0, y_factor, z_factor, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0,
] }
}
/// Return skew matrix.
///
/// This transform skews the vector along the y axis,
/// changing the value proportionally to the x and z values according to the provided factors.
pub fn skew_y(x_factor: f32, z_factor: f32) -> Transform3h<f32> {
Transform3h { data: [
1.0, 0.0, 0.0, 0.0,
x_factor, 1.0, z_factor, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0,
] }
}
/// Return skew matrix.
///
/// This transform skews the vector along the z axis,
/// changing the value proportionally to the x and y values according to the provided factors.
pub fn skew_z(x_factor: f32, y_factor: f32) -> Transform3h<f32> {
Transform3h { data: [
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
x_factor, y_factor, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0,
] }
}
/// Return translation matrix.
///
/// This transform translates the vector in 3D space by the given deltas.
pub fn translate(delta_x: f32, delta_y: f32, delta_z: f32) -> Transform3h<f32> {
Transform3h { data: [
1.0, 0.0, 0.0, delta_x,
0.0, 1.0, 0.0, delta_y,
0.0, 0.0, 1.0, delta_z,
0.0, 0.0, 0.0, 1.0,
] }
}
/// Return rotation matrix about x axis.
///
/// This transform rotates the vector about the x axis by theta radians.
pub fn rotation_x(theta_x: f32) -> Transform3h<f32> {
Transform3h { data: [
1.0, 0.0, 0.0, 0.0,
0.0, theta_x.cos(), -theta_x.sin(), 0.0,
0.0, theta_x.sin(), theta_x.cos(), 0.0,
0.0, 0.0, 0.0, 1.0,
] }
}
/// Return rotation matrix about y axis.
///
/// This transform rotates the vector about the y axis by theta radians.
pub fn rotation_y(theta_y: f32) -> Transform3h<f32> {
Transform3h { data: [
theta_y.cos(), 0.0, theta_y.sin(), 0.0,
0.0, 1.0, 0.0, 0.0,
-theta_y.sin(), 0.0, theta_y.cos(), 0.0,
0.0, 0.0, 0.0, 1.0,
] }
}
/// Return rotation matrix about z axis.
///
/// This transform rotates the vector about the z axis by theta radians.
pub fn rotation_z(theta_z: f32) -> Transform3h<f32> {
Transform3h { data: [
theta_z.cos(), -theta_z.sin(), 0.0, 0.0,
theta_z.sin(), theta_z.cos(), 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0,
] }
}
/// Return rotation matrix about arbitrary axis. Assumes axis components are of unit vector.
///
/// This transform rotates the vector about a specified unit axis by theta radians.
pub fn rotation_arbitrary(axis_x: f32, axis_y: f32, axis_z: f32, theta: f32) -> Transform3h<f32> {
let ct: f32 = theta.cos();
let st: f32 = theta.sin();
Transform3h { data: [
ct + (1.0 - ct) * axis_x * axis_x,
(1.0 - ct) * axis_x * axis_y - st * axis_z,
(1.0 - ct) * axis_x * axis_z + st * axis_y,
0.0,
(1.0 - ct) * axis_y * axis_x + st * axis_z,
ct + (1.0 - ct) * axis_y * axis_y,
(1.0 - ct) * axis_y * axis_z - st * axis_x,
0.0,
(1.0 - ct) * axis_z * axis_x - st * axis_y,
(1.0 - ct) * axis_z * axis_y + st * axis_x,
ct + (1.0 - ct) * axis_z * axis_z,
0.0,
0.0,
0.0,
0.0,
1.0
] }
}
}
impl Transform3h<f64> {
/// Return identity matrix.
///
/// This transform keeps every vector the same.
pub fn identity() -> Transform3h<f64> {
Transform3h { data: [
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0,
] }
}
/// Return scaling matrix.
///
/// This transform scales the x, y, and z components of each vector by the provided factor.
pub fn scale(factor: f64) -> Transform3h<f64> {
Transform3h { data: [
factor, 0.0, 0.0, 0.0,
0.0, factor, 0.0, 0.0,
0.0, 0.0, factor, 0.0,
0.0, 0.0, 0.0, 1.0,
] }
}
/// Return stretching matrix.
///
/// This transform scales the x components of each vector by the provided factor but leaves the y and z components intact.
pub fn stretch_x(factor: f64) -> Transform3h<f64> {
Transform3h { data: [
factor, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0,
] }
}
/// Return stretching matrix.
///
/// This transform scales the y components of each vector by the provided factor but leaves the x and z components intact.
pub fn stretch_y(factor: f64) -> Transform3h<f64> {
Transform3h { data: [
1.0, 0.0, 0.0, 0.0,
0.0, factor, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0,
] }
}
/// Return stretching matrix.
///
/// This transform scales the z components of each vector by the provided factor but leaves the x and y components intact.
pub fn stretch_z(factor: f64) -> Transform3h<f64> {
Transform3h { data: [
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, factor, 0.0,
0.0, 0.0, 0.0, 1.0,
] }
}
/// Return skew matrix.
///
/// This transform skews the vector along the x axis,
/// changing the value proportionally to the y and z values according to the provided factors.
pub fn skew_x(y_factor: f64, z_factor: f64) -> Transform3h<f64> {
Transform3h { data: [
1.0, y_factor, z_factor, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0,
] }
}
/// Return skew matrix.
///
/// This transform skews the vector along the y axis,
/// changing the value proportionally to the x and z values according to the provided factors.
pub fn skew_y(x_factor: f64, z_factor: f64) -> Transform3h<f64> {
Transform3h { data: [
1.0, 0.0, 0.0, 0.0,
x_factor, 1.0, z_factor, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0,
] }
}
/// Return skew matrix.
///
/// This transform skews the vector along the z axis,
/// changing the value proportionally to the x and y values according to the provided factors.
pub fn skew_z(x_factor: f64, y_factor: f64) -> Transform3h<f64> {
Transform3h { data: [
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
x_factor, y_factor, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0,
] }
}
/// Return translation matrix.
///
/// This transform translates the vector in 3D space by the given deltas.
pub fn translate(delta_x: f64, delta_y: f64, delta_z: f64) -> Transform3h<f64> {
Transform3h { data: [
1.0, 0.0, 0.0, delta_x,
0.0, 1.0, 0.0, delta_y,
0.0, 0.0, 1.0, delta_z,
0.0, 0.0, 0.0, 1.0,
] }
}
/// Return rotation matrix about x axis.
///
/// This transform rotates the vector about the x axis by theta radians.
pub fn rotation_x(theta_x: f64) -> Transform3h<f64> {
Transform3h { data: [
1.0, 0.0, 0.0, 0.0,
0.0, theta_x.cos(), -theta_x.sin(), 0.0,
0.0, theta_x.sin(), theta_x.cos(), 0.0,
0.0, 0.0, 0.0, 1.0,
] }
}
/// Return rotation matrix about y axis.
///
/// This transform rotates the vector about the y axis by theta radians.
pub fn rotation_y(theta_y: f64) -> Transform3h<f64> {
Transform3h { data: [
theta_y.cos(), 0.0, theta_y.sin(), 0.0,
0.0, 1.0, 0.0, 0.0,
-theta_y.sin(), 0.0, theta_y.cos(), 0.0,
0.0, 0.0, 0.0, 1.0,
] }
}
/// Return rotation matrix about z axis.
///
/// This transform rotates the vector about the z axis by theta radians.
pub fn rotation_z(theta_z: f64) -> Transform3h<f64> {
Transform3h { data: [
theta_z.cos(), -theta_z.sin(), 0.0, 0.0,
theta_z.sin(), theta_z.cos(), 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0,
] }
}
/// Return rotation matrix about arbitrary axis. Assumes axis components are of unit vector.
///
/// This transform rotates the vector about a specified unit axis by theta radians.
pub fn rotation_arbitrary(axis_x: f64, axis_y: f64, axis_z: f64, theta: f64) -> Transform3h<f64> {
let ct: f64 = theta.cos();
let st: f64 = theta.sin();
Transform3h { data: [
ct + (1.0 - ct) * axis_x * axis_x,
(1.0 - ct) * axis_x * axis_y - st * axis_z,
(1.0 - ct) * axis_x * axis_z + st * axis_y,
0.0,
(1.0 - ct) * axis_y * axis_x + st * axis_z,
ct + (1.0 - ct) * axis_y * axis_y,
(1.0 - ct) * axis_y * axis_z - st * axis_x,
0.0,
(1.0 - ct) * axis_z * axis_x - st * axis_y,
(1.0 - ct) * axis_z * axis_y + st * axis_x,
ct + (1.0 - ct) * axis_z * axis_z,
0.0,
0.0,
0.0,
0.0,
1.0
] }
}
}
/// Get value of transformation matrix at index [row, col]
impl<T: IsFloat> ops::Index<[usize; 2]> for Transform3h<T> {
type Output = T;
fn index(&self, idx: [usize; 2]) -> &T {
return &self.data[idx[0] * 4 + idx[1]]
}
}
/// Modify value of transformation matrix at index [row, col]
impl<T: IsFloat> ops::IndexMut<[usize; 2]> for Transform3h<T> {
fn index_mut(&mut self, idx: [usize; 2]) -> &mut T {
return &mut self.data[idx[0] * 4 + idx[1]]
}
}
/// Multiply transformation matrix by transformation matrix
impl ops::Mul<Transform3h<f32>> for Transform3h<f32> {
type Output = Transform3h<f32>;
fn mul(self, rhs: Transform3h<f32>) -> Transform3h<f32> {
let mut data: [f32; 16] = [0.0; 16];
for lhs_row in 0..4 {
for rhs_col in 0..4 {
data[lhs_row * 4 + rhs_col] = (0..4).into_iter()
.map(|idx| self.data[lhs_row * 4 + idx] * rhs.data[idx * 4 + rhs_col])
.sum();
}
}
Transform3h { data }
}
}
/// Multiply transformation matrix by transformation matrix
impl ops::Mul<Transform3h<f64>> for Transform3h<f64> {
type Output = Transform3h<f64>;
fn mul(self, rhs: Transform3h<f64>) -> Transform3h<f64> {
let mut data: [f64; 16] = [0.0; 16];
for lhs_row in 0..4 {
for rhs_col in 0..4 {
data[lhs_row * 4 + rhs_col] = (0..4).into_iter()
.map(|idx| self.data[lhs_row * 4 + idx] * rhs.data[idx * 4 + rhs_col])
.sum();
}
}
Transform3h { data }
}
}
/// Multiply transformation matrix reference by vector reference
impl ops::Mul<&Vec3h<f32>> for &Transform3h<f32> {
type Output = Vec3h<f32>;
fn mul(self, rhs: &Vec3h<f32>) -> Vec3h<f32> {
Vec3h {
x: rhs.x * self.data[0] + rhs.y * self.data[1] + rhs.z * self.data[2] + rhs.w * self.data[3],
y: rhs.x * self.data[4] + rhs.y * self.data[5] + rhs.z * self.data[6] + rhs.w * self.data[7],
z: rhs.x * self.data[8] + rhs.y * self.data[9] + rhs.z * self.data[10] + rhs.w * self.data[11],
w: rhs.x * self.data[12] + rhs.y * self.data[13] + rhs.z * self.data[14] + rhs.w * self.data[15],
}
}
}
/// Multiply transformation matrix reference by vector reference
impl ops::Mul<&Vec3h<f64>> for &Transform3h<f64> {
type Output = Vec3h<f64>;
fn mul(self, rhs: &Vec3h<f64>) -> Vec3h<f64> {
Vec3h {
x: rhs.x * self.data[0] + rhs.y * self.data[1] + rhs.z * self.data[2] + rhs.w * self.data[3],
y: rhs.x * self.data[4] + rhs.y * self.data[5] + rhs.z * self.data[6] + rhs.w * self.data[7],
z: rhs.x * self.data[8] + rhs.y * self.data[9] + rhs.z * self.data[10] + rhs.w * self.data[11],
w: rhs.x * self.data[12] + rhs.y * self.data[13] + rhs.z * self.data[14] + rhs.w * self.data[15],
}
}
}