oarust 0.15.1

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
//! Locked skeletal-joint transform value.
//!
//! The matrix contract follows Maya's joint transform semantics while
//! retaining OA composition: scale/shear, scale orientation (rotateAxis),
//! animated rotation, joint orientation, optional parent-scale compensation,
//! translation.
//!
//! Donor: `oa/animation/joint.h` and `oa/animation/fnJoint.{h,cpp}`. The
//! donor's `Joint` methods already forward every behavior to `FnJoint`; the
//! Rust port keeps the methods as the single implementation and drops the
//! separate operation namespace.

use crate::{
	Error, Result,
	core::Transform,
	vlm::{AffineDecomposition, Mat4, Quat, RotationOrder, Vec3},
};

/// Per-axis degrees-of-freedom flags.
///
/// Donor: `oa::JointDegreesOfFreedom`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct JointDegreesOfFreedom {
	pub x: bool,
	pub y: bool,
	pub z: bool,
}

impl Default for JointDegreesOfFreedom {
	fn default() -> Self {
		Self {
			x: true,
			y: true,
			z: true,
		}
	}
}

/// Optional per-axis rotation limits in degrees.
///
/// An axis is clamped only when the matching enabled flag is set; enabling
/// both bounds on an axis requires `minimum <= maximum` on it. Limits are
/// disabled by default even though [`JointDegreesOfFreedom::default`] is
/// all-enabled.
///
/// Donor: `oa::JointRotationLimits`.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct JointRotationLimits {
	pub minimum_degrees: Vec3,
	pub maximum_degrees: Vec3,
	pub minimum_enabled: JointDegreesOfFreedom,
	pub maximum_enabled: JointDegreesOfFreedom,
}

impl Default for JointRotationLimits {
	fn default() -> Self {
		Self {
			minimum_degrees: Vec3::default(),
			maximum_degrees: Vec3::default(),
			minimum_enabled: JointDegreesOfFreedom {
				x: false,
				y: false,
				z: false,
			},
			maximum_enabled: JointDegreesOfFreedom {
				x: false,
				y: false,
				z: false,
			},
		}
	}
}

/// Locked skeletal-joint transform value.
///
/// Donor: `oa::Joint`. Setters that the donor guards with contract aborts
/// return `Result` here instead.
#[derive(Clone, Debug, PartialEq)]
pub struct Joint {
	transform: Transform,
	orientation: Quat,
	scale_orientation: Quat,
	segment_scale_compensate: bool,
	rotation_order: RotationOrder,
	degrees_of_freedom: JointDegreesOfFreedom,
	rotation_limits: JointRotationLimits,
	preferred_angles: Vec3,
	stiffness: Vec3,
}

impl Default for Joint {
	fn default() -> Self {
		Self {
			transform: Transform::default(),
			orientation: Quat::identity(),
			scale_orientation: Quat::identity(),
			segment_scale_compensate: true,
			rotation_order: RotationOrder::Xyz,
			degrees_of_freedom: JointDegreesOfFreedom::default(),
			rotation_limits: JointRotationLimits::default(),
			preferred_angles: Vec3::default(),
			stiffness: Vec3::default(),
		}
	}
}

impl From<Transform> for Joint {
	fn from(transform: Transform) -> Self {
		Self {
			transform,
			..Self::default()
		}
	}
}

fn valid_quaternion(value: Quat) -> bool {
	value.is_finite() && value.norm_squared().is_finite() && value.norm_squared() > 0.0
}

fn valid_stiffness(value: Vec3) -> bool {
	value.is_finite() && value.x >= 0.0 && value.y >= 0.0 && value.z >= 0.0
}

fn valid_limits(limits: &JointRotationLimits) -> bool {
	if !limits.minimum_degrees.is_finite() || !limits.maximum_degrees.is_finite() {
		return false;
	}
	let x_valid = !(limits.minimum_enabled.x && limits.maximum_enabled.x)
		|| limits.minimum_degrees.x <= limits.maximum_degrees.x;
	let y_valid = !(limits.minimum_enabled.y && limits.maximum_enabled.y)
		|| limits.minimum_degrees.y <= limits.maximum_degrees.y;
	let z_valid = !(limits.minimum_enabled.z && limits.maximum_enabled.z)
		|| limits.minimum_degrees.z <= limits.maximum_degrees.z;
	x_valid && y_valid && z_valid
}

fn clamp_axis(
	value: f32,
	minimum: f32,
	maximum: f32,
	minimum_enabled: bool,
	maximum_enabled: bool,
) -> f32 {
	if minimum_enabled && value < minimum {
		return minimum;
	}
	if maximum_enabled && value > maximum {
		return maximum;
	}
	value
}

impl Joint {
	/// Validate the complete authoring state.
	///
	/// # Errors
	///
	/// Returns an error for a non-finite transform or orientation, an
	/// inverted enabled limit pair, non-finite preferred angles, or
	/// non-finite/negative stiffness.
	pub fn validate(&self) -> Result<()> {
		self.transform.validate()?;
		if !valid_quaternion(self.orientation) || !valid_quaternion(self.scale_orientation) {
			return Err(Error::invalid_argument(
				"Joint orientations must be finite non-zero quaternions",
			));
		}
		if !valid_limits(&self.rotation_limits) {
			return Err(Error::invalid_argument("Joint rotation limits are invalid"));
		}
		if !self.preferred_angles.is_finite() {
			return Err(Error::invalid_argument(
				"Joint preferred angles must be finite",
			));
		}
		if !valid_stiffness(self.stiffness) {
			return Err(Error::invalid_argument(
				"Joint stiffness must be finite and non-negative",
			));
		}
		Ok(())
	}

	/// Animated local transform.
	pub fn transform(&self) -> &Transform {
		&self.transform
	}

	/// Mutable access to the animated local transform.
	pub fn transform_mut(&mut self) -> &mut Transform {
		&mut self.transform
	}

	/// Joint orientation (Maya jointOrient); normalized on assignment.
	///
	/// # Errors
	///
	/// Returns an error for a non-finite or zero quaternion.
	pub fn set_orientation(&mut self, orientation: Quat) -> Result<()> {
		if !valid_quaternion(orientation) {
			return Err(Error::invalid_argument(
				"Joint orientation must be a finite non-zero quaternion",
			));
		}
		self.orientation = orientation.try_normalized().unwrap_or(orientation);
		Ok(())
	}

	/// Stored joint orientation.
	pub fn orientation(&self) -> Quat {
		self.orientation
	}

	/// Scale orientation (Maya rotateAxis); normalized on assignment.
	///
	/// # Errors
	///
	/// Returns an error for a non-finite or zero quaternion.
	pub fn set_scale_orientation(&mut self, orientation: Quat) -> Result<()> {
		if !valid_quaternion(orientation) {
			return Err(Error::invalid_argument(
				"Joint scale orientation must be a finite non-zero quaternion",
			));
		}
		self.scale_orientation = orientation.try_normalized().unwrap_or(orientation);
		Ok(())
	}

	/// Stored scale orientation.
	pub fn scale_orientation(&self) -> Quat {
		self.scale_orientation
	}

	/// Enable or disable parent-scale compensation.
	pub fn set_segment_scale_compensate(&mut self, enabled: bool) {
		self.segment_scale_compensate = enabled;
	}

	/// Whether parent-scale compensation applies.
	pub fn segment_scale_compensate(&self) -> bool {
		self.segment_scale_compensate
	}

	/// Euler decomposition order retained as metadata.
	pub fn set_rotation_order(&mut self, order: RotationOrder) {
		self.rotation_order = order;
	}

	/// Stored rotation order.
	pub fn rotation_order(&self) -> RotationOrder {
		self.rotation_order
	}

	/// Degrees-of-freedom flags.
	pub fn set_degrees_of_freedom(&mut self, degrees_of_freedom: JointDegreesOfFreedom) {
		self.degrees_of_freedom = degrees_of_freedom;
	}

	/// Stored degrees-of-freedom flags.
	pub fn degrees_of_freedom(&self) -> JointDegreesOfFreedom {
		self.degrees_of_freedom
	}

	/// Rotation limits in degrees.
	///
	/// # Errors
	///
	/// Returns an error for non-finite bounds or an axis whose enabled
	/// minimum exceeds its enabled maximum.
	pub fn set_rotation_limits(&mut self, limits: JointRotationLimits) -> Result<()> {
		if !valid_limits(&limits) {
			return Err(Error::invalid_argument("Joint rotation limits are invalid"));
		}
		self.rotation_limits = limits;
		Ok(())
	}

	/// Stored rotation limits.
	pub fn rotation_limits(&self) -> JointRotationLimits {
		self.rotation_limits
	}

	/// Preferred rest angles in degrees.
	///
	/// # Errors
	///
	/// Returns an error for non-finite angles.
	pub fn set_preferred_angles(&mut self, degrees: Vec3) -> Result<()> {
		if !degrees.is_finite() {
			return Err(Error::invalid_argument(
				"Joint preferred angles must be finite",
			));
		}
		self.preferred_angles = degrees;
		Ok(())
	}

	/// Stored preferred angles in degrees.
	pub fn preferred_angles(&self) -> Vec3 {
		self.preferred_angles
	}

	/// Per-axis stiffness in `[0, ∞)`.
	///
	/// # Errors
	///
	/// Returns an error for non-finite or negative stiffness.
	pub fn set_stiffness(&mut self, stiffness: Vec3) -> Result<()> {
		if !valid_stiffness(stiffness) {
			return Err(Error::invalid_argument(
				"Joint stiffness must be finite and non-negative",
			));
		}
		self.stiffness = stiffness;
		Ok(())
	}

	/// Stored stiffness.
	pub fn stiffness(&self) -> Vec3 {
		self.stiffness
	}

	/// Animated rotation expressed in joint orientation space:
	/// `orientation * transform.rotation`, normalized.
	pub fn oriented_rotation(&self) -> Quat {
		(self.orientation * self.transform.rotation())
			.try_normalized()
			.unwrap_or_else(Quat::identity)
	}

	/// Clamp rotation angles in degrees to the enabled limits.
	///
	/// # Errors
	///
	/// Returns an error for non-finite input.
	pub fn clamp_rotation_degrees(&self, degrees: Vec3) -> Result<Vec3> {
		if !degrees.is_finite() {
			return Err(Error::invalid_argument(
				"Joint rotation angles must be finite",
			));
		}
		let limits = &self.rotation_limits;
		Ok(Vec3 {
			x: clamp_axis(
				degrees.x,
				limits.minimum_degrees.x,
				limits.maximum_degrees.x,
				limits.minimum_enabled.x,
				limits.maximum_enabled.x,
			),
			y: clamp_axis(
				degrees.y,
				limits.minimum_degrees.y,
				limits.maximum_degrees.y,
				limits.minimum_enabled.y,
				limits.maximum_enabled.y,
			),
			z: clamp_axis(
				degrees.z,
				limits.minimum_degrees.z,
				limits.maximum_degrees.z,
				limits.minimum_enabled.z,
				limits.maximum_enabled.z,
			),
		})
	}

	/// Compose the joint matrix in Maya factor order:
	/// `S*Shear * SO * R * O * P^-1 * T` under the row-vector convention.
	///
	/// When segment-scale compensation is enabled, `parent_scale` must be
	/// finite and non-zero on every axis.
	///
	/// # Errors
	///
	/// Returns an error when the joint state is invalid, `parent_scale` is
	/// non-finite, or compensation needs a zero parent-scale axis.
	pub fn matrix(&self, parent_scale: Vec3) -> Result<Mat4> {
		self.validate()?;
		if !parent_scale.is_finite() {
			return Err(Error::invalid_argument("Joint parent scale must be finite"));
		}
		let mut inverse_parent_scale = Vec3 {
			x: 1.0,
			y: 1.0,
			z: 1.0,
		};
		if self.segment_scale_compensate {
			if parent_scale.x == 0.0 || parent_scale.y == 0.0 || parent_scale.z == 0.0 {
				return Err(Error::invalid_argument(
					"Joint segment-scale compensation requires non-zero parent scale",
				));
			}
			inverse_parent_scale = Vec3 {
				x: 1.0 / parent_scale.x,
				y: 1.0 / parent_scale.y,
				z: 1.0 / parent_scale.z,
			};
		}
		let scale_and_shear = AffineDecomposition {
			scale: self.transform.scale(),
			shear: self.transform.shear(),
			..AffineDecomposition::default()
		};
		let compose = |quat: Quat| {
			Mat4::try_from_quaternion(quat)
				.ok_or_else(|| Error::invalid_argument("Joint quaternions must be normalizable"))
		};
		Ok(
			Mat4::compose_affine(scale_and_shear)
				.ok_or_else(|| Error::invalid_argument("Joint scale/shear must be finite"))?
				* compose(self.scale_orientation)?
				* compose(self.transform.rotation())?
				* compose(self.orientation)?
				* Mat4::scaling(inverse_parent_scale)
				* Mat4::translation(self.transform.position()),
		)
	}
}