Updated readability of the code.
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / geometry / Point3D.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.geometry;
6
7 import static java.lang.Math.*;
8
9 /**
10  * Used to represent point in a 3D space or vector.
11  */
12
13 public class Point3D implements Cloneable {
14
15     // coordinates
16     public double x, y, z;
17
18     public Point3D() {
19     }
20
21     public Point3D(final double x, final double y, final double z) {
22         this.x = x;
23         this.y = y;
24         this.z = z;
25     }
26
27     public Point3D(final float x, final float y, final float z) {
28         this.x = x;
29         this.y = y;
30         this.z = z;
31     }
32
33     public Point3D(final int x, final int y, final int z) {
34         this.x = x;
35         this.y = y;
36         this.z = z;
37     }
38
39     /**
40      * Creates new current point by cloning coordinates from parent point.
41      */
42     public Point3D(final Point3D parent) {
43         x = parent.x;
44         y = parent.y;
45         z = parent.z;
46     }
47
48     /**
49      * Add other point to current point. Value of other point will not be changed.
50      *
51      * @param otherPoint point to add.
52      * @return current point.
53      */
54     public Point3D add(final Point3D otherPoint) {
55         x += otherPoint.x;
56         y += otherPoint.y;
57         z += otherPoint.z;
58         return this;
59     }
60
61     public Point3D clone() {
62         return new Point3D(this);
63     }
64
65     /**
66      * Copy coordinates from other point to current point. Value of other point will not be changed.
67      */
68     public Point3D clone(final Point3D otherPoint) {
69         x = otherPoint.x;
70         y = otherPoint.y;
71         z = otherPoint.z;
72         return this;
73     }
74
75     /**
76      * Set current point coordinates to the middle point between two other points.
77      *
78      * @param p1 first point.
79      * @param p2 second point.
80      * @return current point.
81      */
82     public Point3D computeMiddlePoint(final Point3D p1, final Point3D p2) {
83         x = (p1.x + p2.x) / 2d;
84         y = (p1.y + p2.y) / 2d;
85         z = (p1.z + p2.z) / 2d;
86         return this;
87     }
88
89     /**
90      * @return true if current point coordinates are equal to zero.
91      */
92     public boolean isZero() {
93         return (x == 0) && (y == 0) && (z == 0);
94     }
95
96     public double getAngleXZ(final Point3D anotherPoint) {
97         return Math.atan2(x - anotherPoint.x, z - anotherPoint.z);
98     }
99
100     public double getAngleYZ(final Point3D anotherPoint) {
101         return Math.atan2(y - anotherPoint.y, z - anotherPoint.z);
102     }
103
104     public double getAngleXY(final Point3D anotherPoint) {
105         return Math.atan2(x - anotherPoint.x, y - anotherPoint.y);
106     }
107
108     /**
109      * Compute distance to another point.
110      *
111      * @param anotherPoint point to compute distance to.
112      * @return distance to another point.
113      */
114     public double getDistanceTo(final Point3D anotherPoint) {
115         final double xDelta = x - anotherPoint.x;
116         final double yDelta = y - anotherPoint.y;
117         final double zDelta = z - anotherPoint.z;
118
119         return sqrt(((xDelta * xDelta) + (yDelta * yDelta) + (zDelta * zDelta)));
120     }
121
122     /**
123      * @return length of current vector.
124      */
125     public double getVectorLength() {
126         return sqrt(((x * x) + (y * y) + (z * z)));
127     }
128
129     /**
130      * Invert current point coordinates.
131      *
132      * @return current point.
133      */
134     public Point3D invert() {
135         x = -x;
136         y = -y;
137         z = -z;
138         return this;
139     }
140
141     /**
142      * Rotate current point around center point by angleXZ and angleYZ.
143      * <p>
144      * See also: <a href="https://marctenbosch.com/quaternions/">Let's remove Quaternions from every 3D Engine</a>
145      *
146      * @param center   center point.
147      * @param angleXZ angle around XZ axis.
148      * @param angleYZ angle around YZ axis.
149      */
150     public void rotate(final Point3D center, final double angleXZ,
151                        final double angleYZ) {
152         final double s1 = sin(angleXZ);
153         final double c1 = cos(angleXZ);
154
155         final double s2 = sin(angleYZ);
156         final double c2 = cos(angleYZ);
157
158         x -= center.x;
159         y -= center.y;
160         z -= center.z;
161
162         final double y1 = (z * s2) + (y * c2);
163         final double z1 = (z * c2) - (y * s2);
164
165         final double x1 = (z1 * s1) + (x * c1);
166         final double z2 = (z1 * c1) - (x * s1);
167
168         x = x1 + center.x;
169         y = y1 + center.y;
170         z = z2 + center.z;
171     }
172
173     /**
174      * Round current point coordinates to integer values.
175      */
176     public void roundToInteger() {
177         x = (int) x;
178         y = (int) y;
179         z = (int) z;
180     }
181
182     /**
183      * Scale down current point by factor.
184      * All coordinates will be divided by factor.
185      *
186      * @param factor factor to scale by.
187      * @return current point.
188      */
189     public Point3D scaleDown(final double factor) {
190         x /= factor;
191         y /= factor;
192         z /= factor;
193         return this;
194     }
195
196     /**
197      * Scale up current point by factor.
198      * All coordinates will be multiplied by factor.
199      *
200      * @param factor factor to scale by.
201      * @return current point.
202      */
203     public Point3D scaleUp(final double factor) {
204         x *= factor;
205         y *= factor;
206         z *= factor;
207         return this;
208     }
209
210     /**
211      * Set current point coordinates to given values.
212      * @param x X coordinate.
213      * @param y Y coordinate.
214      * @param z Z coordinate.
215      */
216     public void setValues(final double x, final double y, final double z) {
217         this.x = x;
218         this.y = y;
219         this.z = z;
220     }
221
222     /**
223      * Subtract other point from current point. Value of other point will not be changed.
224      *
225      * @return current point.
226      */
227     public Point3D subtract(final Point3D otherPoint) {
228         x -= otherPoint.x;
229         y -= otherPoint.y;
230         z -= otherPoint.z;
231         return this;
232     }
233
234     @Override
235     public String toString() {
236         return "x:" + x + " y:" + y + " z:" + z;
237     }
238
239     /**
240      * Translate current point along X axis by given increment.
241      *
242      * @return current point.
243      */
244     public Point3D translateX(final double xIncrement) {
245         x += xIncrement;
246         return this;
247     }
248
249     /**
250      * Translate current point along Y axis by given increment.
251      *
252      * @return current point.
253      */
254     public Point3D translateY(final double yIncrement) {
255         y += yIncrement;
256         return this;
257     }
258
259     /**
260      * Translate current point along Z axis by given increment.
261      *
262      * @return current point.
263      */
264     public Point3D translateZ(final double zIncrement) {
265         z += zIncrement;
266         return this;
267     }
268
269     /**
270      * Here we assume that Z coordinate is distance to the viewer.
271      * If Z is positive, then point is in front of the viewer, and therefore it is visible.
272      *
273      * @return point visibility status.
274      */
275     public boolean isVisible() {
276         return z > 0;
277     }
278
279     /**
280      * Resets point coordinates to zero along all axes.
281      *
282      * @return current point.
283      */
284     public Point3D zero() {
285         x = 0;
286         y = 0;
287         z = 0;
288         return this;
289     }
290
291 }