@@ -36,12 +36,69 @@ public Ray3f(Vector3f origin, Vector3f direction) {
3636 if (direction == null ) {
3737 throw new IllegalArgumentException ("Direction cannot be null." );
3838 }
39- this .origin = origin ;
40- this .direction = direction ;
39+ this .origin = new Vector3f ( origin ) ;
40+ this .direction = new Vector3f ( direction ) ;
4141 this .direction .normalizeLocal ();
4242 this .directionInv = direction .reciprocal ();
4343 }
4444
45+ /**
46+ * Computes the shortest distance from the ray to a point in 3D space.
47+ *
48+ * <p>This method calculates the perpendicular distance from the given point to the ray. If the
49+ * point lies behind the origin of the ray, the distance from the point to the ray's origin is
50+ * returned. If the point lies along the ray's path (in the direction of the ray), the
51+ * perpendicular distance is calculated. The ray is considered to extend infinitely in both
52+ * directions from the origin.
53+ *
54+ * @param point The point in 3D space to compute the distance to (non-null).
55+ * @return The shortest distance from the ray to the point. If the point is behind the ray's
56+ * origin, the distance from the point to the origin is returned.
57+ * @throws IllegalArgumentException if the point is null.
58+ */
59+ public float distanceToPoint (Vector3f point ) {
60+ if (point == null ) {
61+ throw new IllegalArgumentException ("Point cannot be null." );
62+ }
63+
64+ // Calculate vector from ray origin to the point
65+ Vector3f toPoint = point .subtract (origin );
66+
67+ // Project the vector to the ray's direction (dot product normalized)
68+ float projection = toPoint .dot (direction );
69+
70+ // If the projection is negative, the point is behind the ray's origin
71+ if (projection < 0 ) {
72+ // Return the distance from the origin to the point
73+ return toPoint .length ();
74+ }
75+
76+ // Return the perpendicular distance (calculate as the distance from the point to the closest
77+ // point on the ray)
78+ Vector3f closestPoint = getPointAt (projection );
79+ return closestPoint .subtract (point ).length ();
80+ }
81+
82+ /**
83+ * Computes the point along the ray at a given parameter {@code t}.
84+ *
85+ * <p>The formula for the point is:
86+ *
87+ * <pre>{@code
88+ * point = origin + t * direction
89+ * }</pre>
90+ *
91+ * where {@code t} is a scalar representing the distance along the ray from the origin. Positive
92+ * values of {@code t} will give points in the direction the ray is pointing, while negative
93+ * values will give points in the opposite direction.
94+ *
95+ * @param t The parameter along the ray (can be negative, zero, or positive).
96+ * @return The point at parameter {@code t}.
97+ */
98+ public Vector3f getPointAt (float t ) {
99+ return origin .add (direction .mult (t ));
100+ }
101+
45102 /**
46103 * Returns the origin of the ray.
47104 *
@@ -50,7 +107,7 @@ public Ray3f(Vector3f origin, Vector3f direction) {
50107 * @return The origin of the ray.
51108 */
52109 public Vector3f getOrigin () {
53- return origin ;
110+ return new Vector3f ( origin ) ;
54111 }
55112
56113 /**
@@ -62,7 +119,7 @@ public Vector3f getOrigin() {
62119 * @return The direction vector of the ray.
63120 */
64121 public Vector3f getDirection () {
65- return direction ;
122+ return new Vector3f ( direction ) ;
66123 }
67124
68125 /**
@@ -74,26 +131,6 @@ public Vector3f getDirection() {
74131 * @return The reciprocal of the direction vector of the ray.
75132 */
76133 public Vector3f getDirectionInv () {
77- return directionInv ;
78- }
79-
80- /**
81- * Computes the point along the ray at a given parameter {@code t}.
82- *
83- * <p>The formula for the point is:
84- *
85- * <pre>{@code
86- * point = origin + t * direction
87- * }</pre>
88- *
89- * where {@code t} is a scalar representing the distance along the ray from the origin. Positive
90- * values of {@code t} will give points in the direction the ray is pointing, while negative
91- * values will give points in the opposite direction.
92- *
93- * @param t The parameter along the ray (can be negative, zero, or positive).
94- * @return The point at parameter {@code t}.
95- */
96- public Vector3f getPointAt (float t ) {
97- return origin .add (direction .mult (t ));
134+ return new Vector3f (directionInv );
98135 }
99136}
0 commit comments