|
| 1 | +/* |
| 2 | + * Copyright (c) 2020-2021 jMonkeyEngine |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are |
| 7 | + * met: |
| 8 | + * |
| 9 | + * * Redistributions of source code must retain the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer. |
| 11 | + * |
| 12 | + * * Redistributions in binary form must reproduce the above copyright |
| 13 | + * notice, this list of conditions and the following disclaimer in the |
| 14 | + * documentation and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
| 17 | + * may be used to endorse or promote products derived from this software |
| 18 | + * without specific prior written permission. |
| 19 | + * |
| 20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 22 | + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 23 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 24 | + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 25 | + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 26 | + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 27 | + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 28 | + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 29 | + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | + */ |
| 32 | +package com.jme3.math; |
| 33 | + |
| 34 | +import org.junit.Assert; |
| 35 | +import org.junit.Test; |
| 36 | + |
| 37 | +/** |
| 38 | + * Verify the order in which Tait-Bryan angles are applied by the Quaternion |
| 39 | + * class. This was issue #1388 at GitHub. |
| 40 | + * |
| 41 | + * @author Stephen Gold |
| 42 | + */ |
| 43 | +public class TestIssue1388 { |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testIssue1388() { |
| 47 | + Vector3f in = new Vector3f(4f, 6f, 9f); // test vector, never modified |
| 48 | + Vector3f saveIn = in.clone(); |
| 49 | + /* |
| 50 | + * Three arbitrary rotation angles between -PI/2 and +PI/2 |
| 51 | + */ |
| 52 | + final float xAngle = 1.23f; |
| 53 | + final float yAngle = 0.765f; |
| 54 | + final float zAngle = -0.456f; |
| 55 | + float[] angles = new float[]{xAngle, yAngle, zAngle}; |
| 56 | + float[] saveAngles = new float[]{xAngle, yAngle, zAngle}; |
| 57 | + /* |
| 58 | + * Part 1: verify that the extrinsic rotation order is x-z-y |
| 59 | + * |
| 60 | + * Apply extrinsic rotations to the "in" vector in x-z-y order. |
| 61 | + */ |
| 62 | + Quaternion qx = new Quaternion().fromAngleAxis(xAngle, Vector3f.UNIT_X); |
| 63 | + Quaternion qy = new Quaternion().fromAngleAxis(yAngle, Vector3f.UNIT_Y); |
| 64 | + Quaternion qz = new Quaternion().fromAngleAxis(zAngle, Vector3f.UNIT_Z); |
| 65 | + Vector3f outXZY = qx.mult(in); |
| 66 | + qz.mult(outXZY, outXZY); |
| 67 | + qy.mult(outXZY, outXZY); |
| 68 | + /* |
| 69 | + * Construct a Quaternion using fromAngles(float, float, float), |
| 70 | + * use it to rotate the "in" vector, and compare. |
| 71 | + */ |
| 72 | + Quaternion q1 = new Quaternion().fromAngles(xAngle, yAngle, zAngle); |
| 73 | + Vector3f out1 = q1.mult(in); |
| 74 | + assertEquals(outXZY, out1, 1e-5f); |
| 75 | + /* |
| 76 | + * Construct a Quaternion using fromAngles(float[]), |
| 77 | + * use it to rotate the "in" vector, and compare. |
| 78 | + */ |
| 79 | + Quaternion q2 = new Quaternion().fromAngles(angles); |
| 80 | + Vector3f out2 = q2.mult(in); |
| 81 | + assertEquals(outXZY, out2, 1e-5f); |
| 82 | + /* |
| 83 | + * Construct a Quaternion using only the constructor, |
| 84 | + * use it to rotate the "in" vector, and compare. |
| 85 | + */ |
| 86 | + Quaternion q3 = new Quaternion(angles); |
| 87 | + Vector3f out3 = q3.mult(in); |
| 88 | + assertEquals(outXZY, out3, 1e-5f); |
| 89 | + /* |
| 90 | + * Verify that fromAngles() reverses toAngles() for the chosen angles. |
| 91 | + */ |
| 92 | + float[] out4 = q1.toAngles(null); |
| 93 | + assertEquals(angles, out4, 1e-5f); |
| 94 | + float[] out5 = q2.toAngles(null); |
| 95 | + assertEquals(angles, out5, 1e-5f); |
| 96 | + float[] out6 = q3.toAngles(null); |
| 97 | + assertEquals(angles, out6, 1e-5f); |
| 98 | + /* |
| 99 | + * Part 2: verify intrinsic rotation order |
| 100 | + * |
| 101 | + * Apply intrinsic rotations to the "in" vector in y-z'-x" order. |
| 102 | + */ |
| 103 | + Quaternion q4 = qy.mult(qz).mult(qx); |
| 104 | + Vector3f out7 = q4.mult(in); |
| 105 | + assertEquals(outXZY, out7, 1e-5f); |
| 106 | + /* |
| 107 | + * Verify that the values of "saveAngles" and "in" haven't changed. |
| 108 | + */ |
| 109 | + assertEquals(saveAngles, angles, 0f); |
| 110 | + assertEquals(saveIn, in, 0f); |
| 111 | + } |
| 112 | + |
| 113 | + private void assertEquals(float[] expected, float[] actual, |
| 114 | + float tolerance) { |
| 115 | + Assert.assertEquals(expected[0], actual[0], tolerance); |
| 116 | + Assert.assertEquals(expected[1], actual[1], tolerance); |
| 117 | + Assert.assertEquals(expected[2], actual[2], tolerance); |
| 118 | + } |
| 119 | + |
| 120 | + private void assertEquals(Vector3f expected, Vector3f actual, |
| 121 | + float tolerance) { |
| 122 | + Assert.assertEquals(expected.x, actual.x, tolerance); |
| 123 | + Assert.assertEquals(expected.y, actual.y, tolerance); |
| 124 | + Assert.assertEquals(expected.z, actual.z, tolerance); |
| 125 | + } |
| 126 | +} |
0 commit comments