2 * Sixth 3D engine. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.sixth.e3d.math;
7 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
9 import static java.lang.Math.cos;
10 import static java.lang.Math.sin;
13 * Used to represent orientation in a 3D space.
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.
18 * Orientations are used for rotating object coordinates in a 3D space.
20 public class Orientation implements Cloneable {
23 * The sine and cosine of the angles.
25 private double s1, c1, s2, c2;
28 * The angle of rotation around the XZ axis.
30 private double angleXZ = 0;
33 * The angle of rotation around the YZ axis.
35 private double angleYZ = 0;
37 public Orientation() {
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.
46 public Orientation(final double angleXZ, final double angleYZ) {
47 this.angleXZ = angleXZ;
48 this.angleYZ = angleYZ;
53 public Orientation clone() {
54 return new Orientation(angleXZ, angleYZ);
58 * Computes the sine and cosine of the angles.
60 private void computeMultipliers() {
69 * Rotates the specified point around the XZ and YZ axes relative to the origin.
70 * @param point3d The point to rotate.
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);
77 // Rotate around the YZ axis.
78 point3d.z = (z1 * c2) - (point3d.y * s2);
79 point3d.y = (z1 * s2) + (point3d.y * c2);
83 * Rotates current orientation around the XZ and YZ axes.
85 public void rotate(final double angleXZ, final double angleYZ) {
86 this.angleXZ += angleXZ;
87 this.angleYZ += angleYZ;