From 9d8df3256d8fb41a53289db50c6fe0e617fe18b2 Mon Sep 17 00:00:00 2001 From: Trygve Date: Fri, 3 May 2024 20:33:28 +0200 Subject: [PATCH 1/5] 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 ); From 4cd4f773bfc64419d70be6953e52961aaa5efa16 Mon Sep 17 00:00:00 2001 From: Trygve Date: Fri, 3 May 2024 20:54:45 +0200 Subject: [PATCH 2/5] Fixed stupid mistake in for loop --- src/main.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 600dd18..e598baf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -63,23 +63,30 @@ std::vector> vector_cellmap(HeightMap* heightmap, int interva if (candidate->x +1 == current->x && candidate->y == current->y) { current->next = candidate; candidate->previous = current; + break; } else if (candidate->x -1 == current->x && candidate->y == current->y) { current->next = candidate; candidate->previous = current; + break; } else if (candidate->y +1 == current->y && candidate->x == current->x) { current->next = candidate; candidate->previous = current; + break; } else if (candidate->y -1 == current->y && candidate->x == current->x) { current->next = candidate; candidate->previous = current; - } - else { - current->endpoint = true; + break; } } + + } + if (current->next == nullptr) + { + current->endpoint = true; + std::cout << "🤕"; } } @@ -177,7 +184,7 @@ void write_output_file(std::vector> all_points, const char *f std::vector points = all_points[j]; - Point* current = &points[0]; + Point* current = &points[50]; while (true) { //int x_raw = i%width; From 4f709d3b07dfbb620b89216f71329936a0d938d5 Mon Sep 17 00:00:00 2001 From: Trygve Date: Sun, 5 May 2024 16:53:00 +0200 Subject: [PATCH 3/5] Use linked list to store the order of the points --- src/main.cpp | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index e598baf..7c35e4c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,12 +7,14 @@ #include #include #include +#include #include #include #include #include "gdal/gdal_priv.h" #include #include +#include std::vector produce_cellmap(HeightMap* heightmap, float z) { @@ -45,10 +47,10 @@ std::vector> vector_cellmap(HeightMap* heightmap, int interva std::vector> vector_contours; omp_set_num_threads(12); - #pragma omp parallel + //#pragma omp parallel { std::vector> vec_private; - #pragma omp for + //#pragma omp for for (int i = 1; i <= num_contours; i++) { auto points = produce_cellmap(heightmap, heightmap->min + interval*i); @@ -86,15 +88,17 @@ std::vector> vector_cellmap(HeightMap* heightmap, int interva if (current->next == nullptr) { current->endpoint = true; - std::cout << "🤕"; + std::cout << "🤕";std::cout.flush(); } } - vec_private.push_back(points); + //vec_private.push_back(points); + vector_contours.push_back(points); + std::cout << vector_contours.size(); } - #pragma omp critical + //#pragma omp critical - vector_contours.insert(vector_contours.end(), vec_private.begin(), vec_private.end()); + //vector_contours.insert(vector_contours.end(), vec_private.begin(), vec_private.end()); } return vector_contours; @@ -183,33 +187,44 @@ void write_output_file(std::vector> all_points, const char *f feature->SetField( "Name", j ); - std::vector points = all_points[j]; - Point* current = &points[50]; + std::vector* points = &all_points[j]; + + + + Point* current = &points->at(0); + int points_allocated = 0; while (true) { //int x_raw = i%width; //int y_raw = i/height; int x_raw = current->x; int y_raw = current->y; + /* + if (x_raw > 500 || y_raw > 400) + { + std::cout << "🤯";std::cout.flush(); + continue; + } + */ 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_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_raw ,y_raw ); + geometry->setPoint(geometry->getNumPoints(),x ,y ); current->allocated = true; current = current->previous; //current sometimes becomes random pointer????? - if (current == nullptr) + if (current == nullptr or current->allocated) { - for (int k = 0; ksize(); k++) { - if (points[k].endpoint and !points[k].allocated) + if (points->at(k).endpoint and !points->at(k).allocated) { - current = &points[k]; + current = &points->at(k); } } - if (current == nullptr){std::cout << "🫠"; break; } + if (current == nullptr){std::cout << "🫠";std::cout.flush(); break; } } } @@ -225,7 +240,6 @@ void write_output_file(std::vector> all_points, const char *f exit( 1 ); } OGRFeature::DestroyFeature( feature ); - break; } GDALClose( poDS ); From c535eec958c8c5309775ceb91eb97d4c4b942d4a Mon Sep 17 00:00:00 2001 From: Trygve Date: Mon, 6 May 2024 11:37:10 +0200 Subject: [PATCH 4/5] Debugging without omp --- src/main.cpp | 84 +++++++++++++++++++++++++--------------------------- 1 file changed, 41 insertions(+), 43 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 7c35e4c..1f7b809 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -54,47 +54,60 @@ 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); - + std::vector line; + int x; + int y; for (int j = 0; j < points.size(); j++){ Point* current = &points[j]; + current->allocated=true; + line.push_back(points[j]); + x = points[j].x; + y = points[j].y; + //std::cout << points_allocated << " " << points.size() << "\n"; for (int k = 0; k < points.size(); k++){ 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; + if (!candidate->allocated) { + if (candidate->x == x +1 && candidate->y == y) { + candidate->allocated = true; + x = candidate->x; + y = candidate->y; + line.push_back(*candidate); break; } - else if (candidate->x -1 == current->x && candidate->y == current->y) { - current->next = candidate; - candidate->previous = current; + else if (candidate->x == x -1&& candidate->y == y) { + x = candidate->x; + y = candidate->y; + candidate->allocated = true; + line.push_back(*candidate); break; } - else if (candidate->y +1 == current->y && candidate->x == current->x) { - current->next = candidate; - candidate->previous = current; + else if (candidate->y == y +1&& candidate->x == x) { + x = candidate->x; + y = candidate->y; + candidate->allocated = true; + line.push_back(*candidate); break; } - else if (candidate->y -1 == current->y && candidate->x == current->x) { - current->next = candidate; - candidate->previous = current; + else if (candidate->y == y -1&& candidate->x == x) { + x = candidate->x; + y = candidate->y; + candidate->allocated = true; + line.push_back(*candidate); break; } } - } - if (current->next == nullptr) + if (x == points[j].x && y == points[j].y) { - current->endpoint = true; - std::cout << "🤕";std::cout.flush(); + vector_contours.push_back(line); + std::vector line; } } //vec_private.push_back(points); - vector_contours.push_back(points); - std::cout << vector_contours.size(); + //vector_contours.push_back(points); + //std::cout << vector_contours.size(); } //#pragma omp critical @@ -187,18 +200,14 @@ void write_output_file(std::vector> all_points, const char *f feature->SetField( "Name", j ); - std::vector* points = &all_points[j]; + std::vector points = all_points[j]; - - - Point* current = &points->at(0); - int points_allocated = 0; - while (true) + for (int k = 0; k < points.size(); k++) { //int x_raw = i%width; //int y_raw = i/height; - int x_raw = current->x; - int y_raw = current->y; + int x_raw = points[k].x; + int y_raw = points[k].y; /* if (x_raw > 500 || y_raw > 400) { @@ -211,21 +220,10 @@ void write_output_file(std::vector> all_points, const char *f //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 ); - current->allocated = true; - current = current->previous; + geometry->setPoint(geometry->getNumPoints(),x_raw ,y_raw ); + //current sometimes becomes random pointer????? - if (current == nullptr or current->allocated) - { - for (int k = 0; ksize(); k++) - { - if (points->at(k).endpoint and !points->at(k).allocated) - { - current = &points->at(k); - } - } - if (current == nullptr){std::cout << "🫠";std::cout.flush(); break; } - } + } if ( feature->SetGeometry(geometry) != OGRERR_NONE) From 95649f005baef3303f793d8c368d5c36b4155014 Mon Sep 17 00:00:00 2001 From: Trygve Date: Mon, 6 May 2024 23:50:11 +0200 Subject: [PATCH 5/5] ITS WORKING (somewhat) --- src/main.cpp | 52 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 1f7b809..9aa9fae 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -47,24 +47,22 @@ std::vector> vector_cellmap(HeightMap* heightmap, int interva std::vector> vector_contours; omp_set_num_threads(12); - //#pragma omp parallel + #pragma omp parallel { std::vector> vec_private; - //#pragma omp for + #pragma omp for for (int i = 1; i <= num_contours; i++) { auto points = produce_cellmap(heightmap, heightmap->min + interval*i); std::vector line; int x; int y; + int points_allocated = 0; + x = points[0].x; + y = points[0].y; + points[0].allocated = true; + line.push_back(points[0]); for (int j = 0; j < points.size(); j++){ - Point* current = &points[j]; - current->allocated=true; - line.push_back(points[j]); - x = points[j].x; - y = points[j].y; - - //std::cout << points_allocated << " " << points.size() << "\n"; for (int k = 0; k < points.size(); k++){ Point* candidate = &points[k]; if (!candidate->allocated) { @@ -73,35 +71,51 @@ std::vector> vector_cellmap(HeightMap* heightmap, int interva x = candidate->x; y = candidate->y; line.push_back(*candidate); + points_allocated++; break; } - else if (candidate->x == x -1&& candidate->y == y) { + else if (candidate->x == x -1 && candidate->y == y) { x = candidate->x; y = candidate->y; candidate->allocated = true; line.push_back(*candidate); + points_allocated++; break; } - else if (candidate->y == y +1&& candidate->x == x) { + else if (candidate->x == x && candidate->y == y + 1) { x = candidate->x; y = candidate->y; candidate->allocated = true; line.push_back(*candidate); + points_allocated++; break; } - else if (candidate->y == y -1&& candidate->x == x) { + else if (candidate->x == x && candidate->y == y - 1) { x = candidate->x; y = candidate->y; candidate->allocated = true; line.push_back(*candidate); + points_allocated++; break; } } } - if (x == points[j].x && y == points[j].y) + if (j > points_allocated) { - vector_contours.push_back(line); - std::vector line; + vec_private.push_back(line); + line.clear(); + //std::cout << points.size() << " "; + for (int k = 0; k < points.size(); k++){ + Point* candidate = &points[k]; + if (!candidate->allocated) + { + line.push_back(*candidate); + x = candidate->x; + y = candidate->y; + points_allocated++; + break; + } + } } } @@ -109,9 +123,9 @@ std::vector> vector_cellmap(HeightMap* heightmap, int interva //vector_contours.push_back(points); //std::cout << vector_contours.size(); } - //#pragma omp critical + #pragma omp critical - //vector_contours.insert(vector_contours.end(), vec_private.begin(), vec_private.end()); + vector_contours.insert(vector_contours.end(), vec_private.begin(), vec_private.end()); } return vector_contours; @@ -172,7 +186,7 @@ void write_output_file(std::vector> all_points, const char *f OGRLayer *poLayer; - poLayer = poDS->CreateLayer( "contours", /*&(cellmaps[0]).reference_system*/ NULL, wkbLineString, NULL ); + poLayer = poDS->CreateLayer( "contours", &heightmap->reference_system, wkbLineString, NULL ); if( poLayer == NULL ) { printf( "Layer creation failed.\n" ); @@ -220,7 +234,7 @@ void write_output_file(std::vector> all_points, const char *f //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_raw ,y_raw ); + geometry->setPoint(geometry->getNumPoints(),x ,y ); //current sometimes becomes random pointer?????