Code refactoring
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / geometry / Point3D.java
1 /*
2  * Sixth 3D engine. Copyright ©2012-2020, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 3 of the GNU Lesser General Public License
6  * or later as published by the Free Software Foundation.
7  *
8  */
9
10 package eu.svjatoslav.sixth.e3d.geometry;
11
12 import static java.lang.Math.*;
13
14 /**
15  * Used to represent point in a 3D space or vector.
16  */
17
18 public class Point3D implements Cloneable {
19
20     public double x, y, z;
21
22     public Point3D() {
23     }
24
25     public Point3D(final double x, final double y, final double z) {
26         this.x = x;
27         this.y = y;
28         this.z = z;
29     }
30
31     public Point3D(final float x, final float y, final float z) {
32         this.x = x;
33         this.y = y;
34         this.z = z;
35     }
36
37     public Point3D(final int x, final int y, final int z) {
38         this.x = x;
39         this.y = y;
40         this.z = z;
41     }
42
43     public Point3D(final Point3D parentPoint) {
44         x = parentPoint.x;
45         y = parentPoint.y;
46         z = parentPoint.z;
47     }
48
49     public Point3D add(final Point3D direction) {
50         x += direction.x;
51         y += direction.y;
52         z += direction.z;
53         return this;
54     }
55
56     public Point3D clone() {
57         return new Point3D(this);
58     }
59
60     public Point3D clone(final Point3D source) {
61         x = source.x;
62         y = source.y;
63         z = source.z;
64         return this;
65     }
66
67     public Point3D computeMiddlePoint(final Point3D p1, final Point3D p2) {
68         x = (p1.x + p2.x) / 2d;
69         y = (p1.y + p2.y) / 2d;
70         z = (p1.z + p2.z) / 2d;
71         return this;
72     }
73
74     public boolean isZero() {
75         return (x == 0) && (y == 0) && (z == 0);
76     }
77
78     public double getAngleXZ(final Point3D anotherPoint) {
79         return Math.atan2(x - anotherPoint.x, z - anotherPoint.z);
80     }
81
82     public double getAngleYZ(final Point3D anotherPoint) {
83         return Math.atan2(y - anotherPoint.y, z - anotherPoint.z);
84     }
85
86     public double getAngleXY(final Point3D anotherPoint) {
87         return Math.atan2(x - anotherPoint.x, y - anotherPoint.y);
88     }
89
90     /**
91      * Compute distance to another point.
92      */
93     public double getDistanceTo(final Point3D anotherPoint) {
94         final double xDelta = x - anotherPoint.x;
95         final double yDelta = y - anotherPoint.y;
96         final double zDelta = z - anotherPoint.z;
97
98         return sqrt(((xDelta * xDelta) + (yDelta * yDelta) + (zDelta * zDelta)));
99     }
100
101     public double getVectorLength() {
102         return sqrt(((x * x) + (y * y) + (z * z)));
103     }
104
105     public Point3D invert() {
106         x = -x;
107         y = -y;
108         z = -z;
109         return this;
110     }
111
112     public void rotate(final Point3D center, final double angleXZ,
113                        final double angleYZ) {
114         final double s1 = sin(angleXZ);
115         final double c1 = cos(angleXZ);
116
117         final double s2 = sin(angleYZ);
118         final double c2 = cos(angleYZ);
119
120         x -= center.x;
121         y -= center.y;
122         z -= center.z;
123
124         final double y1 = (z * s2) + (y * c2);
125         final double z1 = (z * c2) - (y * s2);
126
127         final double x1 = (z1 * s1) + (x * c1);
128         final double z2 = (z1 * c1) - (x * s1);
129
130         x = x1 + center.x;
131         y = y1 + center.y;
132         z = z2 + center.z;
133     }
134
135     public void roundToInteger() {
136         x = (int) x;
137         y = (int) y;
138         z = (int) z;
139     }
140
141     public Point3D scaleDown(final double factor) {
142         x /= factor;
143         y /= factor;
144         z /= factor;
145         return this;
146     }
147
148     public Point3D scaleUp(final double factor) {
149         x *= factor;
150         y *= factor;
151         z *= factor;
152         return this;
153     }
154
155     public void setValues(final double x, final double y, final double z) {
156         this.x = x;
157         this.y = y;
158         this.z = z;
159     }
160
161     public Point3D subtract(final Point3D direction) {
162         x -= direction.x;
163         y -= direction.y;
164         z -= direction.z;
165         return this;
166     }
167
168     @Override
169     public String toString() {
170         return "x:" + x + " y:" + y + " z:" + z;
171     }
172
173     public Point3D translateX(final double xIncrement) {
174         x += xIncrement;
175         return this;
176     }
177
178     public Point3D translateY(final double yIncrement) {
179         y += yIncrement;
180         return this;
181     }
182
183     public Point3D translateZ(final double zIncrement) {
184         z += zIncrement;
185         return this;
186     }
187
188     public boolean isVisible() {
189
190         if (z > 0)
191             return true;
192
193         // if ((z > 0) && (x > -1000) && (y > -1000) && (x < 2000) && (y <
194         // 2000))
195         // return true;
196
197         return false;
198     }
199
200     public Point3D zero() {
201         x = 0;
202         y = 0;
203         z = 0;
204         return this;
205     }
206
207 }