Fixed contour generation

This commit is contained in:
Trygve 2024-04-15 21:58:12 +02:00
parent ca42524503
commit 01ddd4e914

View File

@ -12,14 +12,15 @@
CaseMap produce_casemap(HeightMap* heightmap, float z) CaseMap produce_casemap(HeightMap* heightmap, float z)
{ {
uint8_t *cases = (uint8_t *) CPLMalloc(sizeof(uint8_t)*(heightmap->width-1)*(heightmap->height-1)); int length = (heightmap->width-1)*(heightmap->height-1);
for (int i = 0; i<(heightmap->width-1)*(heightmap->height-1); i++) { uint8_t *cases = (uint8_t *) CPLMalloc(sizeof(uint8_t)*length);
int y = i/(heightmap->height-1); for (int i = 0; i<length; i++) {
int x = i%(heightmap->height-1); int y = i/(heightmap->width-1);
uint8_t result = (heightmap->get_pixel(x,y+1)>z) + int x = i%(heightmap->width-1);
(heightmap->get_pixel(x+1,y+1)>z)*2 + uint8_t result = (heightmap->get_pixel(x,y)>z) +
(heightmap->get_pixel(x+1,y)>z)*4 + (heightmap->get_pixel(x+1,y)>z)*2 +
(heightmap->get_pixel(x,y)>z)*8; (heightmap->get_pixel(x+1,y+1)>z)*4 +
(heightmap->get_pixel(x,y+1)>z)*8;
*(cases + i) = result; *(cases + i) = result;
} }
@ -28,7 +29,7 @@ CaseMap produce_casemap(HeightMap* heightmap, float z)
} }
void write_output_file(CaseMap* casemap) void write_output_file(CaseMap* casemap, const char *filepath)
{ {
const char *pszDriverName = "ESRI Shapefile"; const char *pszDriverName = "ESRI Shapefile";
@ -45,7 +46,7 @@ void write_output_file(CaseMap* casemap)
GDALDataset *poDS; GDALDataset *poDS;
poDS = poDriver->Create( "point_out.shp", 0, 0, 0, GDT_Unknown, NULL ); poDS = poDriver->Create( filepath, 0, 0, 0, GDT_Unknown, NULL );
if( poDS == NULL ) if( poDS == NULL )
{ {
printf( "Creation of output file failed.\n" ); printf( "Creation of output file failed.\n" );
@ -54,7 +55,7 @@ void write_output_file(CaseMap* casemap)
OGRLayer *poLayer; OGRLayer *poLayer;
poLayer = poDS->CreateLayer( "point_out", &casemap->reference_system, wkbPoint, NULL ); poLayer = poDS->CreateLayer( "contours", &casemap->reference_system, wkbPoint, NULL );
if( poLayer == NULL ) if( poLayer == NULL )
{ {
printf( "Layer creation failed.\n" ); printf( "Layer creation failed.\n" );
@ -74,8 +75,8 @@ void write_output_file(CaseMap* casemap)
for (int i = 0; i<casemap->height*casemap->width; i++) { for (int i = 0; i<casemap->height*casemap->width; i++) {
if (*(casemap->cases + i) != 0 && *(casemap->cases + i) != 15) if (*(casemap->cases + i) != 0 && *(casemap->cases + i) != 15)
{ {
int x_int = i%casemap->height; int x_int = i%casemap->width;
int y_int = casemap->height*casemap->width - i/casemap->height; int y_int = casemap->height*casemap->width - i/casemap->width;
double x = double(x_int); double x = double(x_int);
double y = double(y_int); double y = double(y_int);
@ -128,7 +129,7 @@ int main(int argc, const char* argv[])
std::cout << "x: " << map.width << " y: " << map.height << "\n"; std::cout << "x: " << map.width << " y: " << map.height << "\n";
std::cout << "max: " << map.max << " min: " << map.min << "\n"; std::cout << "max: " << map.max << " min: " << map.min << "\n";
auto casemap = produce_casemap(&map, 40.0); auto casemap = produce_casemap(&map, 40);
/* /*
for (int y = 0; y < casemap.height; y++) for (int y = 0; y < casemap.height; y++)
{ {
@ -141,5 +142,5 @@ int main(int argc, const char* argv[])
//std::cout << "\n"; //std::cout << "\n";
} }
*/ */
write_output_file(&casemap); write_output_file(&casemap, "out.shp");
} }