1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
|
// Copyright (c) 2005 INRIA (France).
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org).
//
// $URL: https://github.com/CGAL/cgal/blob/v6.1/Surface_mesh_parameterization/include/CGAL/Surface_mesh_parameterization/Fixed_border_parameterizer_3.h $
// $Id: include/CGAL/Surface_mesh_parameterization/Fixed_border_parameterizer_3.h b26b07a1242 $
// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-Commercial
//
// Author(s) : Laurent Saboret, Pierre Alliez, Bruno Levy
#ifndef CGAL_SURFACE_MESH_PARAMETERIZATION_FIXED_BORDER_PARAMETERIZER_3_H
#define CGAL_SURFACE_MESH_PARAMETERIZATION_FIXED_BORDER_PARAMETERIZER_3_H
#include <CGAL/license/Surface_mesh_parameterization.h>
#include <CGAL/disable_warnings.h>
#include <CGAL/Surface_mesh_parameterization/internal/Containers_filler.h>
#include <CGAL/Surface_mesh_parameterization/internal/kernel_traits.h>
#include <CGAL/Surface_mesh_parameterization/Error_code.h>
#include <CGAL/Surface_mesh_parameterization/Circular_border_parameterizer_3.h>
#include <CGAL/Polygon_mesh_processing/connected_components.h>
#include <CGAL/boost/graph/properties.h>
#include <CGAL/circulator.h>
#include <CGAL/Default.h>
#if defined(CGAL_EIGEN3_ENABLED)
#include <CGAL/Eigen_solver_traits.h>
#endif
#include <boost/iterator/function_output_iterator.hpp>
#include <unordered_set>
#include <type_traits>
/// \file Fixed_border_parameterizer_3.h
namespace CGAL {
namespace Surface_mesh_parameterization {
// ------------------------------------------------------------------------------------
// Declaration
// ------------------------------------------------------------------------------------
/// \ingroup PkgSurfaceMeshParameterizationMethods
///
/// The class `Fixed_border_parameterizer_3`
/// is the base class of fixed border parameterization methods (Tutte, Floater, ...).
///
/// A one-to-one mapping is guaranteed if the border of the surface is mapped onto a convex polygon.
///
/// This class is a pure virtual class and thus cannot be instantiated.
/// Nevertheless, it implements most of the parameterization algorithm `parameterize()`.
/// Subclasses are *Strategies* \cgalCite{cgal:ghjv-dpero-95} that modify the behavior of this algorithm:
/// - They provide the template parameters `BorderParameterizer_` and `SolverTraits_`.
/// - They implement `compute_w_ij()` to compute `w_ij`, the `(i,j)`-coefficient of matrix `A`
/// for `j` neighbor vertex of `i`.
///
// @todo `Fixed_border_parameterizer_3` should remove border vertices
// from the linear systems in order to have a symmetric positive definite
// matrix for Tutte Barycentric Mapping and Discrete Conformal Map algorithms.
///
/// \cgalModels{Parameterizer_3}
///
/// \tparam TriangleMesh_ must be a model of `FaceGraph`.
///
/// \tparam BorderParameterizer_ is a strategy to parameterize the surface border
/// and must be a model of `Parameterizer_3`.<br>
/// <b>%Default:</b>
/// \code
/// Circular_border_arc_length_parameterizer_3<TriangleMesh_>
/// \endcode
///
/// \tparam SolverTraits_ must be a model of `SparseLinearAlgebraTraits_d`.<br>
/// Note that the system is *not* symmetric because `Fixed_border_parameterizer_3`
/// does not remove border vertices from the system.<br>
/// <b>%Default:</b> If \ref thirdpartyEigen "Eigen" 3.1 (or greater) is available
/// and `CGAL_EIGEN3_ENABLED` is defined, then an overload of `Eigen_solver_traits`
/// is provided as default parameter:
/// \code
/// CGAL::Eigen_solver_traits<
/// Eigen::BiCGSTAB<Eigen_sparse_matrix<double>::EigenType,
/// Eigen::IncompleteLUT< double > > >
/// \endcode
///
/// \sa `CGAL::Surface_mesh_parameterization::Barycentric_mapping_parameterizer_3<TriangleMesh, BorderParameterizer, SolverTraits>`
/// \sa `CGAL::Surface_mesh_parameterization::Discrete_authalic_parameterizer_3<TriangleMesh, BorderParameterizer, SolverTraits>`
/// \sa `CGAL::Surface_mesh_parameterization::Discrete_conformal_map_parameterizer_3<TriangleMesh, BorderParameterizer, SolverTraits>`
/// \sa `CGAL::Surface_mesh_parameterization::Mean_value_coordinates_parameterizer_3<TriangleMesh, BorderParameterizer, SolverTraits>`
///
template < typename TriangleMesh_,
class BorderParameterizer_ = Default,
class SolverTraits_ = Default>
class Fixed_border_parameterizer_3
{
public:
#ifndef DOXYGEN_RUNNING
typedef typename Default::Get<
BorderParameterizer_,
Circular_border_arc_length_parameterizer_3<TriangleMesh_> >::type Border_parameterizer;
#if !defined(CGAL_EIGEN3_ENABLED)
static_assert(!(std::is_same<SolverTraits_, Default>::value),
"Error: You must either provide 'SolverTraits_' or link CGAL with the Eigen library");
#endif
typedef typename Default::Get<
SolverTraits_,
#if defined(CGAL_EIGEN3_ENABLED)
CGAL::Eigen_solver_traits<
Eigen::BiCGSTAB<Eigen_sparse_matrix<double>::EigenType,
Eigen::IncompleteLUT<double> > >
#else
SolverTraits_ // no parameter provided, and Eigen is not enabled: so don't compile!
#endif
>::type Solver_traits;
#else
/// Border parameterizer type
typedef Border_parameterizer_ Border_parameterizer;
/// Solver traits type
typedef SolverTraits_ Solver_traits;
#endif
/// Triangle mesh type
typedef TriangleMesh_ Triangle_mesh;
typedef TriangleMesh_ TriangleMesh;
/// Mesh vertex type
typedef typename boost::graph_traits<Triangle_mesh>::vertex_descriptor vertex_descriptor;
/// Mesh halfedge type
typedef typename boost::graph_traits<Triangle_mesh>::halfedge_descriptor halfedge_descriptor;
/// Solver vector type
typedef typename Solver_traits::Vector Vector;
/// Solver matrix type
typedef typename Solver_traits::Matrix Matrix;
// Private types
private:
typedef CGAL::Vertex_around_target_circulator<Triangle_mesh> vertex_around_target_circulator;
// Protected types
protected:
// Traits subtypes:
typedef typename internal::Kernel_traits<Triangle_mesh>::Kernel Kernel;
typedef typename internal::Kernel_traits<Triangle_mesh>::PPM PPM;
typedef typename Kernel::FT NT;
typedef typename Kernel::Point_2 Point_2;
typedef typename Kernel::Point_3 Point_3;
typedef typename Kernel::Vector_3 Vector_3;
// Public operations
public:
/// Constructor
Fixed_border_parameterizer_3(Border_parameterizer border_param = Border_parameterizer(),
///< %Object that maps the surface's border to 2D space
Solver_traits sparse_la = Solver_traits())
///< Traits object to access a sparse linear system
: m_borderParameterizer(border_param), m_linearAlgebra(sparse_la)
{ }
/// Destructor of base class should be virtual.
virtual ~Fixed_border_parameterizer_3() { }
// Default copy constructor and operator =() are fine
/// computes a one-to-one mapping from a triangular 3D surface mesh
/// to a piece of the 2D space.
/// The mapping is piecewise linear (linear in each triangle).
/// The result is the `(u,v)` pair image of each vertex of the 3D surface.
///
/// \tparam VertexUVmap must be a model of `ReadWritePropertyMap` with
/// `boost::graph_traits<Triangle_mesh>::%vertex_descriptor` as key type and
/// %Point_2 (type deduced from `Triangle_mesh` using `Kernel_traits`)
/// as value type.
/// \tparam VertexIndexMap must be a model of `ReadablePropertyMap` with
/// `boost::graph_traits<Triangle_mesh>::%vertex_descriptor` as key type and
/// a unique integer as value type.
/// \tparam VertexParameterizedMap must be a model of `ReadWritePropertyMap` with
/// `boost::graph_traits<Triangle_mesh>::%vertex_descriptor` as key type and
/// a Boolean as value type.
///
/// \param mesh a triangulated surface.
/// \param bhd a halfedge descriptor on the boundary of `mesh`.
/// \param uvmap an instantiation of the class `VertexUVmap`.
/// \param vimap an instantiation of the class `VertexIndexMap`.
/// \param vpmap an instantiation of the class `VertexParameterizedMap`.
///
/// \pre `mesh` must be a triangular mesh.
/// \pre The mesh border must be mapped onto a convex polygon.
/// \pre The vertices must be indexed (`vimap` must be initialized)
///
template <typename VertexUVmap,
typename VertexIndexMap,
typename VertexParameterizedMap>
Error_code parameterize(Triangle_mesh& mesh,
halfedge_descriptor bhd,
VertexUVmap uvmap,
VertexIndexMap vimap,
VertexParameterizedMap vpmap)
{
CGAL_precondition(is_valid_polygon_mesh(mesh));
CGAL_precondition(is_triangle_mesh(mesh));
CGAL_precondition(bhd != boost::graph_traits<Triangle_mesh>::null_halfedge() && is_border(bhd, mesh));
Error_code status = OK;
typedef std::unordered_set<vertex_descriptor> Vertex_set;
Vertex_set vertices;
internal::Containers_filler<Triangle_mesh> fc(mesh, vertices);
Polygon_mesh_processing::connected_component(
face(opposite(bhd, mesh), mesh),
mesh,
boost::make_function_output_iterator(fc));
// Count vertices
int nbVertices = static_cast<int>(vertices.size());
if (nbVertices == 0)
return ERROR_EMPTY_MESH;
// Compute (u,v) for border vertices and mark them as "parameterized"
status = get_border_parameterizer().parameterize(mesh, bhd, uvmap, vimap, vpmap);
if (status != OK)
return status;
// Create two sparse linear systems "A*Xu = Bu" and "A*Xv = Bv" (one line/column per vertex)
Matrix A(nbVertices, nbVertices);
Vector Xu(nbVertices), Xv(nbVertices), Bu(nbVertices), Bv(nbVertices);
// Initialize A, Xu, Xv, Bu and Bv after border parameterization
// Fill the border vertices' lines in both linear systems:
// "u = constant" and "v = constant"
//
// @todo Fixed_border_parameterizer_3 should remove border vertices
// from the linear systems in order to have a symmetric positive definite
// matrix for Tutte Barycentric Mapping and Discrete Conformal Map algorithms.
initialize_system_from_mesh_border(A, Bu, Bv, mesh, bhd, uvmap, vimap);
// Fill the matrix for the inner vertices v_i: compute A's coefficient
// w_ij for each neighbor j; then w_ii = - sum of w_ijs
std::unordered_set<vertex_descriptor> main_border;
for(vertex_descriptor v : vertices_around_face(bhd,mesh)){
main_border.insert(v);
}
for(vertex_descriptor v : vertices){
// inner vertices only
if(main_border.find(v) == main_border.end()){
// Compute the line i of matrix A for i inner vertex
status = setup_inner_vertex_relations(A, Bu, Bv, mesh, v, vimap);
if(status != OK)
return status;
}
}
// Solve "A*Xu = Bu". On success, solution is (1/Du) * Xu.
// Solve "A*Xv = Bv". On success, solution is (1/Dv) * Xv.
double Du = 0, Dv = 0;
if(!get_linear_algebra_traits().linear_solver(A, Bu, Xu, Du) ||
!get_linear_algebra_traits().linear_solver(A, Bv, Xv, Dv))
{
status = ERROR_CANNOT_SOLVE_LINEAR_SYSTEM;
}
if(status != OK)
return status;
// WARNING: this package does not support homogeneous coordinates!
CGAL_assertion(Du == 1.0);
CGAL_assertion(Dv == 1.0);
// Copy Xu and Xv coordinates into the (u,v) pair of each vertex
for(vertex_descriptor v : vertices)
{
// inner vertices only
if(main_border.find(v) == main_border.end()){
int index = get(vimap,v);
put(uvmap,v,Point_2(Xu[index],Xv[index]));
put(vpmap,v,true);
}
}
// Check postconditions
// AF status = check_parameterize_postconditions(amesh, A, Bu, Bv);
if(status != OK)
return status;
return status;
}
// Protected operations
protected:
/// initializes `A`, `Bu` and `Bv` after border parameterization.
/// Fill the border vertices' lines in both linear systems:
/// "u = constant" and "v = constant".
///
/// \tparam VertexUVmap must be a model of `ReadWritePropertyMap` with
/// `boost::graph_traits<Triangle_mesh>::%vertex_descriptor` as key type and
/// %Point_2 (type deduced from `Triangle_mesh` using `Kernel_traits`)
/// as value type.
/// \tparam VertexIndexMap must be a model of `ReadablePropertyMap` with
/// `boost::graph_traits<Triangle_mesh>::%vertex_descriptor` as key type and
/// a unique integer as value type.
///
/// \param A the matrix in both linear system
/// \param Bu the right hand side vector in the linear system of x coordinates
/// \param Bv the right hand side vector in the linear system of y coordinates
/// \param mesh a triangulated surface.
/// \param bhd a halfedge descriptor on the boundary of `mesh`.
/// \param uvmap an instantiation of the class `VertexUVmap`.
/// \param vimap an instantiation of the class `VertexIndexMap`.
///
/// \pre Vertices must be indexed (`vimap` must be initialized).
/// \pre `A`, `Bu`, and `Bv` must be allocated.
/// \pre Border vertices must be parameterized.
template <typename VertexUVmap, typename VertexIndexMap>
void initialize_system_from_mesh_border(Matrix& A, Vector& Bu, Vector& Bv,
const Triangle_mesh& mesh,
halfedge_descriptor bhd,
VertexUVmap uvmap,
VertexIndexMap vimap) const
{
for(halfedge_descriptor hd : halfedges_around_face(bhd, mesh)){
// Get vertex index in sparse linear system
int index = get(vimap, target(hd, mesh));
// Write a diagonal coefficient of A
A.set_coef(index, index, 1, true /*new*/);
// get the halfedge uv
// Write constant in Bu and Bv
const Point_2& uv = get(uvmap, target(hd, mesh));
Bu[index] = uv.x();
Bv[index] = uv.y();
}
}
/// computes `w_ij`, coefficient of matrix `A` for `j` neighbor vertex of `i`.
/// Implementation note: Subclasses must at least implement compute_w_ij().
///
/// \param mesh a triangulated surface.
/// \param main_vertex_v_i the vertex of `mesh` with index `i`
/// \param neighbor_vertex_v_j the vertex of `mesh` with index `j`
virtual NT compute_w_ij(const Triangle_mesh& mesh,
vertex_descriptor main_vertex_v_i,
Vertex_around_target_circulator<Triangle_mesh> neighbor_vertex_v_j) const = 0;
/// Compute the line i of matrix A for i inner vertex:
/// - call compute_w_ij() to compute the A coefficient w_ij for each neighbor v_j.
/// - compute w_ii = - sum of w_ijs.
///
/// \pre Vertices must be indexed.
/// \pre Vertex i mustn't be already parameterized.
/// \pre Line i of A must contain only zeros.
// TODO: check if this must be virtual
// virtual
template <typename VertexIndexMap>
Error_code setup_inner_vertex_relations(Matrix& A,
Vector&,
Vector&,
const Triangle_mesh& mesh,
vertex_descriptor vertex,
VertexIndexMap vimap) const
{
int i = get(vimap,vertex);
// circulate over vertices around 'vertex' to compute w_ii and w_ijs
NT w_ii = 0;
int vertexIndex = 0;
vertex_around_target_circulator v_j(halfedge(vertex, mesh), mesh), end = v_j;
CGAL_For_all(v_j, end){
// Call to virtual method to do the actual coefficient computation
NT w_ij = -1.0 * compute_w_ij(mesh, vertex, v_j);
// w_ii = - sum of w_ijs
w_ii -= w_ij;
// Get j index
int j = get(vimap, *v_j);
// Set w_ij in matrix
A.set_coef(i,j, w_ij, true /*new*/);
vertexIndex++;
}
if (vertexIndex < 2)
return ERROR_NON_TRIANGULAR_MESH;
// Set w_ii in matrix
A.set_coef(i,i, w_ii, true /*new*/);
return OK;
}
// Protected accessors
protected:
/// Get the object that maps the surface's border onto a 2D space.
Border_parameterizer& get_border_parameterizer() { return m_borderParameterizer; }
/// Get the sparse linear algebra (traits object to access the linear system).
Solver_traits& get_linear_algebra_traits() { return m_linearAlgebra; }
// Fields
private:
// Object that maps the surface's border onto a 2D space.
Border_parameterizer m_borderParameterizer;
// Traits object to solve a sparse linear system
Solver_traits m_linearAlgebra;
};
} // namespace Surface_mesh_parameterization
} // namespace CGAL
#include <CGAL/enable_warnings.h>
#endif // CGAL_SURFACE_MESH_PARAMETERIZATION_FIXED_BORDER_PARAMETERIZER_3_H
|