Skip to content

Commit aa4f55d

Browse files
committed
some minor optimizations to ray/aabb intersection, does make a difference though...
1 parent 05c2776 commit aa4f55d

File tree

2 files changed

+55
-29
lines changed

2 files changed

+55
-29
lines changed

intersection/IntrRay3AxisAlignedBox3.cs

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -87,60 +87,86 @@ public static bool Intersects(ref Ray3d ray, ref AxisAlignedBox3d box, double ex
8787
Vector3d AWdU = Vector3d.Zero;
8888
Vector3d DdU = Vector3d.Zero;
8989
Vector3d ADdU = Vector3d.Zero;
90-
Vector3d AWxDdU = Vector3d.Zero;
9190
double RHS;
9291

9392
Vector3d diff = ray.Origin - box.Center;
9493
Vector3d extent = box.Extents + expandExtents;
9594

96-
WdU[0] = ray.Direction.x; // ray.Direction.Dot(Vector3d.AxisX);
97-
AWdU[0] = Math.Abs(WdU[0]);
98-
DdU[0] = diff.x; // diff.Dot(Vector3d.AxisX);
99-
ADdU[0] = Math.Abs(DdU[0]);
100-
if (ADdU[0] > extent.x && DdU[0] * WdU[0] >= (double)0) {
95+
WdU.x = ray.Direction.x; // ray.Direction.Dot(Vector3d.AxisX);
96+
AWdU.x = Math.Abs(WdU.x);
97+
DdU.x = diff.x; // diff.Dot(Vector3d.AxisX);
98+
ADdU.x = Math.Abs(DdU.x);
99+
if (ADdU.x > extent.x && DdU.x * WdU.x >= (double)0) {
101100
return false;
102101
}
103102

104-
WdU[1] = ray.Direction.y; // ray.Direction.Dot(Vector3d.AxisY);
105-
AWdU[1] = Math.Abs(WdU[1]);
106-
DdU[1] = diff.y; // diff.Dot(Vector3d.AxisY);
107-
ADdU[1] = Math.Abs(DdU[1]);
108-
if (ADdU[1] > extent.y && DdU[1] * WdU[1] >= (double)0) {
103+
WdU.y = ray.Direction.y; // ray.Direction.Dot(Vector3d.AxisY);
104+
AWdU.y = Math.Abs(WdU.y);
105+
DdU.y = diff.y; // diff.Dot(Vector3d.AxisY);
106+
ADdU.y = Math.Abs(DdU.y);
107+
if (ADdU.y > extent.y && DdU.y * WdU.y >= (double)0) {
109108
return false;
110109
}
111110

112-
WdU[2] = ray.Direction.z; // ray.Direction.Dot(Vector3d.AxisZ);
113-
AWdU[2] = Math.Abs(WdU[2]);
114-
DdU[2] = diff.z; // diff.Dot(Vector3d.AxisZ);
115-
ADdU[2] = Math.Abs(DdU[2]);
116-
if (ADdU[2] > extent.z && DdU[2] * WdU[2] >= (double)0) {
111+
WdU.z = ray.Direction.z; // ray.Direction.Dot(Vector3d.AxisZ);
112+
AWdU.z = Math.Abs(WdU.z);
113+
DdU.z = diff.z; // diff.Dot(Vector3d.AxisZ);
114+
ADdU.z = Math.Abs(DdU.z);
115+
if (ADdU.z > extent.z && DdU.z * WdU.z >= (double)0) {
117116
return false;
118117
}
119118

120119
Vector3d WxD = ray.Direction.Cross(diff);
120+
Vector3d AWxDdU = Vector3d.Zero;
121121

122-
AWxDdU[0] = Math.Abs(WxD.x); // Math.Abs(WxD.Dot(Vector3d.AxisX));
123-
RHS = extent.y * AWdU[2] + extent.z * AWdU[1];
124-
if (AWxDdU[0] > RHS) {
122+
AWxDdU.x = Math.Abs(WxD.x); // Math.Abs(WxD.Dot(Vector3d.AxisX));
123+
RHS = extent.y * AWdU.z + extent.z * AWdU.y;
124+
if (AWxDdU.x > RHS) {
125125
return false;
126126
}
127127

128-
AWxDdU[1] = Math.Abs(WxD.y); // Math.Abs(WxD.Dot(Vector3d.AxisY));
129-
RHS = extent.x * AWdU[2] + extent.z * AWdU[0];
130-
if (AWxDdU[1] > RHS) {
128+
AWxDdU.y = Math.Abs(WxD.y); // Math.Abs(WxD.Dot(Vector3d.AxisY));
129+
RHS = extent.x * AWdU.z + extent.z * AWdU.x;
130+
if (AWxDdU.y > RHS) {
131131
return false;
132132
}
133133

134-
AWxDdU[2] = Math.Abs(WxD.z); // Math.Abs(WxD.Dot(Vector3d.AxisZ));
135-
RHS = extent.x * AWdU[1] + extent.y * AWdU[0];
136-
if (AWxDdU[2] > RHS) {
134+
AWxDdU.z = Math.Abs(WxD.z); // Math.Abs(WxD.Dot(Vector3d.AxisZ));
135+
RHS = extent.x * AWdU.y + extent.y * AWdU.x;
136+
if (AWxDdU.z > RHS) {
137137
return false;
138138
}
139139

140140
return true;
141141
}
142142

143143

144+
/// <summary>
145+
/// Find intersection of ray with AABB, without having to construct any new classes.
146+
/// Returns ray T-value of first intersection (or double.MaxVlaue on miss)
147+
/// </summary>
148+
public static bool FindRayIntersectT(ref Ray3d ray, ref AxisAlignedBox3d box, out double RayParam)
149+
{
150+
double RayParam0 = 0.0;
151+
double RayParam1 = double.MaxValue;
152+
int Quantity = 0;
153+
Vector3d Point0 = Vector3d.Zero;
154+
Vector3d Point1 = Vector3d.Zero;
155+
IntersectionType Type = IntersectionType.Empty;
156+
IntrLine3AxisAlignedBox3.DoClipping(ref RayParam0, ref RayParam1, ref ray.Origin, ref ray.Direction, ref box,
157+
true, ref Quantity, ref Point0, ref Point1, ref Type);
158+
159+
if (Type != IntersectionType.Empty) {
160+
RayParam = RayParam0;
161+
return true;
162+
} else {
163+
RayParam = double.MaxValue;
164+
return false;
165+
}
166+
}
167+
168+
169+
144170

145171
}
146172
}

spatial/DMeshAABBTree.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ namespace g3
2525
/// - FindNearestTriangles(otherAABBTree, maxdist)
2626
/// - IsInside(point)
2727
/// - WindingNumber(point)
28+
/// - FastWindingNumber(point)
2829
/// - DoTraversal(generic_traversal_object)
2930
///
3031
/// </summary>
@@ -2238,11 +2239,10 @@ double box_ray_intersect_t(int iBox, Ray3d ray)
22382239
Vector3f e = box_extents[iBox];
22392240
AxisAlignedBox3d box = new AxisAlignedBox3d(ref c, e.x + box_eps, e.y + box_eps, e.z + box_eps);
22402241

2241-
IntrRay3AxisAlignedBox3 intr = new IntrRay3AxisAlignedBox3(ray, box);
2242-
if (intr.Find()) {
2243-
return intr.RayParam0;
2242+
double ray_t = double.MaxValue;
2243+
if (IntrRay3AxisAlignedBox3.FindRayIntersectT(ref ray, ref box, out ray_t)) {
2244+
return ray_t;
22442245
} else {
2245-
Debug.Assert(intr.Result != IntersectionResult.InvalidQuery);
22462246
return double.MaxValue;
22472247
}
22482248
}

0 commit comments

Comments
 (0)