9 #pragma warning(disable : 4996)
19 #if defined(USE_CGAL_LIB)
21 #include <CGAL/Constrained_Delaunay_triangulation_2.h>
22 #include <CGAL/Delaunay_triangulation_2.h>
23 #include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
24 #include <CGAL/Triangulation_vertex_base_with_info_2.h>
25 #include <CGAL/version_macros.h>
31 : m_associatedCloud(nullptr),
32 m_triIndexes(nullptr),
33 m_globalIterator(nullptr),
34 m_globalIteratorEnd(nullptr),
35 m_numberOfTriangles(0),
36 m_cloudIsOwnedByMesh(false) {}
45 #if defined(USE_CGAL_LIB)
64 const std::vector<int>& segments2D,
65 std::string& outputErrorStr) {
66 #if defined(USE_CGAL_LIB)
69 typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
72 typedef CGAL::Triangulation_vertex_base_with_info_2<std::size_t, K> Vb;
73 typedef CGAL::Constrained_triangulation_face_base_2<K> Fb;
74 typedef CGAL::Triangulation_data_structure_2<Vb, Fb> Tds;
78 #if CGAL_VERSION_NR >= 60000
79 typedef CGAL::Constrained_Delaunay_triangulation_2<K, Tds> CDT;
81 typedef CGAL::No_intersection_tag Itag;
82 typedef CGAL::Constrained_Delaunay_triangulation_2<K, Tds, Itag> CDT;
84 typedef CDT::Point cgalPoint;
86 std::vector<std::pair<cgalPoint, std::size_t>> constraints;
87 std::size_t constrCount = segments2D.size();
90 constraints.reserve(constrCount);
91 }
catch (
const std::bad_alloc&) {
92 outputErrorStr =
"Not enough memory";
100 for (std::size_t i = 0; i < constrCount; ++i) {
101 const CCVector2* pt = &points2D[segments2D[i]];
102 constraints.emplace_back(cgalPoint(pt->
x, pt->
y), segments2D[i]);
105 cdt.insert(constraints.begin(), constraints.end());
113 for (CDT::Face_iterator
face = cdt.faces_begin();
114 face != cdt.faces_end(); ++
face, faceCount += 3) {
116 static_cast<int>(
face->vertex(0)->info());
118 static_cast<int>(
face->vertex(1)->info());
120 static_cast<int>(
face->vertex(2)->info());
128 outputErrorStr =
"CGAL library not supported";
134 std::size_t pointCountToUse,
135 std::string& outputErrorStr) {
136 #if defined(USE_CGAL_LIB)
139 typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
142 typedef CGAL::Triangulation_vertex_base_with_info_2<std::size_t, K> Vb;
143 typedef CGAL::Triangulation_data_structure_2<Vb> Tds;
144 typedef CGAL::Delaunay_triangulation_2<K, Tds> DT;
145 typedef DT::Point cgalPoint;
147 std::vector<std::pair<cgalPoint, std::size_t>> pts;
148 std::size_t pointCount = points2D.size();
151 if (pointCountToUse > 0 && pointCountToUse < pointCount) {
152 pointCount = pointCountToUse;
155 if (pointCount < 3) {
156 outputErrorStr =
"Not enough points";
161 pts.reserve(pointCount);
162 }
catch (
const std::bad_alloc&) {
163 outputErrorStr =
"Not enough memory";
173 for (std::size_t i = 0; i < pointCount; ++i) {
175 pts.emplace_back(cgalPoint(pt->
x, pt->
y), i);
179 DT dt(pts.begin(), pts.end());
187 for (DT::Face_iterator
face = dt.faces_begin();
face != dt.faces_end();
188 ++
face, faceCount += 3) {
190 static_cast<int>(
face->vertex(0)->info());
192 static_cast<int>(
face->vertex(1)->info());
194 static_cast<int>(
face->vertex(2)->info());
202 outputErrorStr =
"CGAL library not supported";
208 const std::vector<CCVector2>& vertices2D,
209 const std::vector<CCVector2>& polygon2D,
210 bool removeOutside ) {
220 unsigned lastValidIndex = 0;
227 const CCVector2& A = vertices2D[_triIndexes[0]];
228 const CCVector2& B = vertices2D[_triIndexes[1]];
229 const CCVector2& C = vertices2D[_triIndexes[2]];
236 if ((removeOutside && isInside) || (!removeOutside && !isInside)) {
238 if (lastValidIndex != i)
271 unsigned lastValidIndex = 0;
278 if ((*B - *A).norm2() <= squareMaxEdgeLength &&
279 (*C - *A).norm2() <= squareMaxEdgeLength &&
280 (*C - *B).norm2() <= squareMaxEdgeLength) {
281 if (lastValidIndex != i)
383 unsigned triangleIndex) {
398 const std::vector<CCVector2>& contourPoints) {
399 size_t count = contourPoints.size();
407 if (contourPoints.back().x == contourPoints.front().x &&
408 contourPoints.back().y == contourPoints.front().y)
411 std::string errorStr;
431 if (!contourPoints) {
442 std::vector<CCVector2> contourPoints2D;
444 contourPoints2D.reserve(
count);
445 }
catch (
const std::bad_alloc&) {
450 if (flatDimension >= 0 && flatDimension <= 2)
452 const unsigned char Z =
static_cast<unsigned char>(flatDimension);
453 const unsigned char X = (Z == 2 ? 0 : Z + 1);
454 const unsigned char Y = (
X == 2 ? 0 :
X + 1);
455 for (
unsigned i = 0; i < contourPoints->
size(); ++i) {
460 assert(flatDimension < 0);
Vector2Tpl< PointCoordinateType > CCVector2
Default 2D Vector.
Vector3Tpl< PointCoordinateType > CCVector3
Default 3D Vector.
float PointCoordinateType
Type of the coordinates of a (N-D) point.
A class to compute and handle a Delaunay 2D mesh on a subset of points.
VerticesIndexes * getTriangleVertIndexes(unsigned triangleIndex) override
Returns the indexes of the vertices of a given triangle.
int * m_globalIterator
Iterator on the list of triangle vertex indexes.
int * m_triIndexes
Triangle vertex indexes.
unsigned m_numberOfTriangles
The number of triangles.
void forEach(genericTriangleAction action) override
Fast iteration mechanism.
static Delaunay2dMesh * TesselateContour(const std::vector< CCVector2 > &contourPoints)
~Delaunay2dMesh() override
Delaunay2dMesh destructor.
virtual unsigned size() const override
Returns the number of triangles.
static bool Available()
Returns whether 2D Delaunay triangulation is supported or not.
GenericIndexedCloud * m_associatedCloud
Associated point cloud.
void placeIteratorAtBeginning() override
Places the mesh iterator at the beginning.
void getBoundingBox(CCVector3 &bbMin, CCVector3 &bbMax) override
Returns the mesh bounding-box.
virtual bool removeOuterTriangles(const std::vector< CCVector2 > &vertices2D, const std::vector< CCVector2 > &polygon2D, bool removeOutside=true)
Removes the triangles falling outside of a given (2D) polygon.
bool removeTrianglesWithEdgesLongerThan(PointCoordinateType maxEdgeLength)
Filters out the triangles based on their edge length.
int * m_globalIteratorEnd
End position of global iterator.
virtual void getTriangleVertices(unsigned triangleIndex, CCVector3 &A, CCVector3 &B, CCVector3 &C) const override
Returns the vertices of a given triangle.
Delaunay2dMesh()
Delaunay2dMesh constructor.
virtual bool buildMesh(const std::vector< CCVector2 > &points2D, std::size_t pointCountToUse, std::string &outputErrorStr)
Build the Delaunay mesh on top a set of 2D points.
virtual void linkMeshWith(GenericIndexedCloud *aCloud, bool passOwnership=false)
Associate this mesh to a point cloud.
GenericTriangle * _getNextTriangle() override
Returns the next triangle (relatively to the global iterator position)
VerticesIndexes * getNextTriangleVertIndexes() override
GenericTriangle * _getTriangle(unsigned triangleIndex) override
Returns the ith triangle.
VerticesIndexes m_dumpTriangleIndexes
Dump triangle index structure to transmit temporary data.
bool m_cloudIsOwnedByMesh
SimpleTriangle m_dumpTriangle
Dump triangle structure to transmit temporary data.
virtual void getBoundingBox(CCVector3 &bbMin, CCVector3 &bbMax)=0
Returns the cloud bounding box.
virtual unsigned size() const =0
Returns the number of points.
A generic 3D point cloud with index-based and presistent access to points.
A generic 3D point cloud with index-based point access.
virtual const CCVector3 * getPoint(unsigned index) const =0
Returns the ith point.
std::function< void(GenericTriangle &)> genericTriangleAction
Generic function to apply to a triangle (used by foreach)
A generic triangle interface.
bool projectPointsOn2DPlane(std::vector< Vec2D > &points2D, const PointCoordinateType *planeEquation=nullptr, CCVector3 *O=nullptr, CCVector3 *X=nullptr, CCVector3 *Y=nullptr, InputVectorsUsage vectorsUsage=None)
Projects points on the best fitting LS plane.
std::vector< unsigned int > face
Generic file read and write utility for python interface.
Triangle described by the indexes of its 3 vertices.