Skip to content

Commit

Permalink
format code and add documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterPetrik committed Aug 10, 2018
1 parent c79e1d0 commit 9ea0bc2
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 20 deletions.
2 changes: 2 additions & 0 deletions python/core/auto_generated/mesh/qgsmeshlayer.sip.in
Expand Up @@ -197,6 +197,8 @@ Interpolates the value on the given point from given dataset.

Returns NaN for NaN values, for values outside range or when
triangular mesh is not yet initialized on given point

.. versionadded:: 3.4
%End

signals:
Expand Down
4 changes: 2 additions & 2 deletions src/core/mesh/qgsmeshlayer.cpp
Expand Up @@ -195,8 +195,8 @@ QgsMeshDatasetValue QgsMeshLayer::datasetValue( const QgsMeshDatasetIndex &index
bool isOnFaces = dataProvider()->datasetGroupMetadata( index ).isOnFaces();
if ( isOnFaces )
{
int native_face_index = mTriangularMesh->trianglesToNativeFaces().at( face_index );
return dataProvider()->datasetValue( index, native_face_index );
int nativeFaceIndex = mTriangularMesh->trianglesToNativeFaces().at( face_index );
return dataProvider()->datasetValue( index, nativeFaceIndex );
}
else
{
Expand Down
2 changes: 2 additions & 0 deletions src/core/mesh/qgsmeshlayer.h
Expand Up @@ -188,6 +188,8 @@ class CORE_EXPORT QgsMeshLayer : public QgsMapLayer
*
* Returns NaN for NaN values, for values outside range or when
* triangular mesh is not yet initialized on given point
*
* \since QGIS 3.4
*/
QgsMeshDatasetValue datasetValue( const QgsMeshDatasetIndex &index, const QgsPointXY &point ) const;

Expand Down
20 changes: 15 additions & 5 deletions src/core/mesh/qgsmeshlayerinterpolator.h
Expand Up @@ -59,11 +59,21 @@ class QgsMeshLayerInterpolator : public QgsRasterInterface
int bandCount() const override;
QgsRasterBlock *block( int, const QgsRectangle &extent, int width, int height, QgsRasterBlockFeedback *feedback = nullptr ) override;


static double interpolateFromVerticesData( const QgsPointXY &p1,
const QgsPointXY &p2,
const QgsPointXY &p3,
double val1, double val2, double val3, const QgsPointXY &pt );
/*
* Interpolates value based on known values on the vertices of a triangle
* \param p1 first vertex of the triangle
* \param p2 second vertex of the triangle
* \param p3 third vertex of the triangle
* \param val1 value on p1 of the triangle
* \param val2 value on p2 of the triangle
* \param val3 value on p3 of the triangle
* \param pt point where to calculate value
* \returns value on the point pt or NaN in case the point is outside the triangle
*/
static double interpolateFromVerticesData(
const QgsPointXY &p1, const QgsPointXY &p2, const QgsPointXY &p3,
double val1, double val2, double val3, const QgsPointXY &pt
);

private:
const QgsTriangularMesh &mTriangularMesh;
Expand Down
27 changes: 14 additions & 13 deletions src/core/mesh/qgstriangularmesh.cpp
Expand Up @@ -15,9 +15,11 @@
* *
***************************************************************************/

#include <memory>
#include <QList>
#include "qgsfeature.h"

#include "qgspolygon.h"
#include "qgslinestring.h"
#include "qgstriangularmesh.h"
#include "qgsrendercontext.h"
#include "qgscoordinatetransform.h"
Expand Down Expand Up @@ -152,8 +154,7 @@ void QgsTriangularMesh::update( QgsMesh *nativeMesh, QgsRenderContext *context )
{
const QgsMeshFace &face = mTriangularMesh.faces.at( i ) ;
QgsGeometry geom = QgsMeshUtils::toGeometry( face, mTriangularMesh.vertices );
bool success = mSpatialIndex.insertFeature( i, geom.boundingBox() );
Q_UNUSED( success );
( void )mSpatialIndex.insertFeature( i, geom.boundingBox() );
}
}

Expand Down Expand Up @@ -182,26 +183,26 @@ int QgsTriangularMesh::faceIndexForPoint( const QgsPointXY &point ) const
const QList<QgsFeatureId> face_indexes = mSpatialIndex.intersects( QgsRectangle( point, point ) );
for ( QgsFeatureId fid : face_indexes )
{
int face_index = static_cast<int>( fid );
const QgsMeshFace &face = mTriangularMesh.faces.at( face_index );
int faceIndex = static_cast<int>( fid );
const QgsMeshFace &face = mTriangularMesh.faces.at( faceIndex );
const QgsGeometry geom = QgsMeshUtils::toGeometry( face, mTriangularMesh.vertices );
if ( geom.contains( &point ) )
return face_index;
return faceIndex;
}
return -1;
}

QgsGeometry QgsMeshUtils::toGeometry( const QgsMeshFace &face, const QVector<QgsMeshVertex> &vertices )
{
QVector<QgsPointXY> ring;
QVector<QgsPoint> ring;
for ( int j = 0; j < face.size(); ++j )
{
int vertex_id = face[j];
Q_ASSERT( vertex_id < vertices.size() );
const QgsPoint &vertex = vertices[vertex_id];
int vertexId = face[j];
Q_ASSERT( vertexId < vertices.size() );
const QgsPoint &vertex = vertices[vertexId];
ring.append( vertex );
}
QgsPolygonXY polygon;
polygon.append( ring );
return QgsGeometry::fromPolygonXY( polygon );
std::unique_ptr< QgsPolygon > polygon = qgis::make_unique< QgsPolygon >();
polygon->setExteriorRing( new QgsLineString( ring ) );
return QgsGeometry( std::move( polygon ) );
}
2 changes: 2 additions & 0 deletions src/core/mesh/qgstriangularmesh.h
Expand Up @@ -83,6 +83,8 @@ class CORE_EXPORT QgsTriangularMesh
/**
* Returns triangle index that contains the given point, -1 if no such triangle exists
* It uses spatial indexing
*
* \since QGIS 3.4
*/
int faceIndexForPoint( const QgsPointXY &point ) const ;

Expand Down

0 comments on commit 9ea0bc2

Please sign in to comment.