-
Notifications
You must be signed in to change notification settings - Fork 23
Added support for loading native object for openvdb #170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
0e31843
eb448e3
d660aef
faf508b
5d0dfac
099ea1a
b5a40a8
79fc2b6
d1c8d41
f7c2f9e
824b4af
3de5c04
3f0cc9b
b92ba88
86b6acf
549ab2e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -109,20 +109,21 @@ | |
| @dataclass | ||
| class DownCastTo: | ||
| """:func:`~dataclasses.dataclass` decorator serving as a marker for a downcast. | ||
|
|
||
| This function is used to create a proxy for an OpenVDB grid type. | ||
| The field :attr:`gridType` contains the OpenVDB grid type that it represents. | ||
| :meth:`OpenVDBField._get_best_grid_type` selects a OpenVDB grid that best matches | ||
| the numpy dtype of the data but in some cases, only target OpenVDB grid types are | ||
| available that loose precision. In this case, this class wraps the orginal OpenVDB | ||
| class to indicate that the downcast. For example, :: | ||
|
|
||
| np.dtype("int32"): ["Int32Grid", DownCastTo("FloatGrid")] | ||
|
|
||
| indicates that NumPy int32 data should be represented by a :class:`openvdb.Int32Grid` | ||
| but if this is not available, a :class:`openvdb.FloatGrid` is used instead, | ||
| which, however, is only able to represent a subset of all 32-bit integers. | ||
| """ | ||
|
|
||
| gridType: str | ||
|
|
||
|
|
||
|
|
@@ -142,7 +143,7 @@ class OpenVDBField(object): | |
|
|
||
| import gridData.OpenVDB as OpenVDB | ||
|
|
||
| vdb_field = OpenVDB.OpenVDBField(grid=np.ones((3, 4, 5)), | ||
| vdb_field = OpenVDB.OpenVDBField(grid=np.ones((3, 4, 5)), | ||
| origin=np.array([1.5, 0, 0]), | ||
| delta=np.array([0.5, 0.5, 0.25]), | ||
| name='density') | ||
|
|
@@ -154,6 +155,32 @@ class OpenVDBField(object): | |
| g.export('output.vdb', format='vdb') | ||
| """ | ||
|
|
||
| # dtype maps for numpy to vdb | ||
| _DTYPES_NP2VDB = { | ||
| np.dtype("bool"): ["BoolGrid"], | ||
| np.dtype("int8"): ["Int32Grid", "FloatGrid"], | ||
| np.dtype("uint8"): ["Int32Grid", "FloatGrid"], | ||
| np.dtype("int16"): ["Int32Grid", "FloatGrid"], | ||
| np.dtype("uint16"): ["Int32Grid", "FloatGrid"], | ||
| np.dtype("int32"): ["Int32Grid", DownCastTo("FloatGrid")], | ||
| np.dtype("uint32"): [DownCastTo("Int32Grid"), DownCastTo("FloatGrid")], | ||
| np.dtype("int64"): ["Int64Grid", DownCastTo("FloatGrid")], | ||
| np.dtype("uint64"): ["Int64Grid", DownCastTo("FloatGrid")], | ||
| np.dtype("float16"): ["HalfGrid", "FloatGrid"], | ||
| np.dtype("float32"): ["FloatGrid"], | ||
| np.dtype("float64"): ["DoubleGrid", DownCastTo("FloatGrid")], | ||
| } | ||
|
|
||
| # dtype maps for vdb to numpy | ||
| _DTYPES_VDB2NP = { | ||
| "BoolGrid": np.dtype("bool"), | ||
| "Int32Grid": np.dtype("int32"), | ||
| "Int64Grid": np.dtype("int64"), | ||
| "FloatGrid": np.dtype("float32"), | ||
| "DoubleGrid": np.dtype("float64"), | ||
| "HalfGrid": np.dtype("float16"), | ||
| } | ||
|
|
||
| def __init__( | ||
| self, | ||
| grid=None, | ||
|
|
@@ -205,8 +232,13 @@ def __init__( | |
| self.metadata = {} | ||
|
|
||
| if grid is not None: | ||
| self._populate(grid, origin, delta) | ||
| self.vdb_grid = self._create_openvdb_grid() | ||
| if isinstance(grid, vdb.GridBase): | ||
| self.vdb_grid = grid | ||
| self._extract_from_vdb_grid() | ||
| else: | ||
| self._populate(grid, origin, delta) | ||
| self.vdb_grid = self._create_openvdb_grid() | ||
|
|
||
| else: | ||
| self.grid = None | ||
| self.origin = None | ||
|
|
@@ -272,6 +304,58 @@ def native(self): | |
| """ | ||
| return self.vdb_grid | ||
|
|
||
| def _extract_from_vdb_grid(self): | ||
| """Extract numpy array, origin, delta from stored VDB grid. | ||
|
|
||
| This method converts the sparse VDB grid to a dense numpy array | ||
| and extracts the transform information. | ||
| """ | ||
| for key in self.vdb_grid.metadata: | ||
| try: | ||
| self.metadata[key] = self.vdb_grid[key] | ||
| except (TypeError, ValueError) as e: | ||
| warnings.warn( | ||
| f"Could not read metadata key '{key}' from VDB grid: {e}", | ||
| UserWarning, | ||
| ) | ||
|
|
||
| transformation = self.vdb_grid.transform | ||
|
|
||
| v_origin = np.array(transformation.indexToWorld([0, 0, 0])) | ||
| v_delta = np.array(transformation.indexToWorld([1, 1, 1])) - v_origin | ||
|
|
||
| self.origin = v_origin | ||
| self.delta = v_delta | ||
|
|
||
| dtype = self._DTYPES_VDB2NP.get(type(self.vdb_grid).__name__) | ||
| if dtype is None: | ||
| warnings.warn( | ||
| f"Unknown VDB grid type '{type(self.vdb_grid).__name__}', defaulting to float32.", | ||
| RuntimeWarning, | ||
| ) | ||
| dtype = np.dtype("float32") | ||
|
|
||
| bbox = self.vdb_grid.evalActiveVoxelBoundingBox() | ||
|
|
||
| if bbox is None or any(bbox[1][i] < bbox[0][i] for i in range(3)): | ||
| warnings.warn( | ||
| "VDB grid has no active voxels (empty bounding box). Returning an empty array of shape (0, 0, 0).", | ||
| RuntimeWarning, | ||
| ) | ||
| self.grid = np.zeros((0, 0, 0), dtype=dtype) | ||
| return | ||
|
|
||
| shape = tuple(np.array(bbox[1]) - np.array(bbox[0]) + 1) | ||
|
|
||
| self.grid = np.zeros(shape, dtype=dtype) | ||
| print(dtype) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. still a print there! |
||
| self.vdb_grid.copyToArray(self.grid, ijk=bbox[0]) | ||
|
|
||
| if not np.all(np.array(bbox[0]) == 0): | ||
| self.origin = np.array( | ||
| transformation.indexToWorld(np.array(bbox[0]).tolist()) | ||
| ) | ||
|
|
||
| def _populate(self, grid, origin, delta): | ||
| """Populate the field with grid data. | ||
|
|
||
|
|
@@ -331,23 +415,8 @@ def _get_best_grid_type(self): | |
| TypeError | ||
| If dtype is not supported or no suitable grid type is available | ||
| """ | ||
| datatypes = { | ||
| np.dtype("bool"): ["BoolGrid"], | ||
| np.dtype("int8"): ["Int32Grid", "FloatGrid"], | ||
| np.dtype("uint8"): ["Int32Grid", "FloatGrid"], | ||
| np.dtype("int16"): ["Int32Grid", "FloatGrid"], | ||
| np.dtype("uint16"): ["Int32Grid", "FloatGrid"], | ||
| np.dtype("int32"): ["Int32Grid", DownCastTo("FloatGrid")], | ||
| np.dtype("uint32"): [DownCastTo("Int32Grid"), DownCastTo("FloatGrid")], | ||
| np.dtype("int64"): ["Int64Grid", DownCastTo("FloatGrid")], | ||
| np.dtype("uint64"): ["Int64Grid", DownCastTo("FloatGrid")], | ||
| np.dtype("float16"): ["HalfGrid", "FloatGrid"], | ||
| np.dtype("float32"): ["FloatGrid"], | ||
| np.dtype("float64"): ["DoubleGrid", DownCastTo("FloatGrid")], | ||
| } | ||
|
|
||
| try: | ||
| vdb_gridtypes = datatypes[self.grid.dtype] | ||
| vdb_gridtypes = self._DTYPES_NP2VDB[self.grid.dtype] | ||
| except KeyError: | ||
| raise TypeError(f"Data type {self.grid.dtype} not supported for VDB") | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -392,6 +392,106 @@ def test_from_native_grid_shape_values_and_dimension(self, grid345): | |
| world = native_grid.transform.indexToWorld((0, 0, 0)) | ||
| assert_allclose([world[0], world[1], world[2]], g.origin, rtol=1e-5) | ||
|
|
||
| def test_file_roundtrip_native_vdbgrid(self, tmpdir, grid345): | ||
| data, g = grid345 | ||
| g.metadata["name"] = "new_density" | ||
|
|
||
| outfile = str(tmpdir / "roundtrip.vdb") | ||
| g.export(outfile) | ||
|
|
||
| grids, _ = vdb.readAll(outfile) | ||
| assert len(grids) == 1 | ||
|
|
||
| grid_vdb = grids[0] | ||
| new_vdb_grid = Grid(grid=grid_vdb) | ||
| assert_allclose(new_vdb_grid.grid, g.grid, rtol=1e-5) | ||
| assert_allclose(new_vdb_grid.origin, g.origin, rtol=1e-5) | ||
| assert_allclose(new_vdb_grid.delta, g.delta, rtol=1e-5) | ||
| assert new_vdb_grid.metadata["name"] == "new_density" | ||
| assert new_vdb_grid.grid.dtype == np.dtype("float32") | ||
| assert_allclose(new_vdb_grid.grid, data, rtol=1e-5) | ||
|
|
||
| def test_extract_from_vdb_grid(self, grid345): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. parameterize the test to try out all normally supported vdb gridtypes |
||
| data, g = grid345 | ||
| g.metadata["name"] = "new_density" | ||
|
|
||
| native = g.convert_to("vdb") | ||
| new_vdb_grid = Grid(grid=native) | ||
|
|
||
| assert_allclose(new_vdb_grid.grid, g.grid, rtol=1e-5) | ||
| assert_allclose(new_vdb_grid.origin, g.origin, rtol=1e-5) | ||
| assert_allclose(new_vdb_grid.delta, g.delta, rtol=1e-5) | ||
| assert new_vdb_grid.metadata["name"] == "new_density" | ||
| assert new_vdb_grid.grid.dtype == np.dtype("float32") | ||
| assert_allclose(new_vdb_grid.grid, data, rtol=1e-5) | ||
|
|
||
|
orbeckst marked this conversation as resolved.
|
||
| @pytest.mark.parametrize( | ||
| "vdb_type,np_dtype", list(gridData.OpenVDB.OpenVDBField._DTYPES_VDB2NP.items()) | ||
| ) | ||
| def test_extract_dtype_roundtrip(self, vdb_type, np_dtype): | ||
| vdb_cls = getattr(vdb, vdb_type, None) | ||
| if vdb_cls is None: | ||
| pytest.skip(f"{vdb_type} not available in this openvdb build") | ||
|
|
||
| native = vdb_cls() | ||
| native.name = "test" | ||
| acc = native.getAccessor() | ||
| acc.setValueOn((0, 0, 0), True if np_dtype == np.dtype("bool") else 1) | ||
| acc.setValueOn((1, 1, 1), True if np_dtype == np.dtype("bool") else 2) | ||
|
|
||
| field = gridData.OpenVDB.OpenVDBField(grid=native) | ||
|
|
||
| assert field.grid.dtype == np_dtype | ||
| assert field.grid.shape != (0, 0, 0) | ||
|
|
||
| def test_extract_unknown_vdb_type_warns(self): | ||
| native = vdb.FloatGrid() | ||
| acc = native.getAccessor() | ||
| acc.setValueOn((0, 0, 0), 1.0) | ||
|
|
||
| patched = { | ||
| k: v | ||
| for k, v in gridData.OpenVDB.OpenVDBField._DTYPES_VDB2NP.items() | ||
| if k != "FloatGrid" | ||
| } | ||
| with patch.object(gridData.OpenVDB.OpenVDBField, "_DTYPES_VDB2NP", patched): | ||
| with pytest.warns(RuntimeWarning, match="Unknown VDB grid type"): | ||
| field = gridData.OpenVDB.OpenVDBField(grid=native) | ||
|
|
||
| assert field.grid.dtype == np.dtype("float32") | ||
|
|
||
| def test_extract_empty_vdb_grid_warns(self): | ||
| native = vdb.FloatGrid() | ||
| native.clear() | ||
|
|
||
| with pytest.warns(RuntimeWarning, match="no active voxels"): | ||
| field = gridData.OpenVDB.OpenVDBField(grid=native) | ||
|
|
||
| assert field.grid.shape == (0, 0, 0) | ||
|
|
||
| def test_extract_unreadable_metadata_warns(self, grid345): | ||
| data, g = grid345 | ||
| g.metadata["readable"] = "hello" | ||
| g.metadata["factor"] = 42 | ||
|
|
||
| native = g.convert_to("vdb") | ||
|
|
||
| original_getitem = native.__class__.__getitem__ | ||
|
|
||
| def patched_getitem(self, key): | ||
| if key == "factor": | ||
| raise TypeError("unsupported metadata type") | ||
| return original_getitem(self, key) | ||
|
|
||
| with patch.object(native.__class__, "__getitem__", patched_getitem): | ||
| with pytest.warns( | ||
| UserWarning, match="Could not read metadata key 'factor'" | ||
| ): | ||
| field = gridData.OpenVDB.OpenVDBField(grid=native) | ||
|
|
||
| assert field.metadata.get("readable") == "hello" | ||
| assert "factor" not in field.metadata | ||
|
|
||
|
|
||
| @pytest.mark.skipif( | ||
| not HAS_OPENVDB, reason="Need openvdb to test import error handling" | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.