-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathDem_TEST.cc
More file actions
246 lines (208 loc) · 7.96 KB
/
Dem_TEST.cc
File metadata and controls
246 lines (208 loc) · 7.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
* Copyright (C) 2016 Open Source Robotics Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include <gtest/gtest.h>
#include <limits>
#include <ignition/math/Angle.hh>
#include <ignition/math/Vector3.hh>
#include "ignition/common/geospatial/Dem.hh"
#include "test_config.h"
using namespace ignition;
class DemTest : public common::testing::AutoLogFixture { };
/////////////////////////////////////////////////
TEST_F(DemTest, MissingFile)
{
common::Dem dem;
EXPECT_NE(dem.Load("/file/shouldn/never/exist.png"), 0);
}
/////////////////////////////////////////////////
TEST_F(DemTest, NotDem)
{
common::Dem dem;
const auto path = common::testing::TestFile("CMakeLists.txt");
EXPECT_NE(dem.Load(path), 0);
}
/////////////////////////////////////////////////
TEST_F(DemTest, UnsupportedDem)
{
common::Dem dem;
const auto path = common::testing::TestFile("data", "heightmap_bowl.png");
EXPECT_NE(dem.Load(path), 0);
}
/////////////////////////////////////////////////
TEST_F(DemTest, NonSquaredDemPortrait)
{
common::Dem dem;
const auto path = common::testing::TestFile("data", "dem_portrait.tif");
EXPECT_EQ(dem.Load(path), 0);
EXPECT_DOUBLE_EQ(dem.Width(), dem.Height());
EXPECT_DOUBLE_EQ(257, dem.Height());
EXPECT_DOUBLE_EQ(257, dem.Width());
EXPECT_DOUBLE_EQ(111565.57640012962, dem.WorldHeight());
EXPECT_DOUBLE_EQ(87912.450080798269, dem.WorldWidth());
EXPECT_DOUBLE_EQ(-6.2633352279663086, dem.MinElevation());
EXPECT_DOUBLE_EQ(920.762939453125, dem.MaxElevation());
}
/////////////////////////////////////////////////
TEST_F(DemTest, NonSquaredDemLandscape)
{
common::Dem dem;
const auto path = common::testing::TestFile("data", "dem_landscape.tif");
EXPECT_EQ(dem.Load(path), 0);
EXPECT_DOUBLE_EQ(dem.Width(), dem.Height());
EXPECT_DOUBLE_EQ(257, dem.Height());
EXPECT_DOUBLE_EQ(257, dem.Width());
EXPECT_DOUBLE_EQ(111565.57640012962, dem.WorldHeight());
EXPECT_DOUBLE_EQ(87912.450080798269, dem.WorldWidth());
EXPECT_DOUBLE_EQ(-4.7324686050415039, dem.MinElevation());
EXPECT_DOUBLE_EQ(921.4481201171875, dem.MaxElevation());
}
/////////////////////////////////////////////////
TEST_F(DemTest, SquaredDem)
{
common::Dem dem;
const auto path = common::testing::TestFile("data", "dem_squared.tif");
EXPECT_EQ(dem.Load(path), 0);
}
/////////////////////////////////////////////////
TEST_F(DemTest, BasicAPI)
{
common::Dem dem;
const auto path = common::testing::TestFile("data", "dem_squared.tif");
EXPECT_EQ(dem.Load(path), 0);
// Check filename
EXPECT_EQ(path, dem.Filename());
// Check the heights and widths
EXPECT_EQ(129, static_cast<int>(dem.Height()));
EXPECT_EQ(129, static_cast<int>(dem.Width()));
EXPECT_FLOAT_EQ(3984.4849f, dem.WorldHeight());
EXPECT_FLOAT_EQ(3139.7456f, dem.WorldWidth());
EXPECT_FLOAT_EQ(65.3583f, dem.MinElevation());
EXPECT_FLOAT_EQ(318.441f, dem.MaxElevation());
// Check Elevation()
unsigned int width = dem.Width();
unsigned int height = dem.Height();
EXPECT_FLOAT_EQ(215.82324f, dem.Elevation(0, 0));
EXPECT_FLOAT_EQ(216.04961f, dem.Elevation(width - 1, 0));
EXPECT_FLOAT_EQ(142.2274f, dem.Elevation(0, height - 1));
EXPECT_FLOAT_EQ(209.14784f, dem.Elevation(width - 1, height - 1));
// Illegal coordinates
double inf = std::numeric_limits<double>::infinity();
EXPECT_DOUBLE_EQ(inf, dem.Elevation(0, height));
EXPECT_DOUBLE_EQ(inf, dem.Elevation(width, 0));
EXPECT_DOUBLE_EQ(inf, dem.Elevation(width, height));
// Check GeoReferenceOrigin()
ignition::math::Angle latitude, longitude;
EXPECT_TRUE(dem.GeoReferenceOrigin(latitude, longitude));
EXPECT_FLOAT_EQ(38.001667f, latitude.Degree());
EXPECT_FLOAT_EQ(-122.22278f, longitude.Degree());
}
/////////////////////////////////////////////////
TEST_F(DemTest, FillHeightmap)
{
common::Dem dem;
const auto path = common::testing::TestFile("data", "dem_squared.tif");
EXPECT_EQ(dem.Load(path), 0);
// Use FillHeightMap() to retrieve a vector<float> after some transformations
int subsampling;
unsigned vertSize;
ignition::math::Vector3d size;
ignition::math::Vector3d scale;
bool flipY;
std::vector<float> elevations;
subsampling = 2;
vertSize = (dem.Width() * subsampling) - 1;
size.X(dem.WorldWidth());
size.Y(dem.WorldHeight());
size.Z(dem.MaxElevation() - dem.MinElevation());
scale.X(size.X() / vertSize);
scale.Y(size.Y() / vertSize);
if (ignition::math::equal(dem.MaxElevation(), 0.0f))
scale.Z(fabs(size.Z()));
else
scale.Z(fabs(size.Z()) / dem.MaxElevation());
flipY = false;
dem.FillHeightMap(subsampling, vertSize, size, scale, flipY, elevations);
// Check the size of the returned vector
EXPECT_EQ(vertSize * vertSize, elevations.size());
// Check the elevation of some control points
EXPECT_FLOAT_EQ(184.94113f, elevations.at(0));
EXPECT_FLOAT_EQ(179.63583f, elevations.at(elevations.size() - 1));
EXPECT_FLOAT_EQ(213.42966f, elevations.at(elevations.size() / 2));
}
/////////////////////////////////////////////////
TEST_F(DemTest, UnfinishedDem)
{
common::Dem dem;
auto path = common::testing::TestFile("data", "dem_unfinished.tif");
EXPECT_EQ(dem.Load(path), 0);
// Check that the min and max elevations are valid for an unfinished
// and unfilled dem.
EXPECT_EQ(33, static_cast<int>(dem.Height()));
EXPECT_EQ(33, static_cast<int>(dem.Width()));
EXPECT_FLOAT_EQ(111287.59f, dem.WorldHeight());
EXPECT_FLOAT_EQ(88878.297f, dem.WorldWidth());
EXPECT_FLOAT_EQ(-32768.0f, dem.MinElevation());
EXPECT_FLOAT_EQ(1909.0f, dem.MaxElevation());
// test another dem file with multiple nodata values
common::Dem demNoData;
path = common::testing::TestFile("data", "dem_nodata.dem");
EXPECT_EQ(demNoData.Load(path), 0);
// Check that the min and max elevations are valid for a dem with multiple
// nodata values
EXPECT_EQ(65, static_cast<int>(demNoData.Height()));
EXPECT_EQ(65, static_cast<int>(demNoData.Width()));
// relaxed tolerances for macOS & windows
EXPECT_NEAR(7499.8281, demNoData.WorldHeight(), 0.1);
EXPECT_NEAR(14150.225, demNoData.WorldWidth(), 0.1);
// -32767 is the nodata value, so it's ignored when computing the min
// elevation
EXPECT_FLOAT_EQ(682.0f, demNoData.MinElevation());
EXPECT_FLOAT_EQ(2932.0f, demNoData.MaxElevation());
}
/////////////////////////////////////////////////
TEST_F(DemTest, NaNNoData)
{
common::Dem dem;
auto path = common::testing::TestFile("data", "dem_nodata_nan.nc");
EXPECT_EQ(dem.Load(path), 0);
// Check that the min and max elevations are valid for a DEM with NaN
// nodata values
EXPECT_EQ(129, static_cast<int>(dem.Height()));
EXPECT_EQ(129, static_cast<int>(dem.Width()));
EXPECT_NEAR(7464.7589424555326, dem.WorldHeight(), 0.1);
EXPECT_NEAR(14244.280980717675, dem.WorldWidth(), 0.1);
EXPECT_FLOAT_EQ(682.0f, dem.MinElevation());
EXPECT_FLOAT_EQ(2932.0f, dem.MaxElevation());
}
/////////////////////////////////////////////////
TEST_F(DemTest, NonEarthDem)
{
// moon
common::Dem dem;
auto path = common::testing::TestFile("data", "dem_moon.tif");
EXPECT_EQ(dem.Load(path), 0);
EXPECT_EQ(33, static_cast<int>(dem.Height()));
EXPECT_EQ(33, static_cast<int>(dem.Width()));
EXPECT_FLOAT_EQ(-212.29616f, dem.MinElevation());
EXPECT_FLOAT_EQ(-205.44009f, dem.MaxElevation());
// unable to determne world width/height of non earth DEM
EXPECT_FLOAT_EQ(-1, dem.WorldHeight());
EXPECT_FLOAT_EQ(-1, dem.WorldWidth());
// unable to get coordinates in WGS84
ignition::math::Angle latitude, longitude;
EXPECT_FALSE(dem.GeoReferenceOrigin(latitude, longitude));
}