Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include <cmath>
#include <algorithm>
#include <limits>

namespace CGAL {
namespace Polygon_mesh_processing {
Expand Down Expand Up @@ -469,6 +470,62 @@ void discrete_mean_curvatures(const TriangleMesh& tmesh,
put(vcm, v, discrete_mean_curvature(v, tmesh, np));
}

#ifndef DOXYGEN_RUNNING
/**
* \ingroup PMP_measure_grp
*
* returns whether a vertex is a saddle vertex, that is whether the sum of the angles
* of the faces incident to `v` at `v` is greater than or equal to \f$ 2\pi \f$.
* Equivalently, `v` is a saddle vertex if its angle defect, and thus its discrete Gaussian
* curvature, is non-positive.
*
* The angle sum is computed using `CGAL::Polygon_mesh_processing::angle_sum()` and compared to \f$ 2\pi \f$
* with a small relative tolerance, so that a vertex in a flat region of the mesh is reported
* as a saddle vertex.
*
* @tparam PolygonMesh a model of `FaceGraph`
* @tparam NamedParameters a sequence of \ref bgl_namedparameters "Named Parameters"
*
* @param v the vertex being tested
* @param pmesh the polygon mesh to which `v` belongs
* @param np an optional sequence of \ref bgl_namedparameters "Named Parameters" among the ones listed below
*
* \cgalNamedParamsBegin
* \cgalParamNBegin{vertex_point_map}
* \cgalParamDescription{a property map associating points to the vertices of `pmesh`}
* \cgalParamType{a class model of `ReadablePropertyMap` with `boost::graph_traits<PolygonMesh>::%vertex_descriptor`
* as key type and `%Point_3` as value type}
* \cgalParamDefault{`boost::get(CGAL::vertex_point, pmesh)`}
* \cgalParamNEnd
*
* \cgalParamNBegin{geom_traits}
* \cgalParamDescription{an instance of a geometric traits class}
* \cgalParamType{The traits class must provide the nested functor `Compute_approximate_angle_3`,
* model of `Kernel::ComputeApproximateAngle_3`.}
* \cgalParamDefault{a \cgal kernel deduced from the point type, using `CGAL::Kernel_traits`}
* \cgalParamExtra{The geometric traits class must be compatible with the vertex point type.}
* \cgalParamNEnd
* \cgalNamedParamsEnd
*
* \warning This function involves trigonometry.
* \warning The result is not meaningful for border vertices, as only the angles of the faces incident to `v` are summed.
*
* \sa `CGAL::Polygon_mesh_processing::angle_sum()`
* \sa `CGAL::Polygon_mesh_processing::discrete_Gaussian_curvature()`
*/
template <typename PolygonMesh,
typename CGAL_NP_TEMPLATE_PARAMETERS>
bool is_saddle_vertex(typename boost::graph_traits<PolygonMesh>::vertex_descriptor v,
const PolygonMesh& pmesh,
const CGAL_NP_CLASS& np = parameters::default_values())
{
// angle_sum() returns a value in degrees
const double ang_sum = CGAL::to_double(angle_sum(v, pmesh, np));
const double bound = (1. - 100. * std::numeric_limits<double>::epsilon()) * 360.;
return (ang_sum >= bound);
}
#endif

} // namespace Polygon_mesh_processing
} // namespace CGAL

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/Surface_mesh.h>
#include <CGAL/Polyhedron_3.h>

#include <CGAL/Polygon_mesh_processing/curvature.h>
#include <CGAL/Polygon_mesh_processing/polygon_soup_to_polygon_mesh.h>

#include <boost/graph/graph_traits.hpp>

#include <array>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>

#define ABS_ERROR 1e-6

namespace PMP = CGAL::Polygon_mesh_processing;

typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef CGAL::Exact_predicates_exact_constructions_kernel EPECK;
typedef K::FT FT;
typedef CGAL::Surface_mesh<K::Point_3> SMesh;
typedef CGAL::Polyhedron_3<K> Polyhedron;
Expand Down Expand Up @@ -111,8 +117,148 @@ void test_angle_sums(const std::string mesh_path,
}
}

// Builds a triangle fan made of `n` triangles around a central vertex (the first vertex)
// located at the origin. The i-th vertex of the ring is at angle 2*i*pi/n, at distance `radius(i)`
// from the z axis, and at height `height(theta)`.
template <typename PolygonMesh, typename Kernel, typename RadiusFunctor, typename HeightFunctor>
void make_fan(PolygonMesh& pmesh, const std::size_t n,
const RadiusFunctor& radius, const HeightFunctor& height)
{
std::vector<typename Kernel::Point_3> points;
std::vector<std::array<std::size_t, 3> > faces;

points.emplace_back(0, 0, 0);
for(std::size_t i=0; i<n; ++i)
{
const double theta = 2 * CGAL_PI * double(i) / double(n);
const double r = radius(i);
points.emplace_back(r * std::cos(theta), r * std::sin(theta), height(theta));
faces.push_back({0, i+1, (i+1)%n + 1});
}

PMP::polygon_soup_to_polygon_mesh(points, faces, pmesh);
assert(is_valid_polygon_mesh(pmesh));
}

template <typename PolygonMesh, typename Kernel = K>
void test_saddle_fans()
{
typedef typename boost::graph_traits<PolygonMesh>::vertex_descriptor vertex_descriptor;

auto unit_radius = [](std::size_t) { return 1.; };
auto irregular_radius = [](std::size_t i) { return 1. + 0.37 * double((7*i) % 5); };

const std::size_t n = 12;

// flat
{
PolygonMesh pmesh;
make_fan<PolygonMesh, Kernel>(pmesh, n, unit_radius, [](double) { return 0.; });
assert(PMP::is_saddle_vertex(*vertices(pmesh).begin(), pmesh));
}
{
PolygonMesh pmesh;
make_fan<PolygonMesh, Kernel>(pmesh, n, irregular_radius, [](double) { return 0.; });
assert(PMP::is_saddle_vertex(*vertices(pmesh).begin(), pmesh));
}

// convex (cone)
{
PolygonMesh pmesh;
make_fan<PolygonMesh, Kernel>(pmesh, n, unit_radius, [](double) { return -0.5; });
assert(!PMP::is_saddle_vertex(*vertices(pmesh).begin(), pmesh));
}
{
PolygonMesh pmesh; // almost flat cone
make_fan<PolygonMesh, Kernel>(pmesh, n, unit_radius, [](double) { return 1e-3; });
assert(!PMP::is_saddle_vertex(*vertices(pmesh).begin(), pmesh));
}

// saddle and monkey saddle
{
PolygonMesh pmesh;
make_fan<PolygonMesh, Kernel>(pmesh, n, unit_radius, [](double t) { return 0.5 * std::cos(2*t); });
assert(PMP::is_saddle_vertex(*vertices(pmesh).begin(), pmesh));
}
{
PolygonMesh pmesh;
make_fan<PolygonMesh, Kernel>(pmesh, n, irregular_radius, [](double t) { return 0.5 * std::cos(3*t); });
const vertex_descriptor v = *vertices(pmesh).begin();
assert(PMP::is_saddle_vertex(v, pmesh));
assert(PMP::is_saddle_vertex(v, pmesh, CGAL::parameters::geom_traits(Kernel())
.vertex_point_map(get(CGAL::vertex_point, pmesh))));
}
}

template <typename PolygonMesh>
void test_saddle_vertices_of_closed_mesh(const std::string mesh_path)
{
std::cout << "test saddle vertices of " << mesh_path << std::endl;

PolygonMesh pmesh;
const std::filesystem::path filename = CGAL::data_file_path(mesh_path);

if(!CGAL::IO::read_polygon_mesh(filename, pmesh) || faces(pmesh).size() == 0)
{
std::cerr << "Invalid input file." << std::endl;
std::exit(1);
}

// all vertices of these convex polyhedra are strictly convex
for(auto v : vertices(pmesh))
assert(!PMP::is_saddle_vertex(v, pmesh));
}

template <typename PolygonMesh>
void test_saddle_vertices_of_quad_grid()
{
// 3x3 grid of planar quads: the 4 interior vertices are flat, the others are on the border
std::vector<K::Point_3> points;
std::vector<std::array<std::size_t, 4> > faces;
for(std::size_t j=0; j<4; ++j)
for(std::size_t i=0; i<4; ++i)
points.emplace_back(double(i), double(j), 0);
for(std::size_t j=0; j<3; ++j)
for(std::size_t i=0; i<3; ++i)
faces.push_back({4*j+i, 4*j+i+1, 4*(j+1)+i+1, 4*(j+1)+i});

PolygonMesh pmesh;
PMP::polygon_soup_to_polygon_mesh(points, faces, pmesh);

std::size_t nb_saddles = 0;
for(auto v : vertices(pmesh))
{
if(PMP::is_saddle_vertex(v, pmesh))
{
assert(!is_border(v, pmesh));
++nb_saddles;
}
}
assert(nb_saddles == 4);
}

void test_saddle_vertices()
{
std::cout << "test saddle vertices" << std::endl;

test_saddle_fans<SMesh>();
test_saddle_fans<Polyhedron>();
test_saddle_fans<CGAL::Surface_mesh<EPECK::Point_3>, EPECK>();

test_saddle_vertices_of_closed_mesh<SMesh>("meshes/regular_tetrahedron.off");
test_saddle_vertices_of_closed_mesh<Polyhedron>("meshes/regular_tetrahedron.off");
test_saddle_vertices_of_closed_mesh<SMesh>("meshes/cube_quad.off");
test_saddle_vertices_of_closed_mesh<Polyhedron>("meshes/cube_quad.off");
test_saddle_vertices_of_closed_mesh<SMesh>("meshes/sphere.off");

test_saddle_vertices_of_quad_grid<SMesh>();
test_saddle_vertices_of_quad_grid<Polyhedron>();
}

int main(int, char**)
{
test_saddle_vertices();

// testing on a simple sphere(r = 0.5), on both Polyhedron & SurfaceMesh:
// Expected: Mean Curvature = 2, Gaussian Curvature = 4
test_curvatures<Polyhedron>("meshes/sphere.off", Average_test_info(2, 4));
Expand Down
Loading