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