QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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(
41 ( pt1.z() * ( pt2.y() - pt3.y() ) + pt2.z() * ( pt3.y() - pt1.y() ) + pt3.z() * ( pt1.y() - pt2.y() ) )
42 / ( ( pt1.x() - pt2.x() ) * ( pt2.y() - pt3.y() ) - ( pt2.x() - pt3.x() ) * ( pt1.y() - pt2.y() ) )
43 );
44 return true;
45 }
46
47 else
48 {
49 QgsDebugError( u"warning, null pointer"_s );
50 return false;
51 }
52}
53
54bool LinTriangleInterpolator::calcFirstDerY( double x, double y, Vector3D *vec )
55{
56 if ( vec && mTIN )
57 {
58 QgsPoint pt1( 0, 0, 0 );
59 QgsPoint pt2( 0, 0, 0 );
60 QgsPoint pt3( 0, 0, 0 );
61
62 if ( !mTIN->triangleVertices( x, y, pt1, pt2, pt3 ) )
63 {
64 return false;
65 }
66
67 vec->setX( 0 );
68 vec->setY( 1.0 );
69 vec->setZ(
70 ( pt1.z() * ( pt2.x() - pt3.x() ) + pt2.z() * ( pt3.x() - pt1.x() ) + pt3.z() * ( pt1.x() - pt2.x() ) )
71 / ( ( pt1.y() - pt2.y() ) * ( pt2.x() - pt3.x() ) - ( pt2.y() - pt3.y() ) * ( pt1.x() - pt2.x() ) )
72 );
73 return true;
74 }
75
76 else
77 {
78 QgsDebugError( u"warning, null pointer"_s );
79 return false;
80 }
81}
82
83bool LinTriangleInterpolator::calcNormVec( double x, double y, QgsPoint &vec )
84{
85 //calculate vector product of the two derivative vectors in x- and y-direction and set the length to 1
86 if ( mTIN )
87 {
88 Vector3D vec1;
89 Vector3D vec2;
90 if ( !calcFirstDerX( x, y, &vec1 ) )
91 {
92 return false;
93 }
94 if ( !calcFirstDerY( x, y, &vec2 ) )
95 {
96 return false;
97 }
98 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
99 const double absvec3 = std::sqrt( vec3.getX() * vec3.getX() + vec3.getY() * vec3.getY() + vec3.getZ() * vec3.getZ() ); //length of vec3
100 vec.setX( vec3.getX() / absvec3 ); //standardize vec3 and assign it to vec
101 vec.setY( vec3.getY() / absvec3 );
102 vec.setZ( vec3.getZ() / absvec3 );
103 return true;
104 }
105
106 else
107 {
108 QgsDebugError( u"warning, null pointer"_s );
109 return false;
110 }
111}
112
113bool LinTriangleInterpolator::calcPoint( double x, double y, QgsPoint &point )
114{
115 if ( mTIN )
116 {
117 QgsPoint pt1( 0, 0, 0 );
118 QgsPoint pt2( 0, 0, 0 );
119 QgsPoint pt3( 0, 0, 0 );
120
121 if ( !mTIN->triangleVertices( x, y, pt1, pt2, pt3 ) )
122 {
123 return false; //point is outside the convex hull or numerical problems
124 }
125
126 const double a = ( pt1.z() * ( pt2.y() - pt3.y() ) + pt2.z() * ( pt3.y() - pt1.y() ) + pt3.z() * ( pt1.y() - pt2.y() ) )
127 / ( ( pt1.x() - pt2.x() ) * ( pt2.y() - pt3.y() ) - ( pt2.x() - pt3.x() ) * ( pt1.y() - pt2.y() ) );
128 const double b = ( pt1.z() * ( pt2.x() - pt3.x() ) + pt2.z() * ( pt3.x() - pt1.x() ) + pt3.z() * ( pt1.x() - pt2.x() ) )
129 / ( ( pt1.y() - pt2.y() ) * ( pt2.x() - pt3.x() ) - ( pt2.y() - pt3.y() ) * ( pt1.x() - pt2.x() ) );
130 const double c = pt1.z() - a * pt1.x() - b * pt1.y();
131
132 point.setX( x );
133 point.setY( y );
134 point.setZ( a * x + b * y + c );
135 return true;
136 }
137 else
138 {
139 QgsDebugError( u"warning, null pointer"_s );
140 return false;
141 }
142}
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:387
void setX(double x)
Sets the point's x-coordinate.
Definition qgspoint.h:376
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:400
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: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:59