Put all classes in one header and rename case to cell

This commit is contained in:
Trygve 2024-04-22 16:10:02 +02:00
parent 040e011a66
commit 8084db97a5
7 changed files with 84 additions and 82 deletions

View File

@ -5,7 +5,7 @@ project(
find_package(GDAL CONFIG REQUIRED) find_package(GDAL CONFIG REQUIRED)
add_executable(${PROJECT_NAME} add_executable(${PROJECT_NAME}
src/HeightMap.cpp src/CaseMap.cpp src/main.cpp src/HeightMap.cpp src/CellMap.cpp src/main.cpp
) )
find_package(OpenMP) find_package(OpenMP)
if(OpenMP_CXX_FOUND) if(OpenMP_CXX_FOUND)

View File

@ -1,17 +0,0 @@
#include <gdal/ogr_spatialref.h>
#include <ogr_spatialref.h>
#include <cstdint>
/**
@brief stores the cases from marching squars for one elevation level
*/
class CaseMap
{
public:
uint8_t* cases; //!< pointer to the first case in the array. uint8_t is a 8 bit unsigned integer
int width; //!< width of image in cases
int height; //!< height of image in cases
OGRSpatialReference reference_system;
CaseMap(int width, int height, uint8_t* cases, OGRSpatialReference reference_system);
int get_case(int x,int y);
};

View File

@ -1,19 +1,20 @@
#include <cstdint> #include <cstdint>
#include <gdal/ogr_spatialref.h> #include <gdal/ogr_spatialref.h>
#include <ogr_spatialref.h> #include <ogr_spatialref.h>
#include "CaseMap.hh" #include "contour_creator.hh"
CaseMap::CaseMap(int width, int height, uint8_t* cases, OGRSpatialReference reference_system)
CellMap::CellMap(int width, int height, uint8_t* cells, OGRSpatialReference reference_system)
{ {
this->width = width; this->width = width;
this->height = height; this->height = height;
this->cases = cases; this->cells = cells;
this->reference_system = reference_system; this->reference_system = reference_system;
} }
int CaseMap::get_case(int x, int y) int CellMap::get_cell(int x, int y)
{ {
// all the cases are in an array of ints from left to right, top to bottom // all the cells are in an array of ints from left to right, top to bottom
int offset = ((this->width * y) + x); int offset = ((this->width * y) + x);
return *(this->cases + offset); return *(this->cells + offset);
} }

View File

@ -5,7 +5,8 @@
#include "gdal/gdal_priv.h" #include "gdal/gdal_priv.h"
#include <gdal/gdal_frmts.h> #include <gdal/gdal_frmts.h>
#include "HeightMap.hh" #include "contour_creator.hh"
HeightMap::HeightMap(const char* filepath) HeightMap::HeightMap(const char* filepath)
{ {

View File

@ -1,17 +0,0 @@
#include <gdal/ogr_spatialref.h>
/**
@brief stores the contents of a tif file with float32 values
*/
class HeightMap
{
public:
float* data;
int width; //!< width of image in pixels
int height; //!< height of image in pixels
float min; //!< Minimum value in image
float max; //!< Maximum value in image
OGRSpatialReference reference_system;
HeightMap(const char* filepath);
float get_pixel(int x,int y);
};

35
src/contour_creator.hh Normal file
View File

@ -0,0 +1,35 @@
#include <gdal/ogr_spatialref.h>
#include <gdal/ogr_spatialref.h>
#include <ogr_spatialref.h>
/**
@brief stores the contents of a tif file with float32 values
*/
class HeightMap
{
public:
float* data;
int width; //!< width of image in pixels
int height; //!< height of image in pixels
float min; //!< Minimum value in image
float max; //!< Maximum value in image
OGRSpatialReference reference_system;
HeightMap(const char* filepath);
float get_pixel(int x,int y);
};
/**
@brief stores the cells from marching squars for one elevation level
*/
class CellMap
{
public:
uint8_t* cells; //!< pointer to the first cell in the array. uint8_t is a 8 bit unsigned integer
int width; //!< width of image in cells
int height; //!< height of image in cells
OGRSpatialReference reference_system;
CellMap(int width, int height, uint8_t* cells, OGRSpatialReference reference_system);
int get_cell(int x,int y);
};

View File

@ -1,5 +1,4 @@
#include "HeightMap.hh" #include "contour_creator.hh"
#include "CaseMap.hh"
#include <gdal/ogr_api.h> #include <gdal/ogr_api.h>
#include "ogrsf_frmts.h" #include "ogrsf_frmts.h"
#include <iostream> #include <iostream>
@ -11,10 +10,10 @@
#include <gdal/gdal_frmts.h> #include <gdal/gdal_frmts.h>
#include <omp.h> #include <omp.h>
CaseMap produce_casemap(HeightMap* heightmap, float z) CellMap produce_cellmap(HeightMap* heightmap, float z)
{ {
int length = (heightmap->width-1)*(heightmap->height-1); int length = (heightmap->width-1)*(heightmap->height-1);
uint8_t *cases = (uint8_t *) CPLMalloc(sizeof(uint8_t)*length); uint8_t *cells = (uint8_t *) CPLMalloc(sizeof(uint8_t)*length);
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);
@ -22,37 +21,37 @@ CaseMap produce_casemap(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;
*(cases + i) = result; *(cells + i) = result;
} }
return CaseMap(heightmap->width-1, heightmap->height-1, cases, heightmap->reference_system); return CellMap(heightmap->width-1, heightmap->height-1, cells, heightmap->reference_system);
} }
std::vector<CaseMap> vector_casemap(HeightMap* heightmap, int interval) std::vector<CellMap> vector_cellmap(HeightMap* heightmap, int interval)
{ {
int num_contours = (heightmap->max - heightmap->min)/interval; int num_contours = (heightmap->max - heightmap->min)/interval;
std::vector<CaseMap> vector_contours; std::vector<CellMap> vector_contours;
omp_set_num_threads(12); omp_set_num_threads(12);
#pragma omp parallel #pragma omp parallel
{
std::vector<CaseMap> vec_private;
#pragma omp for
for (int i = 1; i <= num_contours; i++)
{ {
vec_private.push_back(produce_casemap(heightmap, heightmap->min + interval*i)); std::vector<CellMap> vec_private;
std::cout << "Execute thread " << omp_get_num_threads() << " "; #pragma omp for
for (int i = 1; i <= num_contours; i++)
{
vec_private.push_back(produce_cellmap(heightmap, heightmap->min + interval*i));
std::cout << "Execute thread " << omp_get_num_threads() << " ";
}
#pragma omp critical
vector_contours.insert(vector_contours.end(), vec_private.begin(), vec_private.end());
} }
#pragma omp critical return vector_contours;
vector_contours.insert(vector_contours.end(), vec_private.begin(), vec_private.end());
}
return vector_contours;
} }
void write_output_file(std::vector<CaseMap> casemaps, const char *filepath) void write_output_file(std::vector<CellMap> cellmaps, const char *filepath)
{ {
const char *pszDriverName = "ESRI Shapefile"; const char *pszDriverName = "ESRI Shapefile";
@ -78,7 +77,7 @@ void write_output_file(std::vector<CaseMap> casemaps, const char *filepath)
OGRLayer *poLayer; OGRLayer *poLayer;
poLayer = poDS->CreateLayer( "contours", &(casemaps[0]).reference_system, wkbPoint, NULL ); poLayer = poDS->CreateLayer( "contours", &(cellmaps[0]).reference_system, wkbPoint, NULL );
if( poLayer == NULL ) if( poLayer == NULL )
{ {
printf( "Layer creation failed.\n" ); printf( "Layer creation failed.\n" );
@ -94,25 +93,25 @@ void write_output_file(std::vector<CaseMap> casemaps, const char *filepath)
printf( "Creating Name field failed.\n" ); printf( "Creating Name field failed.\n" );
exit( 1 ); exit( 1 );
} }
for (int j = 0; j < casemaps.size(); j++) for (int j = 0; j < cellmaps.size(); j++)
{ {
CaseMap* casemap = &casemaps[j]; CellMap* cellmap = &cellmaps[j];
for (int i = 0; i<casemap->height*casemap->width; i++) { for (int i = 0; i<cellmap->height*cellmap->width; i++) {
if (*(casemap->cases + i) != 0 && *(casemap->cases + i) != 15) if (*(cellmap->cells + i) != 0 && *(cellmap->cells + i) != 15)
{ {
int x_int = i%casemap->width; int x_int = i%cellmap->width;
int y_int = casemap->height*casemap->width - i/casemap->width; int y_int = cellmap->height*cellmap->width - i/cellmap->width;
double x = double(x_int); double x = double(x_int);
double y = double(y_int); double y = double(y_int);
OGRFeature *poFeature; OGRFeature *poFeature;
poFeature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() ); poFeature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() );
poFeature->SetField( "Name", *(casemap->cases + i) ); poFeature->SetField( "Name", *(cellmap->cells + i) );
//OGRSpatialReference local; //OGRSpatialReference local;
//auto poCT = OGRCreateCoordinateTransformation( &local, &casemap->reference_system ); //auto poCT = OGRCreateCoordinateTransformation( &local, &cellmap->reference_system );
/* /*
if( poCT == NULL || !poCT->Transform( 1, &x, &y ) ) if( poCT == NULL || !poCT->Transform( 1, &x, &y ) )
@ -138,7 +137,7 @@ void write_output_file(std::vector<CaseMap> casemaps, const char *filepath)
GDALClose( poDS ); GDALClose( poDS );
/* /*
OGRDataSourceH datasource = OGR_Dr_CreateDataSource(OGRGetDriverByName("GeoJSON"), "contour.geojson", new char*); OGRDataSourceH datasource = OGR_Dr_CreateDataSource(OGRGetDriverByName("GeoJSON"), "contour.geojson", new char*);
OGRSpatialReference reference_system = casemap->reference_system; OGRSpatialReference reference_system = cellmap->reference_system;
OGRLayerH layer = OGR_DS_CreateLayer(datasource, "contour", &reference_system, wkbLineString25D, new char*); OGRLayerH layer = OGR_DS_CreateLayer(datasource, "contour", &reference_system, wkbLineString25D, new char*);
auto feature = OGR_F_Create(OGR_L_GetLayerDefn(static_cast<OGRLayerH>(layer))); auto feature = OGR_F_Create(OGR_L_GetLayerDefn(static_cast<OGRLayerH>(layer)));
@ -155,20 +154,20 @@ 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";
auto casemap = produce_casemap(&map, 40); auto cellmap = produce_cellmap(&map, 40);
/* /*
for (int y = 0; y < casemap.height; y++) for (int y = 0; y < cellmap.height; y++)
{ {
for (int x = 0; x < casemap.width; x++) for (int x = 0; x < cellmap.width; x++)
{ {
if (casemap.get_case(x, y) && casemap.get_case(x, y)!=15) { if (cellmap.get_cell(x, y) && cellmap.get_cell(x, y)!=15) {
std::cout << x << ","<< y << "=" << casemap.get_case(x, y) << " "; std::cout << x << ","<< y << "=" << cellmap.get_cell(x, y) << " ";
} }
} }
//std::cout << "\n"; //std::cout << "\n";
} }
*/ */
auto casemaps = vector_casemap(&map, 5); auto cellmaps = vector_cellmap(&map, 5);
write_output_file(casemaps, "out.shp"); write_output_file(cellmaps, "out.shp");
} }