def node_tree(): b_pos = (1.0, 0.0, 0.0) b_axis = (0.0, 1.0, 0.0) b_rads = 3.14159265 / 2 c_pos = (1.0, 0.0, 0.0) a = Node(name="a", parent=None) b = Node(name="b", parent=a) b.location = b_pos b.rotate(b_rads, b_axis) c = b.add_child_node(name="c") c.location = c_pos return a, b, c
def test_intersection_with_rotation_around_z(self): root = Node(name='Root') a = Node(name="A", parent=root) b = Node(name="B", parent=a) b.geometry = Sphere(radius=1.0) a.translate((1.0, 0.0, 0.0)) # This rotation make an z translation in b becomes a -y translation in a a.rotate(np.pi / 2, axis=(0.0, 0.0, 1.0)) loc = (-2.0, 0.0, 0.0) vec = (1.0, 0.0, 0.0) scene = Scene(root=root) intersections = scene.intersections(loc, vec) points = tuple([x.point for x in intersections]) assert np.allclose(points, ((0.0, 0.0, 0.0), (2.0, 0.0, 0.0)))
def test_intersection_with_rotation_around_x(self): root = Node(name='Root') a = Node(name="A", parent=root) b = Node(name="B", parent=a) b.geometry = Sphere(radius=1.0) b.translate((1.0, 0.0, 0.0)) # Rotation around x therefore no displace in x b.rotate(np.pi / 2, (1.0, 0.0, 0.0)) loc = (-2.0, 0.0, 0.0) vec = (1.0, 0.0, 0.0) scene = Scene(root=root) intersections = scene.intersections(loc, vec) points = tuple([x.point for x in intersections]) assert points == ((0.0, 0.0, 0.0), (2.0, 0.0, 0.0))
def test_intersection_with_rotation_around_z(self): root = Node(name='Root') a = Node(name="A", parent=root) b = Node(name="B", parent=a) b.geometry = Sphere(radius=1.0) a.translate((1.0, 0.0, 0.0)) # This rotation make an z translation in b becomes a -y translation in a a.rotate(np.pi/2, axis=(0.0, 0.0, 1.0)) loc = (-2.0, 0.0, 0.0) vec = (1.0, 0.0, 0.0) scene = Scene(root=root) intersections = scene.intersections(loc, vec) points = tuple([x.point for x in intersections]) assert np.allclose(points, ((0.0, 0.0, 0.0), (2.0, 0.0, 0.0)))
def test_intersection_with_rotation_around_x(self): root = Node(name='Root') a = Node(name="A", parent=root) b = Node(name="B", parent=a) b.geometry = Sphere(radius=1.0) b.translate((1.0, 0.0, 0.0)) # Rotation around x therefore no displace in x b.rotate(np.pi/2, (1.0, 0.0, 0.0)) loc = (-2.0, 0.0, 0.0) vec = (1.0, 0.0, 0.0) scene = Scene(root=root) intersections = scene.intersections(loc, vec) points = tuple([x.point for x in intersections]) assert points == ((0.0, 0.0, 0.0), (2.0, 0.0, 0.0))
def test_coodinate_system_conversions(self): a = Node(name='a') b = Node(name='b', parent=a) c = Node(name='c', parent=b) d = Node(name='d', parent=a) b.translate((1,1,1)) c.translate((0,1,1)) d.translate((-1, -1, -1)) theta = 0.5 * np.pi b.rotate(theta, (0, 0, 1)) c.rotate(theta, (1, 0, 0)) d.rotate(theta, (0, 1, 0)) # Points in node d to a require just travelling up the nodes. assert np.allclose(d.point_to_node((0, 0, 0), a), (-1, -1, -1)) assert np.allclose(d.point_to_node((1, 1, 1), a), (0, 0, -2)) # Directions in node d to a require just travelling up the nodes. assert np.allclose(d.vector_to_node((1, 0, 0), a), (0, 0, -1)) assert np.allclose(d.vector_to_node((0, 1, 0), a), (0, 1, 0)) assert np.allclose(d.vector_to_node((0, 0, 1), a), (1, 0, 0)) # Points in node d to c requires going up and down nodes assert np.allclose(c.point_to_node((0, 0, 0), d), (-3, 2, 1)) assert np.allclose(c.point_to_node((1, 1, 1), d), (-4, 3, 2)) # Directions in node d to c require going up and down nodes assert np.allclose(c.vector_to_node((1, 0, 0), d), (0, 1, 0)) assert np.allclose(c.vector_to_node((0, 1, 0), d), (-1, 0, 0)) assert np.allclose(c.vector_to_node((0, 0, 1), d), (0, 0, 1))
def test_coodinate_system_conversions(self): a = Node(name='a') b = Node(name='b', parent=a) c = Node(name='c', parent=b) d = Node(name='d', parent=a) b.translate((1, 1, 1)) c.translate((0, 1, 1)) d.translate((-1, -1, -1)) theta = 0.5 * np.pi b.rotate(theta, (0, 0, 1)) c.rotate(theta, (1, 0, 0)) d.rotate(theta, (0, 1, 0)) # Points in node d to a require just travelling up the nodes. assert np.allclose(d.point_to_node((0, 0, 0), a), (-1, -1, -1)) assert np.allclose(d.point_to_node((1, 1, 1), a), (0, 0, -2)) # Directions in node d to a require just travelling up the nodes. assert np.allclose(d.vector_to_node((1, 0, 0), a), (0, 0, -1)) assert np.allclose(d.vector_to_node((0, 1, 0), a), (0, 1, 0)) assert np.allclose(d.vector_to_node((0, 0, 1), a), (1, 0, 0)) # Points in node d to c requires going up and down nodes assert np.allclose(c.point_to_node((0, 0, 0), d), (-3, 2, 1)) assert np.allclose(c.point_to_node((1, 1, 1), d), (-4, 3, 2)) # Directions in node d to c require going up and down nodes assert np.allclose(c.vector_to_node((1, 0, 0), d), (0, 1, 0)) assert np.allclose(c.vector_to_node((0, 1, 0), d), (-1, 0, 0)) assert np.allclose(c.vector_to_node((0, 0, 1), d), (0, 0, 1))
material=material ), parent=world ) # Make a light source. This is a laser emitting light along the whole length of the # cylinder. We need to translate and rotate the light source to get it to fire along # the axis. We use the position delegate to generate photons along the same length # as the cylinder. light = Node( name="light (555nm laser)", light=Light(position_delegate=lambda : (np.random.uniform(-2.5, 2.5), 0.0, 0.0)), parent=world ) cylinder.rotate(np.radians(90), (0.0, 1.0, 0.0)) light.translate((0.0, 0.0, -1.0)) # Setup renderer and the scene for tracing rend = MeshcatRenderer() scene = Scene(world) tracer = PhotonTracer(scene) rend.render(scene) # Trace 100 photons and visualise for ray in light.emit(100): path = tracer.follow(ray) print(path) rend.add_ray_path(path) # You can kill the simulation any time by pressing ctrl-c.
def _make_scene(self): """ Creates the scene based on configuration values. """ # Make world node (l, w, d) = self.size world = Node( name="World", geometry=Box((l * 100, w * 100, d * 100), material=Material(refractive_index=self.n0)), ) # Create components (Absorbers, Luminophores and Scatteres) if len(self._user_components) == 0: self._user_components = self._make_default_components() components = [] for component_data in self._user_components: cls = component_data.pop("cls") coefficient = component_data.pop("coefficient") component = cls(coefficient, **component_data) components.append(component) # Create LSC node lsc = Node( name="LSC", geometry=Box( (l, w, d), material=Material( refractive_index=self.n1, components=components, surface=Surface(delegate=OptionalMirrorAndSolarCell(self)), ), ), parent=world, ) if self._air_gap_mirror_info["want_air_gap_mirror"]: sheet_thickness = 0.25 * d # make it appear thinner than the LSC air_gap_mirror = Node( name="Air Gap Mirror", geometry=Box( (l, w, sheet_thickness), # same surface air but very thin material=Material( refractive_index=self.n0, components=[], surface=Surface(delegate=AirGapMirror(self)), ), ), parent=world, ) # Move adjacent to bottom surface with a small air gap air_gap_mirror.translate((0.0, 0.0, -(0.5 * d + sheet_thickness))) # Use user light if any have been given, otherwise use default values. if len(self._user_lights) == 0: self._user_lights = self._make_default_lights() # Create light nodes for light_data in self._user_lights: name = light_data["name"] light = Light( name=name, direction=light_data["direction"], wavelength=light_data["wavelength"], position=light_data["position"], ) light_node = Node(name=name, light=light, parent=world) light_node.location = light_data["location"] if light_data["rotation"]: light_node.rotate(*light_data["rotation"]) self._scene = Scene(world)
) ) # Light source light = Light( divergence_delegate=functools.partial( Light.cone_divergence, np.radians(20) ) ) light_node = Node( name='light', parent=world_node, location=(0.0, 0.0, 1.0) ) light_node.rotate(np.radians(180), (1, 0, 0)) light_node.light = light scene = Scene(root=world_node) renderer = MeshcatRenderer(max_histories=None, open_browser=True) renderer.render(scene) if __name__ == "__main__": np.random.seed(1) for light_node in scene.light_nodes: for ray in light.emit(20): ray = ray.representation(light_node, world_node) steps = photon_tracer.follow(ray, scene, renderer=renderer) path, decisions = zip(*steps) print(decisions) renderer.add_ray_path(path)