QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
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
21QgsHillshadeFilter::QgsHillshadeFilter( const QString &inputFile, const QString &outputFile, const QString &outputFormat, double lightAzimuth, double lightAngle )
22 : QgsDerivativeFilter( inputFile, outputFile, outputFormat )
23 , mLightAzimuth( static_cast<float>( lightAzimuth ) )
24 , mLightAngle( static_cast<float>( lightAngle ) )
25 , mCosZenithRad( std::cos( static_cast<float>( lightAngle * M_PI ) / 180.0f ) )
26 , mSinZenithRad( std::sin( static_cast<float>( lightAngle * M_PI ) / 180.0f ) )
27 , mAzimuthRad( static_cast<float>( lightAzimuth * M_PI ) / 180.0f )
28{
29}
30
31float QgsHillshadeFilter::processNineCellWindow( float *x11, float *x21, float *x31, float *x12, float *x22, float *x32, float *x13, float *x23, float *x33 )
32{
33 const float derX = calcFirstDerX( x11, x21, x31, x12, x22, x32, x13, x23, x33 );
34 const float derY = calcFirstDerY( x11, x21, x31, x12, x22, x32, x13, x23, x33 );
35
36 if ( derX == mOutputNodataValue || derY == mOutputNodataValue )
37 {
38 return mOutputNodataValue;
39 }
40
41 const float slope_rad = std::atan( std::sqrt( derX * derX + derY * derY ) );
42 float aspect_rad = 0;
43 if ( derX == 0 && derY == 0 ) //aspect undefined, take a neutral value. Better solutions?
44 {
45 aspect_rad = mAzimuthRad / 2.0f;
46 }
47 else
48 {
49 aspect_rad = M_PI + std::atan2( derX, derY );
50 }
51 return std::max( 0.0f, 255.0f * ( ( mCosZenithRad * std::cos( slope_rad ) ) + ( mSinZenithRad * std::sin( slope_rad ) * std::cos( mAzimuthRad - aspect_rad ) ) ) );
52}
53
55{
56 mLightAzimuth = azimuth;
57 mAzimuthRad = azimuth * static_cast<float>( M_PI ) / 180.0f;
58}
59
61{
62 mLightAngle = angle;
63 mCosZenithRad = std::cos( angle * static_cast<float>( M_PI ) / 180.0f );
64 mSinZenithRad = std::sin( angle * static_cast<float>( M_PI ) / 180.0f );
65}
66
67#ifdef HAVE_OPENCL
68
69void QgsHillshadeFilter::addExtraRasterParams( std::vector<float> &params )
70{
71 params.push_back( mCosZenithRad ); // cos_zenith_rad 5
72 params.push_back( mSinZenithRad ); // sin_zenith_rad 6
73 params.push_back( mAzimuthRad ); // azimuth_rad 7
74}
75
76#endif
Adds the ability to calculate derivatives in x- and y-directions.
float calcFirstDerX(float *x11, float *x21, float *x31, float *x12, float *x22, float *x32, float *x13, float *x23, float *x33) const
Calculates the first order derivative in x-direction according to Horn (1981)
float calcFirstDerY(float *x11, float *x21, float *x31, float *x12, float *x22, float *x32, float *x13, float *x23, float *x33) const
Calculates the first order derivative in y-direction according to Horn (1981)
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.
void setLightAngle(float angle)
QgsHillshadeFilter(const QString &inputFile, const QString &outputFile, const QString &outputFormat, double lightAzimuth=300, double lightAngle=40)
void setLightAzimuth(float azimuth)
float mOutputNodataValue
The nodata value of the output layer.