|
| 1 | +"""This demo script renders a test scene using cylindrical coordinates.""" |
| 2 | +import math |
| 3 | + |
| 4 | +from nerte.values.coordinates import Coordinates3D |
| 5 | +from nerte.values.domain import Domain1D |
| 6 | +from nerte.values.linalg import AbstractVector |
| 7 | +from nerte.values.face import Face |
| 8 | +from nerte.values.manifolds.cylindrical import ( |
| 9 | + Plane as CarthesianPlaneInCylindric, |
| 10 | +) |
| 11 | +from nerte.world.object import Object |
| 12 | +from nerte.world.camera import Camera |
| 13 | +from nerte.world.scene import Scene |
| 14 | +from nerte.geometry.geometry import Geometry |
| 15 | +from nerte.geometry.cylindircal_swirl_geometry import ( |
| 16 | + SwirlCylindricRungeKuttaGeometry, |
| 17 | +) |
| 18 | +from nerte.render.image_renderer import ImageRenderer |
| 19 | +from nerte.util.random_color_generator import RandomColorGenerator |
| 20 | + |
| 21 | +# pseudo-random color generator |
| 22 | +COLOR = RandomColorGenerator() |
| 23 | + |
| 24 | + |
| 25 | +def make_camera(canvas_dimension: int) -> Camera: |
| 26 | + """Creates a camera with preset values.""" |
| 27 | + |
| 28 | + location = Coordinates3D((0.1, 0.0, -1.3)) |
| 29 | + manifold = CarthesianPlaneInCylindric( |
| 30 | + b0=AbstractVector((1.0, 0.0, 0.0)), |
| 31 | + b1=AbstractVector((0.0, 1.0, 0.0)), |
| 32 | + x0_domain=Domain1D(-1.0, +1.0), |
| 33 | + x1_domain=Domain1D(-1.0, +1.0), |
| 34 | + offset=AbstractVector((0.0, 0.0, -1.0)), |
| 35 | + ) |
| 36 | + camera = Camera( |
| 37 | + location=location, |
| 38 | + detector_manifold=manifold, |
| 39 | + canvas_dimensions=(canvas_dimension, canvas_dimension), |
| 40 | + ) |
| 41 | + return camera |
| 42 | + |
| 43 | + |
| 44 | +def make_scene(canvas_dimension: int) -> Scene: |
| 45 | + """ |
| 46 | + Creates a scene with a camera pointing towards an object. |
| 47 | + """ |
| 48 | + |
| 49 | + camera = make_camera(canvas_dimension) |
| 50 | + scene = Scene(camera=camera) |
| 51 | + |
| 52 | + # add all faces of the hollow cube as separate object to enable |
| 53 | + # individual colors for each triange |
| 54 | + |
| 55 | + # cylinder |
| 56 | + # top 1 |
| 57 | + point0 = Coordinates3D((0.0, -math.pi, +1.0)) |
| 58 | + point1 = Coordinates3D((1.0, -math.pi, +1.0)) |
| 59 | + point2 = Coordinates3D((1.0, math.pi, +1.0)) |
| 60 | + tri = Face(point0, point1, point2) |
| 61 | + obj = Object(color=next(COLOR)) # pseudo-random color |
| 62 | + obj.add_face(tri) |
| 63 | + scene.add_object(obj) |
| 64 | + # top 2 |
| 65 | + point0 = Coordinates3D((0.0, -math.pi, +1.0)) |
| 66 | + point1 = Coordinates3D((0.0, +math.pi, +1.0)) |
| 67 | + point2 = Coordinates3D((1.0, +math.pi, +1.0)) |
| 68 | + tri = Face(point0, point1, point2) |
| 69 | + obj = Object(color=next(COLOR)) # pseudo-random color |
| 70 | + obj.add_face(tri) |
| 71 | + scene.add_object(obj) |
| 72 | + # side 1 |
| 73 | + point0 = Coordinates3D((1.0, -math.pi, -1.0)) |
| 74 | + point1 = Coordinates3D((1.0, -math.pi, +1.0)) |
| 75 | + point2 = Coordinates3D((1.0, +math.pi, +1.0)) |
| 76 | + tri = Face(point0, point1, point2) |
| 77 | + obj = Object(color=next(COLOR)) # pseudo-random color |
| 78 | + obj.add_face(tri) |
| 79 | + scene.add_object(obj) |
| 80 | + # side 2 |
| 81 | + point0 = Coordinates3D((1.0, -math.pi, -1.0)) |
| 82 | + point1 = Coordinates3D((1.0, +math.pi, -1.0)) |
| 83 | + point2 = Coordinates3D((1.0, +math.pi, +1.0)) |
| 84 | + tri = Face(point0, point1, point2) |
| 85 | + obj = Object(color=next(COLOR)) # pseudo-random color |
| 86 | + obj.add_face(tri) |
| 87 | + scene.add_object(obj) |
| 88 | + # bottom 1 |
| 89 | + point0 = Coordinates3D((0.0, -math.pi, -1.0)) |
| 90 | + point1 = Coordinates3D((1.0, -math.pi, -1.0)) |
| 91 | + point2 = Coordinates3D((1.0, +math.pi, -1.0)) |
| 92 | + tri = Face(point0, point1, point2) |
| 93 | + obj = Object(color=next(COLOR)) # pseudo-random color |
| 94 | + obj.add_face(tri) |
| 95 | + scene.add_object(obj) |
| 96 | + # bottom 2 |
| 97 | + point0 = Coordinates3D((0.0, -math.pi, -1.0)) |
| 98 | + point1 = Coordinates3D((0.0, +math.pi, -1.0)) |
| 99 | + point2 = Coordinates3D((1.0, +math.pi, -1.0)) |
| 100 | + tri = Face(point0, point1, point2) |
| 101 | + obj = Object(color=next(COLOR)) # pseudo-random color |
| 102 | + obj.add_face(tri) |
| 103 | + scene.add_object(obj) |
| 104 | + |
| 105 | + return scene |
| 106 | + |
| 107 | + |
| 108 | +def render( |
| 109 | + scene: Scene, |
| 110 | + geometry: Geometry, |
| 111 | + output_path: str, |
| 112 | + file_prefix: str, |
| 113 | + show: bool, |
| 114 | +) -> None: |
| 115 | + """ |
| 116 | + Renders a preset scene with non-euclidean geometry in orthographic and |
| 117 | + perspective projection. |
| 118 | + """ |
| 119 | + |
| 120 | + for mode in ImageRenderer.Mode: |
| 121 | + # for mode in (ImageRenderer.Mode.PERSPECTIVE,): |
| 122 | + print(f"rendering {mode.name} projection ...") |
| 123 | + image_renderer = ImageRenderer(mode=mode) |
| 124 | + image_renderer.render(scene=scene, geometry=geometry) |
| 125 | + image = image_renderer.last_image() |
| 126 | + if image is not None: |
| 127 | + image.save(f"{output_path}/{file_prefix}_{mode.name}.png") |
| 128 | + if show: |
| 129 | + image.show() |
| 130 | + |
| 131 | + |
| 132 | +def main() -> None: |
| 133 | + """Creates and renders the demo scene.""" |
| 134 | + |
| 135 | + # NOTE: Increase the canvas dimension to improve the image quality. |
| 136 | + # This will also increase rendering time! |
| 137 | + scene = make_scene(canvas_dimension=100) |
| 138 | + geo = SwirlCylindricRungeKuttaGeometry( |
| 139 | + max_ray_length=math.inf, |
| 140 | + step_size=0.1, |
| 141 | + max_steps=50, |
| 142 | + # swirl_strength=5.0, |
| 143 | + swirl_strength=5.0, |
| 144 | + ) |
| 145 | + |
| 146 | + # NOTE: Set show to False if images cannot be displayed. |
| 147 | + render( |
| 148 | + scene=scene, |
| 149 | + geometry=geo, |
| 150 | + output_path="../images", |
| 151 | + file_prefix="demo_2", |
| 152 | + show=False, |
| 153 | + ) |
| 154 | + |
| 155 | + |
| 156 | +if __name__ == "__main__": |
| 157 | + main() |
0 commit comments