2 * Copyright © 2014 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
18 package org.altusmetrum.altoslib_10;
20 public class AltosQuaternion {
21 double r; /* real bit */
22 double x, y, z; /* imaginary bits */
24 public AltosQuaternion multiply(AltosQuaternion b) {
25 return new AltosQuaternion(
26 this.r * b.r - this.x * b.x - this.y * b.y - this.z * b.z,
27 this.r * b.x + this.x * b.r + this.y * b.z - this.z * b.y,
28 this.r * b.y - this.x * b.z + this.y * b.r + this.z * b.x,
29 this.r * b.z + this.x * b.y - this.y * b.x + this.z * b.r);
32 public AltosQuaternion conjugate() {
33 return new AltosQuaternion(
40 public double normal() {
41 return (this.r * this.r +
47 public AltosQuaternion scale(double b) {
48 return new AltosQuaternion(
55 public AltosQuaternion normalize() {
59 return scale(1/Math.sqrt(n));
62 public double dot(AltosQuaternion b) {
63 return (this.r * b.r +
69 public AltosQuaternion rotate(AltosQuaternion b) {
70 return (b.multiply(this)).multiply(b.conjugate());
73 public AltosQuaternion vectors_to_rotation(AltosQuaternion b) {
75 * The cross product will point orthogonally to the two
76 * vectors, forming our rotation axis. The length will be
77 * sin(θ), so these values are already multiplied by that.
80 double x = this.y * b.z - this.z * b.y;
81 double y = this.z * b.x - this.x * b.z;
82 double z = this.x * b.y - this.y * b.x;
84 double s_2 = x*x + y*y + z*z;
85 double s = Math.sqrt(s_2);
87 /* cos(θ) = a · b / (|a| |b|).
89 * a and b are both unit vectors, so the divisor is one
91 double c = this.x*b.x + this.y*b.y + this.z*b.z;
93 double c_half = Math.sqrt ((1 + c) / 2);
94 double s_half = Math.sqrt ((1 - c) / 2);
97 * Divide out the sine factor from the
98 * cross product, then multiply in the
99 * half sine factor needed for the quaternion
101 double s_scale = s_half / s;
103 AltosQuaternion r = new AltosQuaternion(c_half,
107 return r.normalize();
110 public AltosQuaternion(double r, double x, double y, double z) {
117 public AltosQuaternion(AltosQuaternion q) {
124 static public AltosQuaternion vector(double x, double y, double z) {
125 return new AltosQuaternion(0, x, y, z);
128 static public AltosQuaternion rotation(double x, double y, double z,
129 double s, double c) {
130 return new AltosQuaternion(c,
136 static public AltosQuaternion zero_rotation() {
137 return new AltosQuaternion(1, 0, 0, 0);
140 static public AltosQuaternion half_euler(double x, double y, double z) {
141 double s_x = Math.sin(x), c_x = Math.cos(x);
142 double s_y = Math.sin(y), c_y = Math.cos(y);
143 double s_z = Math.sin(z), c_z = Math.cos(z);;
145 return new AltosQuaternion(c_x * c_y * c_z + s_x * s_y * s_z,
146 s_x * c_y * c_z - c_x * s_y * s_z,
147 c_x * s_y * c_z + s_x * c_y * s_z,
148 c_x * c_y * s_z - s_x * s_y * c_z);