Monte-Carlo sampling to find which component captures the ray.
(self, wavelength: float)
| 47 | return depth |
| 48 | |
| 49 | def component(self, wavelength: float) -> Component: |
| 50 | """ Monte-Carlo sampling to find which component captures the ray. |
| 51 | """ |
| 52 | coefs = np.array([x.coefficient(wavelength) for x in self.components]) |
| 53 | if np.any(coefs < 0.0): |
| 54 | raise ValueError("Must be positive.") |
| 55 | count = len(self.components) |
| 56 | bins = list(range(0, count + 1)) |
| 57 | cdf = np.cumsum(coefs) |
| 58 | pdf = cdf / max(cdf) |
| 59 | pdf = np.hstack([0, pdf[:]]) |
| 60 | pdfinv_lookup = np.interp(np.random.uniform(), pdf, bins) |
| 61 | index = int(np.floor(pdfinv_lookup)) |
| 62 | component = self.components[index] |
| 63 | return component |