def setUp(self): self.clean_state() self.vector = (2.0, 3.0, 4.0) vector_length = vector_norm(self.vector) self.unit_vector = tuple(component / vector_length for component in self.vector) self.position = Position.from_xyz((10.0, 20.0, 30.0)) self.direction = Direction.from_xyz(self.vector) self.unit_direction = Direction.from_xyz(self.unit_vector) self.ray = Ray(self.position, self.direction) self.unit_ray = Ray(self.position, self.unit_direction) self.other_position = Position.from_xyz((-9.0, -88.0, 12.34)) self.other_direction = Direction.from_xyz((19.0, 8.0, -0.03)) self.other_ray = Ray(self.other_position, self.other_direction) offset = self.position.dot(self.unit_direction) plane_projector = (-offset,) + self.unit_vector self.plane = Plane(plane_projector) self.orthogonal_directions = [ Direction.from_xyz((0.2, 0.2, -0.25)), Direction.from_xyz((-7.0, 2.0, 2.0)) ] self.orthogonal_rays = [Ray(self.position, direction) for direction in self.orthogonal_directions]
def when_plane_constructed_from_ray(self): self.plane = Plane.orthogonal_to_ray(self.ray)
def __init__(self, **kwargs): super(Aperture, self).__init__(**kwargs) self.plane = Plane.orthogonal_to_ray(self.placement)
class TestPlane(unittest.TestCase): def clean_state(self): self.vector = None self.unit_vector = None self.position = None self.direction = None self.unit_direction = None self.ray = None self.plane = None self.orthogonal_directions = None self.orthogonal_rays = None self.other_position = None self.other_direction = None self.other_ray = None self.intersect_point = None def setUp(self): self.clean_state() self.vector = (2.0, 3.0, 4.0) vector_length = vector_norm(self.vector) self.unit_vector = tuple(component / vector_length for component in self.vector) self.position = Position.from_xyz((10.0, 20.0, 30.0)) self.direction = Direction.from_xyz(self.vector) self.unit_direction = Direction.from_xyz(self.unit_vector) self.ray = Ray(self.position, self.direction) self.unit_ray = Ray(self.position, self.unit_direction) self.other_position = Position.from_xyz((-9.0, -88.0, 12.34)) self.other_direction = Direction.from_xyz((19.0, 8.0, -0.03)) self.other_ray = Ray(self.other_position, self.other_direction) offset = self.position.dot(self.unit_direction) plane_projector = (-offset,) + self.unit_vector self.plane = Plane(plane_projector) self.orthogonal_directions = [ Direction.from_xyz((0.2, 0.2, -0.25)), Direction.from_xyz((-7.0, 2.0, 2.0)) ] self.orthogonal_rays = [Ray(self.position, direction) for direction in self.orthogonal_directions] def tearDown(self): self.clean_state() def test_plane_contains_position(self): point = self.position distance_from_plane = self.plane.distance_to_point(point) self.assertAlmostEqual(0.0, distance_from_plane) def test_plane_contains_orthogonal_rays(self): self.then_plane_contains_orthogonal_rays() def then_plane_contains_orthogonal_rays(self): for orthogonal_ray in self.orthogonal_rays: for scale in [-2.0, -1.0, 0.0, 0.4, 1.0]: point = orthogonal_ray.scaled_endpoint(scale) distance_from_plane = self.plane.distance_to_point(point) self.assertAlmostEqual(0.0, distance_from_plane) # this really is testing Ray.distance_to_point length_of_orthogonal_ray = orthogonal_ray.direction.length() expected_distance_from_ray = fabs(scale) * length_of_orthogonal_ray actual_distance_from_ray = self.ray.distance_to_point(point) self.assertAlmostEqual(expected_distance_from_ray, actual_distance_from_ray) def test_plane_distance(self): self.then_plane_has_correct_distances() def then_plane_has_correct_distances(self): ray_length = vector_norm(self.unit_direction) self.assertEqual(1.0, ray_length) ray = self.unit_ray for scale in [-2.0, -1.0, 0.0, 0.4, 1.0]: point = ray.scaled_endpoint(scale) distance_from_plane = self.plane.distance_to_point(point) self.assertAlmostEqual(scale, distance_from_plane) def test_plane_construction_from_ray(self): self.when_plane_constructed_from_ray() self.then_plane_contains_orthogonal_rays() self.then_plane_has_correct_distances() def when_plane_constructed_from_ray(self): self.plane = Plane.orthogonal_to_ray(self.ray) def test_plane_intersects_ray(self): self.when_plane_constructed_from_ray() self.when_plane_intersects_other_ray() self.then_intersection_point_lies_on_plane() self.then_point_lies_on_othe_ray() def when_plane_intersects_other_ray(self): self.intersect_point = self.plane.intersect(self.other_ray) def then_intersection_point_lies_on_plane(self): self.assertAlmostEqual(0.0, self.plane.distance_to_point(self.intersect_point), 5) def then_point_lies_on_othe_ray(self): self.assertLess(0.0, self.other_ray.distance_to_point(self.intersect_point), 5)
def generate_start_and_stop_objects_as_plane(self): self.target_context = "Plane" self.generate_start_and_stop_position_direction_and_ray() self.start_object = Plane.orthogonal_to_ray(self.start_ray) self.stop_object = Plane.orthogonal_to_ray(self.stop_ray)