Skip to content

Commit a4d79bc

Browse files
committed
Fix quaternion negate
1 parent 3706feb commit a4d79bc

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

lib/src/vector_math/quaternion.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,12 @@ class Quaternion {
262262
return l;
263263
}
264264

265+
/// Negate this.
266+
void negate() {
267+
_qStorage[3] = -_qStorage[3];
268+
conjugate();
269+
}
270+
265271
/// Conjugate this.
266272
void conjugate() {
267273
_qStorage[2] = -_qStorage[2];
@@ -281,6 +287,9 @@ class Quaternion {
281287
/// Normalized copy of this.
282288
Quaternion normalized() => clone()..normalize();
283289

290+
/// Negated copy of this.
291+
Quaternion negated() => clone()..negate();
292+
284293
/// Conjugated copy of this.
285294
Quaternion conjugated() => clone()..conjugate();
286295

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

415424
/// Returns negated copy of this.
416-
Quaternion operator -() => conjugated();
425+
Quaternion operator -() => negated();
417426

418427
/// Access the component of the quaternion at the index [i].
419428
double operator [](int i) => _qStorage[i];

lib/src/vector_math_64/quaternion.dart

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,12 @@ class Quaternion {
262262
return l;
263263
}
264264

265+
/// Negate this.
266+
void negate() {
267+
_qStorage[3] = -_qStorage[3];
268+
conjugate();
269+
}
270+
265271
/// Conjugate this.
266272
void conjugate() {
267273
_qStorage[2] = -_qStorage[2];
@@ -281,6 +287,9 @@ class Quaternion {
281287
/// Normalized copy of this.
282288
Quaternion normalized() => clone()..normalize();
283289

290+
/// Negated copy of this.
291+
Quaternion negated() => clone()..negate();
292+
284293
/// Conjugated copy of this.
285294
Quaternion conjugated() => clone()..conjugate();
286295

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

415424
/// Returns negated copy of this.
416-
Quaternion operator -() => conjugated();
425+
Quaternion operator -() => negated();
417426

418427
/// Access the component of the quaternion at the index [i].
419428
double operator [](int i) => _qStorage[i];

test/quaternion_test.dart

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ void testQuaternionInstacingFromByteBuffer() {
3939
expect(offsetVector.w, equals(5.0));
4040
}
4141

42+
void testNegate(List<Quaternion> input, List<Quaternion> expectedOutput) {
43+
assert(input.length == expectedOutput.length);
44+
for (var i = 0; i < input.length; i++) {
45+
final output1 = -input[i];
46+
final output2 = input[i]..negate();
47+
relativeTest(output1, expectedOutput[i]);
48+
relativeTest(output2, expectedOutput[i]);
49+
}
50+
}
51+
4252
void testConjugate(List<Quaternion> input, List<Quaternion> expectedOutput) {
4353
assert(input.length == expectedOutput.length);
4454
for (var i = 0; i < input.length; i++) {
@@ -73,6 +83,18 @@ void testQuaternionVectorRotate(List<Quaternion> inputA, List<Vector3> inputB,
7383
}
7484
}
7585

86+
void testQuaternionNegate() {
87+
final input = <Quaternion>[];
88+
input.add(Quaternion.identity());
89+
input.add(Quaternion(0.18260, 0.54770, 0.73030, 0.36510));
90+
input.add(Quaternion(0.9889, 0.0, 0.0, 0.14834));
91+
final expectedOutput = <Quaternion>[];
92+
expectedOutput.add(Quaternion(-0.0, -0.0, -0.0, -1.0));
93+
expectedOutput.add(Quaternion(-0.18260, -0.54770, -0.73030, -0.36510));
94+
expectedOutput.add(Quaternion(-0.9889, -0.0, -0.0, -0.1483));
95+
testNegate(input, expectedOutput);
96+
}
97+
7698
void testQuaternionConjugate() {
7799
final input = <Quaternion>[];
78100
input.add(Quaternion.identity());
@@ -229,6 +251,7 @@ void main() {
229251
group('Quaternion', () {
230252
test('Float32List instacing', testQuaternionInstacinfFromFloat32List);
231253
test('ByteBuffer instacing', testQuaternionInstacingFromByteBuffer);
254+
test('Negate', testQuaternionNegate);
232255
test('Conjugate', testQuaternionConjugate);
233256
test('Matrix Quaternion Round Trip',
234257
testQuaternionMatrixQuaternionRoundTrip);

0 commit comments

Comments
 (0)