4e4127cfa7e6c181ac756d72d700de6c424bbede
[fw/altos] / altoslib / AltosRotation.java
1 /*
2  * Copyright © 2014 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package org.altusmetrum.altoslib_7;
19
20 public class AltosRotation {
21         private AltosQuaternion         rotation;
22
23         public double tilt() {
24                 double  rotz = rotation.z * rotation.z - rotation.y * rotation.y - rotation.x * rotation.x + rotation.r * rotation.r;
25
26                 double tilt = Math.acos(rotz) * 180.0 / Math.PI;
27                 return tilt;
28         }
29
30         public void rotate(double dt, double x, double y, double z) {
31                 AltosQuaternion rot = AltosQuaternion.half_euler(x * dt, y * dt, z * dt);
32                 rotation = rot.multiply(rotation).normalize();
33         }
34
35         /* Clone an existing rotation value */
36         public AltosRotation (AltosRotation old) {
37                 this.rotation = new AltosQuaternion(old.rotation);
38         }
39
40         /* Create a new rotation value given an acceleration vector pointing down */
41         public AltosRotation(double x,
42                              double y,
43                              double z,
44                              int pad_orientation) {
45                 AltosQuaternion orient = AltosQuaternion.vector(x, y, z).normalize();
46                 double sky = pad_orientation == 0 ? 1 : -1;
47                 AltosQuaternion up = new AltosQuaternion(0, 0, 0, sky);
48                 rotation = up.vectors_to_rotation(orient);
49         }
50 }