Skip to content

Commit f430832

Browse files
committed
feat: add precomputed directionInv to Ray3f for optimized ray-box
intersection - Introduced the `directionInv` field in the `Ray3f` class to store the reciprocal of the direction vector components. - Precomputing the reciprocal reduces computation overhead in operations like ray-box intersections, improving performance. - Updated the constructor to calculate and assign `directionInv` during ray initialization.
1 parent af6b8ac commit f430832

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

src/main/java/math/Ray3f.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ public class Ray3f {
2222
*/
2323
private final Vector3f direction;
2424

25+
/**
26+
* The reciprocal of the direction vector, used for optimized ray-box
27+
* intersection.
28+
*/
29+
private final Vector3f directionInv;
30+
2531
/**
2632
* Constructs a new {@code Ray3f} with the given origin and direction.
2733
* <p>
@@ -45,10 +51,14 @@ public Ray3f(Vector3f origin, Vector3f direction) {
4551
this.origin = origin;
4652
this.direction = direction;
4753
this.direction.normalizeLocal();
54+
this.directionInv = direction.reciprocal();
4855
}
4956

5057
/**
5158
* Returns the origin of the ray.
59+
* <p>
60+
* The origin is the starting point from which the ray emanates.
61+
* </p>
5262
*
5363
* @return The origin of the ray.
5464
*/
@@ -58,13 +68,32 @@ public Vector3f getOrigin() {
5868

5969
/**
6070
* Returns the normalized direction vector of the ray.
71+
* <p>
72+
* The direction vector defines the direction in which the ray travels. The
73+
* vector is normalized, ensuring consistent calculations for operations like
74+
* intersections.
75+
* </p>
6176
*
6277
* @return The direction vector of the ray.
6378
*/
6479
public Vector3f getDirection() {
6580
return direction;
6681
}
6782

83+
/**
84+
* Returns the reciprocal of the direction vector of the ray.
85+
* <p>
86+
* The reciprocal of the direction vector is precomputed to optimize ray-box
87+
* intersection tests, where division by components of the direction vector is
88+
* required.
89+
* </p>
90+
*
91+
* @return The reciprocal of the direction vector of the ray.
92+
*/
93+
public Vector3f getDirectionInv() {
94+
return directionInv;
95+
}
96+
6897
/**
6998
* Computes the point along the ray at a given parameter {@code t}.
7099
* <p>
@@ -77,10 +106,12 @@ public Vector3f getDirection() {
77106
* </pre>
78107
*
79108
* where {@code t} is a scalar representing the distance along the ray from
80-
* the origin.
109+
* the origin. Positive values of {@code t} will give points in the direction
110+
* the ray is pointing, while negative values will give points in the opposite
111+
* direction.
81112
* </p>
82113
*
83-
* @param t The parameter along the ray (can be negative, zero, or positive)-
114+
* @param t The parameter along the ray (can be negative, zero, or positive).
84115
* @return The point at parameter {@code t}.
85116
*/
86117
public Vector3f getPointAt(float t) {

0 commit comments

Comments
 (0)