/** * Adds a specified set of {@link UnitCartesian} coordinates to another specified set of {@link UnitCartesian} coordinates. * @param left The augend, or value to which {@code right} is to be added. * @param right The addend, or value which is to be added to {@code left}. * @return A set of {@link Cartesian} coordinates that represents the result of the addition. */ @CS2JInfo("This method implements the functionality of the overloaded operator: 'Cartesian +(UnitCartesian,UnitCartesian)'") @Nonnull public static Cartesian add(@Nonnull UnitCartesian left, @Nonnull UnitCartesian right) { return left.add(Cartesian.toCartesian(right)); }
/** * Subtracts a specified set of {@link UnitCartesian} coordinates from another specified set of {@link UnitCartesian} coordinates. * @param left The minuend, or value from which {@code right} is to be subtracted. * @param right The subtrahend, or value which is to be subtracted from {@code left}. * @return A set of {@link Cartesian} coordinates that represents the result of the subtraction. */ @CS2JInfo("This method implements the functionality of the overloaded operator: 'Cartesian -(UnitCartesian,UnitCartesian)'") @Nonnull public static Cartesian subtract(@Nonnull UnitCartesian left, @Nonnull UnitCartesian right) { return left.subtract(Cartesian.toCartesian(right)); }
/** * Subtracts a specified set of {@link UnitCartesian} coordinates from a specified set of {@link Cartesian} coordinates. * @param left The minuend, or value from which {@code right} is to be subtracted. * @param right The subtrahend, or value which is to be subtracted from {@code left}. * @return A set of {@link Cartesian} coordinates that represents the result of the subtraction. */ @CS2JInfo("This method implements the functionality of the overloaded operator: 'Cartesian -(Cartesian,UnitCartesian)'") @Nonnull public static Cartesian subtract(@Nonnull Cartesian left, @Nonnull UnitCartesian right) { return left.subtract(Cartesian.toCartesian(right)); }
/** * Tests the Dot method. */ @Test public final void testDotProduct() { UnitCartesian first = new UnitCartesian(1.0, 3.0, -2.0); UnitCartesian second = new UnitCartesian(4.0, -2.0, -1.0); Assert.assertEquals(0D, first.dot(Cartesian.toCartesian(second)), Constants.Epsilon15); Assert.assertEquals(0D, second.dot(Cartesian.toCartesian(first)), Constants.Epsilon15); Cartesian result = new Cartesian(4.0, -2.0, -1.0); Assert.assertEquals(0, first.dot(result), 0d); }
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 implicit conversion from {@link UnitCartesian} coordinates. */ @Test public final void testConversionFromUnitCartesian() { UnitCartesian unit = new UnitCartesian(1.0, 1.0, 1.0); Cartesian test = Cartesian.toCartesian(unit); Assert.assertEquals(unit.getX(), test.getX(), 0d); Assert.assertEquals(unit.getY(), test.getY(), 0d); Assert.assertEquals(unit.getZ(), test.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); }
/** * Tests the Cross method. */ @Test public final void testCrossProduct() { double angle = Math.PI / 4.0; double cos = Math.cos(angle / 2.0); double sin = Math.sin(angle / 2.0); double a = cos * cos - sin * sin / 3.0; double b = 2.0 * (sin * sin + sin * cos * Math.sqrt(3.0)) / 3.0; double c = 2.0 * (sin * sin - sin * cos * Math.sqrt(3.0)) / 3.0; // The three vectors here are the orthonormal set obtained by rotating // the x-axis, y-axis, and z-axis through an angle of 45 degrees about // the (1,1,1) vector. UnitCartesian first = new UnitCartesian(a, b, c); UnitCartesian second = new UnitCartesian(c, a, b); UnitCartesian third = new UnitCartesian(b, c, a); Cartesian result = first.cross(Cartesian.toCartesian(second)); Assert.assertEquals(third.getX(), result.getX(), Constants.Epsilon14); Assert.assertEquals(third.getY(), result.getY(), Constants.Epsilon14); Assert.assertEquals(third.getZ(), result.getZ(), Constants.Epsilon14); Cartesian cartesian = new Cartesian(c, a, b); result = first.cross(cartesian); Assert.assertEquals(third.getX(), result.getX(), Constants.Epsilon14); Assert.assertEquals(third.getY(), result.getY(), Constants.Epsilon14); Assert.assertEquals(third.getZ(), result.getZ(), Constants.Epsilon14); }