QGIS API Documentation  3.6.0-Noosa (5873452)
qgshillshadefilter.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgshillshadefilter.h - description
3  --------------------------------
4  begin : September 26th, 2011
5  copyright : (C) 2011 by Marco Hugentobler
6  email : marco dot hugentobler at sourcepole dot ch
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgshillshadefilter.h"
19 #include <cmath>
20 
21 QgsHillshadeFilter::QgsHillshadeFilter( const QString &inputFile, const QString &outputFile, const QString &outputFormat, double lightAzimuth,
22  double lightAngle )
23  : QgsDerivativeFilter( inputFile, outputFile, outputFormat )
24  , mLightAzimuth( static_cast<float>( lightAzimuth ) )
25  , mLightAngle( static_cast<float>( lightAngle ) )
26  , mCosZenithRad( std::cos( static_cast<float>( lightAngle * M_PI ) / 180.0f ) )
27  , mSinZenithRad( std::sin( static_cast<float>( lightAngle * M_PI ) / 180.0f ) )
28  , mAzimuthRad( static_cast<float>( lightAzimuth * M_PI ) / 180.0f )
29 {
30 }
31 
32 float QgsHillshadeFilter::processNineCellWindow( float *x11, float *x21, float *x31,
33  float *x12, float *x22, float *x32,
34  float *x13, float *x23, float *x33 )
35 {
36 
37  float derX = calcFirstDerX( x11, x21, x31, x12, x22, x32, x13, x23, x33 );
38  float derY = calcFirstDerY( x11, x21, x31, x12, x22, x32, x13, x23, x33 );
39 
40  if ( derX == mOutputNodataValue || derY == mOutputNodataValue )
41  {
42  return mOutputNodataValue;
43  }
44 
45  float slope_rad = std::atan( std::sqrt( derX * derX + derY * derY ) );
46  float aspect_rad = 0;
47  if ( derX == 0 && derY == 0 ) //aspect undefined, take a neutral value. Better solutions?
48  {
49  aspect_rad = mAzimuthRad / 2.0f;
50  }
51  else
52  {
53  aspect_rad = M_PI + std::atan2( derX, derY );
54  }
55  return std::max( 0.0f, 255.0f * ( ( mCosZenithRad * std::cos( slope_rad ) ) +
56  ( mSinZenithRad * std::sin( slope_rad ) *
57  std::cos( mAzimuthRad - aspect_rad ) ) ) );
58 }
59 
61 {
62  mLightAzimuth = azimuth;
63  mAzimuthRad = azimuth * static_cast<float>( M_PI ) / 180.0f;
64 }
65 
67 {
68  mLightAngle = angle;
69  mCosZenithRad = std::cos( angle * static_cast<float>( M_PI ) / 180.0f );
70  mSinZenithRad = std::sin( angle * static_cast<float>( M_PI ) / 180.0f );
71 }
72 
73 #ifdef HAVE_OPENCL
74 
75 void QgsHillshadeFilter::addExtraRasterParams( std::vector<float> &params )
76 {
77 
78  params.push_back( mCosZenithRad ); // cos_zenith_rad 5
79  params.push_back( mSinZenithRad ); // sin_zenith_rad 6
80  params.push_back( mAzimuthRad ); // azimuth_rad 7
81 
82 }
83 
84 #endif
float calcFirstDerY(float *x11, float *x21, float *x31, float *x12, float *x22, float *x32, float *x13, float *x23, float *x33)
Calculates the first order derivative in y-direction according to Horn (1981)
float calcFirstDerX(float *x11, float *x21, float *x31, float *x12, float *x22, float *x32, float *x13, float *x23, float *x33)
Calculates the first order derivative in x-direction according to Horn (1981)
double ANALYSIS_EXPORT angle(QgsPoint *p1, QgsPoint *p2, QgsPoint *p3, QgsPoint *p4)
Calculates the angle between two segments (in 2 dimension, z-values are ignored)
Definition: MathUtils.cpp:786
void setLightAzimuth(float azimuth)
QgsHillshadeFilter(const QString &inputFile, const QString &outputFile, const QString &outputFormat, double lightAzimuth=300, double lightAngle=40)
void setLightAngle(float angle)
Adds the ability to calculate derivatives in x- and y-directions.
float mOutputNodataValue
The nodata value of the output layer.
float processNineCellWindow(float *x11, float *x21, float *x31, float *x12, float *x22, float *x32, float *x13, float *x23, float *x33) override
Calculates output value from nine input values.