QGIS API Documentation 3.99.0-Master (09f76ad7019)
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
19#include "qgslogger.h"
20
21#include <QString>
22
23using namespace Qt::StringLiterals;
24
25bool LinTriangleInterpolator::calcFirstDerX( double x, double y, Vector3D *vec )
26{
27 if ( vec && mTIN )
28 {
29 QgsPoint pt1( 0, 0, 0 );
30 QgsPoint pt2( 0, 0, 0 );
31 QgsPoint pt3( 0, 0, 0 );
32
33 if ( !mTIN->triangleVertices( x, y, pt1, pt2, pt3 ) )
34 {
35 return false; //point outside the convex hull or numerical problems
36 }
37
38 vec->setX( 1.0 );
39 vec->setY( 0.0 );
40 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() ) ) );
41 return true;
42 }
43
44 else
45 {
46 QgsDebugError( u"warning, null pointer"_s );
47 return false;
48 }
49}
50
51bool LinTriangleInterpolator::calcFirstDerY( double x, double y, Vector3D *vec )
52{
53 if ( vec && mTIN )
54 {
55 QgsPoint pt1( 0, 0, 0 );
56 QgsPoint pt2( 0, 0, 0 );
57 QgsPoint pt3( 0, 0, 0 );
58
59 if ( !mTIN->triangleVertices( x, y, pt1, pt2, pt3 ) )
60 {
61 return false;
62 }
63
64 vec->setX( 0 );
65 vec->setY( 1.0 );
66 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() ) ) );
67 return true;
68 }
69
70 else
71 {
72 QgsDebugError( u"warning, null pointer"_s );
73 return false;
74 }
75}
76
77bool LinTriangleInterpolator::calcNormVec( double x, double y, QgsPoint &vec )
78{
79 //calculate vector product of the two derivative vectors in x- and y-direction and set the length to 1
80 if ( mTIN )
81 {
82 Vector3D vec1;
83 Vector3D vec2;
84 if ( !calcFirstDerX( x, y, &vec1 ) )
85 {
86 return false;
87 }
88 if ( !calcFirstDerY( x, y, &vec2 ) )
89 {
90 return false;
91 }
92 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
93 const double absvec3 = std::sqrt( vec3.getX() * vec3.getX() + vec3.getY() * vec3.getY() + vec3.getZ() * vec3.getZ() ); //length of vec3
94 vec.setX( vec3.getX() / absvec3 ); //standardize vec3 and assign it to vec
95 vec.setY( vec3.getY() / absvec3 );
96 vec.setZ( vec3.getZ() / absvec3 );
97 return true;
98 }
99
100 else
101 {
102 QgsDebugError( u"warning, null pointer"_s );
103 return false;
104 }
105}
106
107bool LinTriangleInterpolator::calcPoint( double x, double y, QgsPoint &point )
108{
109 if ( mTIN )
110 {
111 QgsPoint pt1( 0, 0, 0 );
112 QgsPoint pt2( 0, 0, 0 );
113 QgsPoint pt3( 0, 0, 0 );
114
115 if ( !mTIN->triangleVertices( x, y, pt1, pt2, pt3 ) )
116 {
117 return false; //point is outside the convex hull or numerical problems
118 }
119
120 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() ) );
121 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() ) );
122 const double c = pt1.z() - a * pt1.x() - b * pt1.y();
123
124 point.setX( x );
125 point.setY( y );
126 point.setZ( a * x + b * y + c );
127 return true;
128 }
129 else
130 {
131 QgsDebugError( u"warning, null pointer"_s );
132 return false;
133 }
134}
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.
Point geometry type, with support for z-dimension and m-values.
Definition qgspoint.h:53
void setY(double y)
Sets the point's y-coordinate.
Definition qgspoint.h:370
void setX(double x)
Sets the point's x-coordinate.
Definition qgspoint.h:359
double z
Definition qgspoint.h:58
double x
Definition qgspoint.h:56
void setZ(double z)
Sets the point's z-coordinate.
Definition qgspoint.h:383
double y
Definition qgspoint.h:57
Represents a 3D-Vector, capable of storing x, y and z-coordinates in double values.
Definition Vector3D.h:37
void setX(double x)
Sets the x-component of the vector.
Definition Vector3D.h:106
double getY() const
Returns the y-component of the vector.
Definition Vector3D.h:96
double getX() const
Returns the x-component of the vector.
Definition Vector3D.h:91
void setY(double y)
Sets the y-component of the vector.
Definition Vector3D.h:111
double getZ() const
Returns the z-component of the vector.
Definition Vector3D.h:101
void setZ(double z)
Sets the z-component of the vector.
Definition Vector3D.h:116
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:59