3
3
The data is rendered first and filters may be applied afterwards.
4
4
"""
5
5
6
- from typing import Optional , NewType , Iterable
6
+ from typing import Optional , Iterable
7
7
8
8
from abc import ABC , abstractmethod
9
9
18
18
from nerte .geometry .geometry import Geometry
19
19
from nerte .render .image_renderer import ImageRenderer
20
20
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
26
22
27
23
28
24
def color_for_normalized_value (value : float ) -> Color :
@@ -78,7 +74,7 @@ class Filter(ABC):
78
74
@abstractmethod
79
75
def apply (
80
76
self ,
81
- info_matrix : IntersectionInfoMatrix ,
77
+ info_matrix : GenericMatrix [ IntersectionInfo ] ,
82
78
) -> Image :
83
79
"""
84
80
Returns color for pixel based.
@@ -112,13 +108,12 @@ def color_for_info(
112
108
return self .color_hit ()
113
109
return color_for_miss_reason (info )
114
110
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 :
117
114
raise ValueError (
118
115
"Cannot apply hit filter. Intersection info matrix is empty."
119
116
)
120
- width = len (info_matrix )
121
- height = len (info_matrix [0 ])
122
117
123
118
# initialize image with pink background
124
119
image = Image .new (
@@ -129,7 +124,7 @@ def apply(self, info_matrix: IntersectionInfoMatrix) -> Image:
129
124
for pixel_x in range (width ):
130
125
for pixel_y in range (height ):
131
126
pixel_location = (pixel_x , pixel_y )
132
- info = info_matrix [pixel_x ][ pixel_y ]
127
+ info = info_matrix [pixel_x , pixel_y ]
133
128
pixel_color = self .color_for_info (info )
134
129
image .putpixel (pixel_location , pixel_color .rgb )
135
130
@@ -158,7 +153,7 @@ def __init__(
158
153
159
154
self ._filter = filtr
160
155
self .auto_apply_filter = auto_apply_filter
161
- self ._last_info_matrix : Optional [IntersectionInfoMatrix ] = None
156
+ self ._last_info_matrix : Optional [GenericMatrix [ IntersectionInfo ] ] = None
162
157
163
158
def has_render_data (self ) -> bool :
164
159
"""Returns True, iff render was called previously."""
@@ -222,14 +217,14 @@ def render_pixel_intersection_info(
222
217
223
218
def render_intersection_info (
224
219
self , scene : Scene , geometry : Geometry
225
- ) -> IntersectionInfoMatrix :
220
+ ) -> GenericMatrix [ IntersectionInfo ] :
226
221
"""
227
222
Returns matrix with intersection infos per pixel.
228
223
"""
229
224
230
225
width , height = scene .camera .canvas_dimensions
231
226
# initialize background with nan
232
- info_matrix : IntersectionInfoMatrix = IntersectionInfoMatrix (
227
+ info_matrix = GenericMatrix [ IntersectionInfo ] (
233
228
[
234
229
[IntersectionInfos .UNINIALIZED for _ in range (height )]
235
230
for _ in range (width )
@@ -245,7 +240,7 @@ def render_intersection_info(
245
240
scene .objects (),
246
241
pixel_location ,
247
242
)
248
- info_matrix [pixel_x ][ pixel_y ] = pixel_info
243
+ info_matrix [pixel_x , pixel_y ] = pixel_info
249
244
return info_matrix
250
245
251
246
def render (self , scene : Scene , geometry : Geometry ) -> None :
0 commit comments