Simple program to read arcinfo ascii files and convert into hgt elevation data (for...
[tecorrec.git] / geo / tcElevationOptimization.h
blobf18cb2a0c566c98104ebf2042446f6a98e2a56b4
1 /***************************************************************************
2 * This file is part of Tecorrec. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
4 * *
5 * Tecorrec is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * Tecorrec is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with Tecorrec. If not, write to the Free Software Foundation, *
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
20 #ifndef _tcElevationOptimization_h_
21 #define _tcElevationOptimization_h_
23 /**
24 * @file tcElevationOptimization.h
25 * @brief Optimized elevation.
28 #include "tcChannelDem.h"
29 #include "tcElevationData.h"
30 #include "tcAffineTransform.h"
32 #include <qwt_array.h>
34 #include <QList>
35 #include <QMutex>
37 class tcShadowClassifyingData;
38 class tcGeoImageData;
39 class QwtPlot;
40 class QwtPlotCurve;
41 class QSpinBox;
42 class QSlider;
43 class QThread;
44 class QPushButton;
46 /// Optimized elevation.
47 class tcElevationOptimization : public tcChannelDem
49 Q_OBJECT
51 public:
54 * Constructors + destructor
57 /// Primary constructor.
58 tcElevationOptimization(const QList<tcShadowClassifyingData*>& datasets, tcSrtmModel* dem, tcGeoImageData* imagery);
60 /// Destructor.
61 virtual ~tcElevationOptimization();
64 * Main image interface
67 // Reimplemented
68 virtual tcChannelConfigWidget* configWidget();
70 signals:
72 /// Change statistics.
73 void statistics(const QString& stats);
75 /// Queued non blocking signal to replot.
76 void replotStdDeviations();
78 /// Queued blocking signal to invalidate and redraw.
79 void invalidateAndRedraw();
81 private slots:
84 * Slots
87 /// Reset DEM.
88 void resetDem();
90 /// Load elevation data from DEM.
91 void loadFromDem();
93 /// Apply hard constraints to elevation data.
94 void applyHardConstraints();
96 /// Void-fill bilinear.
97 void voidFillBilinear();
99 /// Void-fill bicubic.
100 void voidFillBicubic();
102 /// Start/stop optimization process.
103 void startStopOptimize();
105 /// Optimize a number of times.
106 void optimize();
108 /// Clear standard deviations.
109 void clearStdDeviations();
111 /// Sample standard deviations.
112 void sampleStdDeviations();
114 /// Sample standard deviations.
115 void saveStdDeviations();
117 /// Invalidate and redraw.
118 void invalidateAndRedrawSlot();
120 protected:
123 * Interface for derived class to implement
126 // Reimplemented
127 virtual void roundPortion(double* x1, double* y1, double* x2, double* y2);
129 // Reimplemented
130 virtual tcAbstractPixelData* loadPortion(double x1, double y1, double x2, double y2, bool changed);
132 private:
135 * Private functions
138 /** Calculate the cost for a set of pixels.
139 * @param x1 Starting horizontal pixel (left).
140 * @param y1 Starting vertical pixel (top).
141 * @param x2 Ending horizontal pixel (right).
142 * @param y2 Ending horizontal pixel (bottom).
143 * @return Cost of the chosen pixels.
145 float cost(int x1, int y1, int x2, int y2) const;
147 /** Calculate the relative cost of changing a pixel's elevation.
148 * @param x Horizontal coordinate of pixel to change.
149 * @param y Vertical coordinate of pixel to change.
150 * @param dh Relative change in elevation.
151 * @return Relative cost of altering the elevation of the specified pixel.
153 float costChange(int x, int y, float dh, int range = 0);
155 /// Optimize a whole range of elevations.
156 void optimizeElevations(int x1, int y1, int x2, int y2, float dh, int range = 0);
158 /// Get pointer to elevation at a pixel.
159 float* pixelElevation(int x, int y) const;
161 /// Adjust elevation at a pixel.
162 void adjustElevation(int x, int y, float dh, int range = 0);
165 * Types
168 /// Worker thread class.
169 class WorkerThread;
171 /// Shadow transition types.
172 enum TransitionType {
173 TransitionEntrance = 0,
174 TransitionExit = 1
177 /// Per-dataset-pixel data to cache
178 struct PerDatasetPixelCache
180 /// Coordinates of shadow entrance and exit.
181 maths::Vector<2,int> shadowTransition[2];
182 /// Light direction.
183 maths::Vector<3,float> light;
184 /// Length of light vector trace along ground.
185 float sinLightElevation;
186 /// How lit pixel is.
187 float lit;
188 /// Distance to shadow entrance and exit.
189 float shadowTransitionDistance[2];
192 /// Per-pixel data to cache.
193 struct PerPixelCache
195 /// Resolution.
196 maths::Vector<2,float> resolution;
197 /// Original elevation.
198 float originalElevation;
199 /// Reliability of original elevation.
200 unsigned char reliability;
201 /// Accurate elevation.
202 float accurateElevation;
203 /// Temporary values.
204 union {
205 float f;
206 int i;
207 } temporary;
211 * Variables
214 /// Number of datasets.
215 int m_numDatasets;
217 /// Datasets.
218 tcGeoImageData** m_datasets;
220 /// Transform local tex coords to dataset tex coords for each dataset.
221 tcAffineTransform2<double>* m_texToDatasetTex;
223 /// Shadow channel.
224 tcChannel** m_shadowChannels;
226 /// Shading channel.
227 tcChannel** m_shadingChannels;
229 /// Current elevation data.
230 tcElevationData* m_elevationData;
232 /// Per-dataset-pixel data.
233 tcTypedPixelData<PerDatasetPixelCache>** m_datum;
235 /// Per-pixel data.
236 tcTypedPixelData<PerPixelCache>* m_data;
238 /// Temporary shadow data.
239 Reference<tcPixelData<float> >* m_shadowData;
241 /// Temporary shading data.
242 Reference<tcPixelData<float> >* m_shadingData;
244 /// Weights.
245 struct
247 float variance;
248 float roughness;
249 float steepness;
250 float litFacing;
251 float shading;
252 float belowShadowLine;
253 float transitionElev;
254 float transitionTangential;
255 float transitionCurvingDown;
256 } m_weights;
258 /// Update voids only.
259 bool m_voidsOnly;
261 /// Currently loading from dem.
262 bool m_loadingFromDem;
264 /// Main processing thread.
265 QThread* m_thread;
267 /// Main processing mutex.
268 QMutex m_processingMutex;
270 /// Signal to stop processing.
271 bool m_stopProcessing;
273 /// Optimize toggle button.
274 QPushButton* m_btnOptimize;
277 * Configuration gui
280 /// Configuration widget.
281 tcChannelConfigWidget* m_configWidget;
283 /// Number of iterations to perform.
284 QSpinBox* m_spinIterations;
286 /// Slider for delta h.
287 QSlider* m_deltaHSlider[2];
289 /// Slider for range.
290 QSlider* m_rangeSlider[2];
292 /// Plot of standard deviations.
293 QwtPlot* m_plot;
295 /// Plot x coordinate data.
296 QwtArray<double> m_plotX;
298 /// Standard deviation curve.
299 QwtPlotCurve* m_stdDevCurve;
301 /// Standard deviation data.
302 QwtArray<double> m_stdDevData;
304 /// Standard deviation curve for void pixels.
305 QwtPlotCurve* m_stdDevVoidCurve;
307 /// Standard deviation data.
308 QwtArray<double> m_stdDevVoidData;
310 /// Standard deviation curve for non-void pixels.
311 QwtPlotCurve* m_stdDevNonVoidCurve;
313 /// Standard deviation data.
314 QwtArray<double> m_stdDevNonVoidData;
317 #endif