modulize
This commit is contained in:
parent
cc64f55c90
commit
3d7cf7b503
1 changed files with 934 additions and 736 deletions
73
voxel.py
Normal file
73
voxel.py
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
import numpy
|
||||
|
||||
from .pyr3d import split, reduce
|
||||
from .util import number_of_moments
|
||||
|
||||
try:
|
||||
profile
|
||||
except NameError:
|
||||
def profile(f):
|
||||
return f
|
||||
|
||||
|
||||
@profile
|
||||
def voxelize(poly, d, ibox=None, poly_order=0):
|
||||
ibox = get_ibox(poly, d) if ibox is None else numpy.array(ibox, dtype=int)
|
||||
d = numpy.array(d)
|
||||
|
||||
grid_shape = ibox[1] - ibox[0]
|
||||
dest_grid = numpy.zeros((*grid_shape, number_of_moments(poly_order)), dtype=float)
|
||||
|
||||
if poly is None or poly['vertices'].shape[0] == 0 or (grid_shape <= 0).any():
|
||||
return dest_grid
|
||||
|
||||
# Stack-based recursive split
|
||||
stack = [(poly, ibox)]
|
||||
while stack:
|
||||
c_poly, c_ibox = stack.pop()
|
||||
|
||||
# Base case 1: moments are 0 for this region
|
||||
if c_poly['vertices'].shape[0] == 0:
|
||||
continue
|
||||
|
||||
box_size = c_ibox[1] - c_ibox[0]
|
||||
split_axis = box_size.argmax()
|
||||
max_dist = box_size[split_axis]
|
||||
|
||||
# Base case 2: calculate moments for single voxel
|
||||
if max_dist == 1:
|
||||
moments = reduce(c_poly, poly_order)
|
||||
offset = (c_ibox[0] - ibox[0])
|
||||
dest_grid[offset[0], offset[1], offset[2], :] += moments
|
||||
continue
|
||||
|
||||
# Recursion case: split along longest side
|
||||
n = numpy.zeros(3)
|
||||
n[split_axis] = -1
|
||||
|
||||
split_ind = c_ibox[0][split_axis] + max_dist // 2
|
||||
plane = (n, d[split_axis] * split_ind)
|
||||
|
||||
children = split(c_poly, plane)
|
||||
|
||||
split_plane = c_ibox.copy()
|
||||
split_plane[:, split_axis] = split_ind
|
||||
|
||||
stack.append((children[0], numpy.vstack((c_ibox[0], split_plane[1])) ))
|
||||
stack.append((children[1], numpy.vstack((split_plane[0], c_ibox[1])) ))
|
||||
return dest_grid
|
||||
|
||||
|
||||
def get_ibox(poly, d):
|
||||
min_box = numpy.floor(numpy.min(poly['vertices'], axis=0) / d)
|
||||
max_box = numpy.ceil(numpy.max(poly['vertices'], axis=0) / d)
|
||||
return numpy.array((min_box, max_box), dtype=int)
|
||||
|
||||
|
||||
def clamp_ibox(poly, ibox, clampbox, d):
|
||||
faces = box_faces_from_verts(clampbox)
|
||||
for face in faces:
|
||||
poly = clip(poly, face)
|
||||
ibox = get_ibox(poly, d)
|
||||
return poly, ibox
|
||||
|
||||
Loading…
Reference in a new issue