86db2004f13f0c390cbe22b51aacb735684b621b
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / math / Orientation.java
1 /*
2  * Sixth 3D engine. Author: Svjatoslav Agejenko. 
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5 package eu.svjatoslav.sixth.e3d.math;
6
7 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
8
9 public class Orientation implements Cloneable {
10
11
12     private double s1, c1, s2, c2;
13
14     /**
15      * The angle of rotation around the XZ axis.
16      */
17     private double angleXZ = 0;
18
19     /**
20      * The angle of rotation around the YZ axis.
21      */
22     private double angleYZ = 0;
23
24     public Orientation() {
25         computeMultipliers();
26     }
27
28     public Orientation(final double angleXZ, final double angleYZ) {
29         this.angleXZ = angleXZ;
30         this.angleYZ = angleYZ;
31         computeMultipliers();
32     }
33
34     @Override
35     public Orientation clone() {
36         return new Orientation(angleXZ, angleYZ);
37     }
38
39     /**
40      * Computes the sine and cosine of the angles.
41      */
42     private void computeMultipliers() {
43         s1 = Math.sin(angleXZ);
44         c1 = Math.cos(angleXZ);
45
46         s2 = Math.sin(angleYZ);
47         c2 = Math.cos(angleYZ);
48     }
49
50     public void rotate(final Point3D point3d) {
51         final double z1 = (point3d.z * c1) - (point3d.x * s1);
52         point3d.x = (point3d.z * s1) + (point3d.x * c1);
53
54         point3d.z = (z1 * c2) - (point3d.y * s2);
55         point3d.y = (z1 * s2) + (point3d.y * c2);
56     }
57
58     public void rotate(final double angleXZ, final double angleYZ) {
59         this.angleXZ += angleXZ;
60         this.angleYZ += angleYZ;
61         computeMultipliers();
62     }
63
64 }