14 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
esther
d046e18c43 changed to create a vector 2024-04-29 15:02:50 +02:00
esther
33dd409c43 changed contour creator 2024-04-29 14:56:06 +02:00
esther
5cd5627bb7 introduced class Point and changed cell map to create lines 2024-04-29 14:54:44 +02:00
29a7ddb083 Cleanup 2024-04-28 22:31:05 +02:00
98a312094e Added box blur 2024-04-28 22:30:37 +02:00
882764a13d Created a lookup table 2024-04-27 18:01:43 +02:00
f962a40ade Fixed segfault 2024-04-27 17:15:37 +02:00
ee0b87c2b3 Start to implement line drawing 2024-04-26 15:40:09 +02:00
3 changed files with 218 additions and 79 deletions

View File

@@ -1,7 +1,7 @@
#include <gdal/cpl_conv.h> #include <gdal/cpl_conv.h>
#include <iostream> #include <iostream>
#include <stdexcept> #include <stdexcept>
#include <omp.h>
#include <gdal/gdal.h> #include <gdal/gdal.h>
#include "gdal/gdal_priv.h" #include "gdal/gdal_priv.h"
#include <gdal/gdal_frmts.h> #include <gdal/gdal_frmts.h>
@@ -48,4 +48,46 @@ float HeightMap::get_pixel(int x, int y)
int offset = ((this->width * y) + x); int offset = ((this->width * y) + x);
//std::cout << " offset: " << offset << " " << x << ","<< y; //std::cout << " offset: " << offset << " " << x << ","<< y;
return *(this->data + offset); return *(this->data + offset);
}
void HeightMap::blur(float standard_deviation)
{
// standard_deviation does not do anything yet. This is currently a simple box blur
int kernel_height = 5;
int kernel_width = 5;
int kernel_size = kernel_height*kernel_width;
float* kernel = (float*) CPLMalloc(sizeof(float)*kernel_size);
for (int i=0; i<kernel_size; i++)
{
*(kernel + i) = 1.0;
}
float* blurred = (float *) CPLMalloc(sizeof(float)*this->width*this->height);
#pragma omp parallel
{
#pragma omp for
for (int i = 0; i < this->height * this->width; i++)
{
float blurred_pixel = 0;
for (int j = 0; j < kernel_size; j++)
{
int x = j%kernel_width - kernel_width/2 + i%this->width;
int y = j/kernel_width - kernel_height/2 + i/this->width;
if (x<0 || x>=this->width)
{
x = 0;
}
if (y<0 || y>=this->height)
{
y = 0;
}
blurred_pixel += this->get_pixel(x, y);
}
*(blurred + i) = blurred_pixel/float(kernel_size);
}
}
free(this->data);
this->data = blurred;
blurred = nullptr;
} }

View File

@@ -19,6 +19,7 @@ class HeightMap
HeightMap(const char* filepath); HeightMap(const char* filepath);
float get_pixel(int x,int y); float get_pixel(int x,int y);
void blur(float standard_deviation);
}; };
/** /**
@@ -34,4 +35,21 @@ class CellMap
OGRSpatialReference reference_system; OGRSpatialReference reference_system;
CellMap(int width, int height, uint8_t* cells, OGRSpatialReference reference_system, double* geotransform); CellMap(int width, int height, uint8_t* cells, OGRSpatialReference reference_system, double* geotransform);
int get_cell(int x,int y); int get_cell(int x,int y);
}; };
class Point
{
public:
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

@@ -1,7 +1,11 @@
#include "contour_creator.hh" #include "contour_creator.hh"
#include <gdal/ogr_api.h> #include <gdal/ogr_api.h>
#include "ogrsf_frmts.h" #include "ogrsf_frmts.h"
#include <gdal/ogr_core.h>
#include <gdal/ogr_feature.h>
#include <gdal/ogr_geometry.h>
#include <iostream> #include <iostream>
#include <iterator>
#include <ostream> #include <ostream>
#include <vector> #include <vector>
#include <cstdint> #include <cstdint>
@@ -10,10 +14,11 @@
#include <gdal/gdal_frmts.h> #include <gdal/gdal_frmts.h>
#include <omp.h> #include <omp.h>
CellMap 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; // 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);
@@ -21,49 +26,161 @@ CellMap produce_cellmap(HeightMap* heightmap, float z)
(heightmap->get_pixel(x+1,y)>z)*2 + (heightmap->get_pixel(x+1,y)>z)*2 +
(heightmap->get_pixel(x+1,y+1)>z)*4 + (heightmap->get_pixel(x+1,y+1)>z)*4 +
(heightmap->get_pixel(x,y+1)>z)*8; (heightmap->get_pixel(x,y+1)>z)*8;
*(cells + i) = result; if (result != 0 and result != 15 ) {
points.push_back(Point(x, y, result));
};
} }
return CellMap(heightmap->width-1, heightmap->height-1, cells, heightmap->reference_system, heightmap->geotransform); return points;
} }
std::tuple<double, double> local_to_projected(double* geotransform, int x, int y)
{
return {
geotransform[1] * x + geotransform[2] * y +
geotransform[1] * 0.5 + geotransform[2] * 0.5 + geotransform[0],
geotransform[4] * x + geotransform[5] * 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;
}
std::vector<CellMap> vector_cellmap(HeightMap* heightmap, int interval) }
}
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; int num_contours = (heightmap->max - heightmap->min)/interval;
std::vector<CellMap> vector_contours; std::vector<OGRFeature*> vector_contours;
omp_set_num_threads(12); omp_set_num_threads(12);
#pragma omp parallel #pragma omp parallel
{ {
std::vector<CellMap> vec_private; std::vector<OGRFeature*> lines;
#pragma omp for #pragma omp for
for (int i = 1; i <= num_contours; i++) for (int i = 1; i <= num_contours; i++)
{ {
vec_private.push_back(produce_cellmap(heightmap, heightmap->min + interval*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 #pragma omp critical
vector_contours.insert(vector_contours.end(), vec_private.begin(), vec_private.end());
vector_contours.insert(vector_contours.end(), lines.begin(), lines.end());
} }
return vector_contours; return vector_contours;
} }
std::tuple<double, double> local_to_projected(double* geotransform, int x, int y)
constexpr std::tuple<double, double, double, double> marching_squares_lookup(int mcase)
{ {
return { switch (mcase) {
geotransform[1] * x + geotransform[2] * y + case 1: return {0, 0.5, 0.5, 0};
geotransform[1] * 0.5 + geotransform[2] * 0.5 + geotransform[0], case 2: return {1, 0.5, 0.5, 0};
geotransform[4] * x + geotransform[5] * y + case 3: return {0, 0.5, 1, 0};
geotransform[4] * 0.5 + geotransform[5] * 0.5 + geotransform[3] case 4: return {0.5, 1, 1, 0.5};
}; case 5: return {0, 0, 0, 0}; //FIXME
case 6: return {0.5, 1, 0.5, 0};
case 7: return {0, 0.5, 0.5, 1};
case 8: return {0, 0.5, 0.5, 1};
case 9: return {0.5, 1, 0.5, 0};
case 10: return {0, 0, 0, 0}; //FIXME
case 11: return {0.5, 1, 1, 0.5};
case 12: return {0, 0.5, 1, 0.5};
case 13: return {0.5, 0, 1, 0.5};
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 = "ESRI Shapefile"; const char *pszDriverName = "GeoJSON";
GDALDriver *poDriver; GDALDriver *poDriver;
GDALAllRegister(); GDALAllRegister();
@@ -86,7 +203,7 @@ void write_output_file(std::vector<CellMap> cellmaps, const char *filepath)
OGRLayer *poLayer; OGRLayer *poLayer;
poLayer = poDS->CreateLayer( "contours", &(cellmaps[0]).reference_system, wkbPoint, NULL ); poLayer = poDS->CreateLayer( "contours", /*&(cellmaps[0]).reference_system*/ NULL, wkbLineString, NULL );
if( poLayer == NULL ) if( poLayer == NULL )
{ {
printf( "Layer creation failed.\n" ); printf( "Layer creation failed.\n" );
@@ -102,56 +219,28 @@ void write_output_file(std::vector<CellMap> cellmaps, const char *filepath)
printf( "Creating Name field failed.\n" ); printf( "Creating Name field failed.\n" );
exit( 1 ); exit( 1 );
} }
for (int j = 0; j < cellmaps.size(); j++) int width = heightmap->width -1;
int height = heightmap->height -1;
int size = width * height;
// gets the lines from the heightmap. Runs the actual algorithm
auto lines = create_contours(poLayer->GetLayerDefn(), heightmap, 5);
for (int j = 0; j < lines.size(); j++)
{ {
CellMap* cellmap = &cellmaps[j];
for (int i = 0; i<cellmap->height*cellmap->width; i++) { if( poLayer->CreateFeature( lines[j] ) != OGRERR_NONE )
if (*(cellmap->cells + i) != 0 && *(cellmap->cells + i) != 15)
{ {
OGRFeature *poFeature; printf( "Failed to create feature in shapefile.\n" );
poFeature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() ); exit( 1 );
poFeature->SetField( "Name", *(cellmap->cells + i) );
//OGRSpatialReference local;
//auto poCT = OGRCreateCoordinateTransformation( &local, &cellmap->reference_system );
/*
if( poCT == NULL || !poCT->Transform( 1, &x, &y ) )
printf( "Transformation failed.\n" );
*/
OGRPoint pt;
int x_raw = i%cellmap->width;
int y_raw = i/cellmap->width;
auto [x, y] = local_to_projected(cellmap->geotransform, x_raw, y_raw);
pt.setX( x );
pt.setY( y );
poFeature->SetGeometry( &pt );
if( poLayer->CreateFeature( poFeature ) != OGRERR_NONE )
{
printf( "Failed to create feature in shapefile.\n" );
exit( 1 );
}
OGRFeature::DestroyFeature( poFeature );
} }
} OGRFeature::DestroyFeature( lines[j] );
} }
GDALClose( poDS ); GDALClose( poDS );
/*
OGRDataSourceH datasource = OGR_Dr_CreateDataSource(OGRGetDriverByName("GeoJSON"), "contour.geojson", new char*);
OGRSpatialReference reference_system = cellmap->reference_system;
OGRLayerH layer = OGR_DS_CreateLayer(datasource, "contour", &reference_system, wkbLineString25D, new char*);
auto feature = OGR_F_Create(OGR_L_GetLayerDefn(static_cast<OGRLayerH>(layer)));
OGRGeometryH geometry = OGR_G_CreateGeometry(wkbLineString25D);
*/
} }
int main(int argc, const char* argv[]) int main(int argc, const char* argv[])
@@ -161,21 +250,11 @@ 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";
map.blur(0.8);
auto cellmap = produce_cellmap(&map, 40); auto cellmap = produce_cellmap(&map, 40);
/*
for (int y = 0; y < cellmap.height; y++)
{
for (int x = 0; x < cellmap.width; x++)
{
if (cellmap.get_cell(x, y) && cellmap.get_cell(x, y)!=15) {
std::cout << x << ","<< y << "=" << cellmap.get_cell(x, y) << " ";
}
}
//std::cout << "\n";
}
*/
auto cellmaps = vector_cellmap(&map, 5);
write_output_file(cellmaps, "out.shp"); write_output_file("out.geojson", &map);
} }