Skip to content

Offical repo for the paper "LATO.2: Factorized 3D Mesh Generation with Vertex and Topology Flow"

feat: using boundary edges to detect holes

lohhhha5f9d7a144cd1
diff --git a/scripts/e2e_inference.py b/scripts/e2e_inference.py
index aa8a73d..2366c25 100644
--- a/scripts/e2e_inference.py
+++ b/scripts/e2e_inference.py
@@ -9,7 +9,8 @@ Outputs into --out_dir:
Usage:
python scripts/e2e_inference.py --mesh_dir <dir> --out_dir outputs/e2e_run/<dir> \
[--vert_num 2000] [--cfg_strength 3.0] [--vflow_steps 24] [--tflow_steps 50] \
- [--render_azimuth 45 --render_elevation 30] [--no-fill_quad_rings]
+ [--render_azimuth 45 --render_elevation 30] \
+ [--no-fill_open_rings] [--max_ring_size 12]
"""
import argparse
@@ -113,14 +114,20 @@ def parse_args():
p.add_argument("--edge_threshold", type=float, default=0.0)
p.add_argument("--chunk_size", type=int, default=20000)
p.add_argument(
- "--fill_quad_rings",
+ "--fill_open_rings",
action=argparse.BooleanOptionalAction,
default=True,
help=(
- "post-process: split chordless 4-vertex rings into two triangles "
- "(pure topology, not the voxel support filter)"
+ "post-process: triangulate chordless rings of open edges (edges with "
+ "fewer than two faces) by minimum area, not the voxel support filter"
),
)
+ p.add_argument(
+ "--max_ring_size",
+ type=int,
+ default=12,
+ help="largest open-edge ring (in edges) that --fill_open_rings closes",
+ )
# conditioning render
p.add_argument("--render_azimuth", type=float, default=45.0)
p.add_argument("--render_elevation", type=float, default=30.0)
@@ -353,7 +360,13 @@ def main():
for k, b in enumerate(keep_idx):
name = batch["name"][b]
- faces = edges_to_faces(edges_list[k], lengths[k], args.fill_quad_rings)
+ faces = edges_to_faces(
+ edges_list[k],
+ lengths[k],
+ verts_list[k].numpy(),
+ fill_open_rings=args.fill_open_rings,
+ max_ring_size=args.max_ring_size,
+ )
if faces.shape[0] == 0:
n_no_topo += 1
logging.warning(
diff --git a/scripts/tflow_inference.py b/scripts/tflow_inference.py
index 01c177a..dffbcc0 100644
--- a/scripts/tflow_inference.py
+++ b/scripts/tflow_inference.py
@@ -7,7 +7,7 @@ Outputs into --out_dir:
Usage:
python scripts/tflow_inference.py --mesh_dir <dir> --out_dir outputs/tflow_run/<dir> \
- [--steps 50] [--no-use_cond] [--no-fill_quad_rings]
+ [--steps 50] [--no-use_cond] [--no-fill_open_rings] [--max_ring_size 12]
"""
import argparse
@@ -69,14 +69,20 @@ def parse_args():
p.add_argument("--edge_threshold", type=float, default=0.0)
p.add_argument("--chunk_size", type=int, default=20000)
p.add_argument(
- "--fill_quad_rings",
+ "--fill_open_rings",
action=argparse.BooleanOptionalAction,
default=True,
help=(
- "post-process: split chordless 4-vertex rings into two triangles "
- "(pure topology, not the voxel support filter)"
+ "post-process: triangulate chordless rings of open edges (edges with "
+ "fewer than two faces) by minimum area, not the voxel support filter"
),
)
+ p.add_argument(
+ "--max_ring_size",
+ type=int,
+ default=12,
+ help="largest open-edge ring (in edges) that --fill_open_rings closes",
+ )
p.add_argument(
"--save_voxel_field",
action=argparse.BooleanOptionalAction,
@@ -186,9 +192,15 @@ def main():
logging.warning(f"{name}: no known vertices; skipping.")
continue
edges = edges_list[b]
- faces = edges_to_faces(edges, num_vertices, args.fill_quad_rings)
-
verts_int = verts_list[b]
+ faces = edges_to_faces(
+ edges,
+ num_vertices,
+ verts_int.numpy(),
+ fill_open_rings=args.fill_open_rings,
+ max_ring_size=args.max_ring_size,
+ )
+
disp = (verts_int.numpy().astype(np.float64) + 0.5) / num_discrete - 0.5
if faces.shape[0] > 0:
diff --git a/utils/inference.py b/utils/inference.py
index 49f900c..12a13b0 100644
--- a/utils/inference.py
+++ b/utils/inference.py
@@ -61,42 +61,144 @@ def pad_verts(verts_list, device):
return verts, mask, lengths
-def triangulate_quad_rings(adj: np.ndarray) -> np.ndarray:
- # thinks meshflow@CVPR2026!
+def _shortest_open_ring(
+ u: int, v: int, open_nbr: list[set], nbr: list[set], max_size: int
+) -> list[int] | None:
+ # Shortest cycle of 4..max_size vertices through the open edge (u, v) that
+ # only walks open edges. Vertices adjacent to both u and v would put a chord
+ # on any such cycle, so they are excluded up front.
+ blocked = nbr[u] & nbr[v]
+ parent = {v: -1}
+ frontier = [v]
+ for _ in range(max_size - 1):
+ reached = []
+ for x in frontier:
+ for y in open_nbr[x]:
+ if y in parent or y in blocked:
+ continue
+ if y == u:
+ if x == v:
+ continue # the edge (u, v) itself
+ ring = [u]
+ while x != -1:
+ ring.append(x)
+ x = parent[x]
+ return ring
+ parent[y] = x
+ reached.append(y)
+ if not reached:
+ break
+ frontier = reached
+ return None
+
+
+def _is_chordless(ring: list[int], nbr: list[set]) -> bool:
+ n = len(ring)
+ for i in range(n - 2):
+ nbr_i = nbr[ring[i]]
+ # ring[0] and ring[-1] are ring-adjacent, not a chord
+ for j in range(i + 2, n - 1 if i == 0 else n):
+ if ring[j] in nbr_i:
+ return False
+ return True
+
+
+def _min_area_triangulation(ring: list[int], verts: np.ndarray) -> list[list[int]]:
+ # Minimum-area triangulation of a closed 3D vertex ring (O(n^3) DP). The cost
+ # is (number of zero-area triangles, total area) so that collinear ring
+ # vertices do not yield degenerate faces unless unavoidable.
+ n = len(ring)
+ p = np.asarray(verts, dtype=np.float64)[ring]
+ d = p[None, :, :] - p[:, None, :] # d[i, k] = p[k] - p[i]
+ area = 0.5 * np.linalg.norm(
+ np.cross(d[:, :, None, :], d[:, None, :, :]), axis=-1
+ ) # area[i, k, j] of triangle (i, k, j)
+ eps = 1e-9 * float(np.sum(np.ptp(p, axis=0) ** 2))
+ area = area.tolist()
+ cost = [[(0, 0.0)] * n for _ in range(n)]
+ split = [[-1] * n for _ in range(n)]
+ for span in range(2, n):
+ for i in range(n - span):
+ j = i + span
+ best = None
+ for k in range(i + 1, j):
+ a = area[i][k][j]
+ c = (
+ cost[i][k][0] + cost[k][j][0] + (a <= eps),
+ cost[i][k][1] + cost[k][j][1] + a,
+ )
+ if best is None or c < best:
+ best = c
+ split[i][j] = k
+ cost[i][j] = best
+ tris = []
+ stack = [(0, n - 1)]
+ while stack:
+ i, j = stack.pop()
+ if j - i < 2:
+ continue
+ k = split[i][j]
+ tris.append([ring[i], ring[k], ring[j]])
+ stack.append((i, k))
+ stack.append((k, j))
+ return tris
+
+
+def triangulate_open_rings(
+ adj: np.ndarray, faces: np.ndarray, verts: np.ndarray, max_ring_size: int = 12
+) -> np.ndarray:
+ # Hole filling on open edges, i.e. predicted edges with fewer than two
+ # incident faces (boundary edges and face-less wire edges). Chordless rings
+ # of 4..max_ring_size open edges are triangulated, smallest rings first, and
+ # face counts are updated after every fill so no edge is closed twice.
+ empty = np.empty((0, 3), dtype=np.int32)
num = int(adj.shape[0])
- if num < 4:
- return np.empty((0, 3), dtype=np.int32)
- nbr_sets = [set(np.nonzero(adj[v])[0].tolist()) for v in range(num)]
+ if num < 4 or max_ring_size < 4:
+ return empty
+ nbr = [set(np.nonzero(adj[v])[0].tolist()) for v in range(num)]
+ count: dict[tuple[int, int], int] = {}
+ for a, b, c in faces.tolist():
+ for x, y in ((a, b), (b, c), (c, a)):
+ key = (x, y) if x < y else (y, x)
+ count[key] = count.get(key, 0) + 1
+ open_nbr: list[set] = [set() for _ in range(num)]
+ open_edges = []
+ for u in range(num):
+ for v in sorted(nbr[u]):
+ if v > u and count.get((u, v), 0) < 2:
+ open_edges.append((u, v))
+ open_nbr[u].add(v)
+ open_nbr[v].add(u)
+
new_faces: list[list[int]] = []
- seen: set[tuple[int, int, int]] = set()
- for a in range(num):
- cand = sorted(v for v in nbr_sets[a] if v > a)
- n_cand = len(cand)
- if n_cand < 2:
- continue
- for i in range(n_cand):
- b = cand[i]
- set_b = nbr_sets[b]
- for j in range(i + 1, n_cand):
- d = cand[j]
- if d in set_b:
- continue
- for c in set_b & nbr_sets[d]:
- if c <= a or c in nbr_sets[a]:
- continue
- for tri in ([a, b, c], [a, c, d]):
- key = tuple(sorted(tri))
- if key in seen:
- continue
- seen.add(key)
- new_faces.append(tri)
+ for size in range(4, max_ring_size + 1):
+ for u, v in open_edges:
+ # a face-less edge can border two rings, hence the loop
+ while count.get((u, v), 0) < 2:
+ ring = _shortest_open_ring(u, v, open_nbr, nbr, size)
+ if ring is None or not _is_chordless(ring, nbr):
+ break
+ for tri in _min_area_triangulation(ring, verts):
+ new_faces.append(tri)
+ for x, y in ((tri[0], tri[1]), (tri[1], tri[2]), (tri[2], tri[0])):
+ key = (x, y) if x < y else (y, x)
+ count[key] = count.get(key, 0) + 1
+ nbr[x].add(y)
+ nbr[y].add(x)
+ if count[key] >= 2:
+ open_nbr[x].discard(y)
+ open_nbr[y].discard(x)
if not new_faces:
- return np.empty((0, 3), dtype=np.int32)
+ return empty
return np.asarray(new_faces, dtype=np.int32)
def edges_to_faces(
- edges: np.ndarray, num_valid: int, fill_quad_rings: bool
+ edges: np.ndarray,
+ num_valid: int,
+ verts: np.ndarray,
+ fill_open_rings: bool = True,
+ max_ring_size: int = 12,
) -> np.ndarray:
adj = np.zeros((num_valid, num_valid), dtype=bool)
if edges.shape[0] > 0:
@@ -115,8 +217,8 @@ def edges_to_faces(
if faces_list
else np.empty((0, 3), dtype=np.int32)
)
- if fill_quad_rings:
- quad = triangulate_quad_rings(adj)
- if quad.shape[0] > 0:
- faces = np.concatenate([faces, quad], axis=0) if faces.shape[0] else quad
+ if fill_open_rings:
+ fill = triangulate_open_rings(adj, faces, verts, max_ring_size)
+ if fill.shape[0] > 0:
+ faces = np.concatenate([faces, fill], axis=0) if faces.shape[0] else fill
return faces