6 Commits

Author SHA1 Message Date
e07ab1445a Seperate line creation so it can be paralized 2024-05-02 22:51:08 +02:00
esther
77467594bd improved vector_cellmap function 2024-05-02 21:02:48 +02:00
0188b9e29d Merge branch 'main' into HEAD 2024-04-30 15:12:48 +02:00
b800ceaf98 Changed file wrriting to use list of points 2024-04-30 15:11:44 +02:00
1dcfb0e737 Changed file wrriting to use list of points 2024-04-30 15:09:18 +02:00
esther
228945a767 tried to implement finding contour lines 2024-04-30 15:07:04 +02:00
2 changed files with 137 additions and 62 deletions

View File

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

View File

@@ -5,6 +5,7 @@
#include <gdal/ogr_feature.h>
#include <gdal/ogr_geometry.h>
#include <iostream>
#include <iterator>
#include <ostream>
#include <vector>
#include <cstdint>
@@ -15,9 +16,9 @@
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);
std::vector<Point> points;
std::vector<Point> points; // Initiating a vector of points
for (int i = 0; i<length; i++) {
int y = i/(heightmap->width-1);
int x = i%(heightmap->width-1);
@@ -35,28 +36,6 @@ std::vector<Point> produce_cellmap(HeightMap* heightmap, float z)
}
std::vector<std::vector<Point>> vector_cellmap(HeightMap* heightmap, int interval)
{
int num_contours = (heightmap->max - heightmap->min)/interval;
std::vector<std::vector<Point>> vector_contours;
omp_set_num_threads(12);
#pragma omp parallel
{
std::vector<std::vector<Point>> vec_private;
#pragma omp for
for (int i = 1; i <= num_contours; i++)
{
vec_private.push_back(produce_cellmap(heightmap, heightmap->min + interval*i));
}
#pragma omp critical
vector_contours.insert(vector_contours.end(), vec_private.begin(), vec_private.end());
}
return vector_contours;
}
std::tuple<double, double> local_to_projected(double* geotransform, int x, int y)
{
return {
@@ -66,6 +45,116 @@ std::tuple<double, double> local_to_projected(double* geotransform, int x, int y
geotransform[4] * 0.5 + geotransform[5] * 0.5 + geotransform[3]
};
}
std::vector<OGRFeature*> create_contours(OGRFeatureDefn* layer_def, std::vector<Point> points, HeightMap* heightmap)
{
std::vector<OGRFeature*> lines;
for (int i = 0; i<points.size();i++)
{
OGRFeature *feature;
OGRLineString *geometry = new OGRLineString();
feature = OGRFeature::CreateFeature(layer_def);
feature->SetField( "Name", i );
Point* current = &points[0];
int points_delegated = 0;
while (points_delegated < points.size())
{
//int x_raw = i%width;
//int y_raw = i/height;
int x_raw = current->x;
int y_raw = current->y;
auto [x, y] = local_to_projected(heightmap->geotransform, x_raw, y_raw);
//std::cout << x << ", " << y << " ";
//auto [x_bl, y_bl, x_tr, y_tr] = marching_squares_lookup(*(cellmap->cells + i) );
//std::cout << 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 );
geometry->setPoint(geometry->getNumPoints(),x ,y );
points_delegated++;
current = current->next;
if (current == nullptr)
{
for (int k = 0; k < points.size(); k++)
{
if (points[k].previous == nullptr)
{
current = &points[k];
}
}
if (current == nullptr)
{
break;
}
}
}
if ( feature->SetGeometry(geometry) != OGRERR_NONE)
{
printf( "Failed to set geometry.\n" );
exit( 1 );
}
OGRGeometryFactory::destroyGeometry(geometry);
lines.push_back(feature);
}
}
std::vector<OGRFeature*> vector_cellmap(OGRFeatureDefn* layer_def, HeightMap* heightmap, int interval)
{
int num_contours = (heightmap->max - heightmap->min)/interval;
std::vector<OGRFeature*> vector_contours;
omp_set_num_threads(12);
#pragma omp parallel
{
std::vector<OGRFeature*> lines;
#pragma omp for
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];
//std::cout << points_allocated << " " << points.size() << "\n";
for (int k = 0; k < points.size(); k++){
Point* candidate = &points[k];
if (candidate->previous == nullptr) {
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;
}
}
}
}
auto lines = create_lines(layer_def, points, heightmap);
}
#pragma omp critical
vector_contours.insert(vector_contours.end(), lines.begin(), lines.end());
}
return vector_contours;
}
constexpr std::tuple<double, double, double, double> marching_squares_lookup(int mcase)
{
@@ -86,7 +175,9 @@ constexpr std::tuple<double, double, double, double> marching_squares_lookup(int
case 14: return {0, 0.5, 0.5, 0};
};
}
void write_output_file(std::vector<CellMap> cellmaps, const char *filepath)
void write_output_file(const char *filepath, HeightMap* heightmap)
{
const char *pszDriverName = "GeoJSON";
@@ -112,7 +203,7 @@ void write_output_file(std::vector<CellMap> cellmaps, const char *filepath)
OGRLayer *poLayer;
poLayer = poDS->CreateLayer( "contours", &(cellmaps[0]).reference_system, wkbLineString, NULL );
poLayer = poDS->CreateLayer( "contours", /*&(cellmaps[0]).reference_system*/ NULL, wkbLineString, NULL );
if( poLayer == NULL )
{
printf( "Layer creation failed.\n" );
@@ -128,42 +219,23 @@ void write_output_file(std::vector<CellMap> cellmaps, const char *filepath)
printf( "Creating Name field failed.\n" );
exit( 1 );
}
for (int j = 0; j < cellmaps.size(); j++)
{
CellMap* cellmap = &cellmaps[j];
int width = heightmap->width -1;
int height = heightmap->height -1;
int size = width * height;
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+x_tr*0.25, y+y_tr*0.25 );
geometry->setPoint( geometry->getNumPoints(), x+x_bl*0.25, y+y_bl*0.25 );
// gets the lines from the heightmap. Runs the actual algorithm
auto lines = create_contours(poLayer->GetLayerDefn(), heightmap, 5);
if ( feature->SetGeometry(geometry) != OGRERR_NONE)
for (int j = 0; j < lines.size(); j++)
{
printf( "Failed to set geometry.\n" );
exit( 1 );
}
OGRGeometryFactory::destroyGeometry(geometry);
if( poLayer->CreateFeature( feature ) != OGRERR_NONE )
if( poLayer->CreateFeature( lines[j] ) != OGRERR_NONE )
{
printf( "Failed to create feature in shapefile.\n" );
exit( 1 );
}
OGRFeature::DestroyFeature( feature );
}
}
OGRFeature::DestroyFeature( lines[j] );
}
@@ -183,6 +255,6 @@ int main(int argc, const char* argv[])
auto cellmap = produce_cellmap(&map, 40);
auto cellmaps = vector_cellmap(&map, 5);
//write_output_file(cellmaps, "out.geojson");
write_output_file("out.geojson", &map);
}