Triangulate a polygonal mesh. Args: faces (np.ndarray): [L, P] polygonal faces vertices (np.ndarray, optional): [N, 3] 3-dimensional vertices. If given, the triangulation is performed according to the distance between vertices. Defaults to None.
(
faces: np.ndarray,
vertices: np.ndarray = None,
backslash: np.ndarray = None
)
| 19 | |
| 20 | |
| 21 | def triangulate( |
| 22 | faces: np.ndarray, |
| 23 | vertices: np.ndarray = None, |
| 24 | backslash: np.ndarray = None |
| 25 | ) -> np.ndarray: |
| 26 | """ |
| 27 | Triangulate a polygonal mesh. |
| 28 | |
| 29 | Args: |
| 30 | faces (np.ndarray): [L, P] polygonal faces |
| 31 | vertices (np.ndarray, optional): [N, 3] 3-dimensional vertices. |
| 32 | If given, the triangulation is performed according to the distance |
| 33 | between vertices. Defaults to None. |
| 34 | backslash (np.ndarray, optional): [L] boolean array indicating |
| 35 | how to triangulate the quad faces. Defaults to None. |
| 36 | |
| 37 | Returns: |
| 38 | (np.ndarray): [L * (P - 2), 3] triangular faces |
| 39 | """ |
| 40 | if faces.shape[-1] == 3: |
| 41 | return faces |
| 42 | P = faces.shape[-1] |
| 43 | if vertices is not None: |
| 44 | assert faces.shape[-1] == 4, "now only support quad mesh" |
| 45 | if backslash is None: |
| 46 | backslash = np.linalg.norm(vertices[faces[:, 0]] - vertices[faces[:, 2]], axis=-1) < \ |
| 47 | np.linalg.norm(vertices[faces[:, 1]] - vertices[faces[:, 3]], axis=-1) |
| 48 | if backslash is None: |
| 49 | loop_indice = np.stack([ |
| 50 | np.zeros(P - 2, dtype=int), |
| 51 | np.arange(1, P - 1, 1, dtype=int), |
| 52 | np.arange(2, P, 1, dtype=int) |
| 53 | ], axis=1) |
| 54 | return faces[:, loop_indice].reshape((-1, 3)) |
| 55 | else: |
| 56 | assert faces.shape[-1] == 4, "now only support quad mesh" |
| 57 | faces = np.where( |
| 58 | backslash[:, None], |
| 59 | faces[:, [0, 1, 2, 0, 2, 3]], |
| 60 | faces[:, [0, 1, 3, 3, 1, 2]] |
| 61 | ).reshape((-1, 3)) |
| 62 | return faces |
| 63 | |
| 64 | |
| 65 | @batched(2, None) |