/** * * Initialize this quaternion with given normalized axis vector and rotation angle * * <p>Implementation Details: * * <ul> * <li>{@link #setIdentity()} if axis is {@link FloatUtil#isZero(float, float) is zero} using * {@link FloatUtil#EPSILON epsilon} * </ul> * * @param angle rotation angle (rads) * @param vector axis vector normalized * @return this quaternion for chaining. * @see <a * href="http://web.archive.org/web/20041029003853/http://www.j3d.org/matrix_faq/matrfaq_latest.html#Q56">Matrix-FAQ * Q56</a> * @see #toAngleAxis(float[]) */ public final Quaternion setFromAngleNormalAxis(final float angle, final float[] vector) { if (VectorUtil.isVec3Zero(vector, 0, FloatUtil.EPSILON)) { setIdentity(); } else { final float halfangle = angle * 0.5f; final float sin = FloatUtil.sin(halfangle); x = vector[0] * sin; y = vector[1] * sin; z = vector[2] * sin; w = FloatUtil.cos(halfangle); } return this; }
/** * Normalize a quaternion required if to be used as a rotational quaternion. * * <p>Implementation Details: * * <ul> * <li>{@link #setIdentity()} if {@link #magnitude()} is {@link FloatUtil#isZero(float, float) * is zero} using {@link FloatUtil#EPSILON epsilon} * </ul> * * @return this quaternion for chaining. */ public final Quaternion normalize() { final float norm = magnitude(); if (FloatUtil.isZero(norm, FloatUtil.EPSILON)) { setIdentity(); } else { final float invNorm = 1f / norm; w *= invNorm; x *= invNorm; y *= invNorm; z *= invNorm; } return this; }