Skip to content

Commit 7f8e2ab

Browse files
committed
fix (filter info matrix)
#33
1 parent d3dec91 commit 7f8e2ab

6 files changed

+32
-42
lines changed

src/nerte/render/image_filter_renderer.py

+11-16
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
The data is rendered first and filters may be applied afterwards.
44
"""
55

6-
from typing import Optional, NewType, Iterable
6+
from typing import Optional, Iterable
77

88
from abc import ABC, abstractmethod
99

@@ -18,11 +18,7 @@
1818
from nerte.geometry.geometry import Geometry
1919
from nerte.render.image_renderer import ImageRenderer
2020
from nerte.render.projection import ProjectionMode
21-
22-
# TODO: provide proper container type
23-
IntersectionInfoMatrix = NewType(
24-
"IntersectionInfoMatrix", list[list[IntersectionInfo]]
25-
)
21+
from nerte.util.generic_matrix import GenericMatrix
2622

2723

2824
def color_for_normalized_value(value: float) -> Color:
@@ -78,7 +74,7 @@ class Filter(ABC):
7874
@abstractmethod
7975
def apply(
8076
self,
81-
info_matrix: IntersectionInfoMatrix,
77+
info_matrix: GenericMatrix[IntersectionInfo],
8278
) -> Image:
8379
"""
8480
Returns color for pixel based.
@@ -112,13 +108,12 @@ def color_for_info(
112108
return self.color_hit()
113109
return color_for_miss_reason(info)
114110

115-
def apply(self, info_matrix: IntersectionInfoMatrix) -> Image:
116-
if len(info_matrix) == 0 or len(info_matrix[0]) == 0:
111+
def apply(self, info_matrix: GenericMatrix[IntersectionInfo]) -> Image:
112+
width, height = info_matrix.dimensions()
113+
if width == 0 or height == 0:
117114
raise ValueError(
118115
"Cannot apply hit filter. Intersection info matrix is empty."
119116
)
120-
width = len(info_matrix)
121-
height = len(info_matrix[0])
122117

123118
# initialize image with pink background
124119
image = Image.new(
@@ -129,7 +124,7 @@ def apply(self, info_matrix: IntersectionInfoMatrix) -> Image:
129124
for pixel_x in range(width):
130125
for pixel_y in range(height):
131126
pixel_location = (pixel_x, pixel_y)
132-
info = info_matrix[pixel_x][pixel_y]
127+
info = info_matrix[pixel_x, pixel_y]
133128
pixel_color = self.color_for_info(info)
134129
image.putpixel(pixel_location, pixel_color.rgb)
135130

@@ -158,7 +153,7 @@ def __init__(
158153

159154
self._filter = filtr
160155
self.auto_apply_filter = auto_apply_filter
161-
self._last_info_matrix: Optional[IntersectionInfoMatrix] = None
156+
self._last_info_matrix: Optional[GenericMatrix[IntersectionInfo]] = None
162157

163158
def has_render_data(self) -> bool:
164159
"""Returns True, iff render was called previously."""
@@ -222,14 +217,14 @@ def render_pixel_intersection_info(
222217

223218
def render_intersection_info(
224219
self, scene: Scene, geometry: Geometry
225-
) -> IntersectionInfoMatrix:
220+
) -> GenericMatrix[IntersectionInfo]:
226221
"""
227222
Returns matrix with intersection infos per pixel.
228223
"""
229224

230225
width, height = scene.camera.canvas_dimensions
231226
# initialize background with nan
232-
info_matrix: IntersectionInfoMatrix = IntersectionInfoMatrix(
227+
info_matrix = GenericMatrix[IntersectionInfo](
233228
[
234229
[IntersectionInfos.UNINIALIZED for _ in range(height)]
235230
for _ in range(width)
@@ -245,7 +240,7 @@ def render_intersection_info(
245240
scene.objects(),
246241
pixel_location,
247242
)
248-
info_matrix[pixel_x][pixel_y] = pixel_info
243+
info_matrix[pixel_x, pixel_y] = pixel_info
249244
return info_matrix
250245

251246
def render(self, scene: Scene, geometry: Geometry) -> None:

src/nerte/render/image_filter_renderer_unittest.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@
2626
from nerte.geometry.cylindircal_geometry import CylindricRungeKuttaGeometry
2727
from nerte.render.projection import ProjectionMode
2828
from nerte.render.image_filter_renderer import (
29-
IntersectionInfoMatrix,
3029
Filter,
3130
HitFilter,
3231
ImageFilterRenderer,
3332
color_for_miss_reason,
3433
color_for_normalized_value,
3534
)
35+
from nerte.util.generic_matrix import GenericMatrix
3636

3737

3838
class ColorForMissReasonTest(BaseTestCase):
@@ -102,7 +102,7 @@ class DummyFilter(Filter):
102102
# pylint: disable=R0903
103103
def apply(
104104
self,
105-
info_matrix: IntersectionInfoMatrix,
105+
info_matrix: GenericMatrix[IntersectionInfo],
106106
) -> Image:
107107
return Image.new(
108108
mode="RGB", size=(5, 5), color=Colors.BLACK.rgb
@@ -177,7 +177,7 @@ def setUp(self) -> None:
177177
IntersectionInfos.RAY_INITIALIZED_OUTSIDE_MANIFOLD,
178178
)
179179
self.info_matrices = tuple(
180-
IntersectionInfoMatrix([[info]]) for info in self.infos
180+
GenericMatrix[IntersectionInfo]([[info]]) for info in self.infos
181181
)
182182
self.filter = HitFilter()
183183

src/nerte/render/meta_info_filter.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
from nerte.values.intersection_info import IntersectionInfo
1212
from nerte.values.extended_intersection_info import ExtendedIntersectionInfo
1313
from nerte.render.image_filter_renderer import (
14-
IntersectionInfoMatrix,
1514
Filter,
1615
color_for_normalized_value,
1716
)
17+
from nerte.util.generic_matrix import GenericMatrix
1818

1919

2020
def _clip(value: float) -> float:
@@ -79,14 +79,12 @@ def color_for_info(self, info: IntersectionInfo) -> Color:
7979
return color_for_normalized_value(value)
8080
return self.color_no_meta_data
8181

82-
def apply(self, info_matrix: IntersectionInfoMatrix) -> Image:
83-
if len(info_matrix) == 0 or len(info_matrix[0]) == 0:
82+
def apply(self, info_matrix: GenericMatrix[IntersectionInfo]) -> Image:
83+
width, height = info_matrix.dimensions()
84+
if width == 0 or height == 0:
8485
raise ValueError(
85-
"Cannot apply meta data filter. Intersection info matrix is"
86-
" empty."
86+
"Cannot apply hit filter. Intersection info matrix is empty."
8787
)
88-
width = len(info_matrix)
89-
height = len(info_matrix[0])
9088

9189
# initialize image with pink background
9290
image = Image.new(
@@ -97,7 +95,7 @@ def apply(self, info_matrix: IntersectionInfoMatrix) -> Image:
9795
for pixel_x in range(width):
9896
for pixel_y in range(height):
9997
pixel_location = (pixel_x, pixel_y)
100-
info = info_matrix[pixel_x][pixel_y]
98+
info = info_matrix[pixel_x, pixel_y]
10199
pixel_color = self.color_for_info(info)
102100
image.putpixel(pixel_location, pixel_color.rgb)
103101

src/nerte/render/meta_info_filter_unittest.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
from nerte.values.color import Color
1414
from nerte.values.intersection_info import IntersectionInfo
1515
from nerte.values.extended_intersection_info import ExtendedIntersectionInfo
16-
from nerte.render.image_filter_renderer import IntersectionInfoMatrix
1716
from nerte.render.meta_info_filter import MetaInfoFilter
17+
from nerte.util.generic_matrix import GenericMatrix
1818

1919

2020
class MetaInfoFilterConstructorTest(BaseTestCase):
@@ -124,7 +124,7 @@ def setUp(self) -> None:
124124
ExtendedIntersectionInfo(ray_depth=0.3, meta_data={"key": 0.5}),
125125
ExtendedIntersectionInfo(ray_depth=0.3, meta_data={"key": 1.0}),
126126
)
127-
self.info_matrix = IntersectionInfoMatrix([list(self.infos)])
127+
self.info_matrix = GenericMatrix[IntersectionInfo]([list(self.infos)])
128128
self.filter = MetaInfoFilter(
129129
meta_data_key="key", min_value=0.0, max_value=1.0
130130
)

src/nerte/render/ray_depth_filter.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
from nerte.values.color import Color, Colors
1313
from nerte.values.intersection_info import IntersectionInfo
1414
from nerte.render.image_filter_renderer import (
15-
IntersectionInfoMatrix,
1615
Filter,
1716
color_for_normalized_value,
1817
color_for_miss_reason,
1918
)
19+
from nerte.util.generic_matrix import GenericMatrix
2020

2121

2222
def _is_finite(mat: np.ndarray) -> np.ndarray:
@@ -146,13 +146,12 @@ def _color_for_pixel(
146146
return self.color_for_normalized_ray_depth_value(pixel_value)
147147
return color_for_miss_reason(info)
148148

149-
def apply(self, info_matrix: IntersectionInfoMatrix) -> Image:
150-
if len(info_matrix) == 0 or len(info_matrix[0]) == 0:
149+
def apply(self, info_matrix: GenericMatrix[IntersectionInfo]) -> Image:
150+
width, height = info_matrix.dimensions()
151+
if width == 0 or height == 0:
151152
raise ValueError(
152-
"Cannot apply ray depth filter. Intersection info matrix is empty."
153+
"Cannot apply hit filter. Intersection info matrix is empty."
153154
)
154-
width = len(info_matrix)
155-
height = len(info_matrix[0])
156155

157156
# initialize image with pink background
158157
image = Image.new(
@@ -162,7 +161,7 @@ def apply(self, info_matrix: IntersectionInfoMatrix) -> Image:
162161
ray_depths_raw = np.full((width, height), math.nan)
163162
for pixel_x in range(width):
164163
for pixel_y in range(height):
165-
info = info_matrix[pixel_x][pixel_y]
164+
info = info_matrix[pixel_x, pixel_y]
166165
if info.hits():
167166
ray_depths_raw[pixel_x, pixel_y] = info.ray_depth()
168167

@@ -172,7 +171,7 @@ def apply(self, info_matrix: IntersectionInfoMatrix) -> Image:
172171
for pixel_x in range(width):
173172
for pixel_y in range(height):
174173
pixel_location = (pixel_x, pixel_y)
175-
info = info_matrix[pixel_x][pixel_y]
174+
info = info_matrix[pixel_x, pixel_y]
176175
pixel_value = ray_depth_normalized[pixel_x, pixel_y]
177176
pixel_color = self._color_for_pixel(info, pixel_value)
178177
image.putpixel(pixel_location, pixel_color.rgb)

src/nerte/render/ray_depth_filter_unittest.py

+3-5
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,9 @@
1212

1313
from nerte.values.color import Color
1414
from nerte.values.intersection_info import IntersectionInfo
15-
from nerte.render.image_filter_renderer import (
16-
IntersectionInfoMatrix,
17-
color_for_miss_reason,
18-
)
15+
from nerte.render.image_filter_renderer import color_for_miss_reason
1916
from nerte.render.ray_depth_filter import RayDepthFilter
17+
from nerte.util.generic_matrix import GenericMatrix
2018

2119

2220
class RayDepthFilterConstructorTest(BaseTestCase):
@@ -138,7 +136,7 @@ def test_color_for_ray_depth(self) -> None:
138136

139137
class RayDepthFilterApplyTest(BaseTestCase):
140138
def setUp(self) -> None:
141-
self.info_matrix = IntersectionInfoMatrix(
139+
self.info_matrix = GenericMatrix[IntersectionInfo](
142140
[
143141
[
144142
IntersectionInfo(ray_depth=math.e ** 0),

0 commit comments

Comments
 (0)