Source code for physicsnemo.mesh.curvature.gaussian

# SPDX-FileCopyrightText: Copyright (c) 2023 - 2026 NVIDIA CORPORATION & AFFILIATES.
# SPDX-FileCopyrightText: All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

r"""Gaussian curvature computation for simplicial meshes.

Implements intrinsic Gaussian curvature using the angle-defect method. Works
for any codimension (intrinsic property).

- For 2D surfaces: :math:`K = k_1 \, k_2` where :math:`k_1, k_2` are
  principal curvatures.
- For 1D curves: :math:`K` represents the discrete turning angle.
- For 3D volumes: :math:`K` represents the volumetric angle defect.

Reference: Meyer et al. (2003), *Discrete Differential-Geometry Operators for
Triangulated 2-Manifolds*, §4.2 (Discrete Gaussian Curvature Operator), Eq. 9.
"""

from typing import TYPE_CHECKING

import torch
from jaxtyping import Float

from physicsnemo.mesh.curvature._angles import compute_angles_at_vertices
from physicsnemo.mesh.curvature._utils import compute_full_angle_n_sphere
from physicsnemo.mesh.geometry.dual_meshes import get_or_compute_dual_volumes_0

if TYPE_CHECKING:
    from physicsnemo.mesh.mesh import Mesh


[docs] def gaussian_curvature_vertices(mesh: "Mesh") -> Float[torch.Tensor, " n_points"]: r"""Compute intrinsic Gaussian curvature at mesh vertices. Uses the angle-defect formula from discrete differential geometry. For a vertex :math:`v` with incident cells :math:`\sigma \ni v` and interior angle :math:`\theta_\sigma(v)` at :math:`v` in each :math:`\sigma`, define the angle defect .. math:: \Theta(v) = \Theta_n - \sum_{\sigma \ni v} \theta_\sigma(v), where :math:`\Theta_n` is the full angle in an :math:`n`-dimensional manifold (e.g. :math:`2\pi` for surfaces). The discrete Gaussian curvature is then .. math:: K(v) = \frac{\Theta(v)}{|{\star}v|}, where :math:`|{\star}v|` is the dual 0-cell (Voronoi) volume at :math:`v`. This is an intrinsic measure of curvature that works for any codimension, as it depends only on distances measured within the manifold (Theorema Egregium). Signed curvature: - Positive: elliptic point (sphere-like). - Zero: flat/parabolic point (plane-like). - Negative: hyperbolic point (saddle-like). Parameters ---------- mesh : Mesh Input simplicial mesh (1D, 2D, or 3D manifold). Returns ------- Float[torch.Tensor, " n_points"] Signed Gaussian curvature at each vertex, shape ``(n_points,)``. For isolated vertices (no incident cells), curvature is ``NaN``. Examples -------- >>> from physicsnemo.mesh.primitives.surfaces import sphere_icosahedral >>> # Sphere of radius r has K = 1/r^2 everywhere >>> sphere_mesh = sphere_icosahedral.load(radius=2.0, subdivisions=3) >>> K = gaussian_curvature_vertices(sphere_mesh) >>> # K.mean() approx 0.25 (= 1 / 2.0^2) Notes ----- Satisfies the discrete Gauss-Bonnet theorem, .. math:: \sum_v K(v) \, |{\star}v| = 2 \pi \, \chi(M), where the sum is over vertices and :math:`\chi(M)` is the Euler characteristic. For a curve in 2D, the sign of :math:`K` follows the edge orientation, so it needs consistently oriented edges (one ending and one starting at each vertex), as :func:`~physicsnemo.mesh.repair.fix_orientation` gives. Where the two edges at a vertex are not, :math:`|K|` is returned. """ n_manifold_dims = mesh.n_manifold_dims ### Compute angle sums at each vertex angle_sums = compute_angles_at_vertices(mesh) # (n_points,) ### Compute full angle for this manifold dimension full_angle = compute_full_angle_n_sphere(n_manifold_dims) ### Compute angle defect # angle_defect = full_angle - sum_of_angles # Positive defect = positive curvature angle_defect = full_angle - angle_sums # (n_points,) ### Compute dual volumes (Voronoi areas) dual_volumes = get_or_compute_dual_volumes_0(mesh) # (n_points,) ### Compute Gaussian curvature # K = angle_defect / dual_volume # Isolated vertices (dual_volume = 0) divide by 1 here and get NaN below; # a floor on the positive dual areas would shrink K on small meshes dual_volumes_safe = torch.where(dual_volumes > 0, dual_volumes, 1.0) gaussian_curvature = angle_defect / dual_volumes_safe # Set isolated vertices (zero dual volume) to NaN gaussian_curvature = torch.where( dual_volumes > 0, gaussian_curvature, torch.full_like(gaussian_curvature, float("nan")), ) return gaussian_curvature
[docs] def gaussian_curvature_cells(mesh: "Mesh") -> Float[torch.Tensor, " n_cells"]: r"""Compute Gaussian curvature at cell centers. Averages the intrinsic vertex-based Gaussian curvature (angle defect) over each cell's vertices. This gives a cell-centered curvature field that is consistent with the vertex-based curvature and, like it, intrinsic. Its area-weighted sum satisfies discrete Gauss-Bonnet only approximately. Parameters ---------- mesh : Mesh Input simplicial mesh. Returns ------- Float[torch.Tensor, " n_cells"] Gaussian curvature at each cell, shape ``(n_cells,)``. Cells whose vertices all have ``NaN`` curvature are set to ``NaN``. Examples -------- >>> from physicsnemo.mesh.primitives.surfaces import sphere_icosahedral >>> sphere_mesh = sphere_icosahedral.load(subdivisions=2) >>> K_cells = gaussian_curvature_cells(sphere_mesh) >>> # Should be positive for sphere """ device = mesh.points.device n_cells = mesh.n_cells ### Handle empty mesh if n_cells == 0: return torch.zeros(0, dtype=mesh.points.dtype, device=device) ### Compute vertex Gaussian curvature (intrinsic, angle-defect based). # Route through the cached property so a previously-computed vertex curvature is # reused (and otherwise cached) instead of recomputed on every cell-curvature call. K_vertices = mesh.gaussian_curvature_vertices # (n_points,) ### Average vertex curvature to cell centers # For each cell, take the mean of its vertices' curvatures, ignoring NaN. cell_vertex_K = K_vertices[mesh.cells] # (n_cells, n_verts_per_cell) # Mask NaN vertices (isolated vertices with no incident cells) valid = ~torch.isnan(cell_vertex_K) # (n_cells, n_verts_per_cell) n_valid = valid.sum(dim=1) # (n_cells,) # Replace NaN with 0 for summation, then divide by count of valid vertices cell_vertex_K_safe = torch.where( valid, cell_vertex_K, torch.zeros_like(cell_vertex_K) ) K_sum = cell_vertex_K_safe.sum(dim=1) # (n_cells,) K_cells = K_sum / n_valid.clamp(min=1).to(K_sum.dtype) # Set cells with no valid vertices to NaN K_cells = torch.where( n_valid > 0, K_cells, torch.full_like(K_cells, float("nan")), ) ### Set boundary cells to NaN. # Boundary vertices have incomplete angular neighborhoods, giving # spuriously large curvature values. Cells that touch the boundary # inherit those values and should be excluded. from physicsnemo.mesh.boundaries._detection import get_boundary_vertices is_boundary_vertex = get_boundary_vertices(mesh) # (n_points,) cell_touches_boundary = is_boundary_vertex[mesh.cells].any(dim=1) # (n_cells,) K_cells = torch.where( cell_touches_boundary, torch.full_like(K_cells, float("nan")), K_cells, ) return K_cells