def local_intersect(self, local_ray): xtmin, xtmax = self.check_axis(local_ray.origin[0], local_ray.direction[0]) ytmin, ytmax = self.check_axis(local_ray.origin[1], local_ray.direction[1]) ztmin, ztmax = self.check_axis(local_ray.origin[2], local_ray.direction[2]) tmin = max(xtmin, ytmin, ztmin) tmax = min(xtmax, ytmax, ztmax) # ray misses cube? if tmin > tmax: return [] return [Intersection(tmin, self), Intersection(tmax, self)]
def intersect_caps(self, ray, xs): # caps only matter if the cylinder is closed, and might possibly be # intersected by the ray. if not self.closed or abs(ray.direction[1]) < self.EPSILON: return [] # check for an intersection with the lower end cap by intersecting # the ray with the plane at y=cyl.minimum t = (self.minimum - ray.origin[1]) / ray.direction[1] if self.check_cap(ray, t, self.minimum): xs.append(Intersection(t, self)) # check for an intersection with the upper end cap by intersecting # the ray with the plane at y=cyl.maximum t = (self.maximum - ray.origin[1]) / ray.direction[1] if self.check_cap(ray, t, self.maximum): xs.append(Intersection(t, self))
def local_intersect(self, local_ray): a = local_ray.direction[0]**2 - \ local_ray.direction[1]**2 + \ local_ray.direction[2]**2 b = 2 * local_ray.origin[0] * local_ray.direction[0] - \ 2 * local_ray.origin[1] * local_ray.direction[1] + \ 2 * local_ray.origin[2] * local_ray.direction[2] if abs(a) < self.EPSILON and abs(b) < self.EPSILON: return [] c = local_ray.origin[0]**2 - \ local_ray.origin[1]**2 + \ local_ray.origin[2]**2 if abs(a) < self.EPSILON: t = -c / (2 * b) xs = [Intersection(t, self)] self.intersect_caps(local_ray, xs) return xs discriminant = b**2 - 4 * a * c # ray does not intersect the cone if discriminant < 0: return [] t0 = (-b - sqrt(discriminant)) / (2 * a) t1 = (-b + sqrt(discriminant)) / (2 * a) if t0 > t1: t0, t1 = t1, t0 xs = [] y0 = local_ray.origin[1] + t0 * local_ray.direction[1] if self.minimum < y0 < self.maximum: xs.append(Intersection(t0, self)) y1 = local_ray.origin[1] + t1 * local_ray.direction[1] if self.minimum < y1 < self.maximum: xs.append(Intersection(t1, self)) self.intersect_caps(local_ray, xs) return xs
def local_intersect(self, local_ray): sphere_to_ray = subtract(local_ray.origin, point(0, 0, 0)) a = dot(local_ray.direction, local_ray.direction) b = 2 * dot(local_ray.direction, sphere_to_ray) c = dot(sphere_to_ray, sphere_to_ray) - 1 discriminant = b**2 - 4 * a * c if discriminant < 0: return [] t1 = (-b - sqrt(discriminant)) / (2 * a) t2 = (-b + sqrt(discriminant)) / (2 * a) i1 = Intersection(t1, self) i2 = Intersection(t2, self) return [i1, i2]
def local_intersect(self, local_ray): a = local_ray.direction[0]**2 + local_ray.direction[2]**2 # ray is parallel to the y axis? if abs(a) < self.EPSILON: xs = [] self.intersect_caps(local_ray, xs) return xs b = 2 * local_ray.origin[0] * local_ray.direction[0] + \ 2 * local_ray.origin[2] * local_ray.direction[2] c = local_ray.origin[0]**2 + local_ray.origin[2]**2 - 1 discriminant = b**2 - 4 * a * c # ray does not intersect the cylinder if discriminant < 0: return [] t0 = (-b - sqrt(discriminant)) / (2 * a) t1 = (-b + sqrt(discriminant)) / (2 * a) if t0 > t1: t0, t1 = t1, t0 xs = [] y0 = local_ray.origin[1] + t0 * local_ray.direction[1] if self.minimum < y0 < self.maximum: xs.append(Intersection(t0, self)) y1 = local_ray.origin[1] + t1 * local_ray.direction[1] if self.minimum < y1 < self.maximum: xs.append(Intersection(t1, self)) self.intersect_caps(local_ray, xs) return xs
def local_intersect(self, local_ray): dir_cross_e2 = cross(local_ray.direction, self.e2) determinant = dot(self.e1, dir_cross_e2) if abs(determinant) < self.EPSILON: return [] f = 1.0 / determinant p1_to_origin = subtract(local_ray.origin, self.p1) u = f * dot(p1_to_origin, dir_cross_e2) if u < 0.0 or u > 1.0: return [] origin_cross_e1 = cross(p1_to_origin, self.e1) v = f * dot(local_ray.direction, origin_cross_e1) if v < 0.0 or (u + v) > 1.0: return [] t = f * dot(self.e2, origin_cross_e1) if self.smoothed: return [Intersection(t, self, u, v)] return [Intersection(t, self)]
def create_intersections_from_string(context, intersections): result = [] for part in intersections.split(','): part = part.strip() if ':' in part: (pos, obj) = part.split(':') if pos == 'sqrt(2)/2': pos_num = sqrt(2) / 2 elif pos == '-sqrt(2)/2': pos_num = -sqrt(2) / 2 elif pos == 'sqrt(2)': pos_num = sqrt(2) else: pos_num = float(pos) var = getattr(context, obj, None) result.append(Intersection(pos_num, var)) else: var = getattr(context, part, None) result.append(var) return result
def step_create_intersection_i_with_uv(context, t, obj_name, u, v): obj = getattr(context, obj_name, None) context.i = Intersection(t, obj, u, v)
def step_impl(context, t): context.i = Intersection(t, context.s)
def step_determine_intersection_i1_at_sqrt_2_for_p(context): context.i1 = Intersection(sqrt(2), context.p)
def local_intersect(self, local_ray): if abs(local_ray.direction[1]) < self.EPSILON: return [] t = -local_ray.origin[1] / local_ray.direction[1] return [Intersection(t, self)]