Fixed git clone URL
[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 import static java.lang.Math.cos;
10 import static java.lang.Math.sin;
11
12 /**
13  * Used to represent orientation in a 3D space.
14  *
15  * Orientations are represented as two angles of rotation around the XZ and YZ axes.
16  * The angles are stored as sines and cosines to avoid unnecessary trigonometric calculations.
17  *
18  * Orientations are used for rotating object coordinates in a 3D space.
19  */
20 public class Orientation implements Cloneable {
21
22     /**
23      * The sine and cosine of the angles.
24      */
25     private double s1, c1, s2, c2;
26
27     /**
28      * The angle of rotation around the XZ axis.
29      */
30     private double angleXZ = 0;
31
32     /**
33      * The angle of rotation around the YZ axis.
34      */
35     private double angleYZ = 0;
36
37     public Orientation() {
38         computeMultipliers();
39     }
40
41     /**
42      * Creates a new orientation with the specified angles.
43      * @param angleXZ The angle of rotation around the XZ axis.
44      * @param angleYZ The angle of rotation around the YZ axis.
45      */
46     public Orientation(final double angleXZ, final double angleYZ) {
47         this.angleXZ = angleXZ;
48         this.angleYZ = angleYZ;
49         computeMultipliers();
50     }
51
52     @Override
53     public Orientation clone() {
54         return new Orientation(angleXZ, angleYZ);
55     }
56
57     /**
58      * Computes the sine and cosine of the angles.
59      */
60     private void computeMultipliers() {
61         s1 = sin(angleXZ);
62         c1 = cos(angleXZ);
63
64         s2 = sin(angleYZ);
65         c2 = cos(angleYZ);
66     }
67
68     /**
69      * Rotates the specified point around the XZ and YZ axes relative to the origin.
70      * @param point3d The point to rotate.
71      */
72     public void rotate(final Point3D point3d) {
73         // Rotate around the XZ axis.
74         final double z1 = (point3d.z * c1) - (point3d.x * s1);
75         point3d.x = (point3d.z * s1) + (point3d.x * c1);
76
77         // Rotate around the YZ axis.
78         point3d.z = (z1 * c2) - (point3d.y * s2);
79         point3d.y = (z1 * s2) + (point3d.y * c2);
80     }
81
82     /**
83      * Rotates current orientation around the XZ and YZ axes.
84      */
85     public void rotate(final double angleXZ, final double angleYZ) {
86         this.angleXZ += angleXZ;
87         this.angleYZ += angleYZ;
88         computeMultipliers();
89     }
90
91 }