/
Support.cpp
452 lines (357 loc) · 13.7 KB
/
Support.cpp
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/******************************************************************************
* Copyright (c) 2011, Michael P. Gerlek (mpg@flaxen.com)
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following
* conditions are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided
* with the distribution.
* * Neither the name of Hobu, Inc. or Flaxen Geo Consulting nor the
* names of its contributors may be used to endorse or promote
* products derived from this software without specific prior
* written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
****************************************************************************/
#include "Support.hpp"
#include <iostream>
#include <string>
#include <stdio.h>
#include <boost/test/unit_test.hpp>
#include <pdal/FileUtils.hpp>
#include <pdal/PointBuffer.hpp>
#include <pdal/StageIterator.hpp>
#include <pdal/Options.hpp>
#include <pdal/PointBuffer.hpp>
#include <pdal/Stage.hpp>
#include "TestConfig.hpp"
#ifdef PDAL_COMPILER_GCC
#pragma GCC diagnostic ignored "-Wfloat-equal"
//#pragma GCC diagnostic ignored "-Wsign-compare"
#endif
std::string Support::datapath()
{
const std::string s = TestConfig::g_data_path;
return s;
}
bool Support::compare_stage_data(pdal::Stage const& a, pdal::Stage const& b)
{
pdal::Schema const& a_schema = a.getSchema();
pdal::Schema const& b_schema = b.getSchema();
if (a_schema != b_schema) return false;
boost::uint64_t a_num_points64 = a.getNumPoints();
boost::uint64_t b_num_points64 = b.getNumPoints();
if (a_num_points64 > std::numeric_limits<boost::uint32_t>::max() ||
b_num_points64 > std::numeric_limits<boost::uint32_t>::max())
throw pdal::pdal_error("unable to do compare_stage_data for > 2^32 points");
boost::uint32_t a_num_points = static_cast<boost::uint32_t>(a_num_points64);
boost::uint32_t b_num_points = static_cast<boost::uint32_t>(b_num_points64);
if (a_num_points != b_num_points) return false;
// if we don't have any sizes here, we'll just use a small default
if (a_num_points == 0) a_num_points = 1024;
if (b_num_points == 0) b_num_points = 1024;
pdal::PointBuffer a_data(a_schema, a_num_points);
pdal::PointBuffer b_data(b_schema, b_num_points);
pdal::StageSequentialIterator* a_itr = a.createSequentialIterator(a_data);
pdal::StageSequentialIterator* b_itr = b.createSequentialIterator(b_data);
if (!a_itr) throw pdal::pdal_error("unable to create sequential iterator for compare_stage_data for stage a");
if (!b_itr) throw pdal::pdal_error("unable to create sequential iterator for compare_stage_data for stage b");
{
boost::uint32_t a_numRead = a_itr->read(a_data);
boost::uint32_t b_numRead = b_itr->read(b_data);
if (a_numRead != b_numRead) return false;
boost::uint8_t const* a_bytes = a_data.getData(0);
boost::uint8_t const* b_bytes = b_data.getData(0);
boost::uint64_t a_length = a_data.getBufferByteLength();
// boost::uint64_t b_length = b_data.getBufferByteLength();
for (boost::uintmax_t i=0; i<a_length; i++)
{
if (*a_bytes != *b_bytes)
{
return false;
}
++a_bytes;
++b_bytes;
}
}
return true;
}
std::string Support::datapath(const std::string& file)
{
const std::string s = datapath() + file;
return s;
}
std::string Support::temppath()
{
const std::string s = TestConfig::g_data_path + "../temp/";
return s;
}
std::string Support::temppath(const std::string& file)
{
const std::string s = temppath() + file;
return s;
}
std::string Support::binpath()
{
const std::string argv0 = boost::unit_test::framework::master_test_suite().argv[0];
const std::string s = pdal::FileUtils::getDirectory(argv0);
return s;
}
std::string Support::binpath(const std::string& file)
{
const std::string s = binpath() + file;
return s;
}
std::string Support::exename(const std::string& name)
{
#ifdef PDAL_PLATFORM_WIN32
return name + ".exe";
#else
return name;
#endif
}
// do a comparison by line of two (text) files, ignoring CRLF differences
boost::uint32_t Support::diff_text_files(const std::string& file1, const std::string& file2, boost::int32_t ignoreLine1)
{
if (!pdal::FileUtils::fileExists(file1) ||
!pdal::FileUtils::fileExists(file2))
return std::numeric_limits<boost::uint32_t>::max();
std::istream* str1 = pdal::FileUtils::openFile(file1, false);
std::istream* str2 = pdal::FileUtils::openFile(file2, false);
BOOST_CHECK(str1);
BOOST_CHECK(str2);
boost::uint32_t numdiffs = 0;
boost::int32_t currLine = 1;
while (!str1->eof() && !str2->eof())
{
std::string buf1;
std::string buf2;
std::getline(*str1, buf1);
std::getline(*str2, buf2);
if (currLine == ignoreLine1)
{
++currLine;
continue;
}
if (str1->eof() && str2->eof())
{
// hit end on both together
break;
}
else if (str1->eof() && !str2->eof())
{
// str1 ended, but str2 still going
while (!str2->eof())
{
std::getline(*str2, buf2);
++numdiffs;
}
break;
}
else if (!str1->eof() && str2->eof())
{
// str2 ended, but str1 still going
while (!str1->eof())
{
std::getline(*str1, buf1);
++numdiffs;
}
break;
}
if (buf1 != buf2)
{
++numdiffs;
}
++currLine;
}
assert(str1->eof());
assert(str2->eof());
pdal::FileUtils::closeFile(str1);
pdal::FileUtils::closeFile(str2);
return numdiffs;
}
boost::uint32_t Support::diff_files(const std::string& file1, const std::string& file2,
boost::uint32_t ignorable_start, boost::uint32_t ignorable_length)
{
boost::uint32_t start[] = { ignorable_start };
boost::uint32_t len[] = { ignorable_length };
return diff_files(file1, file2, start, len, 1);
}
// do a byte-wise comparison of two (binary) files
boost::uint32_t Support::diff_files(const std::string& file1, const std::string& file2,
boost::uint32_t* ignorable_start, boost::uint32_t* ignorable_length, boost::uint32_t num_ignorables)
{
if (!pdal::FileUtils::fileExists(file1) ||
!pdal::FileUtils::fileExists(file2))
return std::numeric_limits<boost::uint32_t>::max();
boost::uintmax_t len1x = pdal::FileUtils::fileSize(file1);
boost::uintmax_t len2x = pdal::FileUtils::fileSize(file2);
const size_t len1 = (size_t)len1x; // BUG
const size_t len2 = (size_t)len2x;
std::istream* str1 = pdal::FileUtils::openFile(file1);
std::istream* str2 = pdal::FileUtils::openFile(file2);
BOOST_CHECK(str1);
BOOST_CHECK(str2);
char* buf1 = new char[len1];
char* buf2 = new char[len2];
str1->read(buf1,len1);
str2->read(buf2,len2);
pdal::FileUtils::closeFile(str1);
pdal::FileUtils::closeFile(str2);
char* p = buf1;
char* q = buf2;
boost::uint32_t numdiffs = 0;
const size_t minlen = (len1 < len2) ? len1 : len2;
const size_t maxlen = (len1 > len2) ? len1 : len2;
for (size_t i=0; i<minlen; i++)
{
if (*p != *q)
{
if (num_ignorables == 0)
{
++numdiffs;
}
else
{
// only count the difference if we are NOT in an ignorable region
bool is_ignorable = false;
for (boost::uint32_t region=0; region<num_ignorables; region++)
{
const boost::uint32_t start = ignorable_start[region];
const boost::uint32_t end = start + ignorable_length[region];
if (i >= start && i < end)
{
// we are in an ignorable region!
is_ignorable = true;
break;
}
}
if (is_ignorable == false)
{
++numdiffs;
}
}
}
++p;
++q;
}
if (minlen != maxlen)
{
numdiffs += (maxlen - minlen);
}
delete[] buf1;
delete[] buf2;
return numdiffs;
}
boost::uint32_t Support::diff_files(const std::string& file1, const std::string& file2)
{
return diff_files(file1, file2, NULL, NULL, 0);
}
bool Support::compare_files(const std::string& file1, const std::string& file2)
{
const boost::uint32_t numdiffs = diff_files(file1, file2);
return (numdiffs == 0);
}
bool Support::compare_text_files(const std::string& file1, const std::string& file2)
{
boost::uint32_t numdiffs = diff_text_files(file1, file2);
return (numdiffs == 0);
}
#define Compare(x,y) BOOST_CHECK(pdal::Utils::compare_approx((x),(y),0.001));
void Support::check_pN(const pdal::PointBuffer& data,
std::size_t index,
double xref, double yref, double zref)
{
const ::pdal::Schema& schema = data.getSchema();
pdal::Dimension const& dimX = schema.getDimension("X");
pdal::Dimension const& dimY = schema.getDimension("Y");
pdal::Dimension const& dimZ = schema.getDimension("Z");
boost::int32_t x0raw = data.getField<boost::int32_t>(dimX, index);
boost::int32_t y0raw = data.getField<boost::int32_t>(dimY, index);
boost::int32_t z0raw = data.getField<boost::int32_t>(dimZ, index);
double x0 = dimX.applyScaling<boost::int32_t>(x0raw);
double y0 = dimY.applyScaling<boost::int32_t>(y0raw);
double z0 = dimZ.applyScaling<boost::int32_t>(z0raw);
Compare(x0, xref);
Compare(y0, yref);
Compare(z0, zref);
}
void Support::check_pN(const pdal::PointBuffer& data,
std::size_t index,
double xref, double yref, double zref,
double tref,
boost::uint16_t rref, boost::uint16_t gref, boost::uint16_t bref)
{
check_pN(data, index, xref, yref, zref);
const ::pdal::Schema& schema = data.getSchema();
boost::optional<pdal::Dimension const&> dimTime = schema.getDimensionOptional("Time");
if (dimTime)
{
double t0 = data.getField<double>(*dimTime, index);
BOOST_CHECK_EQUAL(t0, tref);
}
boost::optional<pdal::Dimension const&> dimRed = schema.getDimensionOptional("Red");
boost::optional<pdal::Dimension const&> dimGreen = schema.getDimensionOptional("Green");
boost::optional<pdal::Dimension const&> dimBlue = schema.getDimensionOptional("Blue");
if (dimRed)
{
boost::uint16_t r0 = data.getField<boost::uint16_t>(*dimRed, index);
boost::uint16_t g0 = data.getField<boost::uint16_t>(*dimGreen, index);
boost::uint16_t b0 = data.getField<boost::uint16_t>(*dimBlue, index);
BOOST_CHECK_EQUAL(r0, rref);
BOOST_CHECK_EQUAL(g0, gref);
BOOST_CHECK_EQUAL(b0, bref);
}
return;
}
void Support::check_p0_p1_p2(const pdal::PointBuffer& data)
{
Support::check_pN(data, 0, 637012.240000, 849028.310000, 431.660000);
Support::check_pN(data, 1, 636896.330000, 849087.700000, 446.390000);
Support::check_pN(data, 2, 636784.740000, 849106.660000, 426.710000);
}
void Support::check_p100_p101_p102(const pdal::PointBuffer& data)
{
Support::check_pN(data, 0, 636661.060000, 849854.130000, 424.900000);
Support::check_pN(data, 1, 636568.180000, 850179.490000, 441.800000);
Support::check_pN(data, 2, 636554.630000, 850040.030000, 499.110000);
}
void Support::check_p355_p356_p357(const pdal::PointBuffer& data)
{
Support::check_pN(data, 0, 636462.600000, 850566.110000, 432.610000);
Support::check_pN(data, 1, 636356.140000, 850530.480000, 432.680000);
Support::check_pN(data, 2, 636227.530000, 850592.060000, 428.670000);
}
void Support::check_p710_p711_p712(const pdal::PointBuffer& data)
{
Support::check_pN(data, 0, 638720.670000, 850926.640000, 417.320000);
Support::check_pN(data, 1, 638672.380000, 851081.660000, 420.670000);
Support::check_pN(data, 2, 638598.880000, 851445.370000, 422.150000);
}
void Support::compareBounds(const pdal::Bounds<double>& p, const pdal::Bounds<double>& q)
{
BOOST_CHECK_CLOSE(p.getMinimum(0), q.getMinimum(0), 1);
BOOST_CHECK_CLOSE(p.getMinimum(1), q.getMinimum(1), 1);
BOOST_CHECK_CLOSE(p.getMinimum(2), q.getMinimum(2), 1);
BOOST_CHECK_CLOSE(p.getMaximum(0), q.getMaximum(0), 1);
BOOST_CHECK_CLOSE(p.getMaximum(1), q.getMaximum(1), 1);
BOOST_CHECK_CLOSE(p.getMaximum(2), q.getMaximum(2), 1);
}