From 702074b4f9d533a341551a90447b10c899675474 Mon Sep 17 00:00:00 2001 From: jan Date: Wed, 28 Mar 2018 15:37:23 -0700 Subject: [PATCH] voxel fix --- chop/__init__.py | 1 + tpy_r3d.py | 252 +++++++++++++++++++++++------------------------ 2 files changed, 126 insertions(+), 127 deletions(-) diff --git a/chop/__init__.py b/chop/__init__.py index 286eeaf..bce2b1c 100644 --- a/chop/__init__.py +++ b/chop/__init__.py @@ -4,4 +4,5 @@ from .split import split, clip from .moments import get_volume, get_moments, number_of_moments from .boundary import build_polyhedron, boundary_from_poly from .shapes import build_tetrahedron, build_box, planes_from_tetrahedron, planes_from_box +from .voxel import voxelize diff --git a/tpy_r3d.py b/tpy_r3d.py index 79e53a7..232383f 100644 --- a/tpy_r3d.py +++ b/tpy_r3d.py @@ -7,12 +7,10 @@ import numpy from numpy import sin, cos, pi from numpy.linalg import norm -from chop.moments import number_of_moments, get_moments -from chop.split import split, clip -from chop.shapes import build_tetrahedron, build_box, planes_from_tetrahedron -from chop.boundary import build_polyhedron, boundary_from_poly +from chop import (number_of_moments, get_moments, split, clip, build_tetrahedron, + build_box, planes_from_tetrahedron, build_polyhedron, boundary_from_poly, + voxelize) from chop.util import check_poly -from chop.voxel import voxelize try: # Check if running with line_profiler @@ -36,128 +34,6 @@ MIN_VOL = 1e-8 POLY_ORDER = 2 -def tetrahedron_volume(vertices): - ad, bd, cd = vertices[0:3] - vertices[3] - return -ad @ numpy.cross(bd, cd) / 6 - - -def flip_plane(plane): - return (-plane[0], -plane[1]) - - -def check_split_moments(original_poly, left_poly, right_poly, poly_order=0): - m0, ml, mr = (get_moments(x, poly_order) for x in (original_poly, left_poly, right_poly)) - logger.debug(f'Check: original {m0[0]} parts {(ml[0], mr[0])} sum {ml[0] + mr[0]}') - - # Make sure each part is smaller than the whole - if not ml[0] < m0[0] * (1.0 + TOL_WARN): - warnings.warn('ml[0] < m0[0]') - assert(ml[0] < m0[0] * (1.0 + TOL_FAIL)) - if not mr[0] < m0[0] * (1.0 + TOL_WARN): - warnings.warn('mr[0] < m0[0]') - assert(mr[0] < m0[0] * (1.0 + TOL_FAIL)) - - # make sure the sum of moments equals the original - diff = numpy.abs(m0 - (ml + mr)) - if not numpy.all(diff < TOL_FAIL): - logger.error(f'diff failed: {diff} \n original {m0} \n left {ml} \n right {mr}') - assert(False) - if not numpy.all(diff < TOL_WARN): - warnings.warn('Moments outside tolerance') - - return m0, ml, mr - - -def plane_centroid(poly): - # make a randomly-oriented plane passing through the centroid - centroid = get_centroid_3d(poly) - n = rand_unit_vector() - d = -n @ centroid - return n, d - - -def plane_face(poly): - # make a plane coplanar with a face of the poly - v0 = numpy.random.randint(poly['vertices'].shape[0]) - v1, v2 = random.sample(list(poly['neighbors'][v0]), 2) - points = (poly['vertices'][vi] for vi in (v0, v1, v2)) - return plane_points(*points) - - -def plane_centroid_edge(poly): - # make a plane coplanar with an edge and the centroid of the poly - v0 = numpy.random.randint(poly['vertices'].shape[0]) - v1 = random.choice(poly['neighbors'][v0]) - p0 = poly['vertices'][v0] - p1 = poly['vertices'][v1] - centroid = get_centroid_3d(poly) - return plane_points(p0, p1, centroid) - - -def plane_edge(poly): - # make a plane coplanar with an edge, but otherwise randomly oriented - v0 = numpy.random.randint(poly['vertices'].shape[0]) - v1 = random.choice(poly['neighbors'][v0]) - p0 = poly['vertices'][v0] - p1 = poly['vertices'][v1] - return plane_points(p0, p1, rand_unit_vector()) - - -def plane_centroid_vertex(poly): - # make a plane coplanar with a vertex and the centroid, but otherwise randomly oriented - p0 = random.choice(poly['vertices']) - centroid = get_centroid_3d(poly) - return plane_points(p0, centroid, rand_unit_vector()) - - -def plane_vertex(poly): - # make a plane coplanar with a vertex, but otherwise randomly oriented - p0 = random.choice(poly['vertices']) - n = rand_unit_vector() - d = -n @ p0 - return n, d - - -def plane_points(p0, p1, p2): - # generate a plane passing through the three points given - n = numpy.cross(p1 - p0, p2 - p0) - n /= norm(n) + 1e-40 - d = -n @ p0 - return n, d - - -def rand_unit_vector(): - # generates a random, isotropically distributed unit vector - s = 0 - while s == 0: - v = numpy.random.randn(3) - s = norm(v) - return v / s - - -def rand_tetrahedron_vertices(min_volume: float = MIN_VOL): - # generates a random tetrahedron with vertices on the unit sphere, - # guaranteeing a volume of at least min_volume - volume = 0 - while volume < min_volume: - verts = [rand_unit_vector() for _ in range(4)] - volume = tetrahedron_volume(verts) - if volume < 0: - verts[2], verts[3] = verts[3], verts[2] - volume = -volume - return verts - - -def build_rand_tetrahedron(min_volume: float = MIN_VOL): - return build_tetrahedron(rand_tetrahedron_vertices()) - - -def get_centroid_3d(poly): - # get the "centroid" by averaging vertices - centroid = numpy.sum(poly['vertices'], axis=0) / poly['vertices'].shape[0] - return centroid - - def test_split_tet_manual(): tet = build_tetrahedron([ [0,0,0], @@ -522,6 +398,128 @@ def test_torus_load_and_chop(): #plot_brep(brep) +def tetrahedron_volume(vertices): + ad, bd, cd = vertices[0:3] - vertices[3] + return -ad @ numpy.cross(bd, cd) / 6 + + +def flip_plane(plane): + return (-plane[0], -plane[1]) + + +def check_split_moments(original_poly, left_poly, right_poly, poly_order=0): + m0, ml, mr = (get_moments(x, poly_order) for x in (original_poly, left_poly, right_poly)) + logger.debug(f'Check: original {m0[0]} parts {(ml[0], mr[0])} sum {ml[0] + mr[0]}') + + # Make sure each part is smaller than the whole + if not ml[0] < m0[0] * (1.0 + TOL_WARN): + warnings.warn('ml[0] < m0[0]') + assert(ml[0] < m0[0] * (1.0 + TOL_FAIL)) + if not mr[0] < m0[0] * (1.0 + TOL_WARN): + warnings.warn('mr[0] < m0[0]') + assert(mr[0] < m0[0] * (1.0 + TOL_FAIL)) + + # make sure the sum of moments equals the original + diff = numpy.abs(m0 - (ml + mr)) + if not numpy.all(diff < TOL_FAIL): + logger.error(f'diff failed: {diff} \n original {m0} \n left {ml} \n right {mr}') + assert(False) + if not numpy.all(diff < TOL_WARN): + warnings.warn('Moments outside tolerance') + + return m0, ml, mr + + +def plane_centroid(poly): + # make a randomly-oriented plane passing through the centroid + centroid = get_centroid(poly) + n = rand_unit_vector() + d = -n @ centroid + return n, d + + +def plane_face(poly): + # make a plane coplanar with a face of the poly + v0 = numpy.random.randint(poly['vertices'].shape[0]) + v1, v2 = random.sample(list(poly['neighbors'][v0]), 2) + points = (poly['vertices'][vi] for vi in (v0, v1, v2)) + return plane_points(*points) + + +def plane_centroid_edge(poly): + # make a plane coplanar with an edge and the centroid of the poly + v0 = numpy.random.randint(poly['vertices'].shape[0]) + v1 = random.choice(poly['neighbors'][v0]) + p0 = poly['vertices'][v0] + p1 = poly['vertices'][v1] + centroid = get_centroid(poly) + return plane_points(p0, p1, centroid) + + +def plane_edge(poly): + # make a plane coplanar with an edge, but otherwise randomly oriented + v0 = numpy.random.randint(poly['vertices'].shape[0]) + v1 = random.choice(poly['neighbors'][v0]) + p0 = poly['vertices'][v0] + p1 = poly['vertices'][v1] + return plane_points(p0, p1, rand_unit_vector()) + + +def plane_centroid_vertex(poly): + # make a plane coplanar with a vertex and the centroid, but otherwise randomly oriented + p0 = random.choice(poly['vertices']) + centroid = get_centroid(poly) + return plane_points(p0, centroid, rand_unit_vector()) + + +def plane_vertex(poly): + # make a plane coplanar with a vertex, but otherwise randomly oriented + p0 = random.choice(poly['vertices']) + n = rand_unit_vector() + d = -n @ p0 + return n, d + + +def plane_points(p0, p1, p2): + # generate a plane passing through the three points given + n = numpy.cross(p1 - p0, p2 - p0) + n /= norm(n) + 1e-40 + d = -n @ p0 + return n, d + + +def rand_unit_vector(): + # generates a random, isotropically distributed unit vector + s = 0 + while s == 0: + v = numpy.random.randn(3) + s = norm(v) + return v / s + + +def rand_tetrahedron_vertices(min_volume: float = MIN_VOL): + # generates a random tetrahedron with vertices on the unit sphere, + # guaranteeing a volume of at least min_volume + volume = 0 + while volume < min_volume: + verts = [rand_unit_vector() for _ in range(4)] + volume = tetrahedron_volume(verts) + if volume < 0: + verts[2], verts[3] = verts[3], verts[2] + volume = -volume + return verts + + +def build_rand_tetrahedron(min_volume: float = MIN_VOL): + return build_tetrahedron(rand_tetrahedron_vertices()) + + +def get_centroid(poly): + # get the centroid by averaging vertices + centroid = numpy.sum(poly['vertices'], axis=0) / poly['vertices'].shape[0] + return centroid + + plane_gen = [plane_centroid, plane_face, plane_centroid_edge, plane_edge, plane_centroid_vertex, plane_vertex]