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