/** * Gets the three components of the vector part of the quaternion. * * @return the vector part. * @see #getQ1() * @see #getQ2() * @see #getQ3() */ public double[] getVectorPart() { return new double[] { getQ1(), getQ2(), getQ3() }; }
/** * Computes the dot-product of two quaternions. * * @param q1 Quaternion. * @param q2 Quaternion. * @return the dot product of {@code q1} and {@code q2}. */ public static double dotProduct(final Quaternion q1, final Quaternion q2) { return q1.getQ0() * q2.getQ0() + q1.getQ1() * q2.getQ1() + q1.getQ2() * q2.getQ2() + q1.getQ3() * q2.getQ3(); }
/** * Computes the sum of two quaternions. * * @param q1 Quaternion. * @param q2 Quaternion. * @return the sum of {@code q1} and {@code q2}. */ public static Quaternion add(final Quaternion q1, final Quaternion q2) { return new Quaternion(q1.getQ0() + q2.getQ0(), q1.getQ1() + q2.getQ1(), q1.getQ2() + q2.getQ2(), q1.getQ3() + q2.getQ3()); }
/** * {@inheritDoc} */ @Override public boolean equals(Object other) { if (this == other) { return true; } if (other instanceof Quaternion) { final Quaternion q = (Quaternion) other; return q0 == q.getQ0() && q1 == q.getQ1() && q2 == q.getQ2() && q3 == q.getQ3(); } return false; }
/** * Subtracts two quaternions. * * @param q1 First Quaternion. * @param q2 Second quaternion. * @return the difference between {@code q1} and {@code q2}. */ public static Quaternion subtract(final Quaternion q1, final Quaternion q2) { return new Quaternion(q1.getQ0() - q2.getQ0(), q1.getQ1() - q2.getQ1(), q1.getQ2() - q2.getQ2(), q1.getQ3() - q2.getQ3()); }
/** * Returns the Hamilton product of two quaternions. * * @param q1 First quaternion. * @param q2 Second quaternion. * @return the product {@code q1} and {@code q2}, in that order. */ public static Quaternion multiply(final Quaternion q1, final Quaternion q2) { // Components of the first quaternion. final double q1a = q1.getQ0(); final double q1b = q1.getQ1(); final double q1c = q1.getQ2(); final double q1d = q1.getQ3(); // Components of the second quaternion. final double q2a = q2.getQ0(); final double q2b = q2.getQ1(); final double q2c = q2.getQ2(); final double q2d = q2.getQ3(); // Components of the product. final double w = q1a * q2a - q1b * q2b - q1c * q2c - q1d * q2d; final double x = q1a * q2b + q1b * q2a + q1c * q2d - q1d * q2c; final double y = q1a * q2c - q1b * q2d + q1c * q2a + q1d * q2b; final double z = q1a * q2d + q1b * q2c - q1c * q2b + q1d * q2a; return new Quaternion(w, x, y, z); }
/** * Checks whether this instance is equal to another quaternion * within a given tolerance. * * @param q Quaternion with which to compare the current quaternion. * @param eps Tolerance. * @return {@code true} if the each of the components are equal * within the allowed absolute error. */ public boolean equals(final Quaternion q, final double eps) { return Precision.equals(q0, q.getQ0(), eps) && Precision.equals(q1, q.getQ1(), eps) && Precision.equals(q2, q.getQ2(), eps) && Precision.equals(q3, q.getQ3(), eps); }
/** * Returns the polar form of the quaternion. * * @return the unit quaternion with positive scalar part. */ public Quaternion getPositivePolarForm() { if (getQ0() < 0) { final Quaternion unitQ = normalize(); // The quaternion of rotation (normalized quaternion) q and -q // are equivalent (i.e. represent the same rotation). return new Quaternion(-unitQ.getQ0(), -unitQ.getQ1(), -unitQ.getQ2(), -unitQ.getQ3()); } else { return this.normalize(); } }
/** * Gets the three components of the vector part of the quaternion. * * @return the vector part. * @see #getQ1() * @see #getQ2() * @see #getQ3() */ public double[] getVectorPart() { return new double[] { getQ1(), getQ2(), getQ3() }; }
/** * Gets the three components of the vector part of the quaternion. * * @return the vector part. * @see #getQ1() * @see #getQ2() * @see #getQ3() */ public double[] getVectorPart() { return new double[] { getQ1(), getQ2(), getQ3() }; }
/** * Computes the dot-product of two quaternions. * * @param q1 Quaternion. * @param q2 Quaternion. * @return the dot product of {@code q1} and {@code q2}. */ public static double dotProduct(final Quaternion q1, final Quaternion q2) { return q1.getQ0() * q2.getQ0() + q1.getQ1() * q2.getQ1() + q1.getQ2() * q2.getQ2() + q1.getQ3() * q2.getQ3(); }
/** * Computes the dot-product of two quaternions. * * @param q1 Quaternion. * @param q2 Quaternion. * @return the dot product of {@code q1} and {@code q2}. */ public static double dotProduct(final Quaternion q1, final Quaternion q2) { return q1.getQ0() * q2.getQ0() + q1.getQ1() * q2.getQ1() + q1.getQ2() * q2.getQ2() + q1.getQ3() * q2.getQ3(); }
/** * Subtracts two quaternions. * * @param q1 First Quaternion. * @param q2 Second quaternion. * @return the difference between {@code q1} and {@code q2}. */ public static Quaternion subtract(final Quaternion q1, final Quaternion q2) { return new Quaternion(q1.getQ0() - q2.getQ0(), q1.getQ1() - q2.getQ1(), q1.getQ2() - q2.getQ2(), q1.getQ3() - q2.getQ3()); }
/** * Computes the sum of two quaternions. * * @param q1 Quaternion. * @param q2 Quaternion. * @return the sum of {@code q1} and {@code q2}. */ public static Quaternion add(final Quaternion q1, final Quaternion q2) { return new Quaternion(q1.getQ0() + q2.getQ0(), q1.getQ1() + q2.getQ1(), q1.getQ2() + q2.getQ2(), q1.getQ3() + q2.getQ3()); }
/** * {@inheritDoc} */ @Override public boolean equals(Object other) { if (this == other) { return true; } if (other instanceof Quaternion) { final Quaternion q = (Quaternion) other; return q0 == q.getQ0() && q1 == q.getQ1() && q2 == q.getQ2() && q3 == q.getQ3(); } return false; }
/** * Computes the sum of two quaternions. * * @param q1 Quaternion. * @param q2 Quaternion. * @return the sum of {@code q1} and {@code q2}. */ public static Quaternion add(final Quaternion q1, final Quaternion q2) { return new Quaternion(q1.getQ0() + q2.getQ0(), q1.getQ1() + q2.getQ1(), q1.getQ2() + q2.getQ2(), q1.getQ3() + q2.getQ3()); }
/** * {@inheritDoc} */ @Override public boolean equals(Object other) { if (this == other) { return true; } if (other instanceof Quaternion) { final Quaternion q = (Quaternion) other; return q0 == q.getQ0() && q1 == q.getQ1() && q2 == q.getQ2() && q3 == q.getQ3(); } return false; }
/** * Subtracts two quaternions. * * @param q1 First Quaternion. * @param q2 Second quaternion. * @return the difference between {@code q1} and {@code q2}. */ public static Quaternion subtract(final Quaternion q1, final Quaternion q2) { return new Quaternion(q1.getQ0() - q2.getQ0(), q1.getQ1() - q2.getQ1(), q1.getQ2() - q2.getQ2(), q1.getQ3() - q2.getQ3()); }
/** * Checks whether this instance is equal to another quaternion * within a given tolerance. * * @param q Quaternion with which to compare the current quaternion. * @param eps Tolerance. * @return {@code true} if the each of the components are equal * within the allowed absolute error. */ public boolean equals(final Quaternion q, final double eps) { return Precision.equals(q0, q.getQ0(), eps) && Precision.equals(q1, q.getQ1(), eps) && Precision.equals(q2, q.getQ2(), eps) && Precision.equals(q3, q.getQ3(), eps); }
/** * Checks whether this instance is equal to another quaternion * within a given tolerance. * * @param q Quaternion with which to compare the current quaternion. * @param eps Tolerance. * @return {@code true} if the each of the components are equal * within the allowed absolute error. */ public boolean equals(final Quaternion q, final double eps) { return Precision.equals(q0, q.getQ0(), eps) && Precision.equals(q1, q.getQ1(), eps) && Precision.equals(q2, q.getQ2(), eps) && Precision.equals(q3, q.getQ3(), eps); }