2929
3030#include < QDir>
3131
32+ // If true regenerate raster reference images
33+ const bool REGENERATE_REFERENCES = false ;
3234
3335class TestNineCellFilters : public QObject
3436{
@@ -38,18 +40,24 @@ class TestNineCellFilters : public QObject
3840 private slots:
3941
4042 void initTestCase ();
43+ void init ();
44+
45+ void testHillshade ();
4146 void testSlope ();
4247 void testAspect ();
43- void testHillshade ();
4448 void testRuggedness ();
4549 void testTotalCurvature ();
4650#ifdef HAVE_OPENCL
51+ void testHillshadeCl ();
4752 void testSlopeCl ();
4853 void testAspectCl ();
54+ void testRuggednessCl ();
4955#endif
5056
5157 private:
5258
59+ void _rasterCompare ( QgsAlignRaster::RasterInfo &out, QgsAlignRaster::RasterInfo &ref );
60+
5361 template <class T > void _testAlg ( const QString &name, bool useOpenCl = false );
5462
5563 static QString referenceFile ( const QString &name )
@@ -64,6 +72,13 @@ class TestNineCellFilters : public QObject
6472};
6573
6674
75+ void TestNineCellFilters::init ()
76+ {
77+ #ifdef HAVE_OPENCL
78+ // Reset to default in case some tests mess it up
79+ QgsOpenClUtils::setSourcePath ( QDir ( QgsApplication::pkgDataPath () ).absoluteFilePath ( QStringLiteral ( " resources/opencl_programs" ) ) );
80+ #endif
81+ }
6782
6883void TestNineCellFilters::initTestCase ()
6984{
@@ -77,16 +92,12 @@ template <class T>
7792void TestNineCellFilters::_testAlg ( const QString &name, bool useOpenCl )
7893{
7994#ifdef HAVE_OPENCL
80- QgsSettings (). setValue ( QStringLiteral ( " useOpenCl" ), true , QgsSettings::Section::Core );
95+ QgsOpenClUtils::setEnabled ( useOpenCl );
8196 QString tmpFile ( tempFile ( name + ( useOpenCl ? " _opencl" : " " ) ) );
8297#else
83- Q_UNUSED ( useOpenCl );
8498 QString tmpFile ( tempFile ( name ) );
8599#endif
86100 QString refFile ( referenceFile ( name ) );
87- QgsAlignRaster::RasterInfo in ( SRC_FILE );
88- QSize inSize ( in.rasterSize () );
89- QSizeF inCellSize ( in.cellSize ( ) );
90101 T ninecellsfilter ( SRC_FILE, tmpFile, " GTiff" );
91102 int res = ninecellsfilter.processRaster ();
92103 QVERIFY ( res == 0 );
@@ -95,20 +106,20 @@ void TestNineCellFilters::_testAlg( const QString &name, bool useOpenCl )
95106 QgsAlignRaster::RasterInfo out ( tmpFile );
96107 QVERIFY ( out.isValid () );
97108
98- // Reference file
99- QgsAlignRaster::RasterInfo ref ( refFile );
100- QSize refSize ( ref. rasterSize () );
101- QSizeF refCellSize ( ref. cellSize ( ) );
102-
103- QCOMPARE ( out. rasterSize (), inSize );
104- QCOMPARE ( out. cellSize (), inCellSize );
105- QCOMPARE ( out. rasterSize (), refSize );
106- QCOMPARE ( out. cellSize (), refCellSize );
109+ // Regenerate reference rasters
110+ if ( ! useOpenCl && REGENERATE_REFERENCES )
111+ {
112+ if ( QFile::exists ( refFile ) )
113+ {
114+ QFile::remove ( refFile );
115+ }
116+ QVERIFY ( QFile::copy ( tmpFile, refFile ) );
117+ }
107118
108- double refId1 ( ref. identify ( 4081812 , 2431750 ) );
109- double refId2 ( ref. identify ( 4081312 , 2431350 ) );
110- QVERIFY ( qAbs ( out. identify ( 4081812 , 2431750 ) - refId1 ) < 0 . 0001f ) ;
111- QVERIFY ( qAbs ( out. identify ( 4081312 , 2431350 ) - refId2 ) < 0 . 0001f );
119+ // Reference
120+ QgsAlignRaster::RasterInfo ref ( refFile );
121+ // qDebug() << "Comparing " << tmpFile << refFile ;
122+ _rasterCompare ( out, ref );
112123
113124}
114125
@@ -118,7 +129,6 @@ void TestNineCellFilters::testSlope()
118129 _testAlg<QgsSlopeFilter>( QStringLiteral ( " slope" ) );
119130}
120131
121-
122132void TestNineCellFilters::testAspect ()
123133{
124134 _testAlg<QgsAspectFilter>( QStringLiteral ( " aspect" ) );
@@ -130,24 +140,75 @@ void TestNineCellFilters::testSlopeCl()
130140 _testAlg<QgsSlopeFilter>( QStringLiteral ( " slope" ), true );
131141}
132142
133-
134143void TestNineCellFilters::testAspectCl ()
135144{
136145 _testAlg<QgsAspectFilter>( QStringLiteral ( " aspect" ), true );
137146}
147+
148+ void TestNineCellFilters::testHillshadeCl ()
149+ {
150+ _testAlg<QgsHillshadeFilter>( QStringLiteral ( " hillshade" ), true );
151+ }
152+
153+ void TestNineCellFilters::testRuggednessCl ()
154+ {
155+ _testAlg<QgsRuggednessFilter>( QStringLiteral ( " ruggedness" ), true );
156+ }
157+
138158#endif
139159
140160void TestNineCellFilters::testHillshade ()
141161{
142162 _testAlg<QgsHillshadeFilter>( QStringLiteral ( " hillshade" ) );
143163}
144164
145-
146165void TestNineCellFilters::testRuggedness ()
147166{
148167 _testAlg<QgsRuggednessFilter>( QStringLiteral ( " ruggedness" ) );
149168}
150169
170+ void TestNineCellFilters::_rasterCompare ( QgsAlignRaster::RasterInfo &out, QgsAlignRaster::RasterInfo &ref )
171+ {
172+ QSize refSize ( ref.rasterSize () );
173+ QSizeF refCellSize ( ref.cellSize ( ) );
174+ QgsAlignRaster::RasterInfo in ( SRC_FILE );
175+ QSize inSize ( in.rasterSize () );
176+ QSizeF inCellSize ( in.cellSize ( ) );
177+ QCOMPARE ( out.rasterSize (), inSize );
178+ QCOMPARE ( out.cellSize (), inCellSize );
179+ QCOMPARE ( out.rasterSize (), refSize );
180+ QCOMPARE ( out.cellSize (), refCellSize );
181+
182+ // If the values differ less than tolerance they are considered equal
183+ double tolerance = 0.0000001 ;
184+
185+ // Check three points
186+ std::map<int , int > controlPoints;
187+ controlPoints[4081812 ] = 2431750 ;
188+ controlPoints[4081312 ] = 2431350 ;
189+ controlPoints[4080263 ] = 2429558 ;
190+ // South West corner
191+ controlPoints[4081512 ] = 2431550 ;
192+ // North east corner
193+ controlPoints[4085367 ] = 2434940 ;
194+ // North west corner
195+ controlPoints[4078263 ] = 2434936 ;
196+ // South east corner
197+ controlPoints[4085374 ] = 2428551 ;
198+
199+ for ( const auto &cp : controlPoints )
200+ {
201+ int x = cp.first ;
202+ int y = cp.second ;
203+ double outVal = out.identify ( x, y );
204+ double refVal = ref.identify ( x, y );
205+ double diff ( qAbs ( outVal - refVal ) );
206+ // qDebug() << outVal << refVal;
207+ // qDebug() << "Identify " << x << "," << y << " diff " << diff << " check: < " << tolerance;
208+ QVERIFY ( diff <= tolerance );
209+ }
210+
211+ }
151212
152213void TestNineCellFilters::testTotalCurvature ()
153214{
0 commit comments