|
| 1 | +from __future__ import absolute_import |
| 2 | +from __future__ import division |
| 3 | +from __future__ import print_function |
| 4 | + |
| 5 | +try: |
| 6 | + from triangle import triangulate |
| 7 | +except ImportError: |
| 8 | + pass |
| 9 | + |
| 10 | +from compas.utilities import pairwise |
| 11 | +from compas.geometry import centroid_points_xy |
| 12 | + |
| 13 | + |
| 14 | +__all__ = ['trimesh_remesh_triangle'] |
| 15 | + |
| 16 | + |
| 17 | +def trimesh_remesh_triangle(mesh, target, boundary=None, holes=None): |
| 18 | + segments = [] |
| 19 | + _holes = [] |
| 20 | + if boundary: |
| 21 | + segments += list(pairwise(boundary + boundary[:1])) |
| 22 | + # if holes: |
| 23 | + # for boundary in holes: |
| 24 | + # segments += list(pairwise(boundary + boundary[:1])) |
| 25 | + # _holes.append(centroid_points_xy(boundary)) |
| 26 | + tri = { |
| 27 | + 'vertices': list(mesh.get_vertices_attributes('xy')), |
| 28 | + 'segments': segments, |
| 29 | + 'holes' : _holes, |
| 30 | + } |
| 31 | + print(tri['vertices']) |
| 32 | + result = triangulate(tri, opts='rpa.5'.format(target)) |
| 33 | + print(result) |
| 34 | + vertices = result['vertices'].tolist() |
| 35 | + triangles = result['triangles'].tolist() |
| 36 | + cls = type(mesh) |
| 37 | + return cls.from_vertices_and_faces(vertices, triangles) |
| 38 | + |
| 39 | + |
| 40 | +# ============================================================================== |
| 41 | +# Main |
| 42 | +# ============================================================================== |
| 43 | + |
| 44 | +if __name__ == '__main__': |
| 45 | + |
| 46 | + from compas.datastructures import Mesh |
| 47 | + from compas.plotters import MeshPlotter |
| 48 | + |
| 49 | + vertices = [(0.0, 0.0, 0.0), (10.0, 0.0, 0.0), (6.0, 10.0, 0.0), (0.0, 10.0, 0.0)] |
| 50 | + faces = [[0, 1, 2, 3]] |
| 51 | + |
| 52 | + mesh = Mesh.from_vertices_and_faces(vertices, faces) |
| 53 | + key = mesh.insert_vertex(0) |
| 54 | + |
| 55 | + area = sum(mesh.face_area(fkey) for fkey in mesh.faces()) / mesh.number_of_faces() |
| 56 | + print(area) |
| 57 | + |
| 58 | + for fkey in mesh.faces(): |
| 59 | + print(len(mesh.face_vertices(fkey))) |
| 60 | + |
| 61 | + finer = trimesh_remesh_triangle(mesh, target=area/10.0) |
| 62 | + print(finer, key) |
| 63 | + |
| 64 | + plotter = MeshPlotter(finer, figsize=(10, 7)) |
| 65 | + plotter.draw_edges() |
| 66 | + plotter.show() |
0 commit comments