|
| 1 | +from compas.colors import Color |
| 2 | +from compas.datastructures import Mesh |
| 3 | +from compas_viewer import Viewer |
| 4 | +from compas_viewer.config import Config |
| 5 | +from tessagon.adaptors.list_adaptor import ListAdaptor |
| 6 | +from tessagon.types.brick_tessagon import BrickTessagon |
| 7 | +from tessagon.types.dissected_hex_quad_tessagon import DissectedHexQuadTessagon |
| 8 | +from tessagon.types.dissected_hex_tri_tessagon import DissectedHexTriTessagon |
| 9 | +from tessagon.types.dissected_square_tessagon import DissectedSquareTessagon |
| 10 | +from tessagon.types.dissected_triangle_tessagon import DissectedTriangleTessagon |
| 11 | +from tessagon.types.dodeca_tessagon import DodecaTessagon |
| 12 | +from tessagon.types.floret_tessagon import FloretTessagon |
| 13 | +from tessagon.types.hex_big_tri_tessagon import HexBigTriTessagon |
| 14 | + |
| 15 | +# Import all tessagon types |
| 16 | +from tessagon.types.hex_tessagon import HexTessagon |
| 17 | +from tessagon.types.hex_tri_tessagon import HexTriTessagon |
| 18 | +from tessagon.types.octo_tessagon import OctoTessagon |
| 19 | +from tessagon.types.pythagorean_tessagon import PythagoreanTessagon |
| 20 | +from tessagon.types.rhombus_tessagon import RhombusTessagon |
| 21 | +from tessagon.types.square_tessagon import SquareTessagon |
| 22 | +from tessagon.types.square_tri_tessagon import SquareTriTessagon |
| 23 | +from tessagon.types.tri_tessagon import TriTessagon |
| 24 | +from tessagon.types.weave_tessagon import WeaveTessagon |
| 25 | +from tessagon.types.zig_zag_tessagon import ZigZagTessagon |
| 26 | + |
| 27 | +import compas_libigl as igl |
| 28 | + |
| 29 | +# Dictionary of tessagon classes |
| 30 | +TESSAGON_TYPES = { |
| 31 | + 1: ("Hex", HexTessagon), |
| 32 | + 2: ("Tri", TriTessagon), |
| 33 | + 3: ("Octo", OctoTessagon), |
| 34 | + 4: ("Square", SquareTessagon), |
| 35 | + 5: ("Rhombus", RhombusTessagon), |
| 36 | + 6: ("HexTri", HexTriTessagon), |
| 37 | + 7: ("DissectedSquare", DissectedSquareTessagon), |
| 38 | + 8: ("DissectedTriangle", DissectedTriangleTessagon), |
| 39 | + 9: ("DissectedHexQuad", DissectedHexQuadTessagon), |
| 40 | + 10: ("DissectedHexTri", DissectedHexTriTessagon), |
| 41 | + 11: ("Floret", FloretTessagon), |
| 42 | + 12: ("Pythagorean", PythagoreanTessagon), |
| 43 | + 13: ("Brick", BrickTessagon), |
| 44 | + 14: ("Weave", WeaveTessagon), |
| 45 | + 15: ("ZigZag", ZigZagTessagon), |
| 46 | + 16: ("HexBigTri", HexBigTriTessagon), |
| 47 | + 17: ("Dodeca", DodecaTessagon), |
| 48 | + 18: ("SquareTri", SquareTriTessagon), |
| 49 | +} |
| 50 | + |
| 51 | +# Pattern selection - change this value to use a different pattern |
| 52 | +PATTERN_TYPE = 15 |
| 53 | + |
| 54 | +# ============================================================================== |
| 55 | +# Input geometry: 3D Mesh |
| 56 | +# ============================================================================== |
| 57 | + |
| 58 | +mesh = Mesh.from_obj("data/minimal_surface.obj") |
| 59 | +for key, attr in mesh.vertices(True): |
| 60 | + y = attr["y"] |
| 61 | + attr["y"] = -attr["z"] |
| 62 | + attr["z"] = y |
| 63 | +mesh.translate([2, 2, 0.5]) |
| 64 | + |
| 65 | +v, f = mesh.to_vertices_and_faces() |
| 66 | + |
| 67 | + |
| 68 | +# ============================================================================== |
| 69 | +# Input geometry: 2D Pattern creation using Tessagon library, can be other mesh. |
| 70 | +# ============================================================================== |
| 71 | + |
| 72 | +options = { |
| 73 | + "function": lambda u, v: [u, v, 0], |
| 74 | + "u_range": [-0.25, 1.25], |
| 75 | + "v_range": [-0.25, 1.25], |
| 76 | + "u_num": 20, |
| 77 | + "v_num": 20, |
| 78 | + "u_cyclic": False, |
| 79 | + "v_cyclic": False, |
| 80 | + "adaptor_class": ListAdaptor, |
| 81 | +} |
| 82 | + |
| 83 | +# Get pattern name and class based on selected pattern type |
| 84 | +pattern_name, pattern_class = TESSAGON_TYPES[PATTERN_TYPE] |
| 85 | + |
| 86 | +# Create the selected tessagon pattern |
| 87 | +tessagon = pattern_class(**options) |
| 88 | +tessagon_mesh = tessagon.create_mesh() |
| 89 | +pv = tessagon_mesh["vert_list"] |
| 90 | +pf = tessagon_mesh["face_list"] |
| 91 | + |
| 92 | +# ============================================================================== |
| 93 | +# Mapping: 3D Mesh, 2D Pattern, UV |
| 94 | +# ============================================================================== |
| 95 | + |
| 96 | +mv, mf = igl.map_mesh((v, f), (pv, pf)) |
| 97 | +mesh_mapped = Mesh.from_vertices_and_faces(mv, mf) |
| 98 | + |
| 99 | +# ============================================================================== |
| 100 | +# Viewer |
| 101 | +# ============================================================================== |
| 102 | + |
| 103 | + |
| 104 | +config = Config() |
| 105 | +config.camera.target = [2, 2, 0.25] |
| 106 | +config.camera.position = [5, 2, 1.5] |
| 107 | +# config.camera.scale = 100 |
| 108 | + |
| 109 | +viewer = Viewer(config=config) |
| 110 | +viewer.scene.add(mesh, name="mesh", show_faces=False, linecolor=Color.grey(), opacity=0.2) |
| 111 | +viewer.scene.add(Mesh.from_vertices_and_faces(pv, pf), name="pattern2d") |
| 112 | +viewer.scene.add(mesh_mapped, name="mesh_mapped", facecolor=Color.red()) |
| 113 | + |
| 114 | +# To see where the pattern is mapped: |
| 115 | +uv = igl.trimesh_lscm((v, f)) |
| 116 | +mesh_flattened = mesh.copy() |
| 117 | +for i in range(mesh.number_of_vertices()): |
| 118 | + mesh_flattened.vertex_attributes(i, "xyz", [uv[i][0], uv[i][1], 0]) |
| 119 | + |
| 120 | +viewer.scene.add(mesh_flattened, name="mesh_flattened") |
| 121 | +viewer.show() |
0 commit comments