Replies: 1 comment
-
|
I have tested the following code. It works fine ✔️ It is using just the available OpenVDB API. I like this approach more than the NumPy trick I already discussed before. Any community feedback would be reassuring that I'm on the right track. Thanks. def boolean_difference(plate_grid, cutter_grid):
"""
CSG difference: plate - cutter using level set math.
Formula: result = max(plate_sdf, -cutter_sdf)
This implementation uses iterators + accessors to handle
non-overlapping narrow bands correctly.
"""
import openvdb as vdb
# 1. Create result as deep copy of plate (preserves all plate's active voxels)
result_grid = plate_grid.deepCopy()
# 2. Get accessors for random value access
result_accessor = result_grid.getAccessor()
plate_accessor = plate_grid.getConstAccessor()
# 3. Process every active voxel/tile in cutter grid
for item in cutter_grid.citerOnValues():
cutter_val = item.value
neg_cutter = -cutter_val
if item.count == 1:
# Single voxel - direct access
ijk = item.min
plate_val = plate_accessor.getValue(ijk) # Returns background if inactive
result_val = max(plate_val, neg_cutter)
result_accessor.setValueOn(ijk, result_val)
else:
# Tile (uniform region) - must iterate through all voxels in the tile
min_coord = item.min
max_coord = item.max
for i in range(min_coord[0], max_coord[0] + 1):
for j in range(min_coord[1], max_coord[1] + 1):
for k in range(min_coord[2], max_coord[2] + 1):
ijk = (i, j, k)
plate_val = plate_accessor.getValue(ijk)
result_val = max(plate_val, neg_cutter)
result_accessor.setValueOn(ijk, result_val)
# 4. Flood-fill to fix background topology and prune uniform regions
result_grid.signedFloodFill()
result_grid.prune()
return result_grid |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Limitation
The OpenVDB Python module is limited:
https://www.openvdb.org/documentation/doxygen/python.html
Conda OpenVDB
I'm using this Python package:
https://anaconda.org/channels/conda-forge/packages/openvdb/overview
CSG operation
I'm going to do a CSG difference operation. But there are no API tools for it, like
csgDifference. Therefore, I have to do it by a workaround using NumPy:Question
The Conda OpenVDB package is exposing the API methods listed below. How can I use the following available methods to replace the NumPy trick shown above in my
boolean_differencefunction? Thanks for your help.The available API methods for
FloatGridare:What I have tried
I have tried to use the
mapOnandcombinemethods, like below. However, the problem is that thecombine()function only operates on voxels active in BOTH grids. For level sets, active voxels exist only in the narrow band around each surface. Where the bands don’t overlap, the operation is skipped. So, the followingmapOnandcombineapproach is not working for me, resulting in unexpected outputs.Beta Was this translation helpful? Give feedback.
All reactions