Skip to content

Commit

Permalink
Fix quaternion negate
Browse files Browse the repository at this point in the history
  • Loading branch information
hket-tommyluk committed Mar 5, 2024
1 parent 3706feb commit aa9f272
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
11 changes: 10 additions & 1 deletion lib/src/vector_math/quaternion.dart
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,12 @@ class Quaternion {
return l;
}

/// Negate this.
void negate() {
_qStorage[3] = -_qStorage[3];
conjugate();
}

/// Conjugate this.
void conjugate() {
_qStorage[2] = -_qStorage[2];
Expand All @@ -281,6 +287,9 @@ class Quaternion {
/// Normalized copy of this.
Quaternion normalized() => clone()..normalize();

/// Negated copy of this.
Quaternion negated() => clone()..negate();

/// Conjugated copy of this.
Quaternion conjugated() => clone()..conjugate();

Expand Down Expand Up @@ -413,7 +422,7 @@ class Quaternion {
Quaternion operator -(Quaternion other) => clone()..sub(other);

/// Returns negated copy of this.
Quaternion operator -() => conjugated();
Quaternion operator -() => negated();

/// Access the component of the quaternion at the index [i].
double operator [](int i) => _qStorage[i];
Expand Down
11 changes: 10 additions & 1 deletion lib/src/vector_math_64/quaternion.dart
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,12 @@ class Quaternion {
return l;
}

/// Negate this.
void negate() {
_qStorage[3] = -_qStorage[3];
conjugate();
}

/// Conjugate this.
void conjugate() {
_qStorage[2] = -_qStorage[2];
Expand All @@ -281,6 +287,9 @@ class Quaternion {
/// Normalized copy of this.
Quaternion normalized() => clone()..normalize();

/// Negated copy of this.
Quaternion negated() => clone()..negate();

/// Conjugated copy of this.
Quaternion conjugated() => clone()..conjugate();

Expand Down Expand Up @@ -413,7 +422,7 @@ class Quaternion {
Quaternion operator -(Quaternion other) => clone()..sub(other);

/// Returns negated copy of this.
Quaternion operator -() => conjugated();
Quaternion operator -() => negated();

/// Access the component of the quaternion at the index [i].
double operator [](int i) => _qStorage[i];
Expand Down
23 changes: 23 additions & 0 deletions test/quaternion_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ void testQuaternionInstacingFromByteBuffer() {
expect(offsetVector.w, equals(5.0));
}

void testNegate(List<Quaternion> input, List<Quaternion> expectedOutput) {
assert(input.length == expectedOutput.length);
for (var i = 0; i < input.length; i++) {
final output1 = -input[i];
final output2 = input[i]..negate();
relativeTest(output1, expectedOutput[i]);
relativeTest(output2, expectedOutput[i]);
}
}

void testConjugate(List<Quaternion> input, List<Quaternion> expectedOutput) {
assert(input.length == expectedOutput.length);
for (var i = 0; i < input.length; i++) {
Expand Down Expand Up @@ -73,6 +83,18 @@ void testQuaternionVectorRotate(List<Quaternion> inputA, List<Vector3> inputB,
}
}

void testQuaternionNegate() {
final input = <Quaternion>[];
input.add(Quaternion.identity());
input.add(Quaternion(0.18260, 0.54770, 0.73030, 0.36510));
input.add(Quaternion(0.9889, 0.0, 0.0, 0.14834));
final expectedOutput = <Quaternion>[];
expectedOutput.add(Quaternion(-0.0, -0.0, -0.0, -1.0));
expectedOutput.add(Quaternion(-0.18260, -0.54770, -0.73030, -0.36510));
expectedOutput.add(Quaternion(-0.9889, -0.0, -0.0, -0.1483));
testNegate(input, expectedOutput);
}

void testQuaternionConjugate() {
final input = <Quaternion>[];
input.add(Quaternion.identity());
Expand Down Expand Up @@ -229,6 +251,7 @@ void main() {
group('Quaternion', () {
test('Float32List instacing', testQuaternionInstacinfFromFloat32List);
test('ByteBuffer instacing', testQuaternionInstacingFromByteBuffer);
test('Negate', testQuaternionNegate);
test('Conjugate', testQuaternionConjugate);
test('Matrix Quaternion Round Trip',
testQuaternionMatrixQuaternionRoundTrip);
Expand Down

0 comments on commit aa9f272

Please sign in to comment.