oarust 0.8.6

GPU-first semantic computing engine written in Rust
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
use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};

use super::scalar::{Float, approx, degrees, radians, valid_tolerance};
use super::vector::Vec3;

/// Explicit Euler-axis application order.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum RotationOrder {
	Xyz,
	Xzy,
	Yxz,
	Yzx,
	Zxy,
	Zyx,
}

/// A packed vector-first quaternion `(x, y, z, w)`.
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Quat<T: Float = f32> {
	pub x: T,
	pub y: T,
	pub z: T,
	pub w: T,
}

pub type DQuat = Quat<f64>;

impl<T: Float> Default for Quat<T> {
	fn default() -> Self {
		Self::identity()
	}
}

impl<T: Float> Quat<T> {
	pub fn identity() -> Self {
		Self {
			x: T::ZERO,
			y: T::ZERO,
			z: T::ZERO,
			w: T::ONE,
		}
	}

	/// Constructs an axis-angle rotation, returning `None` for invalid input.
	pub fn try_from_axis_angle(axis: Vec3<T>, angle_radians: T) -> Option<Self> {
		if !angle_radians.is_finite() {
			return None;
		}
		let axis = axis.try_normalized()?;
		let half_angle = angle_radians * T::HALF;
		let sine = half_angle.sin();
		let result = Self {
			x: axis.x * sine,
			y: axis.y * sine,
			z: axis.z * sine,
			w: half_angle.cos(),
		};
		result.is_finite().then_some(result)
	}

	pub fn is_finite(self) -> bool {
		self.x.is_finite() && self.y.is_finite() && self.z.is_finite() && self.w.is_finite()
	}

	pub fn norm_squared(self) -> T {
		(self.x * self.x + self.y * self.y) + (self.z * self.z + self.w * self.w)
	}

	pub fn norm(self) -> T {
		let squared = self.norm_squared();
		if squared.is_finite() && squared > T::ZERO {
			return squared.sqrt();
		}
		if !self.is_finite() {
			return squared.sqrt();
		}
		let magnitude = self
			.x
			.abs()
			.max(self.y.abs())
			.max(self.z.abs())
			.max(self.w.abs());
		if magnitude == T::ZERO {
			return T::ZERO;
		}
		let scaled = self / magnitude;
		magnitude * scaled.norm_squared().sqrt()
	}

	/// Returns a unit quaternion, or `None` for a zero or non-finite value.
	pub fn try_normalized(self) -> Option<Self> {
		let squared = self.norm_squared();
		if squared.is_finite() && squared > T::ZERO {
			return Some(self / squared.sqrt());
		}
		if !self.is_finite() {
			return None;
		}
		let magnitude = self
			.x
			.abs()
			.max(self.y.abs())
			.max(self.z.abs())
			.max(self.w.abs());
		if magnitude <= T::ZERO {
			return None;
		}
		let scaled = self / magnitude;
		let norm = scaled.norm();
		if !norm.is_finite() || norm <= T::ZERO {
			return None;
		}
		let result = scaled / norm;
		result.is_finite().then_some(result)
	}

	pub fn conjugate(self) -> Self {
		Self {
			x: -self.x,
			y: -self.y,
			z: -self.z,
			w: self.w,
		}
	}

	pub fn dot(self, other: Self) -> T {
		self.x * other.x + self.y * other.y + self.z * other.z + self.w * other.w
	}

	pub fn try_inverse(self, tolerance: T) -> Option<Self> {
		if !valid_tolerance(tolerance) || !self.is_finite() {
			return None;
		}
		let magnitude = self
			.x
			.abs()
			.max(self.y.abs())
			.max(self.z.abs())
			.max(self.w.abs());
		if magnitude <= tolerance {
			return None;
		}
		let scaled = self / magnitude;
		let norm_squared = scaled.norm_squared();
		if !norm_squared.is_finite() || norm_squared <= T::ZERO {
			return None;
		}
		let result = (scaled.conjugate() / norm_squared) / magnitude;
		result.is_finite().then_some(result)
	}

	#[inline]
	pub fn try_rotate(self, vector: Vec3<T>) -> Option<Vec3<T>> {
		let squared = self.norm_squared();
		let unit_tolerance = T::EPSILON * T::from_f64(8.0);
		let unit = if squared.is_finite() && (squared - T::ONE).abs() <= unit_tolerance {
			self
		} else {
			self.try_normalized()?
		};
		let imaginary = Vec3 {
			x: unit.x,
			y: unit.y,
			z: unit.z,
		};
		let twice_cross = imaginary.cross(vector) * T::TWO;
		Some(vector + twice_cross * unit.w + imaginary.cross(twice_cross))
	}

	pub fn try_inverse_rotate(self, vector: Vec3<T>) -> Option<Vec3<T>> {
		self.conjugate().try_rotate(vector)
	}

	/// Creates a rotation from finite Euler angles in radians.
	pub fn try_from_euler_radians(angles: Vec3<T>, order: RotationOrder) -> Option<Self> {
		if !angles.is_finite() {
			return None;
		}
		let x = Self::try_from_axis_angle(
			Vec3 {
				x: T::ONE,
				y: T::ZERO,
				z: T::ZERO,
			},
			angles.x,
		)?;
		let y = Self::try_from_axis_angle(
			Vec3 {
				x: T::ZERO,
				y: T::ONE,
				z: T::ZERO,
			},
			angles.y,
		)?;
		let z = Self::try_from_axis_angle(
			Vec3 {
				x: T::ZERO,
				y: T::ZERO,
				z: T::ONE,
			},
			angles.z,
		)?;
		Some(match order {
			RotationOrder::Xyz => z * (y * x),
			RotationOrder::Xzy => y * (z * x),
			RotationOrder::Yxz => z * (x * y),
			RotationOrder::Yzx => x * (z * y),
			RotationOrder::Zxy => y * (x * z),
			RotationOrder::Zyx => x * (y * z),
		})
	}

	pub fn try_from_euler_degrees(angles: Vec3<T>, order: RotationOrder) -> Option<Self> {
		Self::try_from_euler_radians(
			Vec3 {
				x: radians(angles.x),
				y: radians(angles.y),
				z: radians(angles.z),
			},
			order,
		)
	}

	pub fn try_to_euler_radians(self, order: RotationOrder) -> Option<Vec3<T>> {
		let q = self.try_normalized()?;
		let xx = q.x * q.x;
		let yy = q.y * q.y;
		let zz = q.z * q.z;
		let xy = q.x * q.y;
		let xz = q.x * q.z;
		let yz = q.y * q.z;
		let xw = q.x * q.w;
		let yw = q.y * q.w;
		let zw = q.z * q.w;
		let m11 = T::ONE - T::TWO * (yy + zz);
		let m12 = T::TWO * (xy - zw);
		let m13 = T::TWO * (xz + yw);
		let m21 = T::TWO * (xy + zw);
		let m22 = T::ONE - T::TWO * (xx + zz);
		let m23 = T::TWO * (yz - xw);
		let m31 = T::TWO * (xz - yw);
		let m32 = T::TWO * (yz + xw);
		let m33 = T::ONE - T::TWO * (xx + yy);
		let lock = T::ONE - T::EPSILON * T::from_f64(64.0);
		let mut result = Vec3::default();
		match order {
			RotationOrder::Zyx => {
				result.y = m13.max(-T::ONE).min(T::ONE).asin();
				if m13.abs() < lock {
					result.x = (-m23).atan2(m33);
					result.z = (-m12).atan2(m11);
				} else {
					result.x = m32.atan2(m22);
				}
			}
			RotationOrder::Zxy => {
				result.x = -m23.max(-T::ONE).min(T::ONE).asin();
				if m23.abs() < lock {
					result.y = m13.atan2(m33);
					result.z = m21.atan2(m22);
				} else {
					result.y = (-m31).atan2(m11);
				}
			}
			RotationOrder::Yxz => {
				result.x = m32.max(-T::ONE).min(T::ONE).asin();
				if m32.abs() < lock {
					result.y = (-m31).atan2(m33);
					result.z = (-m12).atan2(m22);
				} else {
					result.z = m21.atan2(m11);
				}
			}
			RotationOrder::Xyz => {
				result.y = -m31.max(-T::ONE).min(T::ONE).asin();
				if m31.abs() < lock {
					result.x = m32.atan2(m33);
					result.z = m21.atan2(m11);
				} else {
					result.z = (-m12).atan2(m22);
				}
			}
			RotationOrder::Xzy => {
				result.z = m21.max(-T::ONE).min(T::ONE).asin();
				if m21.abs() < lock {
					result.x = (-m23).atan2(m22);
					result.y = (-m31).atan2(m11);
				} else {
					result.y = m13.atan2(m33);
				}
			}
			RotationOrder::Yzx => {
				result.z = -m12.max(-T::ONE).min(T::ONE).asin();
				if m12.abs() < lock {
					result.x = m32.atan2(m22);
					result.y = m13.atan2(m11);
				} else {
					result.x = (-m23).atan2(m33);
				}
			}
		}
		Some(result)
	}

	pub fn try_to_euler_degrees(self, order: RotationOrder) -> Option<Vec3<T>> {
		let value = self.try_to_euler_radians(order)?;
		Some(Vec3 {
			x: degrees(value.x),
			y: degrees(value.y),
			z: degrees(value.z),
		})
	}

	pub fn try_nlerp(self, other: Self, amount: T) -> Option<Self> {
		let a = self.try_normalized()?;
		let mut b = other.try_normalized()?;
		if a.dot(b) < T::ZERO {
			b = -b;
		}
		(a + (b - a) * amount).try_normalized()
	}

	pub fn try_slerp(self, other: Self, amount: T) -> Option<Self> {
		let a = self.try_normalized()?;
		let mut b = other.try_normalized()?;
		let mut cosine = a.dot(b);
		if cosine < T::ZERO {
			b = -b;
			cosine = -cosine;
		}
		cosine = cosine.max(-T::ONE).min(T::ONE);
		if cosine > T::ONE - T::TOLERANCE {
			return a.try_nlerp(b, amount);
		}
		let angle = cosine.acos();
		let sine = angle.sin();
		let weight_a = ((T::ONE - amount) * angle).sin() / sine;
		let weight_b = (amount * angle).sin() / sine;
		(a * weight_a + b * weight_b).try_normalized()
	}

	pub fn angle(self) -> Option<T> {
		let q = self.try_normalized()?;
		Some(T::TWO * q.w.abs().max(-T::ONE).min(T::ONE).acos())
	}

	pub fn axis(self) -> Option<Vec3<T>> {
		let mut q = self.try_normalized()?;
		if q.w < T::ZERO {
			q = -q;
		}
		let sine_half = (T::ONE - q.w * q.w).max(T::ZERO).sqrt();
		if sine_half <= T::TOLERANCE {
			return Some(Vec3 {
				x: T::ONE,
				y: T::ZERO,
				z: T::ZERO,
			});
		}
		Some(Vec3 {
			x: q.x / sine_half,
			y: q.y / sine_half,
			z: q.z / sine_half,
		})
	}

	pub fn approximately_equal(self, other: Self, absolute: T, relative: T) -> bool {
		approx(self.x, other.x, absolute, relative)
			&& approx(self.y, other.y, absolute, relative)
			&& approx(self.z, other.z, absolute, relative)
			&& approx(self.w, other.w, absolute, relative)
	}
}

impl<T: Float> Add for Quat<T> {
	type Output = Self;

	fn add(self, other: Self) -> Self {
		Self {
			x: self.x + other.x,
			y: self.y + other.y,
			z: self.z + other.z,
			w: self.w + other.w,
		}
	}
}

impl<T: Float> Sub for Quat<T> {
	type Output = Self;

	fn sub(self, other: Self) -> Self {
		Self {
			x: self.x - other.x,
			y: self.y - other.y,
			z: self.z - other.z,
			w: self.w - other.w,
		}
	}
}

impl<T: Float> Mul<T> for Quat<T> {
	type Output = Self;

	fn mul(self, scalar: T) -> Self {
		Self {
			x: self.x * scalar,
			y: self.y * scalar,
			z: self.z * scalar,
			w: self.w * scalar,
		}
	}
}

impl<T: Float> Mul for Quat<T> {
	type Output = Self;

	fn mul(self, other: Self) -> Self {
		Self {
			x: self.w * other.x + self.x * other.w + self.y * other.z - self.z * other.y,
			y: self.w * other.y - self.x * other.z + self.y * other.w + self.z * other.x,
			z: self.w * other.z + self.x * other.y - self.y * other.x + self.z * other.w,
			w: self.w * other.w - self.x * other.x - self.y * other.y - self.z * other.z,
		}
	}
}

impl<T: Float> Div<T> for Quat<T> {
	type Output = Self;

	fn div(self, scalar: T) -> Self {
		Self {
			x: self.x / scalar,
			y: self.y / scalar,
			z: self.z / scalar,
			w: self.w / scalar,
		}
	}
}

impl<T: Float> Neg for Quat<T> {
	type Output = Self;

	fn neg(self) -> Self {
		Self {
			x: -self.x,
			y: -self.y,
			z: -self.z,
			w: -self.w,
		}
	}
}

impl<T: Float> AddAssign for Quat<T> {
	fn add_assign(&mut self, other: Self) {
		*self = *self + other;
	}
}

impl<T: Float> SubAssign for Quat<T> {
	fn sub_assign(&mut self, other: Self) {
		*self = *self - other;
	}
}

impl<T: Float> MulAssign<T> for Quat<T> {
	fn mul_assign(&mut self, scalar: T) {
		*self = *self * scalar;
	}
}

impl<T: Float> MulAssign for Quat<T> {
	fn mul_assign(&mut self, other: Self) {
		*self = *self * other;
	}
}

impl<T: Float> DivAssign<T> for Quat<T> {
	fn div_assign(&mut self, scalar: T) {
		*self = *self / scalar;
	}
}

impl Mul<Quat<f32>> for f32 {
	type Output = Quat<f32>;

	fn mul(self, quaternion: Quat<f32>) -> Self::Output {
		quaternion * self
	}
}

impl Mul<Quat<f64>> for f64 {
	type Output = Quat<f64>;

	fn mul(self, quaternion: Quat<f64>) -> Self::Output {
		quaternion * self
	}
}