Intersections with ray and scene. Ray is defined in the root node's coordinate system.
(self, ray_origin, ray_direction)
| 81 | yield ray.representation(light, world) |
| 82 | |
| 83 | def intersections(self, ray_origin, ray_direction) -> Sequence[Tuple[Node, Tuple]]: |
| 84 | """ Intersections with ray and scene. Ray is defined in the root node's |
| 85 | coordinate system. |
| 86 | """ |
| 87 | # to-do: Prune which nodes are queried for intersections by first |
| 88 | # intersecting the ray with bounding boxes of the node. |
| 89 | root = self.root |
| 90 | if root is None: |
| 91 | return tuple() |
| 92 | |
| 93 | def distance_sort_key(i): |
| 94 | v = np.array(i.point) - np.array(ray_origin) |
| 95 | d = np.linalg.norm(v) |
| 96 | return d |
| 97 | |
| 98 | all_intersections = self.root.intersections(ray_origin, ray_direction) |
| 99 | # Convert intersection point to root frame/node. |
| 100 | all_intersections = map(lambda x: x.to(root), all_intersections) |
| 101 | # Filter for forward intersections only |
| 102 | all_intersections = tuple( |
| 103 | filter( |
| 104 | lambda x: intersection_point_is_ahead( |
| 105 | ray_origin, ray_direction, x.point |
| 106 | ), |
| 107 | all_intersections, |
| 108 | ) |
| 109 | ) |
| 110 | |
| 111 | # Sort by distance to ray |
| 112 | all_intersections = tuple(sorted(all_intersections, key=distance_sort_key)) |
| 113 | # to-do: Correctly order touching interfaces |
| 114 | # touching_idx = [] |
| 115 | # for idx, pair in enumerate(zip(all_intersections[:-1], all_intersections[1:])): |
| 116 | # if close_to_zero(distance_between(pair[0].point, pair[1].point)): |
| 117 | # touching_idx.append(idx) |
| 118 | # for idx in touching_idx: |
| 119 | # i = list(all_intersections) |
| 120 | # a, b = idx - 1, idx |
| 121 | # if i[a].hit != i[b].hit: |
| 122 | # # Swap order |
| 123 | # i[b], i[a] = i[a], i[b] |
| 124 | # all_intersections = tuple(i) |
| 125 | return all_intersections |