-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdal_utils.cpp
588 lines (515 loc) · 14.6 KB
/
mdal_utils.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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
/*
MDAL - Mesh Data Abstraction Library (MIT License)
Copyright (C) 2018 Peter Petrik (zilolv at gmail dot com)
*/
#include "mdal_utils.hpp"
#include <string>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <math.h>
#include <assert.h>
#include <cmath>
#include <string.h>
#include <stdio.h>
bool MDAL::fileExists( const std::string &filename )
{
std::ifstream in( filename );
return in.good();
}
bool MDAL::startsWith( const std::string &str, const std::string &substr, ContainsBehaviour behaviour )
{
if ( str.size() < substr.size() )
return false;
if ( behaviour == ContainsBehaviour::CaseSensitive )
return str.rfind( substr, 0 ) == 0;
else
return startsWith( toLower( str ), toLower( substr ), ContainsBehaviour::CaseSensitive );
}
bool MDAL::endsWith( const std::string &str, const std::string &substr, ContainsBehaviour behaviour )
{
if ( str.size() < substr.size() )
return false;
if ( behaviour == ContainsBehaviour::CaseSensitive )
return str.rfind( substr ) == ( str.size() - substr.size() );
else
return endsWith( toLower( str ), toLower( substr ), ContainsBehaviour::CaseSensitive );
}
std::vector<std::string> MDAL::split( const std::string &str,
const char delimiter
)
{
std::vector<std::string> list;
std::string::const_iterator start = str.begin();
std::string::const_iterator end = str.end();
std::string::const_iterator next;
std::string token;
do
{
next = std::find( start, end, delimiter );
token = std::string( start, next );
if ( !token.empty() )
list.push_back( token );
if ( next == end )
break;
else
start = next + 1;
}
while ( true );
return list;
}
std::vector<std::string> MDAL::split( const std::string &str,
const std::string &delimiter )
{
std::vector<std::string> list;
std::string::size_type start = 0;
std::string::size_type next;
std::string token;
do
{
next = str.find( delimiter, start );
if ( next == std::string::npos )
token = str.substr( start ); // rest of the string
else
token = str.substr( start, next - start ); // part of the string
if ( !token.empty() )
list.push_back( token );
start = next + delimiter.size();
}
while ( next != std::string::npos );
return list;
}
size_t MDAL::toSizeT( const std::string &str )
{
int i = atoi( str.c_str() );
if ( i < 0 ) // consistent with atoi return
i = 0;
return static_cast< size_t >( i );
}
size_t MDAL::toSizeT( const char &str )
{
int i = atoi( &str );
if ( i < 0 ) // consistent with atoi return
i = 0;
return static_cast< size_t >( i );
}
double MDAL::toDouble( const std::string &str )
{
return atof( str.c_str() );
}
int MDAL::toInt( const std::string &str )
{
return atoi( str.c_str() );
}
std::string MDAL::baseName( const std::string &filename )
{
// https://stackoverflow.com/a/8520815/2838364
std::string fname( filename );
// Remove directory if present.
// Do this before extension removal incase directory has a period character.
const size_t last_slash_idx = fname.find_last_of( "\\/" );
if ( std::string::npos != last_slash_idx )
{
fname.erase( 0, last_slash_idx + 1 );
}
// Remove extension if present.
const size_t period_idx = fname.rfind( '.' );
if ( std::string::npos != period_idx )
{
fname.erase( period_idx );
}
return fname;
}
std::string MDAL::pathJoin( const std::string &path1, const std::string &path2 )
{
//https://stackoverflow.com/questions/6297738/how-to-build-a-full-path-string-safely-from-separate-strings#6297807
#ifdef _MSC_VER
return path1 + "\\" + path2;
#else
return path1 + "/" + path2;
#endif
}
std::string MDAL::dirName( const std::string &filename )
{
std::string dname( filename );
const size_t last_slash_idx = dname.find_last_of( "\\/" );
if ( std::string::npos != last_slash_idx )
{
dname.erase( last_slash_idx, dname.size() - last_slash_idx );
}
return dname;
}
bool MDAL::contains( const std::string &str, const std::string &substr, ContainsBehaviour behaviour )
{
if ( behaviour == ContainsBehaviour::CaseSensitive )
return str.find( substr ) != std::string::npos;
else
{
auto it = std::search(
str.begin(), str.end(),
substr.begin(), substr.end(),
[]( char ch1, char ch2 )
{
#ifdef _MSC_VER
return toupper( ch1 ) == toupper( ch2 );
#else
return std::toupper( ch1 ) == std::toupper( ch2 );
#endif
}
);
return ( it != str.end() );
}
}
void MDAL::debug( const std::string &message )
{
#ifdef NDEBUG
MDAL_UNUSED( message );
#else
std::cout << message << std::endl;
#endif
}
bool MDAL::toBool( const std::string &str )
{
int i = atoi( str.c_str() );
return i != 0;
}
bool MDAL::contains( const std::vector<std::string> &list, const std::string &str )
{
return std::find( list.begin(), list.end(), str ) != list.end();
}
std::string MDAL::join( const std::vector<std::string> parts, const std::string &delimiter )
{
std::stringstream res;
for ( auto iter = parts.begin(); iter != parts.end(); iter++ )
{
if ( iter != parts.begin() ) res << delimiter;
res << *iter;
}
return res.str();
}
std::string MDAL::leftJustified( const std::string &str, size_t width, char fill )
{
std::string ret( str );
if ( ret.size() > width )
{
ret = ret.substr( 0, width );
}
else
{
ret = ret + std::string( width - ret.size(), fill );
}
assert( ret.size() == width );
return ret;
}
std::string MDAL::toLower( const std::string &std )
{
std::string res( std );
#ifdef WIN32
//silence algorithm(1443): warning C4244: '=': conversion from 'int' to 'char'
std::transform( res.begin(), res.end(), res.begin(),
[]( char c ) {return static_cast<char>( ::tolower( c ) );} );
#else
std::transform( res.begin(), res.end(), res.begin(), ::tolower );
#endif
return res;
}
std::string MDAL::replace( const std::string &str, const std::string &substr, const std::string &replacestr, MDAL::ContainsBehaviour behaviour )
{
std::string res( str );
if ( behaviour == ContainsBehaviour::CaseSensitive )
{
while ( res.find( substr ) != std::string::npos )
{
res.replace( res.find( substr ), substr.size(), replacestr );
}
}
else
{
// https://stackoverflow.com/a/40577390/2838364
std::string lower_s = toLower( str );
std::string lower_substring = toLower( substr );
auto position = lower_s.find( lower_substring );
while ( position != std::string::npos )
{
res.replace( position, lower_substring.size(), replacestr );
lower_s.replace( position, lower_substring.size(), replacestr );
position = lower_s.find( lower_substring );
}
}
return res;
}
// http://www.cplusplus.com/faq/sequences/strings/trim/
std::string MDAL::trim( const std::string &s, const std::string &delimiters )
{
return ltrim( rtrim( s, delimiters ), delimiters );
}
// http://www.cplusplus.com/faq/sequences/strings/trim/
std::string MDAL::ltrim( const std::string &s, const std::string &delimiters )
{
return s.substr( s.find_first_not_of( delimiters ) );
}
// http://www.cplusplus.com/faq/sequences/strings/trim/
std::string MDAL::rtrim( const std::string &s, const std::string &delimiters )
{
return s.substr( 0, s.find_last_not_of( delimiters ) + 1 );
}
MDAL::BBox MDAL::computeExtent( const MDAL::Vertices &vertices )
{
BBox b;
if ( vertices.empty() )
return b;
b.minX = vertices[0].x;
b.maxX = vertices[0].x;
b.minY = vertices[0].y;
b.maxY = vertices[0].y;
for ( Vertices::size_type i = 0; i < vertices.size(); i++ )
{
const Vertex &n = vertices[i];
if ( n.x > b.maxX ) b.maxX = n.x;
if ( n.x < b.minX ) b.minX = n.x;
if ( n.y > b.maxY ) b.maxY = n.y;
if ( n.y < b.minY ) b.minY = n.y;
}
return b;
}
bool MDAL::equals( double val1, double val2, double eps )
{
return fabs( val1 - val2 ) < eps;
}
double MDAL::safeValue( double val, double nodata, double eps )
{
if ( std::isnan( val ) )
return val;
if ( std::isnan( nodata ) )
return val;
if ( equals( val, nodata, eps ) )
return std::numeric_limits<double>::quiet_NaN();
return val;
}
double MDAL::parseTimeUnits( const std::string &units )
{
double divBy = 1;
// We are trying to parse strings like
// "seconds since 2001-05-05 00:00:00"
// "hours since 1900-01-01 00:00:0.0"
// "days since 1961-01-01 00:00:00"
const std::vector<std::string> units_list = MDAL::split( units, " since " );
if ( units_list.size() == 2 )
{
// Give me hours
if ( units_list[0] == "seconds" )
{
divBy = 3600.0;
}
else if ( units_list[0] == "minutes" )
{
divBy = 60.0;
}
else if ( units_list[0] == "days" )
{
divBy = 1.0 / 24.0;
}
}
return divBy;
}
MDAL::Statistics _calculateStatistics( const std::vector<double> &values, size_t count, bool isVector )
{
MDAL::Statistics ret;
double min = std::numeric_limits<double>::quiet_NaN();
double max = std::numeric_limits<double>::quiet_NaN();
bool firstIteration = true;
for ( size_t i = 0; i < count; ++i )
{
double magnitude;
if ( isVector )
{
double x = values[2 * i];
double y = values[2 * i + 1];
if ( std::isnan( x ) || std::isnan( y ) )
continue;
magnitude = sqrt( x * x + y * y );
}
else
{
double x = values[i];
if ( std::isnan( x ) )
continue;
magnitude = x;
}
if ( firstIteration )
{
firstIteration = false;
min = magnitude;
max = magnitude;
}
else
{
if ( magnitude < min )
{
min = magnitude;
}
if ( magnitude > max )
{
max = magnitude;
}
}
}
ret.minimum = min;
ret.maximum = max;
return ret;
}
MDAL::Statistics MDAL::calculateStatistics( std::shared_ptr<MDAL::DatasetGroup> grp )
{
return calculateStatistics( grp.get() );
}
MDAL::Statistics MDAL::calculateStatistics( DatasetGroup *grp )
{
Statistics ret;
if ( !grp )
return ret;
for ( std::shared_ptr<Dataset> ds : grp->datasets )
{
MDAL::Statistics dsStats = ds->statistics();
combineStatistics( ret, dsStats );
}
return ret;
}
MDAL::Statistics MDAL::calculateStatistics( std::shared_ptr<Dataset> dataset )
{
Statistics ret;
if ( !dataset )
return ret;
bool isVector = !dataset->group()->isScalar();
size_t bufLen = 2000;
std::vector<double> buffer( isVector ? bufLen * 2 : bufLen );
size_t i = 0;
while ( i < dataset->valuesCount() )
{
size_t valsRead;
if ( isVector )
{
valsRead = dataset->vectorData( i, bufLen, buffer.data() );
}
else
{
valsRead = dataset->scalarData( i, bufLen, buffer.data() );
}
if ( valsRead == 0 )
return ret;
MDAL::Statistics dsStats = _calculateStatistics( buffer, valsRead, isVector );
combineStatistics( ret, dsStats );
i += valsRead;
}
return ret;
}
void MDAL::combineStatistics( MDAL::Statistics &main, const MDAL::Statistics &other )
{
if ( std::isnan( main.minimum ) ||
( !std::isnan( other.minimum ) && ( main.minimum > other.minimum ) ) )
{
main.minimum = other.minimum;
}
if ( std::isnan( main.maximum ) ||
( !std::isnan( other.maximum ) && ( main.maximum < other.maximum ) ) )
{
main.maximum = other.maximum;
}
}
void MDAL::addBedElevationDatasetGroup( MDAL::Mesh *mesh, const Vertices &vertices )
{
if ( !mesh )
return;
if ( 0 == mesh->facesCount() )
return;
std::shared_ptr<DatasetGroup> group = std::make_shared< DatasetGroup >(
mesh->driverName(),
mesh,
mesh->uri(),
"Bed Elevation"
);
group->setIsOnVertices( true );
group->setIsScalar( true );
std::shared_ptr<MDAL::MemoryDataset> dataset = std::make_shared< MemoryDataset >( group.get() );
dataset->setTime( 0.0 );
double *vals = dataset->values();
for ( size_t i = 0; i < vertices.size(); ++i )
{
vals[i] = vertices[i].z;
}
dataset->setStatistics( MDAL::calculateStatistics( dataset ) );
group->datasets.push_back( dataset );
group->setStatistics( MDAL::calculateStatistics( group ) );
mesh->datasetGroups.push_back( group );
}
void MDAL::addFaceScalarDatasetGroup( MDAL::Mesh *mesh,
const std::vector<double> &values,
const std::string &name )
{
if ( !mesh )
return;
if ( values.empty() )
return;
if ( mesh->facesCount() == 0 )
return;
assert( values.size() == mesh->facesCount() );
std::shared_ptr<DatasetGroup> group = std::make_shared< DatasetGroup >(
mesh->driverName(),
mesh,
mesh->uri(),
name
);
group->setIsOnVertices( false );
group->setIsScalar( true );
std::shared_ptr<MDAL::MemoryDataset> dataset = std::make_shared< MemoryDataset >( group.get() );
dataset->setTime( 0.0 );
memcpy( dataset->values(), values.data(), sizeof( double )*values.size() );
dataset->setStatistics( MDAL::calculateStatistics( dataset ) );
group->datasets.push_back( dataset );
group->setStatistics( MDAL::calculateStatistics( group ) );
mesh->datasetGroups.push_back( group );
}
void MDAL::activateFaces( MDAL::MemoryMesh *mesh, std::shared_ptr<MemoryDataset> dataset )
{
// only for data on vertices
if ( !dataset->group()->isOnVertices() )
return;
bool isScalar = dataset->group()->isScalar();
// Activate only Faces that do all Vertex's outputs with some data
int *active = dataset->active();
const double *values = dataset->constValues();
const size_t nFaces = mesh->facesCount();
for ( unsigned int idx = 0; idx < nFaces; ++idx )
{
Face elem = mesh->faces.at( idx );
for ( size_t i = 0; i < elem.size(); ++i )
{
const size_t vertexIndex = elem[i];
if ( isScalar )
{
double val = values[vertexIndex];
if ( std::isnan( val ) )
{
active[idx] = 0; //NOT ACTIVE
break;
}
}
else
{
double x = values[2 * vertexIndex];
double y = values[2 * vertexIndex + 1];
if ( std::isnan( x ) || std::isnan( y ) )
{
active[idx] = 0; //NOT ACTIVE
break;
}
}
}
}
}
bool MDAL::isNativeLittleEndian()
{
// https://stackoverflow.com/a/4181991/2838364
int n = 1;
return ( *( char * )&n == 1 );
}