/** * Forms a set of {@link UnitCartesian} coordinates from this instance. * <p> The normalization of the cartesian components is accomplished in the usual way. It should be noted that this does not guarantee a result whose magnitude will be 1.0 in cases where an individual component underflows upon squaring. * @return The resulting set of {@link UnitCartesian} coordinates. * @exception ArithmeticException The magnitude of the provided coordinates must not be zero. * @exception ArithmeticException The magnitude of the provided coordinates must not be infinite. */ @Nonnull public final UnitCartesian normalize() { double magnitude = 0D; final double[] out$magnitude$1 = { 0D }; final UnitCartesian temp$0 = normalize(out$magnitude$1); magnitude = out$magnitude$1[0]; return temp$0; }
/** * Tests that normalization of a {@link Cartesian} with infinite magnitude produces an {@link ArithmeticException}. */ @Test public final void testNormalizeOfInfiniteMagnitude() { ExpectedExceptionHelper.expectException(getRule$expectedException(), ArithmeticException.class); Cartesian test = new Cartesian(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY); UnitCartesian unit = test.normalize(); }
/** * Tests that normalization of a {@link Cartesian} with zero magnitude produces an {@link UnsupportedCaseException}. */ @Test public final void testNormalizeOfZeroMagnitude() { ExpectedExceptionHelper.expectException(getRule$expectedException(), ArithmeticException.class); Cartesian test = Cartesian.getZero(); UnitCartesian unit = test.normalize(); }
private final void _TestFromMatrix3By3(double angle, @Nonnull Cartesian axis) { Cartesian unit = Cartesian.toCartesian(axis.normalize()); double c = Math.cos(angle); double s = Math.sin(angle); double w = c; double x = s * unit.getX(); double y = s * unit.getY(); double z = s * unit.getZ(); UnitQuaternion quaternion = new UnitQuaternion(w, x, y, z); Matrix3By3 matrix = new Matrix3By3(quaternion); UnitQuaternion test = new UnitQuaternion(matrix); Assert.assertEquals(w, quaternion.getW(), Constants.Epsilon15); Assert.assertEquals(x, quaternion.getX(), Constants.Epsilon15); Assert.assertEquals(y, quaternion.getY(), Constants.Epsilon15); Assert.assertEquals(z, quaternion.getZ(), Constants.Epsilon15); }
/** * Tests the {@link Cartesian#normalize()} method. */ @Test public final void testNormalize() { Cartesian test = new Cartesian(2.0, 3.0, 6.0); UnitCartesian unit = test.normalize(); Assert.assertEquals(2.0 / 7.0, unit.getX(), 0d); Assert.assertEquals(3.0 / 7.0, unit.getY(), 0d); Assert.assertEquals(6.0 / 7.0, unit.getZ(), 0d); }
/** * Tests rotation by a {@link UnitQuaternion}. */ @Test public final void testRotateByUnitQuaternion() { double angle = Math.PI / 3.0; // half angle of 120 degree rotation double cos = Math.cos(angle); double sin = Math.sin(angle); Cartesian axis = Cartesian.toCartesian((new Cartesian(1.0, 1.0, 1.0)).normalize()); // unit vector along [1,1,1] double w = cos; double x = sin * axis.getX(); double y = sin * axis.getY(); double z = sin * axis.getZ(); // The original vector is along the x-axis. Cartesian original = new Cartesian(1.0, 0.0, 0.0); // The rotated vector is along the z-axis. Cartesian rotated = original.rotate(new UnitQuaternion(w, x, y, z)); Assert.assertEquals(0.0, rotated.getX(), Constants.Epsilon15); Assert.assertEquals(0.0, rotated.getY(), Constants.Epsilon15); Assert.assertEquals(1.0, rotated.getZ(), Constants.Epsilon15); }
/** * Tests rotation by an {@link Matrix3By3}. */ @Test public final void testRotateByMatrix3By3() { double angle = Math.PI / 3.0; // half angle of 120 degree rotation double cos = Math.cos(angle); double sin = Math.sin(angle); Cartesian axis = Cartesian.toCartesian((new Cartesian(1.0, 1.0, 1.0)).normalize()); // unit vector along [1,1,1] double w = cos; double x = sin * axis.getX(); double y = sin * axis.getY(); double z = sin * axis.getZ(); // The original vector is along the x-axis. Cartesian original = new Cartesian(1.0, 0.0, 0.0); // The rotated vector is along the z-axis. Cartesian rotated = original.rotate(new Matrix3By3(new UnitQuaternion(w, x, y, z))); Assert.assertEquals(0.0, rotated.getX(), Constants.Epsilon15); Assert.assertEquals(0.0, rotated.getY(), Constants.Epsilon15); Assert.assertEquals(1.0, rotated.getZ(), Constants.Epsilon15); }