Skip to content

Commit 112959e

Browse files
gkioxarifacebook-github-bot
authored andcommitted
taubin smoothing
Summary: Taubin Smoothing for filtering meshes and making them smoother. Taubin smoothing is an iterative approach. Reviewed By: nikhilaravi Differential Revision: D24751149 fbshipit-source-id: fb779e955f1a1f6750e704f1b4c6dfa37aebac1a
1 parent fc7a4ca commit 112959e

File tree

3 files changed

+158
-0
lines changed

3 files changed

+158
-0
lines changed

pytorch3d/ops/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
wmean,
2424
)
2525
from .vert_align import vert_align
26+
from .mesh_filtering import taubin_smoothing
2627

2728

2829
__all__ = [k for k in globals().keys() if not k.startswith("_")]

pytorch3d/ops/mesh_filtering.py

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
2+
import torch
3+
from pytorch3d.structures import Meshes, utils as struct_utils
4+
5+
# ------------------------ Mesh Smoothing ------------------------ #
6+
# This file contains differentiable operators to filter meshes
7+
# The ops include
8+
# 1) Taubin Smoothing
9+
# TODO(gkioxari) add more! :)
10+
# ---------------------------------------------------------------- #
11+
12+
13+
# ----------------------- Taubin Smoothing ----------------------- #
14+
15+
16+
def norm_laplacian(verts: torch.Tensor, edges: torch.Tensor, eps: float = 1e-12):
17+
"""
18+
Norm laplacian computes a variant of the laplacian matrix which weights each
19+
affinity with the normalized distance of the neighboring nodes.
20+
More concretely,
21+
L[i, j] = 1. / wij where wij = ||vi - vj|| if (vi, vj) are neighboring nodes
22+
23+
Args:
24+
verts: tensor of shape (V, 3) containing the vertices of the graph
25+
edges: tensor of shape (E, 2) containing the vertex indices of each edge
26+
"""
27+
edge_verts = verts[edges] # (E, 2, 3)
28+
v0, v1 = edge_verts[:, 0], edge_verts[:, 1]
29+
30+
# Side lengths of each edge, of shape (E,)
31+
w01 = 1.0 / ((v0 - v1).norm(dim=1) + eps)
32+
33+
# Construct a sparse matrix by basically doing:
34+
# L[v0, v1] = w01
35+
# L[v1, v0] = w01
36+
e01 = edges.t() # (2, E)
37+
38+
V = verts.shape[0]
39+
L = torch.sparse.FloatTensor(e01, w01, (V, V))
40+
L = L + L.t()
41+
42+
return L
43+
44+
45+
def taubin_smoothing(
46+
meshes: Meshes, lambd: float = 0.53, mu: float = -0.53, num_iter: int = 10
47+
) -> Meshes:
48+
"""
49+
Taubin smoothing [1] is an iterative smoothing operator for meshes.
50+
At each iteration
51+
verts := (1 - λ) * verts + λ * L * verts
52+
verts := (1 - μ) * verts + μ * L * verts
53+
54+
This function returns a new mesh with smoothed vertices.
55+
Args:
56+
meshes: Meshes input to be smoothed
57+
lambd, mu: float parameters for Taubin smoothing,
58+
lambd > 0, mu < 0
59+
num_iter: number of iterations to execute smoothing
60+
Returns:
61+
mesh: Smoothed input Meshes
62+
63+
[1] Curve and Surface Smoothing without Shrinkage,
64+
Gabriel Taubin, ICCV 1997
65+
"""
66+
verts = meshes.verts_packed() # V x 3
67+
edges = meshes.edges_packed() # E x 3
68+
69+
for _ in range(num_iter):
70+
L = norm_laplacian(verts, edges)
71+
total_weight = torch.sparse.sum(L, dim=1).to_dense().view(-1, 1)
72+
verts = (1 - lambd) * verts + lambd * torch.mm(L, verts) / total_weight
73+
74+
# pyre-ignore
75+
L = norm_laplacian(verts, edges)
76+
total_weight = torch.sparse.sum(L, dim=1).to_dense().view(-1, 1)
77+
verts = (1 - mu) * verts + mu * torch.mm(L, verts) / total_weight
78+
79+
verts_list = struct_utils.packed_to_list(
80+
verts, meshes.num_verts_per_mesh().tolist()
81+
)
82+
mesh = Meshes(verts=list(verts_list), faces=meshes.faces_list())
83+
return mesh

tests/test_mesh_filtering.py

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
2+
3+
4+
import unittest
5+
6+
import torch
7+
from common_testing import TestCaseMixin, get_random_cuda_device
8+
from pytorch3d.ops import taubin_smoothing
9+
from pytorch3d.ops.mesh_filtering import norm_laplacian
10+
from pytorch3d.structures import Meshes
11+
from pytorch3d.utils import ico_sphere
12+
13+
14+
class TestTaubinSmoothing(TestCaseMixin, unittest.TestCase):
15+
def setUp(self) -> None:
16+
super().setUp()
17+
torch.manual_seed(1)
18+
19+
def test_taubin(self):
20+
N = 3
21+
device = get_random_cuda_device()
22+
23+
mesh = ico_sphere(4, device).extend(N)
24+
ico_verts = mesh.verts_padded()
25+
ico_faces = mesh.faces_padded()
26+
27+
rand_noise = torch.rand_like(ico_verts) * 0.2 - 0.1
28+
z_mask = (ico_verts[:, :, -1] > 0).view(N, -1, 1)
29+
rand_noise = rand_noise * z_mask
30+
verts = ico_verts + rand_noise
31+
mesh = Meshes(verts=verts, faces=ico_faces)
32+
33+
smooth_mesh = taubin_smoothing(mesh, num_iter=50)
34+
smooth_verts = smooth_mesh.verts_padded()
35+
36+
smooth_dist = (smooth_verts - ico_verts).norm(dim=-1).mean()
37+
dist = (verts - ico_verts).norm(dim=-1).mean()
38+
self.assertTrue(smooth_dist < dist)
39+
40+
def test_norm_laplacian(self):
41+
V = 32
42+
F = 64
43+
device = get_random_cuda_device()
44+
# random vertices
45+
verts = torch.rand((V, 3), dtype=torch.float32, device=device)
46+
# random valid faces (no self circles, e.g. (v0, v0, v1))
47+
faces = torch.stack([torch.randperm(V) for f in range(F)], dim=0)[:, :3]
48+
faces = faces.to(device=device)
49+
mesh = Meshes(verts=[verts], faces=[faces])
50+
edges = mesh.edges_packed()
51+
52+
eps = 1e-12
53+
54+
L = norm_laplacian(verts, edges, eps=eps)
55+
56+
Lnaive = torch.zeros((V, V), dtype=torch.float32, device=device)
57+
for f in range(F):
58+
f0, f1, f2 = faces[f]
59+
v0 = verts[f0]
60+
v1 = verts[f1]
61+
v2 = verts[f2]
62+
63+
w12 = 1.0 / ((v1 - v2).norm() + eps)
64+
w02 = 1.0 / ((v0 - v2).norm() + eps)
65+
w01 = 1.0 / ((v0 - v1).norm() + eps)
66+
67+
Lnaive[f0, f1] = w01
68+
Lnaive[f1, f0] = w01
69+
Lnaive[f0, f2] = w02
70+
Lnaive[f2, f0] = w02
71+
Lnaive[f1, f2] = w12
72+
Lnaive[f2, f1] = w12
73+
74+
self.assertClose(L.to_dense(), Lnaive)

0 commit comments

Comments
 (0)