Skip to content

Commit 45f63d5

Browse files
🐛 Fix intersection caching and indexing in Torus class
1 parent 893c4eb commit 45f63d5

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

raysect/primitive/torus.pxd

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,14 @@ from raysect.core cimport Primitive, Point3D, Vector3D, Ray, Intersection
3333

3434
cdef class Torus(Primitive):
3535

36-
cdef double _major_radius, _minor_radius
37-
cdef bint _further_intersection
38-
cdef double _next_t
39-
cdef Point3D _cached_origin
40-
cdef Vector3D _cached_direction
41-
cdef Ray _cached_ray
36+
cdef:
37+
double _major_radius, _minor_radius
38+
bint _further_intersection
39+
int _next_t_index
40+
int _num_t
41+
double[4] _cached_t
42+
Point3D _cached_origin
43+
Vector3D _cached_direction
44+
Ray _cached_ray
4245

4346
cdef Intersection _generate_intersection(self, Ray ray, Point3D origin, Vector3D direction, double ray_distance)

raysect/primitive/torus.pyx

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ cdef class Torus(Primitive):
9191

9292
# initialise next intersection caching and control attributes
9393
self._further_intersection = False
94-
self._next_t = 0.0
94+
self._next_t_index = 0
95+
self._num_t = 0
9596
self._cached_origin = None
9697
self._cached_direction = None
9798
self._cached_ray = None
@@ -233,15 +234,19 @@ cdef class Torus(Primitive):
233234
if t[0] > ray.max_distance or t[3] < 0.0:
234235
return None
235236

237+
# cache the all intersection points
238+
self._num_t = num
239+
self._cached_t = t
240+
236241
for i in range(num - 1):
237242
if t[i] >= 0.0:
238243
t_closest = t[i]
239244
if t[i + 1] <= ray.max_distance:
240245
self._further_intersection = True
246+
self._next_t_index = i + 1
241247
self._cached_ray = ray
242248
self._cached_origin = origin
243249
self._cached_direction = direction
244-
self._next_t = t[i + 1]
245250

246251
return self._generate_intersection(ray, origin, direction, t_closest)
247252

@@ -254,12 +259,33 @@ cdef class Torus(Primitive):
254259

255260
cpdef Intersection next_intersection(self):
256261

262+
cdef:
263+
double next_t
264+
Intersection new_intersection
265+
257266
if not self._further_intersection:
258267
return None
259268

260-
# this is the 2nd intersection
261-
self._further_intersection = False
262-
return self._generate_intersection(self._cached_ray, self._cached_origin, self._cached_direction, self._next_t)
269+
next_t = self._cached_t[self._next_t_index]
270+
271+
# generate the next intersection.
272+
new_intersection = self._generate_intersection(
273+
self._cached_ray,
274+
self._cached_origin,
275+
self._cached_direction,
276+
next_t,
277+
)
278+
279+
# update the next intersection index
280+
# if there are no more intersections, disable further intersection state
281+
# and reset the next_t_index to 0
282+
if self._next_t_index < self._num_t - 1:
283+
self._next_t_index += 1
284+
else:
285+
self._further_intersection = False
286+
self._next_t_index = 0
287+
288+
return new_intersection
263289

264290
cdef Intersection _generate_intersection(self, Ray ray, Point3D origin, Vector3D direction, double ray_distance):
265291

0 commit comments

Comments
 (0)