File: mdal_mike21.cpp

package info (click to toggle)
qgis 3.40.13%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,185,160 kB
  • sloc: cpp: 1,615,781; python: 372,865; xml: 23,474; sh: 3,761; perl: 3,664; ansic: 2,829; sql: 2,137; yacc: 1,068; lex: 577; javascript: 540; lisp: 411; makefile: 155
file content (486 lines) | stat: -rw-r--r-- 13,738 bytes parent folder | download | duplicates (7)
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
/*
 MDAL - Mesh Data Abstraction Library (MIT License)
 Copyright (C) 2023 Lutra Consulting Ltd.
*/

#include <stddef.h>
#include <iosfwd>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <map>
#include <cassert>
#include <limits>
#include <algorithm>
#include <regex>

#include "mdal_mike21.hpp"
#include "mdal.h"
#include "mdal_utils.hpp"
#include "mdal_logger.hpp"

#define DRIVER_NAME "Mike21"

// function to split using regex, by default split on whitespace characters
std::vector<std::string> regex_split( const std::string &input, const std::regex &split_regex = std::regex{"\\s+"} )
{
  std::sregex_token_iterator iter( input.begin(), input.end(), split_regex, -1 );
  std::sregex_token_iterator end;
  return {iter, end};
}

static bool parse_vertex_id_gaps( std::map<size_t, size_t> &vertexIDtoIndex, size_t vertexIndex, size_t vertexID )
{
  if ( vertexIndex == vertexID )
    return false;

  std::map<size_t, size_t>::iterator search = vertexIDtoIndex.find( vertexID );
  if ( search != vertexIDtoIndex.end() )
  {
    MDAL::Log::warning( Warn_ElementNotUnique, DRIVER_NAME, "could not find vertex" );
    return true;
  }

  vertexIDtoIndex[vertexID] = vertexIndex;
  return false;
}

static void persist_native_index( std::vector<double> &arr, size_t nativeID, size_t ourId, size_t maxOurId )
{
  if ( !arr.empty() || ( nativeID != ourId + 1 ) )
  {
    // we have gaps in face indexing
    if ( arr.empty() )
    {
      arr.resize( maxOurId );
      for ( size_t i = 0; i < ourId; ++i )
        arr[i] = static_cast<double>( i + 1 );
    }
    arr[ourId] = static_cast<double>( nativeID );
  }
}

MDAL::MeshMike21::MeshMike21( size_t faceVerticesMaximumCount,
                              const std::string &uri,
                              const std::map<size_t, size_t> vertexIDtoIndex )
  : MemoryMesh( DRIVER_NAME,
                faceVerticesMaximumCount,
                uri )
  , mVertexIDtoIndex( vertexIDtoIndex )
{
}

MDAL::MeshMike21::~MeshMike21() = default;

size_t MDAL::MeshMike21::vertexIndex( size_t vertexID ) const
{
  auto ni2i = mVertexIDtoIndex.find( vertexID );
  if ( ni2i != mVertexIDtoIndex.end() )
  {
    return  ni2i->second; // convert from ID to index
  }
  return vertexID;
}

size_t MDAL::MeshMike21::maximumVertexId() const
{
  size_t maxIndex = verticesCount() - 1;
  if ( mVertexIDtoIndex.empty() )
    return maxIndex;
  else
  {
    // std::map is sorted!
    size_t maxID = mVertexIDtoIndex.rbegin()->first;
    return std::max( maxIndex, maxID );
  }
}

MDAL::DriverMike21::DriverMike21( ):
  Driver( DRIVER_NAME,
          "Mike21 Mesh File",
          "*.mesh",
          Capability::ReadMesh | Capability::SaveMesh
        )
{
}

MDAL::DriverMike21 *MDAL::DriverMike21::create()
{
  return new DriverMike21();
}

MDAL::DriverMike21::~DriverMike21() = default;

bool MDAL::DriverMike21::canReadHeader( const std::string &line )
{
  bool header2012 = std::regex_match( line, mRegexHeader2012 );
  bool header2011 = std::regex_match( line, mRegexHeader2011 );
  return header2011 || header2012;
}

bool MDAL::DriverMike21::canReadMesh( const std::string &uri )
{
  std::ifstream in = MDAL::openInputFile( uri );
  std::string line;

  if ( !MDAL::getHeaderLine( in, line ) || !canReadHeader( line ) ||  !MDAL::contains( filters(), MDAL::fileExtension( uri ) ) )
  {
    return false;
  }

  return true;
}

void MDAL::DriverMike21::parseHeader( const std::string &line )
{
  auto matchResults = std::smatch{};
  if ( std::regex_search( line, matchResults, mRegexHeader2012 ) )
  {
    if ( matchResults.size() > 4 )
    {
      mDataType = matchResults[1].str();
      mDataUnit = matchResults[2].str();
      mVertexCount = std::stoi( matchResults[3].str() );
      mCrs = matchResults[4].str();
      return;
    }
  }

  if ( std::regex_search( line, matchResults, mRegexHeader2011 ) )
  {
    if ( matchResults.size() > 2 )
    {
      mVertexCount = std::stoi( matchResults[1].str() );
      mCrs = matchResults[2].str();
      return;
    }
  }
}

std::unique_ptr<MDAL::Mesh> MDAL::DriverMike21::load( const std::string &meshFile, const std::string & )
{
  mMeshFile = meshFile;

  MDAL::Log::resetLastStatus();

  std::ifstream in = MDAL::openInputFile( meshFile );

  std::string line;
  if ( !std::getline( in, line ) || !canReadHeader( line ) )
  {
    MDAL::Log::error( MDAL_Status::Err_UnknownFormat, name(), meshFile + " could not be opened" );
    return nullptr;
  }

  parseHeader( line );

  size_t faceCount = 0;
  size_t maxVerticesPerFace = 2;

  size_t lineNumber = 1;

  while ( std::getline( in, line ) )
  {
    if ( lineNumber == mVertexCount + 1 )
    {
      auto matchResults = std::smatch{};
      if ( std::regex_search( line, matchResults, mRegexElementHeader ) )
      {
        if ( matchResults.size() >= 4 )
        {
          faceCount = MDAL::toSizeT( matchResults[1].str() );
          maxVerticesPerFace = MDAL::toSizeT( matchResults[2].str() );
          size_t meshType = MDAL::toSizeT( matchResults[3].str() );

          if ( !( meshType == 21 || meshType == 25 ) )
          {
            MDAL::Log::error( MDAL_Status::Err_InvalidData, name(), "unknown mesh type." );
            return nullptr;
          }
        }
        else
        {
          MDAL::Log::error( MDAL_Status::Err_InvalidData, name(), "element header not in valid format." );
          return nullptr;
        }
      }
      else
      {
        MDAL::Log::error( MDAL_Status::Err_InvalidData, name(), "element header not in valid format." );
        return nullptr;
      }

    }
    lineNumber++;
  }

  // number of lines in file does not match number of vertices and faces specifed in first and element line
  if ( lineNumber > 2 + mVertexCount + faceCount )
  {
    MDAL::Log::error( MDAL_Status::Err_InvalidData, name(), "Number of lines in file does not fit with number of vertexes and faces specified." );
    return nullptr;
  }

  in.clear();
  in.seekg( 0, std::ios::beg );

  Vertices vertices( mVertexCount );
  Faces faces( faceCount );

  std::map<size_t, size_t> vertexIDtoIndex;
  std::vector<double> vertexType( mVertexCount );

  std::vector<double> nativeVertexIds;
  std::vector<double> nativeFaceIds;

  size_t lastVertexID = 0;
  size_t faceIndex = 0;
  size_t vertexIndex = 0;

  std::vector<std::string> chunks;
  lineNumber = 0;

  while ( std::getline( in, line ) )
  {
    if ( 0 < lineNumber && lineNumber < mVertexCount + 1 )
    {
      chunks = regex_split( MDAL::trim( line ) );
      if ( chunks.size() != 5 )
      {
        MDAL::Log::error( MDAL_Status::Err_InvalidData, name(), "vertex line in invalid format." );
        return nullptr;
      }

      size_t nodeID = toSizeT( chunks[0] );

      if ( nodeID != 0 )
      {
        // specification of Mike21 does not state if vertexIDs need to continuos, expect that they might be not
        // in the same way as in 2DM
        if ( ( lastVertexID != 0 ) && ( nodeID <= lastVertexID ) )
        {
          // the algorithm requires that the file has points orderer by index
          MDAL::Log::error( MDAL_Status::Err_InvalidData, name(), "nodes are not ordered by index" );
          return nullptr;
        }
        lastVertexID = nodeID;
      }

      // in case we have gaps/reorders in native indexes, store it
      persist_native_index( nativeVertexIds, nodeID, vertexIndex, mVertexCount );
      parse_vertex_id_gaps( vertexIDtoIndex, vertexIndex, nodeID - 1 );

      assert( vertexIndex < mVertexCount );
      Vertex &vertex = vertices[vertexIndex];
      vertex.x = toDouble( chunks[1] );
      vertex.y = toDouble( chunks[2] );
      vertex.z = toDouble( chunks[3] );
      vertexType[vertexIndex] = MDAL::toInt( chunks[4] );
      vertexIndex++;
    }

    if ( mVertexCount + 1 < lineNumber )
    {
      chunks = regex_split( MDAL::trim( line ) );
      assert( faceIndex < faceCount );

      size_t faceVertexCount = chunks.size() - 1;
      // if the face should have 4 vertexes last chunk has value 0
      // it actually means that there are only 3 vertexes
      if ( faceVertexCount == 4 && chunks.size() == 5 )
      {
        if ( MDAL::toSizeT( chunks[4] ) == 0 )
          faceVertexCount = faceVertexCount - 1;
      }

      assert( ( faceVertexCount == 3 ) || ( faceVertexCount == 4 ) );
      if ( maxVerticesPerFace < faceVertexCount )
        maxVerticesPerFace = faceVertexCount;

      Face &face = faces[faceIndex];
      face.resize( faceVertexCount );

      // in case we have gaps/reorders in native indexes, store it
      size_t nativeID = MDAL::toSizeT( chunks[0] );
      persist_native_index( nativeFaceIds, nativeID, faceIndex, faceCount );

      for ( size_t i = 0; i < faceVertexCount; ++i )
        face[i] = MDAL::toSizeT( chunks[i + 1] ) - 1; // Mike21 is numbered from 1

      faceIndex++;
    }

    lineNumber++;
  }

  for ( std::vector<Face>::iterator it = faces.begin(); it != faces.end(); ++it )
  {
    Face &face = *it;
    for ( Face::size_type nd = 0; nd < face.size(); ++nd )
    {
      size_t nodeID = face[nd];

      std::map<size_t, size_t>::iterator ni2i = vertexIDtoIndex.find( nodeID );
      if ( ni2i != vertexIDtoIndex.end() )
      {
        face[nd] = ni2i->second; // convert from ID to index
      }
      else if ( vertices.size() < nodeID )
      {
        MDAL::Log::warning( MDAL_Status::Warn_ElementWithInvalidNode, name(), "found invalid node" );
      }
    }
  }

  // create the mesh and set the required data
  std::unique_ptr< MeshMike21 > mesh(
    new MeshMike21(
      maxVerticesPerFace,
      mMeshFile,
      vertexIDtoIndex
    )
  );
  mesh->setFaces( std::move( faces ) );
  mesh->setVertices( std::move( vertices ) );

  // Add Vertex Type
  MDAL::addVertexScalarDatasetGroup( mesh.get(), vertexType, "VertexType" );

  // Add Bed Elevation
  MDAL::addBedElevationDatasetGroup( mesh.get(), mesh->vertices() );

  if ( !nativeFaceIds.empty() )
    MDAL::addFaceScalarDatasetGroup( mesh.get(), nativeFaceIds, "NativeFaceIds" );
  if ( !nativeVertexIds.empty() )
    MDAL::addVertexScalarDatasetGroup( mesh.get(), nativeVertexIds, "NativeVertexIds" );

  mesh->setSourceCrs( mCrs );
  mesh->setMetadata( "crs", mCrs );
  if ( !mDataType.empty() )
    mesh->setMetadata( "data_type", mDataType );

  if ( !mDataUnit.empty() )
    mesh->setMetadata( "data_unit", mDataUnit );

  return std::unique_ptr<Mesh>( mesh.release() );
}

void MDAL::DriverMike21::save( const std::string &fileName, const std::string &, MDAL::Mesh *mesh )
{
  MDAL::Log::resetLastStatus();

  std::ofstream file = MDAL::openOutputFile( fileName, std::ofstream::out );

  if ( !file.is_open() )
  {
    MDAL::Log::error( MDAL_Status::Err_FailToWriteToDisk, name(), "Could not open file " + fileName );
  }

  std::string line;

  const std::string dataType = mesh->getMetadata( "data_type" );
  const std::string dataUnit = mesh->getMetadata( "data_unit" );
  if ( !dataType.empty() && !dataUnit.empty() )
    line.append( dataType + " " + dataUnit + " " );

  line.append( std::to_string( mesh->verticesCount() ) + " " + mesh->getMetadata( "crs" ) );

  file << line << std::endl;

  std::vector<double> vertexTypes;

  std::shared_ptr<MDAL::DatasetGroup> vertexTypeDG = mesh->group( "VertexType" );
  if ( vertexTypeDG )
  {
    vertexTypes.resize( mesh->verticesCount() );
    auto d = vertexTypeDG->datasets[0];
    d->scalarData( 0, mesh->verticesCount(), vertexTypes.data() );
  }

  // write vertices
  std::unique_ptr<MDAL::MeshVertexIterator> vertexIterator = mesh->readVertices();
  double vertex[3];
  for ( size_t i = 0; i < mesh->verticesCount(); ++i )
  {
    vertexIterator->next( 1, vertex );
    line = "";
    line.append( std::to_string( i + 1 ) );
    for ( size_t j = 0; j < 2; ++j )
    {
      line.append( " " );
      line.append( MDAL::coordinateToString( vertex[j] ) );
    }
    line.append( " " );
    line.append( MDAL::doubleToString( vertex[2] ) );

    line.append( " " );
    if ( vertexTypes.size() == mesh->verticesCount() )
    {
      line.append( MDAL::doubleToString( vertexTypes.at( i ) ) );
    }
    else
    {
      line.append( MDAL::doubleToString( 0 ) );
    }

    file << line << std::endl;
  }

  //write element header line
  size_t elementType = 0;
  if ( mesh->faceVerticesMaximumCount() == 3 )
  {
    elementType = 21;
  }
  else if ( mesh->faceVerticesMaximumCount() == 4 )
  {
    elementType = 25;
  }

  line = std::to_string( mesh->facesCount() );
  line.append( " " );
  line.append( std::to_string( mesh->faceVerticesMaximumCount() ) );
  line.append( " " );
  line.append( std::to_string( elementType ) );
  file << line << std::endl;

  // write faces
  std::vector<int> vertexIndices( mesh->faceVerticesMaximumCount() );
  std::unique_ptr<MDAL::MeshFaceIterator> faceIterator = mesh->readFaces();
  for ( size_t i = 0; i < mesh->facesCount(); ++i )
  {
    int faceOffsets[1];
    faceIterator->next( 1, faceOffsets, 4, vertexIndices.data() );

    if ( faceOffsets[0] > 2 && faceOffsets[0] < 5 )
    {
      line = "";
      line.append( std::to_string( i + 1 ) );

      for ( int j = 0; j < faceOffsets[0]; ++j )
      {
        line.append( " " );
        line.append( std::to_string( vertexIndices[j] + 1 ) );
      }

      // if face has 3 vertexes but the mesh as whole is marked as having
      // 4 vertex at maximum, the last element should 0 - indicating no vertex there
      if ( faceOffsets[0] == 3 && mesh->faceVerticesMaximumCount() == 4 )
      {
        line.append( " " );
        line.append( "0" );
      }

    }
    file << line << std::endl;
  }

  file.close();
}

std::string MDAL::DriverMike21::saveMeshOnFileSuffix() const
{
  return "mesh";
}