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
//! Linear algebra library with a focus on computer graphics, heavily using
//! strong typing and const generics (while still allowing access via `.x`,
//! `.y`, `.z` and `.w`).
//!
//! `lina` leans heavily into strong typing by distinguishing points and
//! vectors, Cartesian and homogeneous coordinates, and even elements from
//! different semantic spaces. See [these docs][docs::strong_typing] for more
//! information.
//!
//!
//! # Quick start / overview
//!
//! - **Locations and displacements**
//! - [`Point`] represents a location.
//! - [`Vector`] represents a displacement.
//! - [`HcPoint`] represents a point in homogeneous coordinates.
//! - [`Dir`] represents a direction via unit vector.
//! - Use [`SphericalPos`] and [`SphericalDir`] for spherical coordinates.
//! - **Transformations**
//! - [`Matrix`] represents a linear transformation.
//! - [`HcMatrix`] represents a potentially non-linear transformation in
//! homogeneous coordinates.
//! - Use [`transform`] to get common transformation matrices.
//! - Use `*` or `and_then` to combine two matrices.
//! - Use `*` or `transform` to transform a vector or point with a matrix.
//! - Operators are overloaded as you would expect.
//! - Many types have a [`Space`] parameter to use strong typing.
//! - Use `in_space` or `with_spaces` methods to cast/reinterpret the space parameter.
//! - Recommendation: make type aliases for specific types used in your app,
//! e.g. `type HelioPoint = Point<f64, 3, HelioSpace>`.
//! - Most types have a `to_bytes` method to pass them to graphic APIs.
//! - Other features:
//! - Strongly typed angles: [`Degrees`] and [`Radians`]
//! - Useful functions: [`atan2`], [`clamp`], [`lerp`], [`slerp`], [`cross`], [`dot`], ...
//! - [`ApproxEq`] for approximate float equality
//!
//! This example shows some basic usage:
//!
//! ```
//! use lina::{point3, vec3, transform, Degrees, Vector};
//!
//! // Basic vector/point usage
//! let player_pos = point3(4.0, 5.0, 1.8);
//! let fox_pos = point3(10.0, 3.0, 0.5);
//! let view_direction = vec3(1.3, 0.2, 0.0).normalized();
//! let speed = 1.5;
//! let new_player_pos = player_pos + view_direction * speed;
//!
//! println!("{:.2}m still to go!", player_pos.distance_from(fox_pos));
//!
//! // Create and compose transformation matrices
//! let view_matrix = transform::look_into(new_player_pos, view_direction, Vector::unit_z());
//! let proj_matrix = transform::perspective(
//! Degrees(90.0),
//! 16.0 / 9.0,
//! 0.1..=f32::INFINITY,
//! 1.0..=0.0,
//! );
//! let view_proj = view_matrix.and_then(proj_matrix); // or `proj_matrix * view_matrix`
//!
//! // Transform points with matrices
//! let fox_on_screen = view_proj.transform(fox_pos); // or `view_proj * fox_pos`
//! ```
//!
//! ## Const generics limitations
//!
//! To express the signature of some functions, `feature(generic_const_exprs)`
//! is required. Think of [`Vector::extend`] which returns a `Vector<T, N + 1>`.
//! The `+ 1` is the problem here as this is currently not yet allowed on
//! stable Rust. Most of these functions are related to [`HcPoint`] or
//! [`HcMatrix`].
//!
//! Not only is that feature not stabilized yet, it is also very unfinished and
//! broken. So unfortunately, `lina` cannot use it. Instead, these functions
//! are implemented for a small number of fixed dimensions via macro. But worry
//! not! For one, these are just a few functions without which `lina` can still
//! be used without a problem. Further, for the 3D graphics use case, all
//! relevant functions exist, as one is almost never converned with anything
//! with more than 4 dimensions. So the only relevant disadvantage of this is
//! that the docs look less nice, as there are repetitions of the same
//! function.
//!
//! One further consequence of this is the choice that the const parameters of
//! `HcPoint` and `HcMatrix` don't reflect the number of values/rows/columns,
//! but the the dimension of the space in which the point lives/which the
//! transformation transforms. E.g. `HcPoint<3>` represents a 3D point, by
//! storing 4 numbers.
//!
use ;
use Pod;
use Num;
pub use ;
/// Helper utilities for matrices.
/// A scalar type in the context of this library.
///
/// This is the bare minimum `lina` requires for most operations. It is somewhat
/// restricting (e.g. excluding big nums), but this library does not aim to be
/// super generic. There are better ones for that purpose. These requirements
/// make sense for games and similar applications.
///
/// This is implemented for at least these types:
///
/// - Floats: `f32` and `f64`
/// - Signed integers: `i8`, `i16`, `i32`, `i64`, `i128`, `isize`
/// - Unsigned integers: `u8`, `u16`, `u32`, `u64`, `u128`, `usize`
/// A floating point scalar.
///
/// This is similar to [`Scalar`] as it defines coarse requirements for using
/// functions of this library. It is used whenever `Scalar` is not sufficient,
/// which is basically whenever a function does not make sense for integers.
/// This trait is implemented for at least `f32` and `f64`.
/// Returns the [cross product][wiki] `a ⨯ b`, a vector perpendicular to both
/// input vectors.
///
/// ```
/// use lina::{cross, vec3};
///
/// assert_eq!(
/// cross(vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0)),
/// vec3(0.0, 0.0, 1.0),
/// );
/// assert_eq!(
/// cross(vec3(2.0, 0.0, 2.0), vec3(2.0, 0.0, -2.0)),
/// vec3(0.0, 8.0, 0.0),
/// );
/// ```
///
/// [wiki]: https://en.wikipedia.org/wiki/Cross_product
/// Returns the [dot product][wiki] `a · b`, a scalar value.
///
/// The dot product is equal to the product of the vectors lengths/magnitudes
/// and the cosine of the angle between the two vectors. So if both input
/// vectors are normalized, the dot product is exactly `cos(a)` with `a` being
/// the angle between the two vectors.
///
/// Another way to think about the dot product is to imagine one vector being
/// projected onto the other one. The dot product is incredible useful in many
/// scenarios.
///
/// This function panics if `N = 0` as dot products of 0-dimensional vectors
/// make little sense.
///
/// ```
/// use lina::{dot, vec2};
///
/// assert_eq!(dot(vec2(0, 0), vec2(1, 1)), 0); // dot product of zero vectors are 0
/// assert_eq!(dot(vec2(-2, 0), vec2(3, 0)), -6); // product of lengths times cos(180°) = -1
/// assert_eq!(dot(vec2(8, 0), vec2(0, 5)), 0); // angle is 90°, cos(90°) = 0
/// assert_eq!(dot(vec2(1, 1), vec2(4, 0)), 4);
/// ```
///
/// [wiki]: https://en.wikipedia.org/wiki/Dot_product
/// The [`atan2` function](https://en.wikipedia.org/wiki/Atan2).
///
/// This returns the angle between the positive x axis and the vector `[x, y]`
/// (mind the switched order of the function arguments).
/// Returns the angle between the two given vectors. Returns garbage if either
/// vector has length 0.
///
/// If you already know the vectors are normalized, it's faster to manually
/// calculate `Radians::acos(dot(a, b))`, as this skips calculating the
/// vectors' lengths.
///
///
/// ```
/// use lina::{angle_between, vec2, Radians};
/// use std::f32::consts::PI;
///
/// assert_eq!(angle_between(vec2(1.0, 0.0), vec2(3.0, 0.0)), Radians(0.0));
/// assert_eq!(angle_between(vec2(-2.0, 0.0), vec2(3.0, 0.0)), Radians(PI)); // 180°
/// assert_eq!(angle_between(vec2(0.2, 0.0), vec2(0.0, 7.3)), Radians(PI / 2.0)); // 90°
/// ```
/// Clamps `val` into the given range `min..=max`.
///
/// The trait bound *should* technically be `Ord`, but that's inconvenient when
/// dealing with floats. Panics when passed a NaN.
/// Linearly interpolates between `a` and `b` with the given `factor`.
/// `factor = 0` is 100% `a`, `factor = 1` is 100% `b`.
///
/// If `factor` is outside of the range `0..=1`, the result might not make
/// sense. It is simply following the formula `(1 - factor) * a + factor * b`.
///
/// ```
/// use lina::{lerp, vec2};
///
/// assert_eq!(lerp(10.0, 20.0, 0.6), 16.0);
/// assert_eq!(lerp(vec2(10.0, -5.0), vec2(12.0, 5.0), 0.2), vec2(10.4, -3.0));
/// ```
/// *Spherical* linear interpolation between `a` and `b` with the given `factor`.
/// `factor = 0` is 100% `a`, `factor = 1` is 100% `b`.
///
/// This operation linearly interpolates the angle between the vectors, so to
/// speak. Or viewed differently: it linearly interpolates the sphere surface
/// path from one to the other vector. For more information, see here:
/// <https://en.wikipedia.org/wiki/Slerp>
///
/// The vectors must not be zero! They don't have to be normalized, but don't
/// ask me how to interpret the result if they don't have the same length. The
/// same is true for `factor`: usually only the range `0..=1` makes sense, but
/// this is not enforced. No idea if the results are useful.
///
/// ```
/// use lina::{slerp, vec3};
///
/// assert_eq!(
/// slerp(vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), 0.5),
/// vec3(0.7071067811865475, 0.7071067811865475, 0.0), // sqrt(2) / 2
/// );
/// ```
/// Projects `v` onto `target`, returning `target · (v · target)`.