b889392fef6d6bc242535fde6931ab1b8f969f6c
[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 /**
10  * Used to represent transformation in a 3D space.
11  */
12 public class Orientation implements Cloneable {
13
14
15     private double s1, c1, s2, c2;
16
17     /**
18      * The angle of rotation around the XZ axis.
19      */
20     private double angleXZ = 0;
21
22     /**
23      * The angle of rotation around the YZ axis.
24      */
25     private double angleYZ = 0;
26
27     public Orientation() {
28         computeMultipliers();
29     }
30
31     public Orientation(final double angleXZ, final double angleYZ) {
32         this.angleXZ = angleXZ;
33         this.angleYZ = angleYZ;
34         computeMultipliers();
35     }
36
37     @Override
38     public Orientation clone() {
39         return new Orientation(angleXZ, angleYZ);
40     }
41
42     /**
43      * Computes the sine and cosine of the angles.
44      */
45     private void computeMultipliers() {
46         s1 = Math.sin(angleXZ);
47         c1 = Math.cos(angleXZ);
48
49         s2 = Math.sin(angleYZ);
50         c2 = Math.cos(angleYZ);
51     }
52
53     public void rotate(final Point3D point3d) {
54         final double z1 = (point3d.z * c1) - (point3d.x * s1);
55         point3d.x = (point3d.z * s1) + (point3d.x * c1);
56
57         point3d.z = (z1 * c2) - (point3d.y * s2);
58         point3d.y = (z1 * s2) + (point3d.y * c2);
59     }
60
61     public void rotate(final double angleXZ, final double angleYZ) {
62         this.angleXZ += angleXZ;
63         this.angleYZ += angleYZ;
64         computeMultipliers();
65     }
66
67 }