Refactoring.
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / math / Orientation.java
diff --git a/src/main/java/eu/svjatoslav/sixth/e3d/math/Orientation.java b/src/main/java/eu/svjatoslav/sixth/e3d/math/Orientation.java
new file mode 100644 (file)
index 0000000..958286a
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * Sixth 3D engine. Copyright ©2012-2018, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 3 of the GNU Lesser General Public License
+ * or later as published by the Free Software Foundation.
+ *
+ */
+
+
+package eu.svjatoslav.sixth.e3d.math;
+
+import eu.svjatoslav.sixth.e3d.geometry.Point3D;
+
+public class Orientation implements Cloneable {
+
+    private double s1, c1, s2, c2;
+
+    private double angleXZ = 0;
+    private double angleYZ = 0;
+
+    public Orientation() {
+        computeMultipliers();
+    }
+
+    public Orientation(final double angleXZ, final double angleYZ) {
+        this.angleXZ = angleXZ;
+        this.angleYZ = angleYZ;
+        computeMultipliers();
+    }
+
+    @Override
+    public Orientation clone() {
+        return new Orientation(angleXZ, angleYZ);
+    }
+
+    private void computeMultipliers() {
+        s1 = Math.sin(angleXZ);
+        c1 = Math.cos(angleXZ);
+
+        s2 = Math.sin(angleYZ);
+        c2 = Math.cos(angleYZ);
+    }
+
+    public void rotate(final Point3D point3d) {
+        final double z1 = (point3d.z * c1) - (point3d.x * s1);
+        point3d.x = (point3d.z * s1) + (point3d.x * c1);
+
+        point3d.z = (z1 * c2) - (point3d.y * s2);
+        point3d.y = (z1 * s2) + (point3d.y * c2);
+    }
+
+    public void rotate(final double angleXZ, final double angleYZ) {
+        this.angleXZ += angleXZ;
+        this.angleYZ += angleYZ;
+        computeMultipliers();
+    }
+
+}