|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | + |
| 4 | +import enoki as ek |
| 5 | +import mitsuba |
| 6 | + |
| 7 | + |
| 8 | +def example_shape(radius, center): |
| 9 | + from mitsuba.core.xml import load_string |
| 10 | + |
| 11 | + xml = f""" |
| 12 | + <shape version='0.1.0' type="sphere"> |
| 13 | + <float name="radius" value="{radius}"/> |
| 14 | + <transform name="to_world"> |
| 15 | + <translate x="{center.x}" y="{center.y}" z="{center.z}"/> |
| 16 | + </transform> |
| 17 | + <sensor type="irradiancemeter"> |
| 18 | + <film type="hdrfilm"> |
| 19 | + <integer name="width" value="1"/> |
| 20 | + <integer name="height" value="1"/> |
| 21 | + </film> |
| 22 | + </sensor> |
| 23 | + </shape> |
| 24 | + """ |
| 25 | + return load_string(xml) |
| 26 | + |
| 27 | +def test_construct(variant_scalar_rgb): |
| 28 | + """ |
| 29 | + We construct an irradiance meter attached to a sphere and assert that the |
| 30 | + following parameters get set correctly: |
| 31 | + - associated shape |
| 32 | + - film |
| 33 | + """ |
| 34 | + from mitsuba.core import Vector3f |
| 35 | + center_v = Vector3f(0.0) |
| 36 | + radius = 1.0 |
| 37 | + sphere = example_shape(radius, center_v) |
| 38 | + sensor = sphere.sensor() |
| 39 | + |
| 40 | + assert sensor.shape() == sphere |
| 41 | + assert ek.allclose(sensor.film().size(), [1, 1]) |
| 42 | + |
| 43 | + |
| 44 | +@pytest.mark.parametrize(("center", "radius"), [([2.0, 5.0, 8.3], 2.0), ([0.0, 0.0, 0.0], 1.0), ([1.0, 4.0, 0.0], 5.0)]) |
| 45 | +def test_sampling(variant_scalar_rgb, center, radius): |
| 46 | + """ |
| 47 | + We construct an irradiance meter attached to a sphere and assert that sampled |
| 48 | + rays originate at the sphere's surface |
| 49 | + """ |
| 50 | + from mitsuba.core import Vector3f |
| 51 | + |
| 52 | + center_v = Vector3f(center) |
| 53 | + sphere = example_shape(radius, center_v) |
| 54 | + sensor = sphere.sensor() |
| 55 | + num_samples = 100 |
| 56 | + |
| 57 | + wav_samples = np.random.rand(num_samples) |
| 58 | + pos_samples = np.random.rand(num_samples, 2) |
| 59 | + dir_samples = np.random.rand(num_samples, 2) |
| 60 | + |
| 61 | + for i in range(100): |
| 62 | + ray = sensor.sample_ray_differential(0.0, wav_samples[i], pos_samples[i], dir_samples[i])[0] |
| 63 | + |
| 64 | + # assert that the ray starts at the sphere surface |
| 65 | + assert ek.allclose(ek.norm(center_v - ray.o), radius) |
| 66 | + # assert that all rays point away from the sphere center |
| 67 | + assert ek.dot(ek.normalize(ray.o - center_v), ray.d) > 0.0 |
| 68 | + |
| 69 | +@pytest.mark.parametrize("radiance", [2.04, 1.0, 0.0]) |
| 70 | +def test_incoming_flux(variant_scalar_rgb, radiance): |
| 71 | + """ |
| 72 | + We test the recorded power density of the irradiance meter, by creating a simple scene: |
| 73 | + The irradiance meter is attached to a sphere with unit radius at the coordinate origin |
| 74 | + surrounded by a constant environment emitter. |
| 75 | + We sample a number of rays and average their contribution to the cumulative power |
| 76 | + density. |
| 77 | + We expect the average value to be \\pi * L with L the radiance of the constant |
| 78 | + emitter. |
| 79 | + """ |
| 80 | + from mitsuba.core import Spectrum |
| 81 | + from mitsuba.core.xml import load_string |
| 82 | + |
| 83 | + sensor_xml = f""" |
| 84 | + <shape version='0.1.0' type="sphere"> |
| 85 | + <float name="radius" value="1"/> |
| 86 | + <transform name="to_world"> |
| 87 | + <translate x="0" y="0" z="0"/> |
| 88 | + </transform> |
| 89 | + <sensor type="irradiancemeter"> |
| 90 | + <film type="hdrfilm"> |
| 91 | + <integer name="width" value="1"/> |
| 92 | + <integer name="height" value="1"/> |
| 93 | + </film> |
| 94 | + </sensor> |
| 95 | + </shape> |
| 96 | + """ |
| 97 | + |
| 98 | + emitter_xml = f""" |
| 99 | + <emitter type="constant"> |
| 100 | + <spectrum name="radiance" type='uniform'> |
| 101 | + <float name="value" value="{radiance}"/> |
| 102 | + </spectrum> |
| 103 | + </emitter> |
| 104 | + """ |
| 105 | + |
| 106 | + scene_xml = f""" |
| 107 | + <scene version="0.1.0"> |
| 108 | + {sensor_xml} |
| 109 | + {emitter_xml} |
| 110 | + </scene> |
| 111 | + """ |
| 112 | + scene = load_string(scene_xml) |
| 113 | + sensor = scene.sensors()[0] |
| 114 | + |
| 115 | + power_density_cum = 0.0 |
| 116 | + num_samples = 100 |
| 117 | + |
| 118 | + wav_samples = np.random.rand(num_samples) |
| 119 | + pos_samples = np.random.rand(num_samples, 2) |
| 120 | + dir_samples = np.random.rand(num_samples, 2) |
| 121 | + |
| 122 | + for i in range(100): |
| 123 | + ray, weight = sensor.sample_ray_differential(0.0, wav_samples[i], pos_samples[i], dir_samples[i]) |
| 124 | + |
| 125 | + intersection = scene.ray_intersect(ray) |
| 126 | + power_density_cum += weight * intersection.emitter(scene).eval(intersection) |
| 127 | + power_density_avg = power_density_cum / float(num_samples) |
| 128 | + |
| 129 | + assert ek.allclose(power_density_avg, Spectrum(ek.pi * radiance)) |
| 130 | + |
| 131 | +@pytest.mark.parametrize("radiance", [2.04, 1.0, 0.0]) |
| 132 | +def test_incoming_flux_integrator(variant_scalar_rgb, radiance): |
| 133 | + """ |
| 134 | + We test the recorded power density of the irradiance meter, by creating a simple scene: |
| 135 | + The irradiance meter is attached to a sphere with unit radius at the coordinate origin |
| 136 | + surrounded by a constant environment emitter. |
| 137 | + We render the scene with the path tracer integrator and compare the recorded power |
| 138 | + density with our theoretical expectation. |
| 139 | + We expect the average value to be \\pi * L with L the radiance of the constant |
| 140 | + emitter. |
| 141 | + """ |
| 142 | + |
| 143 | + from mitsuba.core import Spectrum, Bitmap, Struct |
| 144 | + from mitsuba.core.xml import load_string |
| 145 | + |
| 146 | + sensor_xml = f""" |
| 147 | + <shape version='0.1.0' type="sphere"> |
| 148 | + <float name="radius" value="1"/> |
| 149 | + <transform name="to_world"> |
| 150 | + <translate x="0" y="0" z="0"/> |
| 151 | + </transform> |
| 152 | + <sensor type="irradiancemeter"> |
| 153 | + <film type="hdrfilm"> |
| 154 | + <integer name="width" value="1"/> |
| 155 | + <integer name="height" value="1"/> |
| 156 | + </film> |
| 157 | + </sensor> |
| 158 | + </shape> |
| 159 | + """ |
| 160 | + |
| 161 | + emitter_xml = f""" |
| 162 | + <emitter type="constant"> |
| 163 | + <spectrum name="radiance" type='uniform'> |
| 164 | + <float name="value" value="{radiance}"/> |
| 165 | + </spectrum> |
| 166 | + </emitter> |
| 167 | + """ |
| 168 | + |
| 169 | + integrator_xml = f""" |
| 170 | + <integrator type="path"> |
| 171 | +
|
| 172 | + <integer name="max_depth" value="-1"/> |
| 173 | + </integrator> |
| 174 | + """ |
| 175 | + |
| 176 | + sampler_xml = f""" |
| 177 | + <sampler type="independent"> |
| 178 | + <integer name="sample_count" value="100"/> |
| 179 | + </sampler> |
| 180 | + """ |
| 181 | + scene_xml = f""" |
| 182 | + <scene version="0.1.0"> |
| 183 | + {integrator_xml} |
| 184 | + {sensor_xml} |
| 185 | + {emitter_xml} |
| 186 | + {sampler_xml} |
| 187 | + </scene> |
| 188 | + """ |
| 189 | + scene = load_string(scene_xml) |
| 190 | + sensor = scene.sensors()[0] |
| 191 | + |
| 192 | + scene.integrator().render(scene, sensor) |
| 193 | + film = sensor.film() |
| 194 | + |
| 195 | + img = film.bitmap(raw=True).convert(Bitmap.PixelFormat.Y, Struct.Type.Float32, srgb_gamma=False) |
| 196 | + image_np = np.array(img) |
| 197 | + |
| 198 | + ek.allclose(image_np, (radiance*ek.pi)) |
0 commit comments