Refresh GL display when moving mouse on normal view, as it renders pretty fast now
[tecorrec.git] / geo / tcSrtmModel.cpp
blob31e7875488efba218d95080818cfe74296dd1a97
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 /**
21 * @file tcSrtmModel.cpp
22 * @brief SRTM elevation model.
25 #include "tcSrtmModel.h"
26 #include "tcSrtmCache.h"
27 #include "tcSrtmData.h"
29 #include <inttypes.h>
32 * Constructors + destructor
35 /// Default constructor.
36 tcSrtmModel::tcSrtmModel()
37 : m_cache(0)
41 /// Destructor.
42 tcSrtmModel::~tcSrtmModel()
44 delete m_cache;
48 * Main interface
51 /// Get information to allow alignment with actual samples.
52 void tcSrtmModel::sampleAlign(const tcGeo& swCorner, const tcGeo& neCorner, RenderState* state, int maxSamples)
54 state->swCorner = swCorner;
55 state->neCorner = neCorner;
56 state->moreAvailableLon = false;
57 state->moreAvailableLat = false;
59 tcSrtmData* data = 0;
60 if (0 != m_cache)
62 data = m_cache->findData(swCorner, neCorner);
63 if (data)
65 tcGeo resolution = data->sampleResolution();
66 maths::Vector<2,double> swSampleSpace = swCorner / resolution;
67 maths::Vector<2,double> neSampleSpace = neCorner / resolution;
68 maths::Vector<2,double> first(floor(swSampleSpace[0] + 1.0),
69 floor(swSampleSpace[1] + 1.0));
70 maths::Vector<2,double> last( ceil( neSampleSpace[0] - 1.0),
71 ceil( neSampleSpace[1] - 1.0));
72 state->samplesLon = 3 + (int)(last[0] - first[0]);
73 state->samplesLat = 3 + (int)(last[1] - first[1]);
74 state->swSample = swCorner + resolution*(first - swSampleSpace);
75 state->sampleDelta = resolution;
77 // Sample evenly
78 if (maxSamples >= 2)
80 if (state->samplesLon > maxSamples)
82 state->sampleDelta.setLon((neCorner.lon()-swCorner.lon()) / (maxSamples-1));
83 state->swSample.setLon(swCorner.lon() + state->sampleDelta.lon());
84 state->samplesLon = maxSamples;
85 state->moreAvailableLon = true;
87 if (state->samplesLat > maxSamples)
89 state->sampleDelta.setLat((neCorner.lat()-swCorner.lat()) / (maxSamples-1));
90 state->swSample.setLat(swCorner.lat() + state->sampleDelta.lat());
91 state->samplesLat = maxSamples;
92 state->moreAvailableLat = true;
95 return;
99 state->samplesLon = maxSamples;
100 state->samplesLat = maxSamples;
101 state->sampleDelta = (neCorner - swCorner)/(maxSamples-1);
102 state->swSample = swCorner + state->sampleDelta;
105 /// Get the altitude at a sample in a render state.
106 double tcSrtmModel::altitudeAt(const RenderState& state, int x, int y, tcGeo* outCoord, bool corrected, bool* accurate)
108 double lon;
109 if (x <= 0)
111 lon = state.swCorner.lon();
113 else if (x >= state.samplesLon - 1)
115 lon = state.neCorner.lon();
117 else
119 lon = state.swSample.lon() + state.sampleDelta.lon() * (x-1);
121 double lat;
122 if (y <= 0)
124 lat = state.swCorner.lat();
126 else if (y >= state.samplesLat - 1)
128 lat = state.neCorner.lat();
130 else
132 lat = state.swSample.lat() + state.sampleDelta.lat() * (y-1);
134 return altitudeAt(*outCoord = tcGeo(lon, lat), corrected, accurate);
137 /// Get the altitude at a coordinate.
138 double tcSrtmModel::altitudeAt(const tcGeo& coord, bool corrected, bool* accurate)
140 // Round down to nearest degree
141 bool isAccurate = false;
142 double result = 0.0;
143 tcSrtmData* data = 0;
144 if (0 != m_cache)
146 data = m_cache->loadData(coord);
148 if (0 != data)
150 return data->sampleAltitude(coord, corrected, accurate);
152 if (0 != accurate)
154 *accurate = isAccurate;
156 return result;
159 /// Set the data set to use.
160 void tcSrtmModel::setDataSet(const QString& name)
162 delete m_cache;
163 m_cache = new tcSrtmCache(name);