Updated copyright
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / geometry / Point3D.java
1 /*
2  * Sixth 3D engine. Copyright ©2012-2019, 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 extends Point2D implements Cloneable {
19
20     public double 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         super.add(direction);
51         z += direction.z;
52         return this;
53     }
54
55     @Override
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     @Override
75     public boolean isZero() {
76         return (x == 0) && (y == 0) && (z == 0);
77     }
78
79     public double getAngleXZ(final Point3D anotherPoint) {
80         return Math.atan2(x - anotherPoint.x, z - anotherPoint.z);
81     }
82
83     public double getAngleYZ(final Point3D anotherPoint) {
84         return Math.atan2(y - anotherPoint.y, z - anotherPoint.z);
85     }
86
87     /**
88      * Compute distance to another point.
89      */
90     public double getDistanceTo(final Point3D anotherPoint) {
91         final double xDelta = x - anotherPoint.x;
92         final double yDelta = y - anotherPoint.y;
93         final double zDelta = z - anotherPoint.z;
94
95         return sqrt(((xDelta * xDelta) + (yDelta * yDelta) + (zDelta * zDelta)));
96     }
97
98     @Override
99     public double getVectorLength() {
100         return sqrt(((x * x) + (y * y) + (z * z)));
101     }
102
103     @Override
104     public Point3D invert() {
105         x = -x;
106         y = -y;
107         z = -z;
108         return this;
109     }
110
111     public void rotate(final Point3D center, final double angleXZ,
112                        final double angleYZ) {
113         final double s1 = sin(angleXZ);
114         final double c1 = cos(angleXZ);
115
116         final double s2 = sin(angleYZ);
117         final double c2 = cos(angleYZ);
118
119         x -= center.x;
120         y -= center.y;
121         z -= center.z;
122
123         final double y1 = (z * s2) + (y * c2);
124         final double z1 = (z * c2) - (y * s2);
125
126         final double x1 = (z1 * s1) + (x * c1);
127         final double z2 = (z1 * c1) - (x * s1);
128
129         x = x1 + center.x;
130         y = y1 + center.y;
131         z = z2 + center.z;
132     }
133
134     @Override
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         super.subtract(direction);
163         z -= direction.z;
164         return this;
165     }
166
167     @Override
168     public String toString() {
169         return "x:" + x + " y:" + y + " z:" + z;
170     }
171
172     public Point3D translateX(final double xIncrement) {
173         x += xIncrement;
174         return this;
175     }
176
177     public Point3D translateY(final double yIncrement) {
178         y += yIncrement;
179         return this;
180     }
181
182     public Point3D translateZ(final double zIncrement) {
183         z += zIncrement;
184         return this;
185     }
186
187     public boolean isVisible() {
188
189         if (z > 0)
190             return true;
191
192         // if ((z > 0) && (x > -1000) && (y > -1000) && (x < 2000) && (y <
193         // 2000))
194         // return true;
195
196         return false;
197     }
198
199     @Override
200     public Point3D zero() {
201         super.zero();
202         z = 0;
203         return this;
204     }
205
206 }