base/quaternion
쿼터니언. (원본 base/quaternion.js, 3차원 회전이고 골격 애니메이션 보간의 기본 단위다)
- 성분 순서는 원본과 같이 (x, y, z, w) 이고, w 가 실수부다. 기본값은 단위 쿼터니언 (0, 0, 0, 1) 이다.
- multiply 는 해밀턴 곱 this x other 다. 행렬로 바꾸면 R(this x other) = R(this) x R(other) 라서, 열벡터에는 other 회전이 먼저 걸린다. (원본 말로 "this 회전 뒤에 other 회전을 로컬로 누적")
- create_from_euler(x, y, z) 는 라디안이고 qX x qY x qZ 와 같다. 행렬로는 Rx x Ry x Rz 라서 점에는 Z, Y, X 순서로 걸린다. (three.js 의 'XYZ' 순서와 같은 식이다)
- 원본은 x, y, z, w 를 감춘 필드와 프로퍼티로 두지만, 러스트 판은 공개 필드를 둔 Copy 값이다. 원본 clone 은 복사로 갈음한다.
- 수는 원본 자바스크립트 수처럼 f64 다.
- 원본에 없는 행렬에서 쿼터니언으로 바꾸기, 오일러로 되돌리기는 만들지 않았다. (행렬로는 Matrix4::create_rotation_from_quaternion)
Quaternion. (the original base/quaternion.js; a 3D rotation and the basic unit of skeletal animation interpolation)
- The component order is (x, y, z, w) as in the original, with w the real part. The default is the identity quaternion (0, 0, 0, 1).
- multiply is the Hamilton product this x other. As matrices, R(this x other) = R(this) x R(other), so the other rotation applies first to column vectors. (in the original's words, "accumulates the other rotation locally after the this rotation")
- create_from_euler(x, y, z) is in radians and equals qX x qY x qZ. As matrices it is Rx x Ry x Rz, so it applies to points in Z, Y, X order. (the same formula as three.js's 'XYZ' order)
- The original keeps x, y, z, w as hidden fields with properties, but the Rust version is a Copy value with public fields. The original clone is replaced by copying.
- Numbers are f64 like the original JavaScript numbers.
- Conversion from a matrix to a quaternion and back to Euler angles, which the original lacks, were not added. (for a matrix, Matrix4::create_rotation_from_quaternion)
Quaternionstruct traits Clone, Copy, Debug, PartialEq, Default
쿼터니언. (3차원 회전, x, y, z, w 를 공개 필드로 둔 Copy 값, 기본값은 단위 쿼터니언)
Quaternion. (a 3D rotation, a Copy value with x, y, z, w as public fields, defaults to the identity quaternion)
use vanilla::{math, Matrix4, Quaternion};
let start = Quaternion::identity();
let end = Quaternion::create_from_euler(0.0, math::PI * 0.5, 0.0);
let middle = Quaternion::slerp(start, end, 0.5);
let expected = Quaternion::create_from_euler(0.0, math::PI * 0.25, 0.0);
assert!((middle.y - expected.y).abs() < 1e-5);
assert!((middle.w - expected.w).abs() < 1e-5);
let rotation_matrix = Matrix4::create_rotation_from_quaternion(middle);
let forward = rotation_matrix.transform_direction(0.0, 0.0, 1.0);
assert!((forward.x - forward.z).abs() < 1e-5);생성.
Constructor.
- xf64
- 벡터부 x.Vector part x.
- yf64
- 벡터부 y.Vector part y.
- zf64
- 벡터부 z.Vector part z.
- wf64
- 실수부.Real part.
반환ReturnsQuaternion (x, y, z, w) 쿼터니언. (정규화하지 않는다)The quaternion (x, y, z, w). (not normalized)
짝Counterpart: vanilla.js Quaternion.create
단위 쿼터니언. (0, 0, 0, 1)
Identity quaternion. (0, 0, 0, 1)
반환ReturnsQuaternion 회전이 없는 쿼터니언.A quaternion with no rotation.
짝Counterpart: vanilla.js Quaternion.identity
오일러 각으로 만든다. (XYZ 순서 라디안, qX x qY x qZ 와 같다)
Creates from Euler angles. (XYZ order in radians, equals qX x qY x qZ)
- x_radianf64
- X 축 회전 각. (라디안)X axis rotation angle. (radians)
- y_radianf64
- Y 축 회전 각. (라디안)Y axis rotation angle. (radians)
- z_radianf64
- Z 축 회전 각. (라디안)Z axis rotation angle. (radians)
반환ReturnsQuaternion 세 축 회전을 합친 단위 쿼터니언. (점에는 Z, Y, X 순서로 걸린다)Unit quaternion combining the three axis rotations. (applies to points in Z, Y, X order)
짝Counterpart: vanilla.js Quaternion.createFromEuler
use vanilla::Quaternion;
let from_euler = Quaternion::create_from_euler(0.3, -0.7, 1.1);
let quaternion_x = Quaternion::create_from_euler(0.3, 0.0, 0.0);
let quaternion_y = Quaternion::create_from_euler(0.0, -0.7, 0.0);
let quaternion_z = Quaternion::create_from_euler(0.0, 0.0, 1.1);
let multiplied = quaternion_x.multiply(quaternion_y).multiply(quaternion_z);
assert!((from_euler.x - multiplied.x).abs() < 1e-5);
assert!((from_euler.y - multiplied.y).abs() < 1e-5);
assert!((from_euler.z - multiplied.z).abs() < 1e-5);
assert!((from_euler.w - multiplied.w).abs() < 1e-5);구면 선형 보간. (최단 경로, 거의 같은 방향이면 선형 보간 뒤 정규화한다)
Spherical linear interpolation. (shortest path; if nearly the same direction, linear interpolation followed by normalization)
- fromQuaternion
- 시작 회전.Start rotation.
- toQuaternion
- 끝 회전. (from 과 내적이 음수면 부호를 뒤집어 짧은 쪽으로 간다)End rotation. (if its dot product with from is negative, the sign is flipped to take the short way)
- normalized_timef64
- 보간 비율. (0 이면 from, 1 이면 to 와 같은 회전)Interpolation ratio. (0 gives the same rotation as from, 1 the same as to)
반환ReturnsQuaternion 보간한 회전. (정규화해 돌려준다)The interpolated rotation. (returned normalized)
짝Counterpart: vanilla.js Quaternion.slerp
use vanilla::Quaternion;
let diagonal_component = std::f64::consts::FRAC_1_SQRT_2;
let to = Quaternion::create(0.0, 0.0, -diagonal_component, -diagonal_component);
let middle = Quaternion::slerp(Quaternion::identity(), to, 0.5);
assert!((middle.z - 0.38268343).abs() < 1e-5);
assert!((middle.w - 0.9238795).abs() < 1e-5);벡터부 x. (회전 축 x 에 sin(각 / 2) 를 곱한 값)
Vector part x. (rotation axis x multiplied by sin(angle / 2))
반환Returnsf64
짝Counterpart: vanilla.js Quaternion.x
벡터부 y. (회전 축 y 에 sin(각 / 2) 를 곱한 값)
Vector part y. (rotation axis y multiplied by sin(angle / 2))
반환Returnsf64
짝Counterpart: vanilla.js Quaternion.y
벡터부 z. (회전 축 z 에 sin(각 / 2) 를 곱한 값)
Vector part z. (rotation axis z multiplied by sin(angle / 2))
반환Returnsf64
짝Counterpart: vanilla.js Quaternion.z
대입.
Assignment.
- xf64
- 넣을 벡터부 x.Vector part x to set.
- yf64
- 넣을 벡터부 y.Vector part y to set.
- zf64
- 넣을 벡터부 z.Vector part z to set.
- wf64
- 넣을 실수부.Real part to set.
짝Counterpart: vanilla.js Quaternion.set
정규화. (길이가 0 이하이면 단위 쿼터니언)
Normalization. (the identity quaternion if the length is 0 or less)
반환ReturnsQuaternion 길이가 1 인 새 쿼터니언. 길이가 0 이하이면 단위 쿼터니언.A new quaternion of length 1. The identity quaternion if the length is 0 or less.
짝Counterpart: vanilla.js Quaternion.normalize
곱하기. (해밀턴 곱 this x other, this 회전 뒤에 other 회전을 로컬로 누적한다)
Multiplication. (Hamilton product this x other; accumulates the other rotation locally after the this rotation)
- otherQuaternion
- 오른쪽에 곱할 쿼터니언. (점에는 other 회전이 먼저 걸린다)Quaternion to multiply on the right. (the other rotation applies to points first)
반환ReturnsQuaternion self x other 인 새 쿼터니언. (정규화하지 않는다)A new quaternion equal to self x other. (not normalized)
짝Counterpart: vanilla.js Quaternion.multiply
use vanilla::{math, Quaternion};
let quarter_turn = Quaternion::create_from_euler(0.0, 0.0, math::PI * 0.5);
let half_turn = quarter_turn.multiply(quarter_turn);
assert!((half_turn.z - 1.0).abs() < 1e-6);
assert!(half_turn.w.abs() < 1e-6);