This commit is contained in:
jan 2018-03-20 15:10:45 -07:00
commit 3d7cf7b503

View file

@ -1,6 +1,5 @@
import warnings
from time import perf_counter
import copy
import random
import logging
@ -8,14 +7,17 @@ import numpy
from numpy import sin, cos, pi
from numpy.linalg import norm
import pyr3d
from . import pyr3d
from .shapes import build_tetrahedron, build_box, planes_from_tetrahedron, volume_of_tetrahedron
from .boundary import build_polyhedron, boundary_from_poly
from .util import check_poly, number_of_moments
from .voxel import voxelize
logger = logging.getLogger(__name__)
try:
profile
except:
except NameError:
def profile(f):
return f
@ -27,10 +29,8 @@ TOL_FAIL = 1e-4
# minimum volume allowed for test polyhedra
MIN_VOL = 1e-8
# for recursive tests, the stack capacity and
# maximum recursion depth allowed
STACK_SIZE = 64
MAX_DEPTH = 6 #10
# maximum recursion depth allowed for recursive tests
MAX_DEPTH = 8
# order of polynomial integration for all tests
POLY_ORDER = 0
@ -58,7 +58,7 @@ def check(om, m1, m2):
def thru_cent_3d(poly):
# make a randomly-oriented plane passing through the centroid
centroid = get_centroid_3d(poly)
n = rand_uvec_3d();
n = rand_uvec_3d()
d = -n @ centroid
return n, d
@ -128,7 +128,7 @@ def rand_tet_3d(minvol: float):
tetvol = 0.0
while tetvol < minvol:
verts = [rand_uvec_3d() for _ in range(4)]
tetvol = pyr3d.orient(verts)
tetvol = volume_of_tetrahedron(verts)
if tetvol < 0.0:
verts[2], verts[3] = verts[3], verts[2]
tetvol = -tetvol
@ -142,14 +142,14 @@ def get_centroid_3d(poly):
def test_split_tet_manual():
tet = pyr3d.init_tet([
tet = build_tetrahedron([
[0,0,0],
[2,0,0],
[0,2,0],
[0,0,2]])
logger.debug(f'Splitting manual tet {tet}')
logger.debug(f'orient volume {pyr3d.orient(tet["vertices"])}')
logger.debug(f'Splitting manual tetrahedron {tet}')
logger.debug(f'orient volume {volume_of_tetrahedron(tet["vertices"])}')
plane = numpy.array((-1,0,0)), 1
p_left, p_right = pyr3d.split(tet, plane)
@ -157,9 +157,9 @@ def test_split_tet_manual():
logger.debug(f'left {p_left}')
logger.debug(f'right {p_right}')
pyr3d.is_good(tet)
pyr3d.is_good(p_left)
pyr3d.is_good(p_right)
check_poly(tet)
check_poly(p_left)
check_poly(p_right)
om = pyr3d.reduce(tet, 0)
m1 = pyr3d.reduce(p_left, 0)
@ -182,7 +182,7 @@ def test_split_tets_thru_centroid():
# generate a random tet and clip plane
ntets = 1
opoly = [pyr3d.init_tet(rand_tet_3d(MIN_VOL)) for _ in range(ntets)]
opoly = [build_tetrahedron(rand_tet_3d(MIN_VOL)) for _ in range(ntets)]
plane = thru_cent_3d(opoly[ntets//7])
# split them all about the same plane
@ -190,9 +190,9 @@ def test_split_tets_thru_centroid():
poly1, poly2 = zip(*poly12)
for orig, p1, p2 in zip(opoly, poly1, poly2):
pyr3d.is_good(orig)
pyr3d.is_good(p1)
pyr3d.is_good(p2)
check_poly(orig)
check_poly(p1)
check_poly(p2)
# reduce the original and its two parts
om = pyr3d.reduce(orig, POLY_ORDER)
@ -205,8 +205,8 @@ def test_split_tets_thru_centroid():
def time_split():
NUM_TRIALS = int(10e3)
logger.info(f'Intersecting {NUM_TRIALS} pairs of tetrahedra.')
polys = [pyr3d.init_tet(rand_tet_3d(MIN_VOL)) for _ in range(NUM_TRIALS)]
facesets = [pyr3d.tet_faces_from_verts(rand_tet_3d(MIN_VOL)) for _ in range(NUM_TRIALS)]
polys = [build_tetrahedron(rand_tet_3d(MIN_VOL)) for _ in range(NUM_TRIALS)]
facesets = [planes_from_tetrahedron(rand_tet_3d(MIN_VOL)) for _ in range(NUM_TRIALS)]
t0 = perf_counter()
for t in range(NUM_TRIALS):
# clip the first tet against the faces of the second
@ -237,17 +237,17 @@ def test_tet_tet_timing():
logger.info(f'Intersecting {NUM_TRIALS} pairs of tetrahedra.')
for t in range(NUM_TRIALS):
# generate the first random tet
poly = pyr3d.init_tet(rand_tet_3d(MIN_VOL))
poly = build_tetrahedron(rand_tet_3d(MIN_VOL))
# generate the second random tet
faces = pyr3d.tet_faces_from_verts(rand_tet_3d(MIN_VOL))
faces = planes_from_tetrahedron(rand_tet_3d(MIN_VOL))
# clip the first tet against the faces of the second
for face in faces:
poly = pyr3d.clip(poly, face)
# find the moments (up to quadratic order) of the clipped poly
om = pyr3d.reduce(poly, POLY_ORDER)
_om = pyr3d.reduce(poly, POLY_ORDER)
if (t+1) % 1000 == 0:
t_each = (perf_counter() - t0) / (t + 1)
@ -286,7 +286,7 @@ def test_split_nonconvex():
'neighbors': neighbors,
}
logger.debug('good:', pyr3d.is_good(opoly))
logger.debug('good: {}'.format(check_poly(opoly)))
# split along the x-axis (two single connected components this direction)
plane = [numpy.array((1, 0, 0)),
@ -334,12 +334,12 @@ def test_recursive_splitting_nondegenerate():
# recursively splits a polyhedron (starting from a tet) through the centroid,
# checking to see that the resulting volumes add up properly.
NUM_TRIALS=10
NUM_TRIALS = 10
logger.info(f'Recursively splitting {NUM_TRIALS} tetrahedra, maximum splits per tet is {MAX_DEPTH}.')
for _ in range(NUM_TRIALS):
# generate a random tet
opoly = pyr3d.init_tet(rand_tet_3d(MIN_VOL))
stack_split(opoly, plane_fun=lambda p: thru_cent_3d(p))
opoly = build_tetrahedron(rand_tet_3d(MIN_VOL))
stack_split(opoly, plane_fun=thru_cent_3d)
def test_recursive_splitting_degenerate():
@ -347,11 +347,11 @@ def test_recursive_splitting_degenerate():
# that is always degenerate with the tet in some way,
# checking to see that the resulting volumes add up properly.
NUM_TRIALS=10
NUM_TRIALS = 10
logger.info(f'Recursively splitting {NUM_TRIALS} tetrahedra, maximum splits per tet is {MAX_DEPTH}.')
for _ in range(NUM_TRIALS):
# generate a random tet
opoly = pyr3d.init_tet(rand_tet_3d(MIN_VOL))
opoly = build_tetrahedron(rand_tet_3d(MIN_VOL))
stack_split(opoly)
@ -363,18 +363,18 @@ def test_recursive_splitting_degenerate_perturbed():
MIN_PERTURB_ORDER = -17
MAX_PERTURB_ORDER = -1
NUM_TRIALS=10
NUM_TRIALS = 10
logger.info(f'Recursively splitting {NUM_TRIALS} tetrahedra, maximum splits per tet is {MAX_DEPTH}.')
for t in range(NUM_TRIALS):
# generate a random tet
opoly = pyr3d.init_tet(rand_tet_3d(MIN_VOL))
opoly = build_tetrahedron(rand_tet_3d(MIN_VOL))
# compute the order of magnitude by which to perturb the clip plane,
# determined by the trial number
perturb = pow(10, MIN_PERTURB_ORDER + t%(MAX_PERTURB_ORDER - MIN_PERTURB_ORDER))
# generate a random tet
opoly = pyr3d.init_tet(rand_tet_3d(MIN_VOL))
opoly = build_tetrahedron(rand_tet_3d(MIN_VOL))
stack_split(opoly, perturb=perturb)
@ -387,18 +387,17 @@ def test_voxelization():
# create a random tet in the unit box
verts = (numpy.array(rand_tet_3d(MIN_VOL)) + 1) / 2
poly = pyr3d.init_tet(verts)
poly = build_tetrahedron(verts)
# get its original moments for reference
moments = pyr3d.reduce(poly, POLY_ORDER)
# voxelize it
dx = numpy.ones((3,)) / NGRID
ibox = pyr3d.get_ibox(poly, dx)
logger.info(f'Voxelizing a tetrahedron to a grid with dx = {dx} and moments of order {POLY_ORDER}')
logger.info(f'Minimum index box = {ibox}')
grid = pyr3d.voxelize(poly, ibox, dx, POLY_ORDER)
# logger.info(f'Minimum index box = {ibox}')
grid = voxelize(poly, dx, poly_order=POLY_ORDER)
# visualize_voxels(grid)
@ -412,7 +411,7 @@ def test_voxelization():
def visualize_voxels(grid):
import gridlock
g = gridlock.Grid([numpy.arange(n+1) for n in grid.shape], shifts=[[0,0,0]])
g = gridlock.Grid([numpy.arange(n+1) for n in grid.shape], shifts=[[0, 0, 0]])
g.grids[0][:] = grid[:, :, :, 0]
g.visualize_isosurface()
@ -421,10 +420,10 @@ def visualize_voxels(grid):
def stack_split(opoly, plane_fun=None, perturb=0):
stack = [(opoly, 0)]
i=0
i = 0
# recursively split the poly
while stack:
i+=1
i += 1
opoly, depth = stack.pop()
# generate a random plane from one of a few
@ -471,7 +470,7 @@ def stack_split(opoly, plane_fun=None, perturb=0):
stack.append((poly1, depth + 1))
if m2[0] > MIN_VOL:
stack.append((poly2, depth + 1))
if i % 100 == 0:
if i % 300 == 0:
logger.info(f'Stack split iter {i} length {len(stack)} depth {depth}')
@ -493,12 +492,11 @@ def test_poly_pyramid():
]
# initialize a general polyhedron
opoly = pyr3d.init_poly(vertices, faces)
print(opoly)
pyr3d.is_good(opoly)
opoly = build_polyhedron(vertices, faces)
check_poly(opoly)
stack_split(opoly)
brep = pyr3d.into_boundary(opoly)
brep = boundary_from_poly(opoly)
logger.debug(f'Round-trip converted pyramid: {brep}')
#plot_brep(brep)
@ -543,7 +541,7 @@ def test_torus_load_and_chop():
# Recursively splits a polyhedron (starting from a torus) with a cut plane
# that is always degenerate with the poly in some way,
# checking to see that the resulting volumes add up properly.
# This checks non-convex geometry, and also r3d_init_poly() when more than
# This checks non-convex geometry, and also build_polyhedron() when more than
# three edges per vertex are needed.
n_th = 20
@ -553,16 +551,16 @@ def test_torus_load_and_chop():
# make vertices
th, phi = numpy.meshgrid(
numpy.linspace(0, 2*pi, n_th, endpoint=False),
numpy.linspace(0, 2*pi, n_phi, endpoint=False),
)
numpy.linspace(0, 2*pi, n_th, endpoint=False),
numpy.linspace(0, 2*pi, n_phi, endpoint=False),
)
th, phi = th.ravel(order='F'), phi.ravel(order='F')
vertices = numpy.column_stack((
(r_maj + r_min * cos(th)) * cos(phi),
(r_maj + r_min * cos(th)) * sin(phi),
r_min * sin(th),
))
(r_maj + r_min * cos(th)) * cos(phi),
(r_maj + r_min * cos(th)) * sin(phi),
r_min * sin(th),
))
# shift away from the origin so that none of the moments are identically zero
vertices += (0.5, 0.7, 0.9)
@ -582,11 +580,11 @@ def test_torus_load_and_chop():
# initialize a general polyhedron
opoly = pyr3d.init_poly(vertices, faces)
# pyr3d.is_good(opoly)
opoly = build_polyhedron(vertices, faces)
# check_poly(opoly)
stack_split(opoly)
brep = pyr3d.into_boundary(opoly)
brep = boundary_from_poly(opoly)
logger.debug(f'Round-trip converted torus: {brep}')
#plot_brep(brep)