11from compas .datastructures import Mesh
2- from tessagon .types .hex_tessagon import HexTessagon
3- from tessagon .adaptors .list_adaptor import ListAdaptor
42from compas_viewer import Viewer
5- import compas_libigl as igl
6- from compas_libigl import _mapping
7- import numpy as np
3+ from tessagon . adaptors . list_adaptor import ListAdaptor
4+ from tessagon . types . hex_tessagon import HexTessagon
5+ from compas . colors import Color
86
9- viewer = Viewer ()
7+ import compas_libigl as igl
108
119# ==============================================================================
1210# Input geometry: 3D Mesh
1311# ==============================================================================
1412
15- # Load the mesh
1613mesh = Mesh .from_obj ("data/minimal_surface.obj" )
1714for key , attr in mesh .vertices (True ):
1815 y = attr ["y" ]
1916 attr ["y" ] = - attr ["z" ]
2017 attr ["z" ] = y
18+ mesh .translate ([2 , 2 , 0.5 ])
2119
2220v , f = mesh .to_vertices_and_faces ()
2321
24- viewer .scene .add (mesh , name = "mesh" )
2522
2623# ==============================================================================
2724# Input geometry: 2D Pattern creation using Tessagon library, can be other mesh.
4441
4542
4643# ==============================================================================
47- # Parametrization: choose between simple, lcsm and harmonic methods.
44+ # Place the pattern mesh at the center of the 3D mesh
45+ # ==============================================================================
46+ pattern2d = Mesh .from_vertices_and_faces (pv , pf )
47+ c = pattern2d .aabb ().corner (0 )
48+ pattern2d .translate ([- c [0 ], - c [1 ], - c [2 ]])
49+ pv , pf = pattern2d .to_vertices_and_faces ()
50+
51+
52+ # ==============================================================================
53+ # Parametrization
4854# ==============================================================================
4955
50- uv = igl .trimesh_simple ((v , f ))
51- # uv = igl.trimesh_lscm((v, f))
52- # uv = igl.trimesh_harmonic((v, f))
56+ uv = igl .trimesh_lscm ((v , f ))
5357
5458mesh_flattened = mesh .copy ()
5559mesh_flattened .vertices_attribute ("z" , 0 )
5660
5761for i in range (mesh .number_of_vertices ()):
5862 mesh_flattened .vertex_attributes (i , "xy" , uv [i ])
5963
60- viewer .scene .add (mesh_flattened , name = "mesh_flattened" )
61-
6264# ==============================================================================
63- # Mapping: 3D Mesh, 2D Pattern, UV
64- # Eigen::Ref<const compas::RowMatrixXd> v,
65- # Eigen::Ref<const compas::RowMatrixXi> f,
66- # Eigen::Ref<const compas::RowMatrixXd> uv,
67- # Eigen::Ref<compas::RowMatrixXd> pattern_v,
68- # Eigen::Ref<const compas::RowMatrixXi> pattern_f,
69- # Eigen::Ref<const compas::RowMatrixXd> pattern_uv,
70- # std::vector<std::vector<int>>& pattern_polygonal_faces
65+ # Rescale the flattened uv mesh to 1x1 scale
7166# ==============================================================================
67+ box = mesh_flattened .aabb ()
68+ c = box .corner (0 )
69+ mesh_flattened .translate ([- c [0 ], - c [1 ], - c [2 ]])
70+ mesh_flattened .scale (1.0 / box .xsize , 1.0 / box .ysize , 1 )
7271
73- v_numpy = np .array (v , dtype = np .float64 )
74- f_numpy = np .array (f , dtype = np .int32 )
75- uv_numpy = np .array (uv , dtype = np .float64 )
76- pattern_v_numpy = np .array (pv , dtype = np .float64 )
77- pattern_f_numpy = np .array (pf , dtype = np .int32 )
78- pattern_uv = igl .trimesh_simple ((pv , pf ))
79- pattern_uv_numpy = np .array (pattern_uv , dtype = np .float64 )
80- pattern_polygonal_faces = pf
81-
82- print ("Before mapping" )
83- print (pattern_v_numpy [0 ])
84- _mapping .mapMesh3D_AABB (v_numpy , f_numpy , uv_numpy , pattern_v_numpy , pattern_f_numpy , pattern_uv_numpy , pattern_polygonal_faces )
85- print ("After mapping" )
86-
87- for i in range (100 ):
88- print (pattern_v_numpy [i ])
89-
90- mapped_mesh = Mesh .from_vertices_and_faces (pattern_v_numpy , pattern_f_numpy )
91- viewer .scene .add (mapped_mesh , name = "mapped_mesh" )
72+ for i in range (mesh .number_of_vertices ()):
73+ uv [i ] = mesh_flattened .vertex_attributes (i , "xy" )
9274
75+ # ==============================================================================
76+ # Mapping: 3D Mesh, 2D Pattern, UV
77+ # ==============================================================================
9378
79+ p_uv = igl .trimesh_simple ((pv , pf ))
80+ mesh_mapped = igl .map_mesh (v , f , uv , pv , pf , p_uv )
9481
9582# ==============================================================================
9683# Viewer
9784# ==============================================================================
9885
99-
100- viewer .show ()
86+ viewer = Viewer ()
87+ viewer .scene .add (mesh , name = "mesh" , show_faces = False , linecolor = Color .grey (), opacity = 0.2 )
88+ viewer .scene .add (pattern2d , name = "pattern2d" )
89+ viewer .scene .add (mesh_flattened , name = "mesh_flattened" )
90+ viewer .scene .add (mesh_mapped , name = "mesh_mapped" , facecolor = Color .red ())
91+ viewer .show ()
0 commit comments