Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix quaternion negate #316

Merged
merged 3 commits into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading