altoslib: Use common constants for flash action messages
[fw/altos] / altoslib / AltosQuaternion.java
index 1b4a9419c875799b3abaaca022e0535e75288c61..98790f5fcfecbc68701dd28b930434a5d69eff5c 100644 (file)
@@ -3,7 +3,8 @@
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; version 2 of the License.
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  */
 
-package org.altusmetrum.altoslib_11;
+package org.altusmetrum.altoslib_13;
 
-public class AltosQuaternion implements AltosJsonable {
+public class AltosQuaternion {
        double  r;              /* real bit */
        double  x, y, z;        /* imaginary bits */
 
+       /* Multiply by b */
        public AltosQuaternion multiply(AltosQuaternion b) {
                return new AltosQuaternion(
                        this.r * b.r - this.x * b.x - this.y * b.y - this.z * b.z,
@@ -30,35 +32,36 @@ public class AltosQuaternion implements AltosJsonable {
        }
 
        public AltosQuaternion conjugate() {
-               return new AltosQuaternion(
-                       this.r,
-                       -this.x,
-                       -this.y,
-                       -this.z);
+               return new AltosQuaternion(this.r,
+                                          -this.x,
+                                          -this.y,
+                                          -this.z);
        }
 
        public double normal() {
-               return (this.r * this.r +
-                       this.x * this.x +
-                       this.y * this.y +
-                       this.z * this.z);
+               return Math.sqrt(this.r * this.r +
+                                this.x * this.x +
+                                this.y * this.y +
+                                this.z * this.z);
        }
 
+       /* Scale by a real value */
        public AltosQuaternion scale(double b) {
-               return new AltosQuaternion(
-                       this.r * b,
-                       this.x * b,
-                       this.y * b,
-                       this.z * b);
+               return new AltosQuaternion(this.r * b,
+                                          this.x * b,
+                                          this.y * b,
+                                          this.z * b);
        }
 
+       /* Divide by the length to end up with a quaternion of length 1 */
        public AltosQuaternion normalize() {
                double  n = normal();
                if (n <= 0)
                        return this;
-               return scale(1/Math.sqrt(n));
+               return scale(1/n);
        }
 
+       /* dot product */
        public double dot(AltosQuaternion b) {
                return (this.r * b.r +
                        this.x * b.x +
@@ -66,10 +69,14 @@ public class AltosQuaternion implements AltosJsonable {
                        this.z * b.z);
        }
 
+       /* Rotate 'this' by 'b' */
        public AltosQuaternion rotate(AltosQuaternion b) {
                return (b.multiply(this)).multiply(b.conjugate());
        }
 
+       /* Given two vectors (this and b), compute a quaternion
+        * representing the rotation between them
+        */
        public AltosQuaternion vectors_to_rotation(AltosQuaternion b) {
                /*
                 * The cross product will point orthogonally to the two
@@ -115,10 +122,17 @@ public class AltosQuaternion implements AltosJsonable {
        }
 
        public AltosQuaternion(AltosQuaternion q) {
-               this.r = q.r;
-               this.x = q.x;
-               this.y = q.y;
-               this.z = q.z;
+               r = q.r;
+               x = q.x;
+               y = q.y;
+               z = q.z;
+       }
+
+       public AltosQuaternion() {
+               r = 1;
+               x = 0;
+               y = 0;
+               z = 0;
        }
 
        static public AltosQuaternion vector(double x, double y, double z) {
@@ -137,7 +151,13 @@ public class AltosQuaternion implements AltosJsonable {
                return new AltosQuaternion(1, 0, 0, 0);
        }
 
-       static public AltosQuaternion half_euler(double x, double y, double z) {
+       static public AltosQuaternion euler(double x, double y, double z) {
+
+               /* Halve the euler angles */
+               x = x / 2.0;
+               y = y / 2.0;
+               z = z / 2.0;
+
                double  s_x = Math.sin(x), c_x = Math.cos(x);
                double  s_y = Math.sin(y), c_y = Math.cos(y);
                double  s_z = Math.sin(z), c_z = Math.cos(z);;
@@ -147,24 +167,4 @@ public class AltosQuaternion implements AltosJsonable {
                                           c_x * s_y * c_z + s_x * c_y * s_z,
                                           c_x * c_y * s_z - s_x * s_y * c_z);
        }
-
-       public AltosJson json() {
-               AltosJson j = new AltosJson();
-
-               j.put("r", r);
-               j.put("x", x);
-               j.put("y", y);
-               j.put("z", z);
-               return j;
-       }
-
-       public AltosQuaternion(AltosJson j) {
-               if (j == null)
-                       return;
-
-               r = j.get_double("r", 1);
-               x = j.get_double("x", 0);
-               y = j.get_double("y", 0);
-               z = j.get_double("z", 0);
-       }
 }