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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
//! Transformation matrices for common transformations.
//!
//! - **Linear** transformations:
//! - Scale: [`scale`] and [`scale_nonuniform`]
//!
//! - **Affine** transformations:
//! - [`translate`]
//! - [`look_into`] (world space to camera/view space; rotation + translation)
//!
//! - **Perspective projection**: [`perspective`]
//!
use RangeInclusive;
use crate::;
/// Linear transformation matrix that scales all `N` axis by `factor`.
///
/// Example for `Mat3` (with `f` being `factor`):
///
/// ```text
/// ⎡ f 0 0 ⎤
/// ⎢ 0 f 0 ⎥
/// ⎣ 0 0 f ⎦
/// ```
///
/// # Example
///
/// ```
/// use lina::{Mat3f, transform, vec3};
///
/// let m = transform::scale(3.5);
///
/// assert_eq!(m, Mat3f::from_rows([
/// [3.5, 0.0, 0.0],
/// [0.0, 3.5, 0.0],
/// [0.0, 0.0, 3.5],
/// ]));
/// assert_eq!(m.transform(vec3(1.0, 2.0, 3.0)), vec3(3.5, 7.0, 10.5));
/// ```
/// Linear transformation matrix that scales each axis according to `factors`.
///
/// Equivalent to [`Matrix::from_diagonal`]. Example for `Mat3` (with `factors`
/// being `[x, y, z]`):
///
/// ```text
/// ⎡ x 0 0 ⎤
/// ⎢ 0 y 0 ⎥
/// ⎣ 0 0 z ⎦
/// ```
///
/// # Example
///
/// ```
/// use lina::{Mat3f, transform, vec3};
///
/// let m = transform::scale_nonuniform([2.0f32, 3.0, 8.0]);
///
/// assert_eq!(m, Mat3f::from_rows([
/// [2.0, 0.0, 0.0],
/// [0.0, 3.0, 0.0],
/// [0.0, 0.0, 8.0],
/// ]));
/// assert_eq!(m.transform(vec3(10.0, 20.0, 30.0)), vec3(20.0, 60.0, 240.0));
/// ```
/// 3D Rotation around the x-axis by `angle` (using the right-hand rule).
///
/// ```text
/// ⎡ 1 0 0 ⎤
/// ⎢ 0 cos(θ) -sin(θ) ⎥
/// ⎣ 0 sin(θ) cos(θ) ⎦
/// ```
/// 3D Rotation around the y-axis by `angle` (using the right-hand rule).
///
/// ```text
/// ⎡ cos(θ) 0 sin(θ) ⎤
/// ⎢ 0 1 0 ⎥
/// ⎣ -sin(θ) 0 cos(θ) ⎦
/// ```
/// 3D Rotation around the z-axis by `angle` (using the right-hand rule).
///
/// ```text
/// ⎡ cos(θ) -sin(θ) 0 ⎤
/// ⎢ sin(θ) cos(θ) 0 ⎥
/// ⎣ 0 0 1 ⎦
/// ```
/// 3D Rotation around the given axis by `angle` (using the right-hand rule).
/// 3D rotation to align `from` with `to`, i.e. `M * from = to`.
///
/// Panics if `from == -to`, as in that case, the rotation is ambigious/not well
/// defined.
///
/// ```should_panic
/// use lina::{vec3, transform};
///
/// let d = vec3(1.0, 2.0, 0.0).to_dir();
/// let m = transform::rotate3d_aligning(d, -d);
/// ```
/// Affine transformation matrix that translates according to `v`.
///
/// Example for `HcMat3` (with `v` being `[x, y, z]`):
///
/// ```text
/// ⎡ 1 0 0 x ⎤
/// ⎢ 0 1 0 y ⎥
/// ⎢ 0 0 1 z ⎥
/// ⎣ 0 0 0 1 ⎦
/// ```
///
/// # Example
///
/// ```
/// use lina::{HcMat3f, transform, vec3, point3};
///
/// let m = transform::translate(vec3(2.0, 3.0, 8.0));
///
/// assert_eq!(m, HcMat3f::from_rows([
/// [1.0, 0.0, 0.0, 2.0],
/// [0.0, 1.0, 0.0, 3.0],
/// [0.0, 0.0, 1.0, 8.0],
/// [0.0, 0.0, 0.0, 1.0],
/// ]));
/// assert_eq!(m.transform(point3(10.0, 20.0, 30.0)), point3(12.0, 23.0, 38.0));
/// ```
/// Affine transformation from world space into camera/view space, typically
/// called the "view matrix".
///
/// In view space, the camera is at the origin, +x points right, +y points up.
/// This view space is right-handed, and thus, +z points outside of the screen
/// and -z points into the screen. Please see [the part on handedness in the
/// these docs][hand-docs] for more information. The returned matrix only
/// translates and rotates, meaning that sizes and angles are unchanged
/// compared to world space.
///
/// The following camera properties have to be given:
/// - `eye`: the position of the camera.
/// - `direction`: the direction the camera is looking in. **Must not** be the
/// zero vector. Does not have to be normalized.
/// - `up`: a vector defining "up" in camera space. **Must not** be the zero
/// vector and **must not** be linearly dependent to `direction` (i.e. they
/// must not point into the same or exactly opposite directions). Does not
/// have to be normalized.
///
/// To avoid float precision problems, `direction` and `up` should *not* have
/// a tiny length and should *not* point in *almost* the same direction.
///
///
///
/// [hand-docs]: ../docs/viewing_pipeline/index.html#choice-of-view-space--handedness
///
/// ## A note on the `up` vector
///
/// There are two main types of cameras in games that are distinguished by
/// whether or not they can "fly a loop" (think airplane game with
/// out-of-cockpit camera).
///
/// In most games, this looping ability is not necessary: in those games, if you
/// move the mouse/controller all the way up or down, the camera stops turning
/// once you look almost straight down or up. Those are usually games with a
/// clear "down" direction (e.g. gravity). In these cases, you usually just
/// pass `(0, 0, 1)` (or `(0, 1, 0)` if you prefer your +y up) as `up` vector
/// and make sure the player cannot look exactly up or down. The latter you can
/// achieve by just having a min and max vertical angle (e.g. 1° and 179°).
///
/// In games where a looping camera is required, you have to maintain and evolve
/// the `up` vector over time. For example, if the player moves the
/// mouse/controller you don't just adjust the look direction, but also the up
/// vector.
/// Homogeneous transformation for *perspective* projection from view space to
/// NDC/projection space.
///
/// View space is assumed to be right-handed, i.e. +y pointing up and -z
/// pointing into the screen (satisfied by [`look_into`]). In NDC, `x/w` and
/// `y/w` are in range -1 to 1 and denote the horizontal and vertical screen
/// position, respectively. The +x axis points to the right, the +y axis points
/// up. `z` represents the depth and `z/w` is in range `depth_range_out`.
///
/// **Function inputs**:
///
/// - `vertical_fov`: the vertical field of view of your projection. Has to be
/// less than half a turn (π radians or 180°)!
///
/// - `aspect_ratio`: `width / height` of your target surface, e.g. screen or
/// application window. Has to be positive!
///
/// - `depth_range_in`: the near and far plane of the projection, in world or
/// view space (equivalent since the view matrix does not change distances).
/// The far plane may be ∞ (e.g. `f32::INFINITY`), which is handled properly
/// by this function.
///
/// - `depth_range_out`: the `z` range after the transformation. For `a..=b`,
/// `a` is what the near plane is mapped to and `b` is what the far plane is
/// mapped to. Usually, only the following values make sense:
/// - `0.0..=1.0` as default for WebGPU, Direct3D, Metal, Vulkan.
/// - `-1.0..=1.0` as default for OpenGL.
/// - `1.0..=0.0` for *reverse z* projection. Using this together with a
/// floating point depth buffer is **strongly recommended** as it vastly
/// improves depth precision.
///
///
/// # Example
///
/// (Don't use the finite near and far plane values in this example as "good
/// defaults" for your application. Use values fitting for your use case.)
///
/// ```
/// use lina::{Degrees, transform, HcMat3f};
///
/// let m = transform::perspective(Degrees(90.0), 2.0, 0.1..=f32::INFINITY, 1.0..=0.0);
/// assert_eq!(m, HcMat3f::from_rows([
/// [0.5, 0.0, 0.0, 0.0],
/// [0.0, 1.0, 0.0, 0.0],
/// [0.0, 0.0, 0.0, 0.1],
/// [0.0, 0.0, -1.0, 0.0],
/// ]));
///
/// let m = transform::perspective(Degrees(90.0), 1.0, 0.1..=100.0, 0.0..=1.0);
/// assert_eq!(m, HcMat3f::from_rows([
/// [1.0, 0.0, 0.0, 0.0],
/// [0.0, 1.0, 0.0, 0.0],
/// [0.0, 0.0, -1.001001, -0.1001001],
/// [0.0, 0.0, -1.0, 0.0],
/// ]));
/// ```
///
///
/// # Adjustments for different view spaces or NDCs
///
/// Different spaces just require minor adjustments of the input or output
/// points. These adjustments can be represented as matrices, of course. And
/// since you can multiply them with the matrix returned by this function, you
/// end up with one correct matrix.
///
/// ## Left-handed view space (+z pointing into the screen)
///
/// Flip the `z` sign of all your points *before* transforming with this matrix.
///
/// ```
/// use lina::{Degrees, HcMat3f, transform, ViewSpace};
///
/// let flip_z_sign = <HcMat3f<ViewSpace, ViewSpace>>::from_diagonal([1.0, 1.0, -1.0, 1.0]);
/// let rh_proj_matrix = transform::perspective(Degrees(90.0), 2.0, 0.1..=100.0, 1.0..=0.0);
/// let lh_proj_matrix = flip_z_sign.and_then(rh_proj_matrix);
/// ```
///
/// ## +y pointing down in NDC (Vulkan)
///
/// Flip the `y` sign of all your points *after* transforming with this matrix.
///
/// ```
/// use lina::{Degrees, HcMat3f, transform, ProjSpace};
///
/// let flip_y_sign = <HcMat3f<ProjSpace, ProjSpace>>::from_diagonal([1.0, -1.0, 1.0, 1.0]);
/// let y_up_proj_matrix = transform::perspective(Degrees(90.0), 2.0, 0.1..=100.0, 1.0..=0.0);
/// let y_down_proj_matrix = y_up_proj_matrix.and_then(flip_y_sign);
/// ```
/// Homogeneous transformation for *orthographic* projection from view space to
/// NDC/projection space.
///
/// With orthographic projection, also called parallel projection, things
/// further from the camera don't get smaller. All "view rays" are parallel.
/// It's the limit of pulling the camera back and zooming in at the same time.
/// It's eseentially unsuitable for first-person cameras as we humans are not
/// really used to this kind of projection. Instead, its most common use in
/// games is for shadow mapping. But "top down" (e.g. strategy) games could
/// make use of this projection for the main camera as well.
///
/// View space is assumed to be right-handed, i.e. +y pointing up and -z
/// pointing into the screen (satisfied by [`look_into`]). In NDC, `x` and `y`
/// are in range -1 to 1 and denote the horizontal and vertical screen
/// position, respectively. The +x axis points to the right, the +y axis points
/// up. `z` represents the depth and is in range `depth_range_out`.
///
/// Unlike with [`perspective`], this matrix maps `z` linearly from
/// `depth_range_in` to `depth_range_out`. Consequently, you cannot use
/// infinity for any of those values. On the other hand, you can now pass 0 as
/// near plane. Also note that the "reverse depth + float buffer"-trick does
/// not make sense with this matrix; you likely want an integer depth buffer as
/// this provides constant precision.
///
/// This matrix maps points inside one axis aligned box to another. The
/// following table shows the dimensions of these boxes, which depend on the
/// arguments to this function:
///
/// | | Input Box | Output Box |
/// | --- | ---------------- | ----------------- |
/// | `x` | `left..=right` | `-1..1` |
/// | `y` | `bottom..=top` | `-1..1` |
/// | `z` | `depth_range_in` | `depth_range_out` |
///
/// Note: if your camera (the input to the view matrix) is already at the center
/// of your projection, `left = -right` and `bottom = -top`. Having the
/// flexibility to set all these bounds independently means you don't have to
/// correctly position your camera, which can be convenient in some
/// situations.
///
/// For using this with a different view or NDC space, see [`perspective`].
///
/// # Example
///
/// ```
/// use lina::{Degrees, transform, HcMat3f};
///
/// let m = transform::ortho(-25.0, 25.0, -12.5, 12.5, 0.0..=100.0, 0.0..=1.0);
/// assert_eq!(m, HcMat3f::from_rows([
/// [0.04, 0.0, 0.0, 0.0],
/// [ 0.0, 0.08, 0.0, 0.0],
/// [ 0.0, 0.0, -0.01, 0.0],
/// [ 0.0, 0.0, 0.0, 1.0],
/// ]));
/// ```