From 9d8df3256d8fb41a53289db50c6fe0e617fe18b2 Mon Sep 17 00:00:00 2001 From: Trygve Date: Fri, 3 May 2024 20:33:28 +0200 Subject: [PATCH] Started on continus line drawing --- src/contour_creator.hh | 2 + src/main.cpp | 106 ++++++++++++++++++++++++----------------- 2 files changed, 63 insertions(+), 45 deletions(-) diff --git a/src/contour_creator.hh b/src/contour_creator.hh index 4bc45f5..64c9beb 100644 --- a/src/contour_creator.hh +++ b/src/contour_creator.hh @@ -45,6 +45,8 @@ class Point int mscase; Point * next; Point * previous; + bool endpoint = false; + bool allocated = false; Point(int x, int y, int mscase){ this -> x = x; this -> y = y; diff --git a/src/main.cpp b/src/main.cpp index 8da17d0..600dd18 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -52,30 +52,38 @@ std::vector> vector_cellmap(HeightMap* heightmap, int interva for (int i = 1; i <= num_contours; i++) { auto points = produce_cellmap(heightmap, heightmap->min + interval*i); - for (int j = 0; j < points.size(); j++){ - Point current = points[j]; + Point* current = &points[j]; //std::cout << points_allocated << " " << points.size() << "\n"; for (int k = 0; k < points.size(); k++){ - Point candidate = points[k]; - if (candidate.next != nullptr) { - if (candidate.x +1 == current.x) { - current.next = &candidate; - } - else if (candidate.x -1 == current.x) { - current.next = &candidate; - } - else if (candidate.y +1 == current.x) { - current.next = &candidate; - } - else if (candidate.y -1 == current.x) { - current.next = &candidate; - } + Point* candidate = &points[k]; + if (candidate->previous == nullptr and candidate != current->previous) { + if (candidate->x +1 == current->x && candidate->y == current->y) { + current->next = candidate; + candidate->previous = current; + } + else if (candidate->x -1 == current->x && candidate->y == current->y) { + current->next = candidate; + candidate->previous = current; + } + else if (candidate->y +1 == current->y && candidate->x == current->x) { + current->next = candidate; + candidate->previous = current; + } + else if (candidate->y -1 == current->y && candidate->x == current->x) { + current->next = candidate; + candidate->previous = current; + } + else { + current->endpoint = true; + } } } + } + vec_private.push_back(points); } #pragma omp critical @@ -162,47 +170,55 @@ void write_output_file(std::vector> all_points, const char *f for (int j = 0; j < all_points.size(); j++) { - OGRFeature *feature; - OGRLineString *geometry = new OGRLineString(); - feature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() ); - feature->SetField( "Name", j ); + OGRFeature *feature; + OGRLineString *geometry = new OGRLineString(); + feature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() ); + feature->SetField( "Name", j ); std::vector points = all_points[j]; - - for (int i = 0; i < points.size(); i++) + Point* current = &points[0]; + while (true) { - //int x_raw = i%width; //int y_raw = i/height; - int x_raw = points[i].x; - int y_raw = points[i].y; + int x_raw = current->x; + int y_raw = current->y; auto [x, y] = local_to_projected(heightmap->geotransform, x_raw, y_raw); //auto [x_bl, y_bl, x_tr, y_tr] = marching_squares_lookup(*(cellmap->cells + i) ); - //std::cout << x << ", " << y << " "; + //std::cout << x_raw << ", " << y_raw << " ";std::cout.flush(); //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 ); - geometry->setPoint(geometry->getNumPoints(),x ,y ); - - - - + geometry->setPoint(geometry->getNumPoints(),x_raw ,y_raw ); + current->allocated = true; + current = current->previous; + //current sometimes becomes random pointer????? + if (current == nullptr) + { + for (int k = 0; kSetGeometry(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 ); + break; } GDALClose( poDS );