2 * Sixth 3D engine. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.sixth.e3d.geometry;
7 import eu.svjatoslav.sixth.e3d.renderer.octree.IntegerPoint;
9 import static java.lang.Math.*;
12 * Used to represent point in a 3D space or vector.
15 public class Point3D implements Cloneable {
18 public double x, y, z;
23 public Point3D(final double x, final double y, final double z) {
29 public Point3D(final float x, final float y, final float z) {
35 public Point3D(final int x, final int y, final int z) {
41 public Point3D(IntegerPoint point) {
49 * Creates new current point by cloning coordinates from parent point.
51 public Point3D(final Point3D parent) {
58 * Add other point to current point. Value of other point will not be changed.
60 * @param otherPoint point to add.
61 * @return current point.
63 public Point3D add(final Point3D otherPoint) {
71 * Add coordinates of current point to other point. Value of current point will not be changed.
73 * @return current point.
75 public Point3D addTo(final Point3D... otherPoints) {
76 for (final Point3D otherPoint : otherPoints) otherPoint.add(this);
81 * Create new point by cloning position of current point.
83 * @return newly created clone.
85 public Point3D clone() {
86 return new Point3D(this);
90 * Copy coordinates from other point to current point. Value of other point will not be changed.
92 public Point3D clone(final Point3D otherPoint) {
100 * Set current point coordinates to the middle point between two other points.
102 * @param p1 first point.
103 * @param p2 second point.
104 * @return current point.
106 public Point3D computeMiddlePoint(final Point3D p1, final Point3D p2) {
107 x = (p1.x + p2.x) / 2d;
108 y = (p1.y + p2.y) / 2d;
109 z = (p1.z + p2.z) / 2d;
114 * @return true if current point coordinates are equal to zero.
116 public boolean isZero() {
117 return (x == 0) && (y == 0) && (z == 0);
120 public double getAngleXZ(final Point3D anotherPoint) {
121 return Math.atan2(x - anotherPoint.x, z - anotherPoint.z);
124 public double getAngleYZ(final Point3D anotherPoint) {
125 return Math.atan2(y - anotherPoint.y, z - anotherPoint.z);
128 public double getAngleXY(final Point3D anotherPoint) {
129 return Math.atan2(x - anotherPoint.x, y - anotherPoint.y);
133 * Compute distance to another point.
135 * @param anotherPoint point to compute distance to.
136 * @return distance to another point.
138 public double getDistanceTo(final Point3D anotherPoint) {
139 final double xDelta = x - anotherPoint.x;
140 final double yDelta = y - anotherPoint.y;
141 final double zDelta = z - anotherPoint.z;
143 return sqrt(((xDelta * xDelta) + (yDelta * yDelta) + (zDelta * zDelta)));
147 * @return length of current vector.
149 public double getVectorLength() {
150 return sqrt(((x * x) + (y * y) + (z * z)));
154 * Invert current point coordinates.
156 * @return current point.
158 public Point3D invert() {
166 * Rotate current point around center point by angleXZ and angleYZ.
168 * See also: <a href="https://marctenbosch.com/quaternions/">Let's remove Quaternions from every 3D Engine</a>
170 * @param center center point.
171 * @param angleXZ angle around XZ axis.
172 * @param angleYZ angle around YZ axis.
174 public Point3D rotate(final Point3D center, final double angleXZ,
175 final double angleYZ) {
176 final double s1 = sin(angleXZ);
177 final double c1 = cos(angleXZ);
179 final double s2 = sin(angleYZ);
180 final double c2 = cos(angleYZ);
186 final double y1 = (z * s2) + (y * c2);
187 final double z1 = (z * c2) - (y * s2);
189 final double x1 = (z1 * s1) + (x * c1);
190 final double z2 = (z1 * c1) - (x * s1);
199 public Point3D rotate(final double angleXZ, final double angleYZ) {
200 return rotate(new Point3D(0, 0, 0), angleXZ, angleYZ);
204 * Round current point coordinates to integer values.
206 public void roundToInteger() {
213 * Scale down current point by factor.
214 * All coordinates will be divided by factor.
216 * @param factor factor to scale by.
217 * @return current point.
219 public Point3D scaleDown(final double factor) {
227 * Scale up current point by factor.
228 * All coordinates will be multiplied by factor.
230 * @param factor factor to scale by.
231 * @return current point.
233 public Point3D scaleUp(final double factor) {
241 * Set current point coordinates to given values.
243 * @param x X coordinate.
244 * @param y Y coordinate.
245 * @param z Z coordinate.
247 public void setValues(final double x, final double y, final double z) {
254 * Subtract other point from current point. Value of other point will not be changed.
256 * @return current point.
258 public Point3D subtract(final Point3D otherPoint) {
266 public String toString() {
267 return "x:" + x + " y:" + y + " z:" + z;
271 * Translate current point along X axis by given increment.
273 * @return current point.
275 public Point3D translateX(final double xIncrement) {
281 * Translate current point along Y axis by given increment.
283 * @return current point.
285 public Point3D translateY(final double yIncrement) {
291 * Translate current point along Z axis by given increment.
293 * @return current point.
295 public Point3D translateZ(final double zIncrement) {
301 * Here we assume that Z coordinate is distance to the viewer.
302 * If Z is positive, then point is in front of the viewer, and therefore it is visible.
304 * @return point visibility status.
306 public boolean isVisible() {
311 * Resets point coordinates to zero along all axes.
313 * @return current point.
315 public Point3D zero() {