QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
LinTriangleInterpolator.cpp
Go to the documentation of this file.
1/***************************************************************************
2 LinTriangleInterpolator.cpp
3 ---------------------------
4 copyright : (C) 2004 by Marco Hugentobler
6 ***************************************************************************/
7
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
18#include "qgslogger.h"
19
20bool LinTriangleInterpolator::calcFirstDerX( double x, double y, Vector3D *vec )
21{
22 if ( vec && mTIN )
23 {
24 QgsPoint pt1( 0, 0, 0 );
25 QgsPoint pt2( 0, 0, 0 );
26 QgsPoint pt3( 0, 0, 0 );
27
28 if ( !mTIN->triangleVertices( x, y, pt1, pt2, pt3 ) )
29 {
30 return false; //point outside the convex hull or numerical problems
31 }
32
33 vec->setX( 1.0 );
34 vec->setY( 0.0 );
35 vec->setZ( ( pt1.z() * ( pt2.y() - pt3.y() ) + pt2.z() * ( pt3.y() - pt1.y() ) + pt3.z() * ( pt1.y() - pt2.y() ) ) / ( ( pt1.x() - pt2.x() ) * ( pt2.y() - pt3.y() ) - ( pt2.x() - pt3.x() ) * ( pt1.y() - pt2.y() ) ) );
36 return true;
37 }
38
39 else
40 {
41 QgsDebugError( QStringLiteral( "warning, null pointer" ) );
42 return false;
43 }
44}
45
46bool LinTriangleInterpolator::calcFirstDerY( double x, double y, Vector3D *vec )
47{
48 if ( vec && mTIN )
49 {
50 QgsPoint pt1( 0, 0, 0 );
51 QgsPoint pt2( 0, 0, 0 );
52 QgsPoint pt3( 0, 0, 0 );
53
54 if ( !mTIN->triangleVertices( x, y, pt1, pt2, pt3 ) )
55 {
56 return false;
57 }
58
59 vec->setX( 0 );
60 vec->setY( 1.0 );
61 vec->setZ( ( pt1.z() * ( pt2.x() - pt3.x() ) + pt2.z() * ( pt3.x() - pt1.x() ) + pt3.z() * ( pt1.x() - pt2.x() ) ) / ( ( pt1.y() - pt2.y() ) * ( pt2.x() - pt3.x() ) - ( pt2.y() - pt3.y() ) * ( pt1.x() - pt2.x() ) ) );
62 return true;
63 }
64
65 else
66 {
67 QgsDebugError( QStringLiteral( "warning, null pointer" ) );
68 return false;
69 }
70}
71
72bool LinTriangleInterpolator::calcNormVec( double x, double y, QgsPoint &vec )
73{
74 //calculate vector product of the two derivative vectors in x- and y-direction and set the length to 1
75 if ( mTIN )
76 {
77 Vector3D vec1;
78 Vector3D vec2;
79 if ( !calcFirstDerX( x, y, &vec1 ) )
80 {
81 return false;
82 }
83 if ( !calcFirstDerY( x, y, &vec2 ) )
84 {
85 return false;
86 }
87 const Vector3D vec3( vec1.getY() * vec2.getZ() - vec1.getZ() * vec2.getY(), vec1.getZ() * vec2.getX() - vec1.getX() * vec2.getZ(), vec1.getX() * vec2.getY() - vec1.getY() * vec2.getX() ); //calculate vector product
88 const double absvec3 = std::sqrt( vec3.getX() * vec3.getX() + vec3.getY() * vec3.getY() + vec3.getZ() * vec3.getZ() ); //length of vec3
89 vec.setX( vec3.getX() / absvec3 ); //standardize vec3 and assign it to vec
90 vec.setY( vec3.getY() / absvec3 );
91 vec.setZ( vec3.getZ() / absvec3 );
92 return true;
93 }
94
95 else
96 {
97 QgsDebugError( QStringLiteral( "warning, null pointer" ) );
98 return false;
99 }
100}
101
102bool LinTriangleInterpolator::calcPoint( double x, double y, QgsPoint &point )
103{
104 if ( mTIN )
105 {
106 QgsPoint pt1( 0, 0, 0 );
107 QgsPoint pt2( 0, 0, 0 );
108 QgsPoint pt3( 0, 0, 0 );
109
110 if ( !mTIN->triangleVertices( x, y, pt1, pt2, pt3 ) )
111 {
112 return false; //point is outside the convex hull or numerical problems
113 }
114
115 const double a = ( pt1.z() * ( pt2.y() - pt3.y() ) + pt2.z() * ( pt3.y() - pt1.y() ) + pt3.z() * ( pt1.y() - pt2.y() ) ) / ( ( pt1.x() - pt2.x() ) * ( pt2.y() - pt3.y() ) - ( pt2.x() - pt3.x() ) * ( pt1.y() - pt2.y() ) );
116 const double b = ( pt1.z() * ( pt2.x() - pt3.x() ) + pt2.z() * ( pt3.x() - pt1.x() ) + pt3.z() * ( pt1.x() - pt2.x() ) ) / ( ( pt1.y() - pt2.y() ) * ( pt2.x() - pt3.x() ) - ( pt2.y() - pt3.y() ) * ( pt1.x() - pt2.x() ) );
117 const double c = pt1.z() - a * pt1.x() - b * pt1.y();
118
119 point.setX( x );
120 point.setY( y );
121 point.setZ( a * x + b * y + c );
122 return true;
123 }
124 else
125 {
126 QgsDebugError( QStringLiteral( "warning, null pointer" ) );
127 return false;
128 }
129}
virtual bool calcFirstDerX(double x, double y, Vector3D *result)
Calculates the first derivative with respect to x for a linear surface and assigns it to vec.
bool calcNormVec(double x, double y, QgsPoint &result) override
Calculates the normal vector and assigns it to vec.
virtual bool calcFirstDerY(double x, double y, Vector3D *result)
Calculates the first derivative with respect to y for a linear surface and assigns it to vec.
QgsDualEdgeTriangulation * mTIN
bool calcPoint(double x, double y, QgsPoint &result) override
Performs a linear interpolation in a triangle and assigns the x-,y- and z-coordinates to point.
bool triangleVertices(double x, double y, QgsPoint &p1, int &n1, QgsPoint &p2, int &n2, QgsPoint &p3, int &n3) override
Finds out in which triangle the point with coordinates x and y is and assigns the numbers of the vert...
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:49
void setY(double y)
Sets the point's y-coordinate.
Definition qgspoint.h:343
void setX(double x)
Sets the point's x-coordinate.
Definition qgspoint.h:332
double z
Definition qgspoint.h:54
double x
Definition qgspoint.h:52
void setZ(double z)
Sets the point's z-coordinate.
Definition qgspoint.h:356
double y
Definition qgspoint.h:53
Class Vector3D represents a 3D-Vector, capable to store x-,y- and z-coordinates in double values.
Definition Vector3D.h:36
void setX(double x)
Sets the x-component of the vector.
Definition Vector3D.h:105
double getY() const
Returns the y-component of the vector.
Definition Vector3D.h:95
double getX() const
Returns the x-component of the vector.
Definition Vector3D.h:90
void setY(double y)
Sets the y-component of the vector.
Definition Vector3D.h:110
double getZ() const
Returns the z-component of the vector.
Definition Vector3D.h:100
void setZ(double z)
Sets the z-component of the vector.
Definition Vector3D.h:115
As part of the API refactoring and improvements which landed in the Processing API was substantially reworked from the x version This was done in order to allow much of the underlying Processing framework to be ported into c
#define QgsDebugError(str)
Definition qgslogger.h:38