@@ -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