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