MCPcopy Create free account
hub / github.com/PyMesh/PyMesh / quad_to_tri

Function quad_to_tri

python/pymesh/meshutils/quad_to_tri.py:4–62  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

2from ..meshio import form_mesh
3
4def 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)

Callers 2

test_single_quadMethod · 0.90

Calls 7

form_meshFunction · 0.85
enumerateFunction · 0.85
get_face_attributeMethod · 0.80
has_attributeMethod · 0.45
get_attributeMethod · 0.45
add_attributeMethod · 0.45
set_attributeMethod · 0.45

Tested by 2

test_single_quadMethod · 0.72