altoslib: Switch preserved state format to JSON
[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_11;
19
20 public class AltosRotation implements AltosHashable, AltosJsonable {
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 / 2.0, y * dt / 2.0, z * dt / 2.0);
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
51         public AltosHashSet hashSet() {
52                 AltosHashSet h = new AltosHashSet();
53
54                 h.putHashable("rotation", rotation);
55                 return h;
56         }
57
58         public AltosJson json() {
59                 return rotation.json();
60         }
61
62         public AltosRotation(AltosHashSet h) {
63                 rotation = new AltosQuaternion(h.getHash("rotation"));
64         }
65
66         public static AltosRotation fromHashSet(AltosHashSet h, AltosRotation def) {
67                 if (h == null)
68                         return def;
69
70                 return new AltosRotation(h);
71         }
72
73         public AltosRotation(AltosJson j) {
74                 rotation = new AltosQuaternion(j);
75         }
76
77         public static AltosRotation fromJson(AltosJson j, AltosRotation def) {
78                 if (j == null)
79                         return def;
80
81                 return new AltosRotation(j);
82         }
83 }