1 /***************************************************************************
2 * This file is part of Tecorrec. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
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. *
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. *
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 ***************************************************************************/
21 * @file tcSrtmModel.cpp
22 * @brief SRTM elevation model.
25 #include "tcSrtmModel.h"
26 #include "tcSrtmCache.h"
27 #include "tcSrtmData.h"
32 * Constructors + destructor
35 /// Default constructor.
36 tcSrtmModel::tcSrtmModel()
42 tcSrtmModel::~tcSrtmModel()
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;
62 data
= m_cache
->findData(swCorner
, neCorner
);
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
;
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;
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
)
111 lon
= state
.swCorner
.lon();
113 else if (x
>= state
.samplesLon
- 1)
115 lon
= state
.neCorner
.lon();
119 lon
= state
.swSample
.lon() + state
.sampleDelta
.lon() * (x
-1);
124 lat
= state
.swCorner
.lat();
126 else if (y
>= state
.samplesLat
- 1)
128 lat
= state
.neCorner
.lat();
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;
143 tcSrtmData
* data
= 0;
146 data
= m_cache
->loadData(coord
);
150 return data
->sampleAltitude(coord
, corrected
, accurate
);
154 *accurate
= isAccurate
;
159 /// Set the data set to use.
160 void tcSrtmModel::setDataSet(const QString
& name
)
163 m_cache
= new tcSrtmCache(name
);