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