QGIS API Documentation  3.6.0-Noosa (5873452)
LinTriangleInterpolator.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  LinTriangleInterpolator.cpp
3  ---------------------------
4  copyright : (C) 2004 by Marco Hugentobler
5  email : [email protected]
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 
20 bool LinTriangleInterpolator::calcFirstDerX( double x, double y, Vector3D *vec )
21 {
22 
23  if ( vec && mTIN )
24  {
25  QgsPoint pt1( 0, 0, 0 );
26  QgsPoint pt2( 0, 0, 0 );
27  QgsPoint pt3( 0, 0, 0 );
28 
29  if ( !mTIN->getTriangle( x, y, pt1, pt2, pt3 ) )
30  {
31  return false;//point outside the convex hull or numerical problems
32  }
33 
34  vec->setX( 1.0 );
35  vec->setY( 0.0 );
36  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() ) ) );
37  return true;
38  }
39 
40  else
41  {
42  QgsDebugMsg( QStringLiteral( "warning, null pointer" ) );
43  return false;
44  }
45 }
46 
47 bool LinTriangleInterpolator::calcFirstDerY( double x, double y, Vector3D *vec )
48 {
49  if ( vec && mTIN )
50  {
51  QgsPoint pt1( 0, 0, 0 );
52  QgsPoint pt2( 0, 0, 0 );
53  QgsPoint pt3( 0, 0, 0 );
54 
55  if ( !mTIN->getTriangle( x, y, pt1, pt2, pt3 ) )
56  {
57  return false;
58  }
59 
60  vec->setX( 0 );
61  vec->setY( 1.0 );
62  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() ) ) );
63  return true;
64  }
65 
66  else
67  {
68  QgsDebugMsg( QStringLiteral( "warning, null pointer" ) );
69  return false;
70  }
71 }
72 
73 bool LinTriangleInterpolator::calcNormVec( double x, double y, Vector3D *vec )
74 {
75 //calculate vector product of the two derivative vectors in x- and y-direction and set the length to 1
76  if ( vec && mTIN )
77  {
78  Vector3D vec1;
79  Vector3D vec2;
80  if ( !calcFirstDerX( x, y, &vec1 ) )
81  {return false;}
82  if ( !calcFirstDerY( x, y, &vec2 ) )
83  {return false;}
84  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
85  double absvec3 = std::sqrt( vec3.getX() * vec3.getX() + vec3.getY() * vec3.getY() + vec3.getZ() * vec3.getZ() );//length of vec3
86  vec->setX( vec3.getX() / absvec3 );//standardize vec3 and assign it to vec
87  vec->setY( vec3.getY() / absvec3 );
88  vec->setZ( vec3.getZ() / absvec3 );
89  return true;
90  }
91 
92  else
93  {
94  QgsDebugMsg( QStringLiteral( "warning, null pointer" ) );
95  return false;
96  }
97 
98 }
99 
100 bool LinTriangleInterpolator::calcPoint( double x, double y, QgsPoint &point )
101 {
102  if ( mTIN )
103  {
104  QgsPoint pt1( 0, 0, 0 );
105  QgsPoint pt2( 0, 0, 0 );
106  QgsPoint pt3( 0, 0, 0 );
107 
108  if ( !mTIN->getTriangle( x, y, pt1, pt2, pt3 ) )
109  {
110  return false;//point is outside the convex hull or numerical problems
111  }
112 
113  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() ) );
114  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() ) );
115  double c = pt1.z() - a * pt1.x() - b * pt1.y();
116 
117  point.setX( x );
118  point.setY( y );
119  point.setZ( a * x + b * y + c );
120  return true;
121  }
122  else
123  {
124  QgsDebugMsg( QStringLiteral( "warning, null pointer" ) );
125  return false;
126  }
127 
128 }
129 
130 
131 
132 
133 
134 
135 
136 
137 
138 
void setX(double x)
Sets the x-component of the vector.
Definition: Vector3D.h:103
double y
Definition: qgspoint.h:42
void setZ(double z)
Sets the point's z-coordinate.
Definition: qgspoint.h:237
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...
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
bool getTriangle(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...
double getZ() const
Returns the z-component of the vector.
Definition: Vector3D.h:98
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
DualEdgeTriangulation * mTIN
Class Vector3D represents a 3D-Vector, capable to store x-,y- and z-coordinates in double values...
Definition: Vector3D.h:33
void setZ(double z)
Sets the z-component of the vector.
Definition: Vector3D.h:113
Point geometry type, with support for z-dimension and m-values.
Definition: qgspoint.h:37
bool calcNormVec(double x, double y, Vector3D *result) override
Calculates the normal vector and assigns it to vec.
void setX(double x)
Sets the point's x-coordinate.
Definition: qgspoint.h:213
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...
void setY(double y)
Sets the point's y-coordinate.
Definition: qgspoint.h:224
double getY() const
Returns the y-component of the vector.
Definition: Vector3D.h:93
void setY(double y)
Sets the y-component of the vector.
Definition: Vector3D.h:108
double z
Definition: qgspoint.h:43
double getX() const
Returns the x-component of the vector.
Definition: Vector3D.h:88
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...
double x
Definition: qgspoint.h:41