Skip to content

Commit 3f8d14c

Browse files
committed
NoData value handling, README changes
1 parent b7fd101 commit 3f8d14c

File tree

2 files changed

+44
-10
lines changed

2 files changed

+44
-10
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22

33
Fast generation of 2.5D meshes from elevation models.
44

5+
![image](https://user-images.githubusercontent.com/1951843/47350997-15d3da00-d685-11e8-8d9f-e394fc17859e.png)
6+
7+
![image](https://user-images.githubusercontent.com/1951843/47351205-7e22bb80-d685-11e8-87c5-33b21ae05b75.png)
8+
59
## Dependencies
610

7-
GDAL is the only requirement. To install it run:
11+
GDAL is the only dependency. To install it run:
812

913
```
1014
sudo apt-get install -y libgdal-dev
@@ -24,6 +28,8 @@ make
2428
./dem2mesh -rtc -verbose -inputFile dem.tif -outputFile mesh.ply
2529
```
2630

31+
:warning: The DEM should use a coordinate reference system (CRS) that has matching horizontal and vertical units (for example, UTM). If you are getting stretched or deformed results, double check your CRS and use `gdalwarp` to reproject the raster DEM. https://www.gdal.org/gdalwarp.html
32+
2733
## Options
2834

2935
| Flag | Description | Required |
@@ -32,5 +38,6 @@ make
3238
| -outputFile | Path to PLY mesh output ||
3339
| -maxVertexCount | Target number of vertices for the output mesh. This number is not always guaranteed to be reached. Defaults to `100000` | |
3440
| -maxTileLength | Max length of a tile. Smaller values take longer to process but reduce memory usage by splitting the meshing process into tiles. Defaults to `1000`. | |
41+
| -bandNum | Raster band # to use. Defaults to `1`. | |
3542
| -rtc | Use Relative To Center (RTC) X/Y coordinates in the output PLY mesh. This can be useful since most 3D visualization software use floating coordinate precision to represent vertices and using absolute coordinates might lead to jittering artifacts. | |
3643
| -verbose | Print verbose output. | |

src/main.cpp

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,15 @@ cmdLineParameter< char* >
7171
OutputFile( "outputFile" );
7272
cmdLineParameter< int >
7373
MaxVertexCount( "maxVertexCount" ) ,
74-
MaxTileLength( "maxTileLength" );
74+
MaxTileLength( "maxTileLength" ) ,
75+
BandNum ( "bandNum" );
7576
cmdLineReadable
7677
Rtc ( "rtc" ),
7778
Verbose( "verbose" );
7879

7980
cmdLineReadable* params[] = {
80-
&InputFile , &OutputFile , &MaxVertexCount , &MaxTileLength,
81+
&InputFile , &OutputFile ,
82+
&MaxVertexCount , &MaxTileLength, &BandNum,
8183
&Rtc, &Verbose ,
8284
NULL
8385
};
@@ -88,6 +90,7 @@ void help(char *ex){
8890
<< "\t -" << OutputFile.name << " <output PLY mesh>" << std::endl
8991
<< "\t [-" << MaxVertexCount.name << " <target number vertices> (Default: 100000)]" << std::endl
9092
<< "\t [-" << MaxTileLength.name << " <max length of a tile. Smaller values take longer to process but reduce memory usage by splitting the meshing process into tiles.> (Default: 1000)]" << std::endl
93+
<< "\t [-" << BandNum.name << " <Band number> (Default: 1)]" << std::endl
9194
<< "\t [-" << Rtc.name << "]" << std::endl
9295
<< "\t [-" << Verbose.name << "]" << std::endl;
9396
exit(EXIT_FAILURE);
@@ -280,12 +283,17 @@ void readBin(const std::string &filename, int blockX, int blockY){
280283

281284
void simplify(int target_count){
282285
unsigned long start_size = Simplify::triangles.size();
286+
if (target_count >= static_cast<int>(start_size)){
287+
logWriter("No simplification needed\n");
288+
return;
289+
}
290+
283291
const double AGRESSIVENESS = 5.0;
284292

285293
Simplify::simplify_mesh(target_count, AGRESSIVENESS, Verbose.set);
286294
if ( Simplify::triangles.size() >= start_size) {
287295
std::cerr << "Unable to reduce mesh.\n";
288-
exit(1);
296+
exit(EXIT_FAILURE);
289297
}
290298
}
291299

@@ -314,6 +322,7 @@ int main(int argc, char **argv) {
314322
if( !InputFile.set || !OutputFile.set ) help(argv[0]);
315323
if ( !MaxVertexCount.set ) MaxVertexCount.value = 100000;
316324
if ( !MaxTileLength.set ) MaxTileLength.value = 1000;
325+
if ( !BandNum.set ) BandNum.value = 1;
317326

318327
logWriter.verbose = Verbose.set;
319328
logWriter.outputFile = "dem2mesh.txt";
@@ -332,14 +341,22 @@ int main(int argc, char **argv) {
332341

333342
logWriter("Extent is (%f, %f), (%f, %f)\n", extent.min.x, extent.max.x, extent.min.y, extent.max.y);
334343

344+
GDALRasterBand *band = dataset->GetRasterBand(BandNum.value);
345+
346+
int hasNoData = FALSE;
347+
double nodata = band->GetNoDataValue(&hasNoData);
348+
349+
if (hasNoData){
350+
logWriter("NoData value: %.18g\n", nodata);
351+
}
352+
logWriter("Description: %s\n", band->GetDescription());
353+
335354
unsigned long long int vertex_count = static_cast<unsigned long long int>(arr_height) *
336355
static_cast<unsigned long long int>(arr_width);
337356

338357
logWriter("Reading raster...\n");
339358
logWriter("Total vertices before simplification: %llu\n", vertex_count);
340359

341-
GDALRasterBand *band = dataset->GetRasterBand(1);
342-
343360
int qtreeLevels = 0;
344361
int numBlocks = 1;
345362
while(true){
@@ -378,7 +395,7 @@ int main(int argc, char **argv) {
378395
if (band->RasterIO( GF_Read, xOffset, yOffset + y, blockSizeX + blockXPad, 1,
379396
rasterData, blockSizeX + blockXPad, 1, GDT_Float32, 0, 0 ) == CE_Failure){
380397
std::cerr << "Cannot access raster data" << std::endl;
381-
exit(1);
398+
exit(EXIT_FAILURE);
382399
}
383400

384401
for (int x = 0; x < blockSizeX + blockXPad; x++){
@@ -403,7 +420,12 @@ int main(int argc, char **argv) {
403420
if (y == 0 || x == 0 || y == rows - 2 || x == cols - 2) t1.deleted = -1; // freeze
404421
else t1.deleted = 0;
405422

406-
Simplify::triangles.push_back(t1);
423+
if (!hasNoData ||
424+
(Simplify::vertices[t1.v[0]].p.z != nodata &&
425+
Simplify::vertices[t1.v[1]].p.z != nodata &&
426+
Simplify::vertices[t1.v[2]].p.z != nodata)){
427+
Simplify::triangles.push_back(t1);
428+
}
407429

408430
Simplify::Triangle t2;
409431
t2.v[0] = cols * (y + 1) + x;
@@ -412,7 +434,13 @@ int main(int argc, char **argv) {
412434
if (y == 0 || x == 0 || y == rows - 2 || x == cols - 2) t2.deleted = -1; // freeze
413435
else t2.deleted = 0;
414436

415-
Simplify::triangles.push_back(t2);
437+
if (!hasNoData ||
438+
(Simplify::vertices[t2.v[0]].p.z != nodata &&
439+
Simplify::vertices[t2.v[1]].p.z != nodata &&
440+
Simplify::vertices[t2.v[2]].p.z != nodata)){
441+
Simplify::triangles.push_back(t2);
442+
}
443+
416444
}
417445
}
418446

@@ -428,7 +456,6 @@ int main(int argc, char **argv) {
428456

429457
logWriter("Sampled %d faces, target is %d\n", static_cast<int>(Simplify::triangles.size()), target_count);
430458
logWriter("Simplifying...\n");
431-
432459
simplify(target_count);
433460

434461
if (qtreeLevels == 0){

0 commit comments

Comments
 (0)