GDAL
viewshed_executor.h
1/******************************************************************************
2 *
3 * Project: Viewshed Generation
4 * Purpose: Core algorithm implementation for viewshed generation.
5 * Author: Tamas Szekeres, szekerest@gmail.com
6 *
7 * (c) 2024 info@hobu.co
8 *
9 ******************************************************************************
10 *
11 * SPDX-License-Identifier: MIT
12 ****************************************************************************/
13
14#pragma once
15
16#include <array>
17#include <limits>
18#include <mutex>
19
20#include "gdal_priv.h"
22
23#include "viewshed_types.h"
24
25namespace gdal
26{
27namespace viewshed
28{
29
33struct Lines
34{
35 std::vector<double> cur;
36 std::vector<double> result;
37 std::vector<double> prev;
38 std::vector<double>
40 std::vector<double> prevTmp;
41 std::vector<double> sd;
42
44 Lines() : cur(), result(), prev(), pitchMask(), prevTmp(), sd()
45 {
46 }
47
50 explicit Lines(size_t lineLen)
51 : cur(lineLen), result(lineLen), prev(), pitchMask(), prevTmp(), sd()
52 {
53 }
54};
55
58class DummyBand : public GDALRasterBand
59{
60 CPLErr IReadBlock(int, int, void *) override;
61};
63
64class Progress;
65
69{
70 public:
72 GDALRasterBand &dstBand, int nX, int nY,
73 const Window &oOutExtent, const Window &oCurExtent,
74 const Options &opts, Progress &oProgress,
75 bool emitWarningIfNoData);
76
77 ViewshedExecutor(GDALRasterBand &srcBand, GDALRasterBand &dstBand, int nX,
78 int nY, const Window &oOutExtent, const Window &oCurExtent,
79 const Options &opts, Progress &oProgress,
80 bool emitWarningIfNoData);
81 bool run();
82
84 bool hasFoundNoData() const
85 {
86 return m_hasFoundNoData;
87 }
88
89 private:
91 DummyBand m_dummyBand;
92 GDALRasterBand &m_srcBand;
93 GDALRasterBand &m_sdBand;
94 GDALRasterBand &m_dstBand;
95 const bool m_hasSdBand;
96 double m_noDataValue = 0;
97 bool m_hasNoData = false;
98 bool m_emitWarningIfNoData = false;
99 bool m_hasFoundNoData = false;
100 const Window oOutExtent;
101 const Window oCurExtent;
102 const int m_nX;
103 const int m_nY;
104 const Options oOpts;
105 Progress &oProgress;
106 double m_dfHeightAdjFactor{0};
107 double m_dfMinDistance2;
108 double m_dfMaxDistance2;
109 double m_dfZObserver{0};
110 std::mutex iMutex{};
111 std::mutex oMutex{};
112 GDALGeoTransform m_gt{};
113 std::array<double, 5> m_testAngle{};
114 double m_lowTanPitch{std::numeric_limits<double>::quiet_NaN()};
115 double m_highTanPitch{std::numeric_limits<double>::quiet_NaN()};
116 double (*oZcalc)(int, int, double, double, double){};
117
118 double calcHeightAdjFactor();
119
120 void setOutputNormal(Lines &lines, int pos, double dfZ);
121 void setOutputSd(Lines &lines, int pos, double dfZ);
122
123 bool readLine(int nLine, Lines &lines);
124 bool writeLine(int nLine, std::vector<double> &vResult);
125 bool processLine(int nLine, Lines &lines);
126 bool processFirstLine(Lines &lines);
127 void processFirstLineLeft(const LineLimits &ll, Lines &lines, bool sdCalc);
128 void processFirstLineRight(const LineLimits &ll, Lines &lines, bool sdCalc);
129 void processFirstLineTopOrBottom(const LineLimits &ll, Lines &lines);
130 void processLineLeft(int nYOffset, LineLimits &ll, Lines &lines,
131 bool sdCalc);
132 void processLineRight(int nYOffset, LineLimits &ll, Lines &lines,
133 bool sdCalc);
134 LineLimits adjustHeight(int iLine, Lines &lines);
135 bool maskInitial(std::vector<double> &vResult, const LineLimits &ll,
136 int nLine);
137 bool maskAngleLeft(std::vector<double> &vResult, int nLine);
138 bool maskAngleRight(std::vector<double> &vResult, int nLine);
139 void maskLineLeft(std::vector<double> &vResult, const LineLimits &ll,
140 int nLine);
141 void maskLineRight(std::vector<double> &vResult, const LineLimits &ll,
142 int nLine);
143 void calcPitchMask(double dfZ, double dfDist, double dfResult,
144 double &maskVal);
145 void applyPitchMask(std::vector<double> &vResult,
146 const std::vector<double> &vPitchMaskVal);
147 void calcTestAngles();
148
149 inline bool sdMode() const
150 {
151 return m_hasSdBand;
152 }
153};
154
155} // namespace viewshed
156} // namespace gdal
Pool of worker threads.
Definition cpl_worker_thread_pool.h:64
Class that encapsulates a geotransform matrix.
Definition gdal_geotransform.h:42
A single raster band (or channel).
Definition gdal_rasterband.h:108
Support for progress reporting in viewshed construction.
Definition progress.h:23
Executes a viewshed computation on a source band, placing the result in the destination band.
Definition viewshed_executor.h:69
bool run()
Run the viewshed computation.
Definition viewshed_executor.cpp:1132
bool hasFoundNoData() const
Return whether an input pixel is at the nodata value.
Definition viewshed_executor.h:84
CPLErr
Error category / error level.
Definition cpl_error.h:45
Class to manage a pool of worker threads.
This file is legacy since GDAL 3.12, but will be kept at least in the whole GDAL 3....
Container for lines necessary for processing.
Definition viewshed_executor.h:34
std::vector< double > prevTmp
Saved prev values when in SD mode.
Definition viewshed_executor.h:40
Lines()
Constructor.
Definition viewshed_executor.h:44
std::vector< double > sd
SD mask.
Definition viewshed_executor.h:41
std::vector< double > prev
Height values for previous line.
Definition viewshed_executor.h:37
std::vector< double > pitchMask
Height/indicator values for pitch masking.
Definition viewshed_executor.h:39
std::vector< double > cur
Current line being processed.
Definition viewshed_executor.h:35
Lines(size_t lineLen)
Constructor that initializes to line length.
Definition viewshed_executor.h:50
std::vector< double > result
Result values for current line.
Definition viewshed_executor.h:36
Options for viewshed generation.
Definition viewshed_types.h:59
A window in a raster including pixels in [xStart, xStop) and [yStart, yStop).
Definition viewshed_types.h:118