|
| 1 | + |
| 2 | +import processing.core.*; |
| 3 | + |
| 4 | +public class Quaternion implements PConstants { |
| 5 | + public float w, x, y, z; |
| 6 | + |
| 7 | + /** |
| 8 | + * Contructs a new Quaternion and it set to the identity |
| 9 | + */ |
| 10 | + public Quaternion() { |
| 11 | + setToIdentity(); |
| 12 | + } |
| 13 | + |
| 14 | + public Quaternion(float w, float x, float y, float z) { |
| 15 | + set(w, x, y, z); |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * Set the value of Quaternions components. |
| 20 | + * |
| 21 | + * @param w scalar component |
| 22 | + * @param x vector components x value |
| 23 | + * @param y vector components y value |
| 24 | + * @param z vector components z value |
| 25 | + */ |
| 26 | + public void set(float w, float x, float y, float z) { |
| 27 | + this.w = w; |
| 28 | + this.x = x; |
| 29 | + this.y = y; |
| 30 | + this.z = z; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Set the value of a Quaternions components with the values of another Quaternion |
| 35 | + * @param q |
| 36 | + */ |
| 37 | + public void set(Quaternion q) { |
| 38 | + this.set(q.w, q.x, q.y, q.z); |
| 39 | + } |
| 40 | + |
| 41 | + public void set(float s, PVector v) { |
| 42 | + set(s, v.x, v.y, v.z); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Sets Quaternion to the identity Quaternion |
| 47 | + */ |
| 48 | + public void setToIdentity() { |
| 49 | + set( 1.0f, 0.0f, 0.0f, 0.0f ); |
| 50 | + } |
| 51 | + |
| 52 | + public void mult(Quaternion q1, Quaternion q2, Quaternion target) { |
| 53 | + float s = q1.w * q2.w - q1.x * q2.x - q1.y * q2.y - q1.z * q2.z; |
| 54 | + float vx = q1.w * q2.x + q1.x * q2.w + q1.y * q2.z - q1.z * q2.y; |
| 55 | + float vy = q1.w * q2.y + q1.y * q2.w + q1.z * q2.x - q1.x * q2.z; |
| 56 | + float vz = q1.w * q2.z + q1.z * q2.w + q1.x * q2.y - q1.y * q2.x; |
| 57 | + target.set(s, vx, vy, vz); |
| 58 | + } |
| 59 | + |
| 60 | + public Quaternion mult(Quaternion q1, Quaternion q2) { |
| 61 | + Quaternion result = new Quaternion(); |
| 62 | + this.mult(q1, q2, result); |
| 63 | + return result; |
| 64 | + } |
| 65 | + |
| 66 | + public Quaternion mult(float s) { |
| 67 | + return new Quaternion(w*s, x*s, y*s, z*s); |
| 68 | + } |
| 69 | + |
| 70 | + public void inverse(Quaternion source, Quaternion target) { |
| 71 | + target.set( source.w, -source.x, -source.y, -source.z ); |
| 72 | + } |
| 73 | + |
| 74 | + public Quaternion inverse() { |
| 75 | + return new Quaternion(this.w, -this.x, -this.y, -this.z); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Computes the power of the quaternion, defined as follows: |
| 80 | + * let Q=(cos(alpha), sin(alpha) u), |
| 81 | + * then Q^t = (cos(t*alpha), sin(t*alpha)u). |
| 82 | + */ |
| 83 | + public Quaternion power(float exponent) { |
| 84 | + float theta2 = PApplet.acos(w); |
| 85 | + theta2 *= exponent; |
| 86 | + PVector U = new PVector(x,y,z); |
| 87 | + U.normalize(); |
| 88 | + U.mult(PApplet.sin(theta2)); |
| 89 | + return new Quaternion(PApplet.cos(theta2),U.x, U.y, U.z); |
| 90 | + } |
| 91 | + |
| 92 | + public String toString() { |
| 93 | + return "(" + w + ", " + x + ", " + y + ", " + z + ")"; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Set target Quaternion to the rotation between two unit vectors (shortest arc) |
| 98 | + * |
| 99 | + * @param start normalized starting point on rotation |
| 100 | + * @param end normalized ending point of rotation |
| 101 | + */ |
| 102 | + public void rotationBetween( PVector start, PVector end, Quaternion target ) { |
| 103 | + float dot = PVector.dot(start, end); |
| 104 | + |
| 105 | + if( dot < -0.999999) { |
| 106 | + //Start and end vectors are opposites rotation should be 180 degrees |
| 107 | + PVector xAxis = new PVector(1, 0, 0); |
| 108 | + PVector cross = xAxis.cross(start); |
| 109 | + //If cross product is zero rotate around Y instead of x |
| 110 | + if( cross.mag() > 0.000001) { |
| 111 | + target.set(0.0f, 1.0f, 0.0f, 0.0f); //180 degree rotation around the x-axis |
| 112 | + } else { |
| 113 | + target.set(0.0f, 0.0f, 1.0f, 0.0f); //180 degree rotation around y-axis |
| 114 | + } |
| 115 | + |
| 116 | + } else if ( dot > 0.999999) { |
| 117 | + //Start and end vectors are the same rotation should do nothing |
| 118 | + target.setToIdentity(); |
| 119 | + } else { |
| 120 | + //Start and end vectors are not identical or opposite |
| 121 | + target.set( dot, start.cross(end) ); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + public void fromAngleAxis( float angle, PVector axis, Quaternion target) { |
| 126 | + float s = PApplet.sin(angle/2); |
| 127 | + target.set(PApplet.cos(angle/2), axis.x * s, axis.y * s, axis.z * s); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * Convert Quaternion to an angle and Axis pair |
| 132 | + * @return float array whos first value is angle and remaining values represent the axis |
| 133 | + */ |
| 134 | + public float[] toAngleAxis() { |
| 135 | + float[] result = new float[4]; |
| 136 | + |
| 137 | + float sa = (float) Math.sqrt(1.0f - w * w); |
| 138 | + if (sa < EPSILON) { |
| 139 | + sa = 1.0f; |
| 140 | + } |
| 141 | + |
| 142 | + result[0] = (float) Math.acos(w) * 2.0f; |
| 143 | + result[1] = x / sa; |
| 144 | + result[2] = y / sa; |
| 145 | + result[3] = z / sa; |
| 146 | + |
| 147 | + return result; |
| 148 | + } |
| 149 | +} |
0 commit comments