|
| 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 |
0 commit comments