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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.altoslib_11;
21 public class AltosQuaternion {
22 double r; /* real bit */
23 double x, y, z; /* imaginary bits */
25 public AltosQuaternion multiply(AltosQuaternion b) {
26 return new AltosQuaternion(
27 this.r * b.r - this.x * b.x - this.y * b.y - this.z * b.z,
28 this.r * b.x + this.x * b.r + this.y * b.z - this.z * b.y,
29 this.r * b.y - this.x * b.z + this.y * b.r + this.z * b.x,
30 this.r * b.z + this.x * b.y - this.y * b.x + this.z * b.r);
33 public AltosQuaternion conjugate() {
34 return new AltosQuaternion(
41 public double normal() {
42 return (this.r * this.r +
48 public AltosQuaternion scale(double b) {
49 return new AltosQuaternion(
56 public AltosQuaternion normalize() {
60 return scale(1/Math.sqrt(n));
63 public double dot(AltosQuaternion b) {
64 return (this.r * b.r +
70 public AltosQuaternion rotate(AltosQuaternion b) {
71 return (b.multiply(this)).multiply(b.conjugate());
74 public AltosQuaternion vectors_to_rotation(AltosQuaternion b) {
76 * The cross product will point orthogonally to the two
77 * vectors, forming our rotation axis. The length will be
78 * sin(θ), so these values are already multiplied by that.
81 double x = this.y * b.z - this.z * b.y;
82 double y = this.z * b.x - this.x * b.z;
83 double z = this.x * b.y - this.y * b.x;
85 double s_2 = x*x + y*y + z*z;
86 double s = Math.sqrt(s_2);
88 /* cos(θ) = a · b / (|a| |b|).
90 * a and b are both unit vectors, so the divisor is one
92 double c = this.x*b.x + this.y*b.y + this.z*b.z;
94 double c_half = Math.sqrt ((1 + c) / 2);
95 double s_half = Math.sqrt ((1 - c) / 2);
98 * Divide out the sine factor from the
99 * cross product, then multiply in the
100 * half sine factor needed for the quaternion
102 double s_scale = s_half / s;
104 AltosQuaternion r = new AltosQuaternion(c_half,
108 return r.normalize();
111 public AltosQuaternion(double r, double x, double y, double z) {
118 public AltosQuaternion(AltosQuaternion q) {
125 public AltosQuaternion() {
132 static public AltosQuaternion vector(double x, double y, double z) {
133 return new AltosQuaternion(0, x, y, z);
136 static public AltosQuaternion rotation(double x, double y, double z,
137 double s, double c) {
138 return new AltosQuaternion(c,
144 static public AltosQuaternion zero_rotation() {
145 return new AltosQuaternion(1, 0, 0, 0);
148 static public AltosQuaternion half_euler(double x, double y, double z) {
149 double s_x = Math.sin(x), c_x = Math.cos(x);
150 double s_y = Math.sin(y), c_y = Math.cos(y);
151 double s_z = Math.sin(z), c_z = Math.cos(z);;
153 return new AltosQuaternion(c_x * c_y * c_z + s_x * s_y * s_z,
154 s_x * c_y * c_z - c_x * s_y * s_z,
155 c_x * s_y * c_z + s_x * c_y * s_z,
156 c_x * c_y * s_z - s_x * s_y * c_z);