Convert quad mesh into triangles. Args: mesh (:class:`Mesh`): Input quad mesh. keep_symmetry (`boolean`): (optional) Whether to split quad symmetrically into triangles. Default is False. Returns: The resulting triangle mesh.
(mesh, keep_symmetry=False)
| 2 | from ..meshio import form_mesh |
| 3 | |
| 4 | def quad_to_tri(mesh, keep_symmetry=False): |
| 5 | """ |
| 6 | Convert quad mesh into triangles. |
| 7 | |
| 8 | Args: |
| 9 | mesh (:class:`Mesh`): Input quad mesh. |
| 10 | keep_symmetry (`boolean`): (optional) Whether to split quad |
| 11 | symmetrically into triangles. Default is False. |
| 12 | |
| 13 | Returns: |
| 14 | The resulting triangle mesh. |
| 15 | """ |
| 16 | assert(mesh.vertex_per_face == 4) |
| 17 | |
| 18 | quads = mesh.faces |
| 19 | if not keep_symmetry: |
| 20 | triangles = np.array([ |
| 21 | [[f[0], f[1], f[2]],[f[0], f[2], f[3]]] |
| 22 | for f in quads ]).reshape((-1, 3)) |
| 23 | quad_index = np.repeat(np.arange(len(quads), dtype=int), 2) |
| 24 | tri_mesh = form_mesh(mesh.vertices, triangles) |
| 25 | if mesh.has_attribute("corner_texture"): |
| 26 | uv = mesh.get_attribute("corner_texture").reshape((-1, 2), order="C") |
| 27 | uv = np.array([[ |
| 28 | uv[i*4,:], uv[i*4+1,:], uv[i*4+2], |
| 29 | uv[i*4,:], uv[i*4+2,:], uv[i*4+3]] for i in range(len(quads))]) |
| 30 | tri_mesh.add_attribute("corner_texture") |
| 31 | tri_mesh.set_attribute("corner_texture", uv.ravel(order="C")) |
| 32 | else: |
| 33 | num_vertices = mesh.num_vertices |
| 34 | mesh.add_attribute("face_centroid") |
| 35 | centers = mesh.get_face_attribute("face_centroid") |
| 36 | triangles = np.array([ |
| 37 | [ |
| 38 | [f[0], f[1], num_vertices+i], |
| 39 | [f[1], f[2], num_vertices+i], |
| 40 | [f[2], f[3], num_vertices+i], |
| 41 | [f[3], f[0], num_vertices+i], |
| 42 | ] for i,f in enumerate(quads) ]).reshape((-1, 3)) |
| 43 | vertices = np.vstack([mesh.vertices, centers]) |
| 44 | quad_index = np.repeat(np.arange(len(quads), dtype=int), 4) |
| 45 | tri_mesh = form_mesh(vertices, triangles) |
| 46 | if mesh.has_attribute("corner_texture"): |
| 47 | uv = mesh.get_attribute("corner_texture").reshape((-1, 4, 2), order="C") |
| 48 | center_uv = np.mean(uv, axis=1) |
| 49 | uv = uv.reshape((-1, 2)) |
| 50 | assert(len(center_uv == len(quads))) |
| 51 | uv = np.array([[ |
| 52 | uv[i*4 ,:], uv[i*4+1,:], center_uv[i,:], |
| 53 | uv[i*4+1,:], uv[i*4+2,:], center_uv[i,:], |
| 54 | uv[i*4+2,:], uv[i*4+3,:], center_uv[i,:], |
| 55 | uv[i*4+3,:], uv[i*4 ,:], center_uv[i,:]] |
| 56 | for i in range(len(quads)) ]) |
| 57 | tri_mesh.add_attribute("corner_texture") |
| 58 | tri_mesh.set_attribute("corner_texture", uv.ravel(order="C")) |
| 59 | |
| 60 | tri_mesh.add_attribute("cell_index") |
| 61 | tri_mesh.set_attribute("cell_index", quad_index) |