-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy path_volume.py
More file actions
441 lines (341 loc) · 12.5 KB
/
_volume.py
File metadata and controls
441 lines (341 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
from itertools import product
from math import ceil
import numpy as np
import pygfx
from ._base import GraphicFeature, GraphicFeatureEvent, block_reentrance
VOLUME_RENDER_MODES = {
"mip": pygfx.VolumeMipMaterial,
"minip": pygfx.VolumeMinipMaterial,
"iso": pygfx.VolumeIsoMaterial,
"slice": pygfx.VolumeSliceMaterial,
}
class TextureArrayVolume(GraphicFeature):
"""
Manages an array of Textures representing chunks of an image. Chunk size is the GPU's max texture limit.
Creates and manages multiple pygfx.Texture objects.
"""
event_info_spec = [
{
"dict key": "key",
"type": "slice, index, numpy-like fancy index",
"description": "key at which image data was sliced/fancy indexed",
},
{
"dict key": "value",
"type": "np.ndarray | float",
"description": "new data values",
},
]
def __init__(self, data):
super().__init__(property_name="data")
data = self._fix_data(data)
shared = pygfx.renderers.wgpu.get_shared()
self._texture_size_limit = shared.device.limits["max-texture-dimension-3d"]
# create a new buffer that will be used for the texture data
self._value = np.zeros(data.shape, dtype=data.dtype)
self.value[:] = data[:]
# data start indices for each Texture
self._row_indices = np.arange(
0,
ceil(self.value.shape[1] / self._texture_size_limit)
* self._texture_size_limit,
self._texture_size_limit,
)
self._col_indices = np.arange(
0,
ceil(self.value.shape[2] / self._texture_size_limit)
* self._texture_size_limit,
self._texture_size_limit,
)
self._zdim_indices = np.arange(
0,
ceil(self.value.shape[0] / self._texture_size_limit)
* self._texture_size_limit,
self._texture_size_limit,
)
shape = (self.zdim_indices.size, self.row_indices.size, self.col_indices.size)
# buffer will be an array of textures
self._buffer: np.ndarray[pygfx.Texture] = np.empty(shape=shape, dtype=object)
self._iter = None
# iterate through each chunk of passed `data`
# create a pygfx.Texture from this chunk
for _, buffer_index, data_slice in self:
texture = pygfx.Texture(self.value[data_slice], dim=3)
self.buffer[buffer_index] = texture
@property
def value(self) -> np.ndarray:
"""The full array that represents all the data within this TextureArray"""
return self._value
def set_value(self, graphic, value):
self[:] = value
@property
def buffer(self) -> np.ndarray[pygfx.Texture]:
"""array of buffers that are mapped to the GPU"""
return self._buffer
@property
def row_indices(self) -> np.ndarray:
"""
row indices that are used to chunk the big data array
into individual Textures on the GPU
"""
return self._row_indices
@property
def col_indices(self) -> np.ndarray:
"""
column indices that are used to chunk the big data array
into individual Textures on the GPU
"""
return self._col_indices
@property
def zdim_indices(self) -> np.ndarray:
"""
z dimension indices that are used to chunk the big data array
into individual Textures on the GPU
"""
return self._zdim_indices
def _fix_data(self, data):
if data.ndim not in (3, 4):
raise ValueError(
"Volume Image data must be 3D with or without an RGB(A) dimension, i.e. "
"it must be of shape [z, rows, cols], [z, rows, cols, 3] or [z, rows, cols, 4]"
)
# let's just cast to float32 always
return data.astype(np.float32)
def __iter__(self):
self._iter = product(
enumerate(self.zdim_indices),
enumerate(self.row_indices),
enumerate(self.col_indices),
)
return self
def __next__(
self,
) -> tuple[pygfx.Texture, tuple[int, int, int], tuple[slice, slice, slice]]:
"""
Iterate through each Texture within the texture array
Returns
-------
Texture, tuple[int, int], tuple[slice, slice]
| Texture: pygfx.Texture
| tuple[int, int]: chunk index, i.e corresponding index of ``self.buffer`` array
| tuple[slice, slice]: data slice of big array in this chunk and Texture
"""
# chunk indices
(
(chunk_z, data_z_start),
(chunk_row, data_row_start),
(chunk_col, data_col_start),
) = next(self._iter)
# indices for to self.buffer for this chunk
chunk_index = (chunk_z, chunk_row, chunk_col)
# stop indices of big data array for this chunk
z_stop = min(self.value.shape[0], data_z_start + self._texture_size_limit)
row_stop = min(self.value.shape[1], data_row_start + self._texture_size_limit)
col_stop = min(self.value.shape[2], data_col_start + self._texture_size_limit)
# zdim, row and column slices that slice the data for this chunk from the big data array
data_slice = (
slice(data_z_start, z_stop),
slice(data_row_start, row_stop),
slice(data_col_start, col_stop),
)
# texture for this chunk
texture = self.buffer[chunk_index]
return texture, chunk_index, data_slice
def __getitem__(self, item):
return self.value[item]
@block_reentrance
def __setitem__(self, key, value):
self.value[key] = value
for texture in self.buffer.ravel():
texture.update_range((0, 0, 0), texture.size)
event = GraphicFeatureEvent(
self._property_name, info={"key": key, "value": value}
)
self._call_event_handlers(event)
def __len__(self):
return self.buffer.size
def create_volume_material_kwargs(graphic, mode: str):
kwargs = {
"clim": (graphic.vmin, graphic.vmax),
"map": graphic._texture_map,
"interpolation": graphic.interpolation,
"pick_write": True,
}
if mode == "iso":
more_kwargs = {
attr: getattr(graphic, attr)
for attr in [
"threshold",
"step_size",
"substep_size",
"emissive",
"shininess",
]
}
elif mode == "slice":
more_kwargs = {"plane": graphic.plane}
else:
more_kwargs = {}
kwargs.update(more_kwargs)
return kwargs
class VolumeRenderMode(GraphicFeature):
"""Volume rendering mode, controls world object material"""
event_info_spec = [
{
"dict key": "value",
"type": "str",
"description": "volume rendering mode that has been set",
},
]
def __init__(self, value: str):
self._validate(value)
self._value = value
super().__init__(property_name="mode")
@property
def value(self) -> str:
return self._value
def _validate(self, value):
if value not in VOLUME_RENDER_MODES.keys():
raise ValueError(
f"Given render mode: {value} is invalid. Valid render modes are: {VOLUME_RENDER_MODES.keys()}"
)
@block_reentrance
def set_value(self, graphic, value: str):
self._validate(value)
VolumeMaterialCls = VOLUME_RENDER_MODES[value]
kwargs = create_volume_material_kwargs(graphic, mode=value)
new_material = VolumeMaterialCls(**kwargs)
# since the world object is a group
for volume_tile in graphic.world_object.children:
volume_tile.material = new_material
# so we have one place to reference it
graphic._material = new_material
self._value = value
event = GraphicFeatureEvent(type=self._property_name, info={"value": value})
self._call_event_handlers(event)
class VolumeIsoThreshold(GraphicFeature):
"""Isosurface threshold"""
event_info_spec = [
{
"dict key": "value",
"type": "float",
"description": "new isosurface threshold",
},
]
def __init__(self, value: float):
self._value = value
super().__init__(property_name="threshold")
@property
def value(self) -> float:
return self._value
@block_reentrance
def set_value(self, graphic, value: float):
graphic._material.threshold = value
self._value = graphic._material.threshold
event = GraphicFeatureEvent(type=self._property_name, info={"value": value})
self._call_event_handlers(event)
class VolumeIsoStepSize(GraphicFeature):
"""Isosurface step_size"""
event_info_spec = [
{
"dict key": "value",
"type": "float",
"description": "new isosurface step_size",
},
]
def __init__(self, value: float):
self._value = value
super().__init__(property_name="step_size")
@property
def value(self) -> float:
return self._value
@block_reentrance
def set_value(self, graphic, value: float):
graphic._material.step_size = value
self._value = graphic._material.step_size
event = GraphicFeatureEvent(type=self._property_name, info={"value": value})
self._call_event_handlers(event)
class VolumeIsoSubStepSize(GraphicFeature):
"""Isosurface substep_size"""
event_info_spec = [
{
"dict key": "value",
"type": "float",
"description": "new isosurface step_size",
},
]
def __init__(self, value: float):
self._value = value
super().__init__(property_name="substep_size")
@property
def value(self) -> float:
return self._value
@block_reentrance
def set_value(self, graphic, value: float):
graphic._material.substep_size = value
self._value = graphic._material.substep_size
event = GraphicFeatureEvent(type=self._property_name, info={"value": value})
self._call_event_handlers(event)
class VolumeIsoEmissive(GraphicFeature):
"""Isosurface emissive color"""
event_info_spec = [
{
"dict key": "value",
"type": "pygfx.Color",
"description": "new isosurface emissive color",
},
]
def __init__(self, value: pygfx.Color | str | tuple | np.ndarray):
self._value = pygfx.Color(value)
super().__init__(property_name="emissive")
@property
def value(self) -> pygfx.Color:
return self._value
@block_reentrance
def set_value(self, graphic, value: pygfx.Color | str | tuple | np.ndarray):
graphic._material.emissive = value
self._value = graphic._material.emissive
event = GraphicFeatureEvent(type=self._property_name, info={"value": value})
self._call_event_handlers(event)
class VolumeIsoShininess(GraphicFeature):
"""Isosurface shininess"""
event_info_spec = [
{
"dict key": "value",
"type": "int",
"description": "new isosurface shininess",
},
]
def __init__(self, value: int):
self._value = value
super().__init__(property_name="shininess")
@property
def value(self) -> int:
return self._value
@block_reentrance
def set_value(self, graphic, value: float):
graphic._material.shininess = value
self._value = graphic._material.shininess
event = GraphicFeatureEvent(type=self._property_name, info={"value": value})
self._call_event_handlers(event)
class VolumeSlicePlane(GraphicFeature):
"""Volume plane"""
event_info_spec = [
{
"dict key": "value",
"type": "tuple[float, float, float, float]",
"description": "new plane slice",
},
]
def __init__(self, value: tuple[float, float, float, float]):
self._value = value
super().__init__(property_name="plane")
@property
def value(self) -> tuple[float, float, float, float]:
return self._value
@block_reentrance
def set_value(self, graphic, value: tuple[float, float, float, float]):
graphic._material.plane = value
self._value = graphic._material.plane
event = GraphicFeatureEvent(type=self._property_name, info={"value": value})
self._call_event_handlers(event)