def run(self): self.geo = None geo = self.get_input_geometry_ref(0) if geo is None: return if geo.getNumVertexes() == 0: return pts = [Point_3(*i) for i in geo.getVertexes()] res = Polyhedron_3() CGAL_Convex_hull_3.convex_hull_3(pts, res) pts = {} [pts.update({v: idx}) for idx, v in enumerate(res.vertices())] ff = [] for f in res.facets(): he = f.halfedge() done = he faces = [] while True: faces.append(pts[he.vertex()]) he = he.next() if he == done: break ff.append(faces) self.geo = Mesh() self.geo.addVertices([[i.x(), i.y(), i.z()] for i in res.points()]) self.geo.addFaces(ff)
def convex_hull_3d(): """Convex hull for three dimensions This is not implemented in CGAL swig """ from CGAL.CGAL_Kernel import Point_3 from CGAL.CGAL_Kernel import Plane_3 from CGAL import CGAL_Convex_hull_3 from CGAL.CGAL_Polyhedron_3 import Polyhedron_3 pts = [] pts.append(Point_3(0, 0, 0)) pts.append(Point_3(0, 1, 0)) pts.append(Point_3(1, 1, 0)) pts.append(Point_3(1, 0, 0)) pts.append(Point_3(0, 0, 1)) pts.append(Point_3(0, 1, 1)) pts.append(Point_3(1, 1, 1)) pts.append(Point_3(1, 0, 1)) res = Polyhedron_3() CGAL_Convex_hull_3.convex_hull_3(pts, res) print('Convex hull has {} vertices'.format(res.size_of_vertices())) print('is strongly convex: {}'.format(CGAL_Convex_hull_3.is_strongly_convex_3(res)))
def cgal_convexify_polyhedron(hrep): gen = np.array(cdd.Polyhedron(hrep).get_generators()) #If the polygon is empty or degenerate, return 0 if gen.shape[0] < 3: return 0 points = [Point_3(x, y, z) for x, y, z in gen[:, 1:]] poly = Polyhedron_3() convex_hull_3(points, poly) lines = [np.array([as_list(p)]) for p in poly.points()] return np.vstack(lines)
def test_combinatorial_repairing_functions(): print("Testing combinarial repairing functions...") P = Polyhedron_3() P.make_triangle(Point_3(0, 0, 0), Point_3(1, 0, 0), Point_3(0, 1, 0)) P.make_triangle(Point_3(1, 0, 0), Point_3(0, 0, 0), Point_3(0, -1, 0)) # stitch_borders CGAL_Polygon_mesh_processing.stitch_borders(P) # P.clear() h1 = P.make_triangle(Point_3(0, 0, 0), Point_3(1, 0, 0), Point_3(0, 1, 0)) h2 = P.make_triangle(Point_3(1, 0, 0), Point_3(0, 0, 0), Point_3(0, -1, 0)) h1 = h1.opposite() while (h1.vertex().point() != Point_3(1, 0, 0)): h1 = h1.next() h2 = h2.opposite() while (h2.vertex().point() != Point_3(0, 0, 0)): h2 = h2.next() hpairs = [] hpairs.append(Halfedge_pair(h1, h2)) CGAL_Polygon_mesh_processing.stitch_borders(P, hpairs) # polygon_soup_to_polygon_mesh P = Polyhedron_3() points = Point_3_Vector() points.reserve(3) points.append(Point_3(0, 0, 0)) points.append(Point_3(0, 1, 0)) points.append(Point_3(1, 0, 0)) polygons = Polygon_Vector() polygon = Int_Vector() polygon.reserve(3) polygon.append(0) polygon.append(1) polygon.append(2) polygons.append(polygon) CGAL_Polygon_mesh_processing.polygon_soup_to_polygon_mesh( points, polygons, P) assert (P.size_of_vertices() == 3) # remove_isolated_vertices (4.8) CGAL_Polygon_mesh_processing.remove_isolated_vertices(P)
def to_polyhedron(self): polyhedron_modifier = Polyhedron_modifier() polyhedron_modifier.begin_surface(len(self.vertices), len(self.faces)) for vertex in self.vertices[1:]: polyhedron_modifier.add_vertex(Point_3(vertex[0], vertex[1], vertex[2])) for face in self.faces[1:]: polyhedron_modifier.begin_facet() for vertex in face: polyhedron_modifier.add_vertex_to_facet(vertex - 1) polyhedron_modifier.end_facet() polyhedron = Polyhedron_3() polyhedron.delegate(polyhedron_modifier) return polyhedron
def _poly_from_mesh(self, mesh): """Refactoring out to make this independent.""" poly = Polyhedron_3() for facet in mesh: #Yes yes yes, I know, * magic, but it's _FASTER_ points = [Point_3(*point) for point in facet[0]] if len(points) == 3: poly.make_triangle(*points) else: raise RuntimeError, ( "Invalid point list for poly facet: %d sides" % len(points)) return poly
def refine_mesh(self): filepath_in = self.name + ".off" a = self.name + "_Refine" self.name = a filepath_out = self.name + ".off" P=Polyhedron_3(filepath_in) flist = [] for fh in P.facets(): flist.append(fh) outf = [] outv = [] CGAL_Polygon_mesh_processing.refine(P, flist, outf, outv) P.write_to_file(filepath_out) return P
def cgal_volume_convex(hrep): gen = np.array(cdd.Polyhedron(hrep).get_generators()) #If the polygon is empty or degenerate, return 0 if gen.shape[0] < 3: return 0 #p = np.vstack([gen, gen[0, :]]) p = gen points = [Point_3(x, y, z) for x, y, z in p[:, 1:]] poly = Polyhedron_3() convex_hull_3(points, poly) points = list(poly.points()) center = mult_cgal(reduce(add_cgal, points), 1 / float(len(points))) tetrahedrons = [tetrahedron_from_facet(f, center) for f in poly.facets()] return sum([abs(t.volume()) for t in tetrahedrons])
# declare a modifier interfacing the incremental_builder m = Polyhedron_modifier() # define a triangle m.begin_surface(3, 1) m.add_vertex(Point_3(0, 0, 0)) m.add_vertex(Point_3(0, 1, 0)) m.add_vertex(Point_3(1, 0.5, 0)) m.begin_facet() m.add_vertex_to_facet(0) m.add_vertex_to_facet(1) m.add_vertex_to_facet(2) m.end_facet() P = Polyhedron_3() # create the triangle in P P.delegate(m) print("(v,f,e) = ", P.size_of_vertices(), P.size_of_facets(), divmod(P.size_of_halfedges(), 2)[0]) # clear the modifier m.clear() # define another triangle, reusing vertices in the polyhedron m.begin_surface(1, 1, 0, ABSOLUTE_INDEXING) m.add_vertex(Point_3(-1, 0.5, 0)) m.begin_facet() m.add_vertex_to_facet(1) m.add_vertex_to_facet(0) m.add_vertex_to_facet(3)
from __future__ import print_function from CGAL.CGAL_Kernel import Point_3 from CGAL.CGAL_Kernel import Vector_3 from CGAL.CGAL_Kernel import Plane_3 from CGAL.CGAL_Kernel import Segment_3 from CGAL.CGAL_Polyhedron_3 import Polyhedron_3 from CGAL.CGAL_AABB_tree import AABB_tree_Polyhedron_3_Facet_handle p = Point_3(1.0, 0.0, 0.0) q = Point_3(0.0, 1.0, 0.0) r = Point_3(0.0, 0.0, 1.0) s = Point_3(0.0, 0.0, 0.0) polyhedron = Polyhedron_3() polyhedron.make_tetrahedron(p, q, r, s) # constructs AABB tree tree = AABB_tree_Polyhedron_3_Facet_handle(polyhedron.facets()) # constructs segment query a = Point_3(-0.2, 0.2, -0.2) b = Point_3(1.3, 0.2, 1.3) segment_query = Segment_3(a, b) # tests intersections with segment query if tree.do_intersect(segment_query): print("intersection(s)") else: print("no intersection") # computes #intersections with segment query
t2 = [0, 0, 0, 0, 0, 1, 0, 1, -1] t3 = [0, 0, 0, 0, 0, 1, 0, 2, -1] t4 = [0, 0, 0, 0, 0, 1, 0, 3, -1] triangles = [t1, t2, t3, t4] tree_tri = AABB_tree_Triangle_3_soup() tree_tri.insert_from_array(triangles) assert (tree_tri.size() == 4) assert (tree_seg.do_intersect(s)) test_insert_from_array() poly = Polyhedron_3() poly.make_tetrahedron(Point_3(0, 0, 0), Point_3(1, 0, 0), Point_3(0, 1, 0), Point_3(0, 0, 1)) tree = AABB_tree_Polyhedron_3_Facet_handle() lst = [] for f in poly.facets(): lst.append(f) tree.rebuild(lst) tree = AABB_tree_Polyhedron_3_Facet_handle(lst) print(tree.size()) lst = [] for f in poly.facets():
from CGAL.CGAL_Kernel import Point_3 from CGAL.CGAL_Kernel import Plane_3 from CGAL import CGAL_Convex_hull_3 from CGAL.CGAL_Polyhedron_3 import Polyhedron_3 pts = [] pts.append(Point_3(0, 0, 0)) pts.append(Point_3(0, 1, 0)) pts.append(Point_3(1, 1, 0)) pts.append(Point_3(1, 0, 0)) pts.append(Point_3(0, 0, 1)) pts.append(Point_3(0, 1, 1)) pts.append(Point_3(1, 1, 1)) pts.append(Point_3(1, 0, 1)) res = Polyhedron_3() CGAL_Convex_hull_3.convex_hull_3(pts, res) print("convex hull has ", res.size_of_vertices(), " vertices") print("is strongly convex: ", CGAL_Convex_hull_3.is_strongly_convex_3(res)) planes = [] planes.append(Plane_3(-1, 0, 0, 0)) planes.append(Plane_3(1, 0, 0, -1)) planes.append(Plane_3(0, -1, 0, 0)) planes.append(Plane_3(0, 1, 0, -1)) planes.append(Plane_3(0, 0, -1, 0)) planes.append(Plane_3(0, 0, 1, -1))
def to_polyhedron(self): return Polyhedron_3(self.filename)
from CGAL.CGAL_Polyhedron_3 import Polyhedron_3 from CGAL.CGAL_Mesh_3 import Mesh_3_Complex_3_in_triangulation_3 from CGAL.CGAL_Mesh_3 import Polyhedral_mesh_domain_3 from CGAL.CGAL_Mesh_3 import Mesh_3_parameters from CGAL.CGAL_Mesh_3 import Default_mesh_criteria from CGAL import CGAL_Mesh_3 import os datadir = os.environ.get('DATADIR', '../data') datafile = datadir + '/elephant.off' # Create input polyhedron polyhedron = Polyhedron_3(datafile) # Create domain domain = Polyhedral_mesh_domain_3(polyhedron) params = Mesh_3_parameters() # Mesh criteria (no cell_size set) criteria = Default_mesh_criteria() criteria.facet_angle(25).facet_size(0.15).facet_distance( 0.008).cell_radius_edge_ratio(3) # Mesh generation c3t3 = CGAL_Mesh_3.make_mesh_3(domain, criteria, params) # Output c3t3.output_to_medit("out_1.mesh") # Set tetrahedron size (keep cell_radius_edge), ignore facets new_criteria = Default_mesh_criteria() new_criteria.cell_radius_edge_ratio(3).cell_size(0.03)
def get_poly(): P = Polyhedron_3() P.make_tetrahedron(Point_3(1, 0, 0), Point_3(0, 0, 1), Point_3(0, 0, 0), Point_3(0, 1, 0)) return P
def AABB_polyhedron_facet_intersection(): """Facet intersection mode AABB (axis aligned bounding boxes) The AABB is a data structure for finding intersections We do the following: * Create a polyhedron * Create a segment and plane * Find the intersection of segment and plane with polyhedron """ from CGAL.CGAL_Kernel import Point_3 from CGAL.CGAL_Kernel import Vector_3 from CGAL.CGAL_Kernel import Plane_3 from CGAL.CGAL_Kernel import Segment_3 from CGAL.CGAL_Polyhedron_3 import Polyhedron_3 from CGAL.CGAL_AABB_tree import AABB_tree_Polyhedron_3_Facet_handle p = Point_3(1.0, 0.0, 0.0) q = Point_3(0.0, 1.0, 0.0) r = Point_3(0.0, 0.0, 1.0) s = Point_3(0.0, 0.0, 0.0) polyhedron = Polyhedron_3() polyhedron.make_tetrahedron(p, q, r, s) # construct the AABB tree tree = AABB_tree_Polyhedron_3_Facet_handle(polyhedron.facets()) # construct segment query a = Point_3(-0.2, 0.2, -0.2) b = Point_3(1.3, 0.2, 1.3) segment_query = Segment_3(a,b) # do the intersection of segment with polyhedron if tree.do_intersect(segment_query): print("Intersection") else: print("No intersection") # compute the number of intersections with the segment print(tree.number_of_intersected_primitives(segment_query), " intersection") # compute first intersection with segment intersection = tree.any_intersection(segment_query) if not intersection.empty(): # get the intersection object op = intersection.value() object = op[0] if object.is_Point_3(): print("intersection object is a point") # compute all intersections with the segment query primitives = [] tree.all_intersected_primitives(segment_query,primitives) # construct plane query vec = Vector_3(0, 0, 1.0) plane_query = Plane_3(Point_3(0, 0, 0.5), vec) # compute first intersection of tetrahedron and plane intersection = tree.any_intersection(plane_query) if not intersection.empty(): op = intersection.value() object = op[0] if object.is_Segment_3(): print("intersection object is a segment")