QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgstopologicalmesh.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgstopologicalmesh.h - QgsTopologicalMesh
3 
4  ---------------------
5  begin : 18.6.2021
6  copyright : (C) 2021 by Vincent Cloarec
7  email : vcloarec at gmail dot com
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 #ifndef QGSTOPOLOGICALMESH_H
17 #define QGSTOPOLOGICALMESH_H
18 
19 #include <QSet>
20 
21 #include "qgsmeshdataprovider.h"
22 
23 #if defined(_MSC_VER)
24 template CORE_EXPORT QVector<int> SIP_SKIP;
25 template CORE_EXPORT QList<int> SIP_SKIP;
26 template CORE_EXPORT QVector<QVector<int>> SIP_SKIP;
27 #endif
28 
30 
33 
47 class CORE_EXPORT QgsTopologicalMesh
48 {
49  public:
50 
51  using FaceNeighbors = QVector<int>;
52 
62  class CORE_EXPORT TopologicalFaces
63  {
64  public:
65 
67  QVector<QgsMeshFace> meshFaces() const {return mFaces;}
68 
70  void clear();
71 
73  QVector<FaceNeighbors> facesNeighborhood() const;
74 
76  int vertexToFace( int vertexIndex ) const;
77 
78  private:
79  QVector<QgsMeshFace> mFaces; // the faces containing the vertices indexes in the mesh
80  QVector<FaceNeighbors> mFacesNeighborhood; // neighborhood of the faces, face indexes are local
81  QMultiHash<int, int> mVerticesToFace; // map of vertices to incident face, face indexes are local
82  QList<int> mBoundaries; // list of boundary vertices indexes in the mesh
83 
84  friend class QgsTopologicalMesh;
86  };
87 
88 
96  class CORE_EXPORT Changes
97  {
98  public:
99 
101  QVector<QgsMeshFace> addedFaces() const;
102 
104  QVector<QgsMeshFace> removedFaces() const;
105 
107  QList<int> removedFaceIndexes() const;
108 
110  QVector<QgsMeshVertex> addedVertices() const;
111 
113  QList<int> verticesToRemoveIndexes() const;
114 
116  QList<int> changedCoordinatesVerticesIndexes() const;
117 
119  QList<double> newVerticesZValues() const;
120 
122  QList<QgsPointXY> newVerticesXYValues() const;
123 
125  QList<QgsPointXY> oldVerticesXYValues() const;
126 
128  QList<int> nativeFacesIndexesGeometryChanged() const;
129 
131  bool isEmpty() const;
132 
133  protected:
134  int mAddedFacesFirstIndex = 0;
135  QList<int> mFaceIndexesToRemove; // the removed faces indexes in the mesh
136  QVector<QgsMeshFace> mFacesToAdd;
137  QVector<FaceNeighbors> mFacesNeighborhoodToAdd;
138  QVector<QgsMeshFace> mFacesToRemove;
139  QVector<FaceNeighbors> mFacesNeighborhoodToRemove;
140  QList<std::array<int, 4>> mNeighborhoodChanges; // {index of concerned face, neigbor position, previous value, changed value}
141 
142  QVector<QgsMeshVertex> mVerticesToAdd;
143  QVector<int> mVertexToFaceToAdd;
145  QList<QgsMeshVertex> mRemovedVertices;
147  QList<std::array<int, 3>> mVerticesToFaceChanges; // {index of concerned vertex, previous value, changed value}
148 
150  QList<double> mNewZValues;
151  QList<double> mOldZValues;
152  QList<QgsPointXY> mNewXYValues;
153  QList<QgsPointXY> mOldXYValues;
155 
157  void clearChanges();
158 
159  private:
160  int addedFaceIndexInMesh( int internalIndex ) const;
161  int removedFaceIndexInMesh( int internalIndex ) const;
162 
163  friend class QgsTopologicalMesh;
164  };
165 
170  static QgsTopologicalMesh createTopologicalMesh( QgsMesh *mesh, int maxVerticesPerFace, QgsMeshEditingError &error );
171 
173  static TopologicalFaces createNewTopologicalFaces( const QVector<QgsMeshFace> &faces, bool uniqueSharedVertexAllowed, QgsMeshEditingError &error );
174 
175  //----------- access element methods
176 
178  QVector<int> neighborsOfFace( int faceIndex ) const;
179 
181  QList<int> facesAroundVertex( int vertexIndex ) const;
182 
184  QgsMesh *mesh() const;
185 
187  int firstFaceLinked( int vertexIndex ) const;
188 
190  bool isVertexOnBoundary( int vertexIndex ) const;
191 
193  bool isVertexFree( int vertexIndex ) const;
194 
196  QList<int> freeVerticesIndexes() const;
197 
199  QgsMeshVertexCirculator vertexCirculator( int vertexIndex ) const;
200 
201  //----------- editing methods
202 
204  QgsMeshEditingError facesCanBeAdded( const TopologicalFaces &topologicalFaces ) const;
205 
210  Changes addFaces( const TopologicalFaces &topologicFaces );
211 
216  QgsMeshEditingError facesCanBeRemoved( const QList<int> facesIndexes );
217 
222  Changes removeFaces( const QList<int> facesIndexes );
223 
227  bool edgeCanBeFlipped( int vertexIndex1, int vertexIndex2 ) const;
228 
233  Changes flipEdge( int vertexIndex1, int vertexIndex2 );
234 
238  bool canBeMerged( int vertexIndex1, int vertexIndex2 ) const;
239 
244  Changes merge( int vertexIndex1, int vertexIndex2 );
245 
249  bool canBeSplit( int faceIndex ) const;
250 
255  Changes splitFace( int faceIndex );
256 
261  Changes addVertexInFace( int faceIndex, const QgsMeshVertex &vertex );
262 
267  Changes insertVertexInFacesEdge( int faceIndex, int position, const QgsMeshVertex &vertex );
268 
273  Changes addFreeVertex( const QgsMeshVertex &vertex );
274 
280  Changes removeVertexFillHole( int vertexIndex );
281 
287  Changes removeVertices( const QList<int> &vertices );
288 
292  Changes changeZValue( const QList<int> &verticesIndexes, const QList<double> &newValues );
293 
297  Changes changeXYValue( const QList<int> &verticesIndexes, const QList<QgsPointXY> &newValues );
298 
299 
301  void applyChanges( const Changes &changes );
302 
304  void reverseChanges( const Changes &changes );
305 
308 
313  void reindex();
314 
318  bool renumber();
319 
322 
324  static QgsMeshEditingError checkTopology( const QgsMesh &mesh, int maxVerticesPerFace );
325 
326  private:
327 
329  static TopologicalFaces createTopologicalFaces(
330  const QVector<QgsMeshFace> &faces,
331  QVector<int> *globalVertexToFace,
332  QgsMeshEditingError &error,
333  bool allowUniqueSharedVertex );
334 
336  QSet<int> concernedFacesBy( const QList<int> faceIndexes ) const;
337 
339  void referenceAsFreeVertex( int vertexIndex );
341  void dereferenceAsFreeVertex( int vertexIndex );
342 
347  bool eitherSideFacesAndVertices( int vertexIndex1,
348  int vertexIndex2,
349  int &face1,
350  int &face2,
351  int &neighborVertex1InFace1,
352  int &neighborVertex1InFace2,
353  int &neighborVertex2inFace1,
354  int &neighborVertex2inFace2 ) const;
355 
356  bool renumberVertices( QVector<int> &oldToNewIndex ) const;
357  bool renumberFaces( QVector<int> &oldToNewIndex ) const;
358 
359  //Attributes
360  QgsMesh *mMesh = nullptr;
361  QVector<int> mVertexToFace;
362  QVector<FaceNeighbors> mFacesNeighborhood;
363 
364  QSet<int> mFreeVertices;
365 
366  int mMaximumVerticesPerFace = 0;
367 
369 
370 };
371 
379 class CORE_EXPORT QgsMeshVertexCirculator
380 {
381  public:
382 
384  QgsMeshVertexCirculator( const QgsTopologicalMesh &topologicalMesh, int vertexIndex );
385 
391  QgsMeshVertexCirculator( const QgsTopologicalMesh::TopologicalFaces &topologicalFaces, int faceIndex, int vertexIndex );
392 
398  QgsMeshVertexCirculator( const QgsTopologicalMesh::TopologicalFaces &topologicalFaces, int vertexIndex );
399 
401  int turnCounterClockwise() const;
402 
404  int turnClockwise() const;
405 
407  int currentFaceIndex() const;
408 
410  QgsMeshFace currentFace() const;
411 
413  bool goBoundaryClockwise() const;
414 
416  bool goBoundaryCounterClockwise() const;
417 
419  int oppositeVertexClockwise() const;
420 
422  int oppositeVertexCounterClockwise() const;
423 
425  bool isValid() const;
426 
428  QList<int> facesAround() const;
429 
431  int degree() const;
432 
433  private:
434  const QVector<QgsMeshFace> mFaces;
435  const QVector<QgsTopologicalMesh::FaceNeighbors> mFacesNeighborhood;
436  const int mVertexIndex = -1;
437  mutable int mCurrentFace = -1;
438  mutable int mLastValidFace = -1;
439  bool mIsValid = false;
440  mutable int mDegree = -1;
441 
442  int positionInCurrentFace() const;
443 };
444 
445 #endif // QGSTOPOLOGICALMESH_H
QgsTopologicalMesh::isVertexFree
bool isVertexFree(int vertexIndex) const
Returns whether the vertex is a free vertex.
Definition: qgstopologicalmesh.cpp:614
QgsTopologicalMesh::edgeCanBeFlipped
bool edgeCanBeFlipped(int vertexIndex1, int vertexIndex2) const
Returns true if the edge can be flipped (only available for edge shared by two faces with 3 vertices)
Definition: qgstopologicalmesh.cpp:1915
QgsTopologicalMesh::Changes::mNewZValues
QList< double > mNewZValues
Definition: qgstopologicalmesh.h:150
QgsTopologicalMesh::merge
Changes merge(int vertexIndex1, int vertexIndex2)
Merges faces separated by vertices with indexes vertexIndex1 and vertexIndex2 The method returns a in...
Definition: qgstopologicalmesh.cpp:2091
QgsTopologicalMesh::TopologicalFaces
Class that contains independent faces an topological information about this faces.
Definition: qgstopologicalmesh.h:62
QgsTopologicalMesh::addFreeVertex
Changes addFreeVertex(const QgsMeshVertex &vertex)
Adds a free vertex in the face, that is a vertex tha tis not included or linked with any faces.
Definition: qgstopologicalmesh.cpp:1112
QgsTopologicalMesh::Changes
Class that contains topological differences between two states of a topological mesh,...
Definition: qgstopologicalmesh.h:96
QgsTopologicalMesh::FaceNeighbors
QVector< int > FaceNeighbors
Definition: qgstopologicalmesh.h:51
QgsPoint
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:48
QgsTopologicalMesh::canBeMerged
bool canBeMerged(int vertexIndex1, int vertexIndex2) const
Returns true if faces separated by vertices with indexes vertexIndex1 and vertexIndex2 can be merged.
Definition: qgstopologicalmesh.cpp:2048
QgsTopologicalMesh::Changes::mNewXYValues
QList< QgsPointXY > mNewXYValues
Definition: qgstopologicalmesh.h:152
QgsTopologicalMesh::Changes::mOldZValues
QList< double > mOldZValues
Definition: qgstopologicalmesh.h:151
QgsTopologicalMesh::reverseChanges
void reverseChanges(const Changes &changes)
Reverses the changes.
Definition: qgstopologicalmesh.cpp:436
QgsTopologicalMesh::changeZValue
Changes changeZValue(const QList< int > &verticesIndexes, const QList< double > &newValues)
Changes the Z values of the vertices with indexes in vertices indexes with the values in newValues.
Definition: qgstopologicalmesh.cpp:2513
QgsMesh
Mesh - vertices, edges and faces.
Definition: qgsmeshdataprovider.h:58
QgsTopologicalMesh::Changes::mChangeCoordinateVerticesIndexes
QList< int > mChangeCoordinateVerticesIndexes
Definition: qgstopologicalmesh.h:149
QgsMeshVertexCirculator
Convenient class that turn around a vertex and provide information about faces and vertices.
Definition: qgstopologicalmesh.h:379
QgsTopologicalMesh::isVertexOnBoundary
bool isVertexOnBoundary(int vertexIndex) const
Returns whether the vertex is on a boundary.
Definition: qgstopologicalmesh.cpp:604
QgsTopologicalMesh::neighborsOfFace
QVector< int > neighborsOfFace(int faceIndex) const
Returns the indexes of neighbor faces of the face with index faceIndex.
Definition: qgstopologicalmesh.cpp:1777
QgsTopologicalMesh::facesAroundVertex
QList< int > facesAroundVertex(int vertexIndex) const
Returns the indexes of faces that are around the vertex with index vertexIndex.
Definition: qgstopologicalmesh.cpp:1782
QgsTopologicalMesh::TopologicalFaces::meshFaces
QVector< QgsMeshFace > meshFaces() const
Returns faces.
Definition: qgstopologicalmesh.h:67
QgsTopologicalMesh
Class that wraps a QgsMesh to ensure the consistency of the mesh during editing and help to access to...
Definition: qgstopologicalmesh.h:47
QgsTopologicalMesh::Changes::mFacesNeighborhoodToAdd
QVector< FaceNeighbors > mFacesNeighborhoodToAdd
Definition: qgstopologicalmesh.h:137
QgsTopologicalMesh::flipEdge
Changes flipEdge(int vertexIndex1, int vertexIndex2)
Flips edge (vertexIndex1, vertexIndex2) The method returns a instance of the class QgsTopologicalMesh...
Definition: qgstopologicalmesh.cpp:1956
QgsTopologicalMesh::Changes::mNeighborhoodChanges
QList< std::array< int, 4 > > mNeighborhoodChanges
Definition: qgstopologicalmesh.h:140
QgsTopologicalMesh::freeVerticesIndexes
QList< int > freeVerticesIndexes() const
Returns a list of vertices are not linked to any faces.
Definition: qgstopologicalmesh.cpp:625
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsTopologicalMesh::Changes::mVerticesToFaceRemoved
QList< int > mVerticesToFaceRemoved
Definition: qgstopologicalmesh.h:146
QgsTopologicalMesh::Changes::mFacesToRemove
QVector< QgsMeshFace > mFacesToRemove
Definition: qgstopologicalmesh.h:138
QgsTopologicalMesh::Changes::mRemovedVertices
QList< QgsMeshVertex > mRemovedVertices
Definition: qgstopologicalmesh.h:145
QgsTopologicalMesh::canBeSplit
bool canBeSplit(int faceIndex) const
Returns true if face with index faceIndex can be split.
Definition: qgstopologicalmesh.cpp:2181
QgsTopologicalMesh::changeXYValue
Changes changeXYValue(const QList< int > &verticesIndexes, const QList< QgsPointXY > &newValues)
Changes the (X,Y) values of the vertices with indexes in vertices indexes with the values in newValue...
Definition: qgstopologicalmesh.cpp:2532
QgsMeshEditingError
Class that represents an error during mesh editing.
Definition: qgsmesheditor.h:42
QgsTopologicalMesh::renumber
bool renumber()
Renumbers the indexes of vertices and faces using the Reverse CutHill McKee Algorithm.
Definition: qgstopologicalmesh.cpp:743
QgsTopologicalMesh::removeVertices
Changes removeVertices(const QList< int > &vertices)
Removes all the vertices with index in the list vertices If vertices in linked with faces,...
Definition: qgstopologicalmesh.cpp:1472
QgsTopologicalMesh::Changes::mFacesToAdd
QVector< QgsMeshFace > mFacesToAdd
Definition: qgstopologicalmesh.h:136
QgsTopologicalMesh::addVertexInFace
Changes addVertexInFace(int faceIndex, const QgsMeshVertex &vertex)
Adds a vertex in the face with index faceIndex.
Definition: qgstopologicalmesh.cpp:2264
QgsTopologicalMesh::vertexCirculator
QgsMeshVertexCirculator vertexCirculator(int vertexIndex) const
Returns a vertex circulator linked to this mesh around the vertex with index vertexIndex.
Definition: qgstopologicalmesh.cpp:510
QgsTopologicalMesh::Changes::mVertexToFaceToAdd
QVector< int > mVertexToFaceToAdd
Definition: qgstopologicalmesh.h:143
QgsTopologicalMesh::removeVertexFillHole
Changes removeVertexFillHole(int vertexIndex)
Removes the vertex with index vertexIndex.
Definition: qgstopologicalmesh.cpp:1157
QgsMeshFace
QVector< int > QgsMeshFace
List of vertex indexes.
Definition: qgsmeshdataprovider.h:42
QgsTopologicalMesh::Changes::mVerticesToFaceChanges
QList< std::array< int, 3 > > mVerticesToFaceChanges
Definition: qgstopologicalmesh.h:147
QgsTopologicalMesh::checkConsistency
QgsMeshEditingError checkConsistency() const
Checks the consistency of the topological mesh and return false if there is a consistency issue.
Definition: qgstopologicalmesh.cpp:539
SIP_NO_FILE
#define SIP_NO_FILE
Definition: fromencodedcomponenthelper.h:18
QgsTopologicalMesh::insertVertexInFacesEdge
Changes insertVertexInFacesEdge(int faceIndex, int position, const QgsMeshVertex &vertex)
Inserts a vertex in the edge of face with index faceIndex at position .
Definition: qgstopologicalmesh.cpp:2327
QgsTopologicalMesh::Changes::mNativeFacesIndexesGeometryChanged
QList< int > mNativeFacesIndexesGeometryChanged
Definition: qgstopologicalmesh.h:154
QgsTopologicalMesh::Changes::mFaceIndexesToRemove
QList< int > mFaceIndexesToRemove
Definition: qgstopologicalmesh.h:135
QgsTopologicalMesh::removeFaces
Changes removeFaces(const QList< int > facesIndexes)
Removes faces with index in faceIndexes.
Definition: qgstopologicalmesh.cpp:1808
QgsTopologicalMesh::splitFace
Changes splitFace(int faceIndex)
Splits face with index faceIndex The method returns a instance of the class QgsTopologicalMesh::Chang...
Definition: qgstopologicalmesh.cpp:2188
QgsTopologicalMesh::Changes::mVerticesToAdd
QVector< QgsMeshVertex > mVerticesToAdd
Definition: qgstopologicalmesh.h:142
QgsTopologicalMesh::checkTopology
static QgsMeshEditingError checkTopology(const QgsMesh &mesh, int maxVerticesPerFace)
Checks the topology of the mesh mesh, if error occurs, this mesh can't be edited.
Definition: qgstopologicalmesh.cpp:584
QgsTopologicalMesh::addFaces
Changes addFaces(const TopologicalFaces &topologicFaces)
Adds faces topologicFaces to the topologic mesh.
Definition: qgstopologicalmesh.cpp:282
QgsTopologicalMesh::createTopologicalMesh
static QgsTopologicalMesh createTopologicalMesh(QgsMesh *mesh, int maxVerticesPerFace, QgsMeshEditingError &error)
Creates a topologicaly consistent mesh with mesh, this static method modifies mesh to be topological ...
Definition: qgstopologicalmesh.cpp:1639
QgsTopologicalMesh::Changes::mVerticesToRemoveIndexes
QList< int > mVerticesToRemoveIndexes
Definition: qgstopologicalmesh.h:144
QgsTopologicalMesh::applyChanges
void applyChanges(const Changes &changes)
Applies the changes.
Definition: qgstopologicalmesh.cpp:354
QgsTopologicalMesh::createNewTopologicalFaces
static TopologicalFaces createNewTopologicalFaces(const QVector< QgsMeshFace > &faces, bool uniqueSharedVertexAllowed, QgsMeshEditingError &error)
Creates new topological faces that are not yet included in the mesh.
Definition: qgstopologicalmesh.cpp:1681
QgsTopologicalMesh::Changes::mFacesNeighborhoodToRemove
QVector< FaceNeighbors > mFacesNeighborhoodToRemove
Definition: qgstopologicalmesh.h:139
QgsTopologicalMesh::counterClockwiseFaces
static QgsMeshEditingError counterClockwiseFaces(QgsMeshFace &face, QgsMesh *mesh)
Checks the topology of the face and sets it counter clockwise if necessary.
Definition: qgstopologicalmesh.cpp:634
QgsTopologicalMesh::mesh
QgsMesh * mesh() const
Returns a pointer to the wrapped mesh.
Definition: qgstopologicalmesh.cpp:592
QgsTopologicalMesh::facesCanBeRemoved
QgsMeshEditingError facesCanBeRemoved(const QList< int > facesIndexes)
Returns whether faces with index in faceIndexes can be removed/ The method an error object with type ...
Definition: qgstopologicalmesh.cpp:1789
qgsmeshdataprovider.h
QgsTopologicalMesh::reindex
void reindex()
Reindexes faces and vertices, after this operation, the topological mesh can't be edited anymore and ...
Definition: qgstopologicalmesh.cpp:693
QgsTopologicalMesh::facesCanBeAdded
QgsMeshEditingError facesCanBeAdded(const TopologicalFaces &topologicalFaces) const
Returns whether the faces can be added to the mesh.
Definition: qgstopologicalmesh.cpp:1499
QgsTopologicalMesh::firstFaceLinked
int firstFaceLinked(int vertexIndex) const
Returns the index of the first face linked, returns -1 if it is a free vertex or out of range index.
Definition: qgstopologicalmesh.cpp:597
QgsTopologicalMesh::Changes::mOldXYValues
QList< QgsPointXY > mOldXYValues
Definition: qgstopologicalmesh.h:153