QGIS API Documentation  3.24.2-Tisler (13c1a02865)
qgstriangularmesh.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgstriangularmesh.h
3  -------------------
4  begin : April 2018
5  copyright : (C) 2018 by Peter Petrik
6  email : zilolv at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef QGSTRIANGULARMESH_H
19 #define QGSTRIANGULARMESH_H
20 
21 
22 #define SIP_NO_FILE
23 
24 #include <QVector>
25 #include <QVector3D>
26 #include <QSet>
27 #include <QList>
28 #include <memory>
29 #include "qgis_core.h"
30 #include "qgsmeshdataprovider.h"
31 #include "qgsgeometry.h"
32 #include "qgsmeshspatialindex.h"
33 #include "qgstopologicalmesh.h"
34 #include "qgscoordinatetransform.h"
35 
36 class QgsRenderContext;
37 class QgsRectangle;
38 
51 class CORE_EXPORT QgsTriangularMesh // TODO rename to QgsRendererMesh in QGIS 4
52 {
53  public:
58 
65  bool update( QgsMesh *nativeMesh, const QgsCoordinateTransform &transform = QgsCoordinateTransform() );
66 
73  const QVector<QgsMeshVertex> &vertices() const ;
74 
76  const QVector<QgsMeshFace> &triangles() const ;
77 
79  const QVector<QgsMeshEdge> &edges() const ;
80 
86  Q_DECL_DEPRECATED const QVector<QgsMeshVertex> &centroids() const ;
87 
92  const QVector<QgsMeshVertex> &faceCentroids() const ;
93 
98  const QVector<QgsMeshVertex> &edgeCentroids() const ;
99 
101  const QVector<int> &trianglesToNativeFaces() const ;
102 
104  const QVector<int> &edgesToNativeEdges() const ;
105 
114  QgsPointXY transformFromLayerToTrianglesCoordinates( const QgsPointXY &point ) const;
115 
125  int faceIndexForPoint( const QgsPointXY &point ) const ;
126 
136  int faceIndexForPoint_v2( const QgsPointXY &point ) const;
137 
147  int nativeFaceIndexForPoint( const QgsPointXY &point ) const ;
148 
158  QList<int> nativeFaceIndexForRectangle( const QgsRectangle &rectangle ) const ;
159 
169  QList<int> faceIndexesForRectangle( const QgsRectangle &rectangle ) const ;
170 
178  QList<int> edgeIndexesForRectangle( const QgsRectangle &rectangle ) const ;
179 
188  QVector<QVector3D> vertexNormals( float vertScale ) const;
189 
205  QVector<QgsTriangularMesh *> simplifyMesh( double reductionFactor, int minimumTrianglesCount = 10 ) const;
206 
213  double averageTriangleSize() const;
214 
225  int levelOfDetail() const;
226 
234  QgsRectangle extent() const;
235 
240  bool contains( const QgsMesh::ElementType &type ) const;
241 
251  class Changes
252  {
253  public:
254 
256  Changes() = default;
257 
261  Changes( const QgsTopologicalMesh::Changes &topologicalChanges, const QgsMesh &nativeMesh );
262 
263  private:
264  // triangular mesh elements that can be changed if the triangular mesh is updated, are not stored and have to be retrieve
265  QVector<QgsMeshVertex> mAddedVertices;
266  QList<int> mVerticesIndexesToRemove;
267  QList<int> mChangedVerticesCoordinates;
268  mutable QList<double> mOldZValue;
269  QList<double> mNewZValue;
270  QList<QgsPointXY> mOldXYValue;
271  QList<QgsPointXY> mNewXYValue;
272 
273  QVector<QgsMeshFace> mNativeFacesToAdd;
274  QList<int> mNativeFaceIndexesToRemove;
275  QVector<QgsMeshFace> mNativeFacesToRemove;
276  QList<int> mNativeFaceIndexesGeometryChanged;
277  QVector<QgsMeshFace> mNativeFacesGeometryChanged;
278  mutable QList<int> mTriangleIndexesGeometryChanged;
279 
280  mutable int mTrianglesAddedStartIndex = 0; // defined each time the changes are apply
281  mutable QList<int> mRemovedTriangleIndexes; // defined when changes are apply the first time
282 
283  friend class QgsTriangularMesh;
284  };
285 
291  void applyChanges( const Changes &changes );
292 
298  void reverseChanges( const Changes &changes, const QgsMesh &nativeMesh );
299 
307 
315 
323 
324  private:
325 
336  void triangulate( const QgsMeshFace &face, int nativeIndex );
337 
338  void addVertex( const QgsMeshVertex &vertex );
339 
340  QgsMeshVertex transformVertex( const QgsMeshVertex &vertex, Qgis::TransformDirection direction ) const;
341 
342  // calculate the centroid of the native mesh, mNativeMeshCentroids container must have the emplacment for the corresponding centroid before calling this method
343  QgsMeshVertex calculateCentroid( const QgsMeshFace &nativeFace );
344 
345  // check clock wise and calculate average size of triangles
346  void finalizeTriangles();
347 
348  // vertices: map CRS; 0-N ... native vertices, N+1 - len ... extra vertices
349  // faces are derived triangles
350  QgsMesh mTriangularMesh;
351  QVector<int> mTrianglesToNativeFaces; //len(mTrianglesToNativeFaces) == len(mTriangles). Mapping derived -> native
352  QVector<int> mEdgesToNativeEdges; //len(mEdgesToNativeEdges) == len(mEdges). Mapping derived -> native
353 
354  // centroids of the native faces in map CRS
355  QVector<QgsMeshVertex> mNativeMeshFaceCentroids;
356 
357  // centroids of the native edges in map CRS
358  QVector<QgsMeshVertex> mNativeMeshEdgeCentroids;
359 
360  QgsMeshSpatialIndex mSpatialFaceIndex;
361  QgsMeshSpatialIndex mSpatialEdgeIndex;
362  QgsCoordinateTransform mCoordinateTransform; //coordinate transform used to convert native mesh vertices to map vertices
363 
364  mutable QgsRectangle mExtent;
365  mutable bool mIsExtentValid = false;
366 
367  // average size of the triangles
368  double mAverageTriangleSize = 0;
369  int mLod = 0;
370 
371  const QgsTriangularMesh *mBaseTriangularMesh = nullptr;
372 
373  friend class TestQgsTriangularMesh;
374 };
375 
376 namespace QgsMeshUtils
377 {
379  CORE_EXPORT QgsGeometry toGeometry( const QgsMeshFace &face, const QVector<QgsMeshVertex> &vertices );
380 
382  CORE_EXPORT QgsMeshVertex centroid( const QgsMeshFace &face, const QVector<QgsMeshVertex> &vertices );
383 
385  CORE_EXPORT std::unique_ptr< QgsPolygon > toPolygon( const QgsMeshFace &face, const QVector<QgsMeshVertex> &vertices );
386 
391  CORE_EXPORT QSet<int> nativeFacesFromTriangles( const QList<int> &triangleIndexes, const QVector<int> &trianglesToNativeFaces );
392 
397  CORE_EXPORT QSet<int> nativeEdgesFromEdges( const QList<int> &edgesIndexes, const QVector<int> &edgesToNativeEdges );
398 
403  CORE_EXPORT QSet<int> nativeVerticesFromTriangles( const QList<int> &triangleIndexes, const QVector<QgsMeshFace> &triangles );
404 
409  CORE_EXPORT QSet<int> nativeVerticesFromEdges( const QList<int> &edgesIndexes, const QVector<QgsMeshEdge> &edges );
410 
415  bool isInTriangleFace( const QgsPointXY point, const QgsMeshFace &face, const QVector<QgsMeshVertex> &vertices );
416 
421  void setCounterClockwise( QgsMeshFace &triangle, const QgsMeshVertex &v0, const QgsMeshVertex &v1, const QgsMeshVertex &v2 );
422 
423 };
424 
425 #endif // QGSTRIANGULARMESH_H
TransformDirection
Indicates the direction (forward or inverse) of a transform.
Definition: qgis.h:930
Class for doing transforms between two map coordinate systems.
A geometry is the spatial representation of a feature.
Definition: qgsgeometry.h:125
A spatial index for QgsMeshFace or QgsMeshEdge objects.
A class to represent a 2D point.
Definition: qgspointxy.h:59
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:49
A rectangle specified with double values.
Definition: qgsrectangle.h:42
Contains information about the context of a rendering operation.
Class that contains topological differences between two states of a topological mesh,...
The Changes class is used to make changes of the triangular and to keep traces of this changes If a C...
Changes()=default
Default constructor, no changes.
Triangular/Derived Mesh is mesh with vertices in map coordinates.
QgsRectangle nativeExtent()
Returns the extent of the mesh in the native mesh coordinates system, returns empty extent if the tra...
QgsTriangularMesh()
Ctor.
void reverseChanges(const Changes &changes, const QgsMesh &nativeMesh)
Reverses the changes on the triangular mesh (see Changes)
void applyChanges(const Changes &changes)
Applies the changes on the triangular mesh (see Changes)
QgsMeshVertex triangularToNativeCoordinates(const QgsMeshVertex &vertex) const
Transforms the vertex from triangular mesh coordinates system to native coordinates system.
QgsMeshVertex nativeToTriangularCoordinates(const QgsMeshVertex &vertex) const
Transforms the vertex from native coordinates system to triangular mesh coordinates system.
~QgsTriangularMesh()
Dtor.
bool isInTriangleFace(const QgsPointXY point, const QgsMeshFace &face, const QVector< QgsMeshVertex > &vertices)
Tests if point p is on the face defined with vertices.
CORE_EXPORT QSet< int > nativeEdgesFromEdges(const QList< int > &edgesIndexes, const QVector< int > &edgesToNativeEdges)
Returns unique native faces indexes from list of triangle indexes.
CORE_EXPORT std::unique_ptr< QgsPolygon > toPolygon(const QgsMeshFace &face, const QVector< QgsMeshVertex > &vertices)
Returns face as polygon geometry, caller is responsible for delete.
CORE_EXPORT QgsGeometry toGeometry(const QgsMeshFace &face, const QVector< QgsMeshVertex > &vertices)
Returns face as polygon geometry.
void setCounterClockwise(QgsMeshFace &triangle, const QgsMeshVertex &v0, const QgsMeshVertex &v1, const QgsMeshVertex &v2)
Checks if the triangle is counter clockwise, if not sets it counter clockwise.
CORE_EXPORT QSet< int > nativeVerticesFromEdges(const QList< int > &edgesIndexes, const QVector< QgsMeshEdge > &edges)
Returns unique native faces indexes from list of vertices of triangles.
CORE_EXPORT QgsMeshVertex centroid(const QgsMeshFace &face, const QVector< QgsMeshVertex > &vertices)
Returns the centroid of the face.
CORE_EXPORT QSet< int > nativeVerticesFromTriangles(const QList< int > &triangleIndexes, const QVector< QgsMeshFace > &triangles)
Returns unique native vertex indexes from list of vertices of triangles.
CORE_EXPORT QSet< int > nativeFacesFromTriangles(const QList< int > &triangleIndexes, const QVector< int > &trianglesToNativeFaces)
Returns unique native faces indexes from list of triangle indexes.
QVector< int > QgsMeshFace
List of vertex indexes.
Mesh - vertices, edges and faces.
ElementType
Defines type of mesh elements.