Fixed bug in arcToHgt where it wrote to end of file instead of last row of file
[tecorrec.git] / geo / tcElevationOptimization.h
blobdc7976664437fd4aaf785984cfcd163900499d22
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 /// Initialise snapshotting.
121 void initSnapShotting();
123 /// Load a sequence of snapshots.
124 void loadSnapShots();
126 /// Load a single snapshot.
127 void loadSnapShot(int id);
129 protected:
132 * Interface for derived class to implement
135 // Reimplemented
136 virtual void roundPortion(double* x1, double* y1, double* x2, double* y2);
138 // Reimplemented
139 virtual tcAbstractPixelData* loadPortion(double x1, double y1, double x2, double y2, bool changed);
141 private:
144 * Private functions
147 /** Calculate the cost for a set of pixels.
148 * @param x1 Starting horizontal pixel (left).
149 * @param y1 Starting vertical pixel (top).
150 * @param x2 Ending horizontal pixel (right).
151 * @param y2 Ending horizontal pixel (bottom).
152 * @return Cost of the chosen pixels.
154 float cost(int x1, int y1, int x2, int y2) const;
156 /** Calculate the relative cost of changing a pixel's elevation.
157 * @param x Horizontal coordinate of pixel to change.
158 * @param y Vertical coordinate of pixel to change.
159 * @param dh Relative change in elevation.
160 * @return Relative cost of altering the elevation of the specified pixel.
162 float costChange(int x, int y, float dh, int range = 0);
164 /// Optimize a whole range of elevations.
165 void optimizeElevations(int x1, int y1, int x2, int y2, float dh, int range = 0);
167 /// Get pointer to elevation at a pixel.
168 float* pixelElevation(int x, int y) const;
170 /// Adjust elevation at a pixel.
171 void adjustElevation(int x, int y, float dh, int range = 0);
174 * Types
177 /// Worker thread class.
178 class WorkerThread;
180 /// Shadow transition types.
181 enum TransitionType {
182 TransitionEntrance = 0,
183 TransitionExit = 1
186 /// Per-dataset-pixel data to cache
187 struct PerDatasetPixelCache
189 /// Coordinates of shadow entrance and exit.
190 maths::Vector<2,int> shadowTransition[2];
191 /// Light direction.
192 maths::Vector<3,float> light;
193 /// Length of light vector trace along ground.
194 float sinLightElevation;
195 /// How lit pixel is.
196 float lit;
197 /// Distance to shadow entrance and exit.
198 float shadowTransitionDistance[2];
201 /// Per-pixel data to cache.
202 struct PerPixelCache
204 /// Resolution.
205 maths::Vector<2,float> resolution;
206 /// Original elevation.
207 float originalElevation;
208 /// Reliability of original elevation.
209 unsigned char reliability;
210 /// Accurate elevation.
211 float accurateElevation;
212 /// Temporary values.
213 union {
214 float f;
215 int i;
216 } temporary;
220 * Variables
223 /// Number of datasets.
224 int m_numDatasets;
226 /// Datasets.
227 tcGeoImageData** m_datasets;
229 /// Transform local tex coords to dataset tex coords for each dataset.
230 tcAffineTransform2<double>* m_texToDatasetTex;
232 /// Shadow channel.
233 tcChannel** m_shadowChannels;
235 /// Shading channel.
236 tcChannel** m_shadingChannels;
238 /// Current elevation data.
239 tcElevationData* m_elevationData;
241 /// Per-dataset-pixel data.
242 tcTypedPixelData<PerDatasetPixelCache>** m_datum;
244 /// Per-pixel data.
245 tcTypedPixelData<PerPixelCache>* m_data;
247 /// Temporary shadow data.
248 Reference<tcPixelData<float> >* m_shadowData;
250 /// Temporary shading data.
251 Reference<tcPixelData<float> >* m_shadingData;
253 /// Weights.
254 struct
256 float variance;
257 float roughness;
258 float steepness;
259 float litFacing;
260 float shading;
261 float belowShadowLine;
262 float transitionElev;
263 float transitionTangential;
264 float transitionCurvingDown;
265 } m_weights;
267 /// Update voids only.
268 bool m_voidsOnly;
270 /// Currently loading from dem.
271 bool m_loadingFromDem;
273 /// Main processing thread.
274 QThread* m_thread;
276 /// Main processing mutex.
277 QMutex m_processingMutex;
279 /// Signal to stop processing.
280 bool m_stopProcessing;
282 /// Optimize toggle button.
283 QPushButton* m_btnOptimize;
286 * Configuration gui
289 /// Configuration widget.
290 tcChannelConfigWidget* m_configWidget;
292 /// Number of iterations to perform.
293 QSpinBox* m_spinIterations;
295 /// Slider for delta h.
296 QSlider* m_deltaHSlider[2];
298 /// Slider for range.
299 QSlider* m_rangeSlider[2];
301 /// Plot of standard deviations.
302 QwtPlot* m_plot;
304 /// Plot x coordinate data.
305 QwtArray<double> m_plotX;
307 /// Standard deviation curve.
308 QwtPlotCurve* m_stdDevCurve;
310 /// Standard deviation data.
311 QwtArray<double> m_stdDevData;
313 /// Standard deviation curve for void pixels.
314 QwtPlotCurve* m_stdDevVoidCurve;
316 /// Standard deviation data.
317 QwtArray<double> m_stdDevVoidData;
319 /// Standard deviation curve for non-void pixels.
320 QwtPlotCurve* m_stdDevNonVoidCurve;
322 /// Standard deviation data.
323 QwtArray<double> m_stdDevNonVoidData;
325 /// Snapshotting config file.
326 QString m_snapshotFile;
328 /// Snapshot read file.
329 QString m_snapshotReadFile;
331 /// Whether currently reading snapshots.
332 bool m_snapshotReading;
334 /// Snapshot time slider.
335 QSlider* m_snapshotSlider;
338 #endif