|
| 1 | +import matplotlib.pyplot as plt |
| 2 | +import numpy as np |
| 3 | + |
| 4 | +import mitsuba |
| 5 | + |
| 6 | +mitsuba.set_variant("scalar_rgb") |
| 7 | + |
| 8 | +from mitsuba.core import ScalarTransform4f |
| 9 | +from mitsuba.core.xml import load_dict |
| 10 | + |
| 11 | +direction_r = [1, 0, -1] |
| 12 | +direction_g = [1, 1, -1] |
| 13 | +direction_b = [0, 1, -1] |
| 14 | +film_resolution = 32 |
| 15 | + |
| 16 | + |
| 17 | +def scene_dict(sensor_to_world=None): |
| 18 | + if sensor_to_world is None: |
| 19 | + sensor_to_world = ScalarTransform4f.look_at( |
| 20 | + origin=[0, 0, 0], |
| 21 | + target=[0, 0, 1], |
| 22 | + up=[0, 1, 0], |
| 23 | + ) |
| 24 | + |
| 25 | + return { |
| 26 | + "type": "scene", |
| 27 | + "shape": { |
| 28 | + "type": "rectangle", |
| 29 | + "bsdf": { |
| 30 | + "type": "roughconductor" |
| 31 | + }, |
| 32 | + }, |
| 33 | + "illumination_r": { |
| 34 | + "type": "directional", |
| 35 | + "direction": direction_r, |
| 36 | + "irradiance": { |
| 37 | + "type": "rgb", |
| 38 | + "value": [1, 0, 0], |
| 39 | + }, |
| 40 | + }, |
| 41 | + "illumination_g": { |
| 42 | + "type": "directional", |
| 43 | + "direction": direction_g, |
| 44 | + "irradiance": { |
| 45 | + "type": "rgb", |
| 46 | + "value": [0, 1, 0], |
| 47 | + }, |
| 48 | + }, |
| 49 | + "illumination_b": { |
| 50 | + "type": "directional", |
| 51 | + "direction": direction_b, |
| 52 | + "irradiance": { |
| 53 | + "type": "rgb", |
| 54 | + "value": [0, 0, 1], |
| 55 | + }, |
| 56 | + }, |
| 57 | + "hdistant": { |
| 58 | + "type": "hdistant", |
| 59 | + "to_world": sensor_to_world, |
| 60 | + "sampler": { |
| 61 | + "type": "independent", |
| 62 | + "sample_count": 3200, |
| 63 | + }, |
| 64 | + "film": { |
| 65 | + "type": "hdrfilm", |
| 66 | + "width": film_resolution, |
| 67 | + "height": film_resolution, |
| 68 | + "pixel_format": "rgb", |
| 69 | + "component_format": "float32", |
| 70 | + "rfilter": { |
| 71 | + "type": "box" |
| 72 | + }, |
| 73 | + } |
| 74 | + }, |
| 75 | + #"camera": { |
| 76 | + # "type": "perspective", |
| 77 | + # "to_world": ScalarTransform4f.look_at( |
| 78 | + # origin=[5, 5, 5], |
| 79 | + # target=[0, 0, 0], |
| 80 | + # up=[0, 0, 1], |
| 81 | + # ), |
| 82 | + # "sampler": { |
| 83 | + # "type": "independent", |
| 84 | + # "sample_count": 32, |
| 85 | + # }, |
| 86 | + # "film": { |
| 87 | + # "type": "hdrfilm", |
| 88 | + # "width": 320, |
| 89 | + # "height": 240, |
| 90 | + # "pixel_format": "luminance", |
| 91 | + # "component_format": "float32", |
| 92 | + # } |
| 93 | + #}, |
| 94 | + "integrator": { |
| 95 | + "type": "path" |
| 96 | + }, |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | +for name, sensor_to_world in { |
| 101 | + "default": |
| 102 | + ScalarTransform4f.look_at( |
| 103 | + origin=[0, 0, 0], |
| 104 | + target=[0, 0, 1], |
| 105 | + up=[0, 1, 0], |
| 106 | + ), |
| 107 | + "rotated": |
| 108 | + ScalarTransform4f.look_at( |
| 109 | + origin=[0, 0, 0], |
| 110 | + target=[0, 0, 1], |
| 111 | + up=[1, 1, 0], |
| 112 | + ), |
| 113 | +}.items(): |
| 114 | + scene = load_dict(scene_dict(sensor_to_world=sensor_to_world)) |
| 115 | + sensor = scene.sensors()[0] |
| 116 | + scene.integrator().render(scene, sensor) |
| 117 | + |
| 118 | + # Plot recorded leaving radiance |
| 119 | + img = np.array(sensor.film().bitmap()).squeeze() |
| 120 | + img -= np.min(img) |
| 121 | + img = img / np.max(img) |
| 122 | + plt.imshow(img, origin="lower") |
| 123 | + |
| 124 | + # Add illumination setup |
| 125 | + from mitsuba.core.warp import uniform_hemisphere_to_square |
| 126 | + |
| 127 | + # -- We must convert emitter directions to the surface scattering frame |
| 128 | + def direction_to_pixel_coords(direction): |
| 129 | + d = -np.array(sensor_to_world.inverse().transform_vector(direction)) |
| 130 | + d = d / np.linalg.norm(d) |
| 131 | + return uniform_hemisphere_to_square(d) * float(film_resolution) |
| 132 | + |
| 133 | + plt.scatter(*direction_to_pixel_coords(direction_r), color="r") |
| 134 | + plt.scatter(*direction_to_pixel_coords(direction_g), color="g") |
| 135 | + plt.scatter(*direction_to_pixel_coords(direction_b), color="b") |
| 136 | + |
| 137 | + # -- Add up and target directions to film view |
| 138 | + center = np.array([ |
| 139 | + 0.5 * float(film_resolution), |
| 140 | + 0.5 * float(film_resolution), |
| 141 | + ]) |
| 142 | + up = 0.75 * np.array([0.0, 0.5 * float(film_resolution)]) |
| 143 | + orange = (1, 0.4, 0) |
| 144 | + plt.arrow( |
| 145 | + *center, |
| 146 | + *up, |
| 147 | + width=0.3, |
| 148 | + head_width=1, |
| 149 | + color=orange, |
| 150 | + ) |
| 151 | + plt.scatter(*center, color=orange) |
| 152 | + plt.scatter(*center, color="none", s=250, edgecolors=orange) |
| 153 | + |
| 154 | + # Add axis labels |
| 155 | + plt.xlabel("pixel index") |
| 156 | + plt.ylabel("pixel index") |
| 157 | + |
| 158 | + plt.savefig(f"sensor_hdistant_{name}.svg") |
| 159 | + plt.close() |
0 commit comments