mirror of
https://gitlab.com/Trygve/contour-creator.git
synced 2024-12-28 08:40:17 +00:00
Created a lookup table
This commit is contained in:
parent
f962a40ade
commit
882764a13d
72
src/main.cpp
72
src/main.cpp
@ -56,13 +56,33 @@ std::vector<CellMap> vector_cellmap(HeightMap* heightmap, int interval)
|
||||
std::tuple<double, double> local_to_projected(double* geotransform, int x, int y)
|
||||
{
|
||||
return {
|
||||
geotransform[1] * x + geotransform[2] * y +
|
||||
geotransform[1] * 0.5 + geotransform[2] * 0.5 + geotransform[0],
|
||||
geotransform[4] * x + geotransform[5] * y +
|
||||
geotransform[4] * 0.5 + geotransform[5] * 0.5 + geotransform[3]
|
||||
};
|
||||
geotransform[1] * x + geotransform[2] * y +
|
||||
geotransform[1] * 0.5 + geotransform[2] * 0.5 + geotransform[0],
|
||||
geotransform[4] * x + geotransform[5] * y +
|
||||
geotransform[4] * 0.5 + geotransform[5] * 0.5 + geotransform[3]
|
||||
};
|
||||
}
|
||||
|
||||
constexpr std::tuple<double, double, double, double> marching_squares_lookup(int mcase)
|
||||
{
|
||||
switch (mcase) {
|
||||
case 1: return {0, 0.5, 0.5, 0};
|
||||
case 2: return {1, 0.5, 0.5, 0};
|
||||
case 3: return {0, 0.5, 1, 0};
|
||||
case 4: return {0.5, 1, 1, 0.5};
|
||||
case 5: return {0, 0, 0, 0}; //FIXME
|
||||
case 6: return {0.5, 1, 0.5, 0};
|
||||
case 7: return {0, 0.5, 0.5, 1};
|
||||
case 8: return {0, 0.5, 0.5, 1};
|
||||
case 9: return {0.5, 1, 0.5, 0};
|
||||
case 10: return {0, 0, 0, 0}; //FIXME
|
||||
case 11: return {0.5, 1, 1, 0.5};
|
||||
case 12: return {0, 0.5, 1, 0.5};
|
||||
case 13: return {0.5, 0, 1, 0.5};
|
||||
case 14: return {0, 0.5, 0.5, 0};
|
||||
}
|
||||
|
||||
}
|
||||
void write_output_file(std::vector<CellMap> cellmaps, const char *filepath)
|
||||
{
|
||||
|
||||
@ -108,35 +128,41 @@ void write_output_file(std::vector<CellMap> cellmaps, const char *filepath)
|
||||
for (int j = 0; j < cellmaps.size(); j++)
|
||||
{
|
||||
CellMap* cellmap = &cellmaps[j];
|
||||
OGRFeature *feature;
|
||||
OGRLineString *geometry = new OGRLineString();
|
||||
feature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() );
|
||||
feature->SetField( "Name", j );
|
||||
|
||||
for (int i = 0; i < cellmap->height*cellmap->width; i++)
|
||||
{
|
||||
if (*(cellmap->cells + i) != 0 && *(cellmap->cells + i) != 15)
|
||||
{
|
||||
OGRFeature *feature;
|
||||
OGRLineString *geometry = new OGRLineString();
|
||||
feature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() );
|
||||
feature->SetField( "Name", j );
|
||||
int x_raw = i%cellmap->width;
|
||||
int y_raw = i/cellmap->width;
|
||||
auto [x, y] = local_to_projected(cellmap->geotransform, x_raw, y_raw);
|
||||
auto [x_bl, y_bl, x_tr, y_tr] = marching_squares_lookup(*(cellmap->cells + i) );
|
||||
//std::cout << x << ", " << y << " ";
|
||||
geometry->setPoint( geometry->getNumPoints(), x, y );
|
||||
}
|
||||
geometry->setPoint( geometry->getNumPoints(), x+x_tr*0.25, y+y_tr*0.25 );
|
||||
geometry->setPoint( geometry->getNumPoints(), x+x_bl*0.25, y+y_bl*0.25 );
|
||||
|
||||
|
||||
if ( feature->SetGeometry(geometry) != OGRERR_NONE)
|
||||
{
|
||||
printf( "Failed to set geometry.\n" );
|
||||
exit( 1 );
|
||||
}
|
||||
OGRGeometryFactory::destroyGeometry(geometry);
|
||||
if( poLayer->CreateFeature( feature ) != OGRERR_NONE )
|
||||
{
|
||||
printf( "Failed to create feature in shapefile.\n" );
|
||||
exit( 1 );
|
||||
}
|
||||
OGRFeature::DestroyFeature( feature );
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if ( feature->SetGeometry(geometry) != OGRERR_NONE)
|
||||
{
|
||||
printf( "Failed to set geometry.\n" );
|
||||
exit( 1 );
|
||||
}
|
||||
OGRGeometryFactory::destroyGeometry(geometry);
|
||||
if( poLayer->CreateFeature( feature ) != OGRERR_NONE )
|
||||
{
|
||||
printf( "Failed to create feature in shapefile.\n" );
|
||||
exit( 1 );
|
||||
}
|
||||
OGRFeature::DestroyFeature( feature );
|
||||
|
||||
}
|
||||
|
||||
GDALClose( poDS );
|
||||
|
Loading…
Reference in New Issue
Block a user