improved vector_cellmap function

This commit is contained in:
esther 2024-05-02 21:02:48 +02:00
parent 0188b9e29d
commit 77467594bd
2 changed files with 24 additions and 30 deletions

View File

@ -43,10 +43,13 @@ class Point
int x; int x;
int y; int y;
int mscase; int mscase;
Point * next;
Point * previous;
Point(int x, int y, int mscase){ Point(int x, int y, int mscase){
this -> x = x; this -> x = x;
this -> y = y; this -> y = y;
this -> mscase = mscase; this -> mscase = mscase;
this -> next = nullptr;
this -> previous = nullptr;
}; };
}; };

View File

@ -5,6 +5,7 @@
#include <gdal/ogr_feature.h> #include <gdal/ogr_feature.h>
#include <gdal/ogr_geometry.h> #include <gdal/ogr_geometry.h>
#include <iostream> #include <iostream>
#include <iterator>
#include <ostream> #include <ostream>
#include <vector> #include <vector>
#include <cstdint> #include <cstdint>
@ -15,9 +16,9 @@
std::vector<Point> produce_cellmap(HeightMap* heightmap, float z) std::vector<Point> produce_cellmap(HeightMap* heightmap, float z)
{ {
int length = (heightmap->width-1)*(heightmap->height-1); int length = (heightmap->width-1)*(heightmap->height-1); // Defining length of vector
uint8_t *cells = (uint8_t *) CPLMalloc(sizeof(uint8_t)*length); uint8_t *cells = (uint8_t *) CPLMalloc(sizeof(uint8_t)*length);
std::vector<Point> points; std::vector<Point> points; // Initiating a vector of points
for (int i = 0; i<length; i++) { for (int i = 0; i<length; i++) {
int y = i/(heightmap->width-1); int y = i/(heightmap->width-1);
int x = i%(heightmap->width-1); int x = i%(heightmap->width-1);
@ -51,37 +52,27 @@ std::vector<std::vector<Point>> vector_cellmap(HeightMap* heightmap, int interva
for (int i = 1; i <= num_contours; i++) for (int i = 1; i <= num_contours; i++)
{ {
auto points = produce_cellmap(heightmap, heightmap->min + interval*i); auto points = produce_cellmap(heightmap, heightmap->min + interval*i);
std::vector<Point> line;
line.push_back(points.back());
points.pop_back();
while (points.size() != 0){
Point current_point = points.back();
for (int j = 0; j < points.size(); j++){ for (int j = 0; j < points.size(); j++){
Point next = points[j]; Point current = points[j];
if (&next != &points.back()) { //std::cout << points_allocated << " " << points.size() << "\n";
if (next.x +1 == current_point.x) { for (int k = 0; k < points.size(); k++){
line.push_back(next); Point candidate = points[k];
points.erase(points.begin()+j); if (candidate.next != nullptr) {
if (candidate.x +1 == current.x) {
current.next = &candidate;
} }
else if (next.x -1 == current_point.x) { else if (candidate.x -1 == current.x) {
line.push_back(next); current.next = &candidate;
points.erase(points.begin()+j);
} }
else if (next.y +1 == current_point.y) { else if (candidate.y +1 == current.x) {
line.push_back(next); current.next = &candidate;
points.erase(points.begin()+j);
} }
else if (next.y -1 == current_point.y) { else if (candidate.y -1 == current.x) {
line.push_back(next); current.next = &candidate;
points.erase(points.begin()+j);
}
else {
vec_private.push_back(line);
std::vector<Point> line;
line.push_back(points.back());
points.pop_back();
} }
} }
} }
} }