diff --git a/CMakeLists.txt b/CMakeLists.txt index 07f31aa..4367b9c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,7 +5,7 @@ project( find_package(GDAL CONFIG REQUIRED) 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) if(OpenMP_CXX_FOUND) diff --git a/src/CaseMap.hh b/src/CaseMap.hh deleted file mode 100644 index e0a5a59..0000000 --- a/src/CaseMap.hh +++ /dev/null @@ -1,17 +0,0 @@ -#include -#include -#include -/** - @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); -}; \ No newline at end of file diff --git a/src/CaseMap.cpp b/src/CellMap.cpp similarity index 51% rename from src/CaseMap.cpp rename to src/CellMap.cpp index cc23b86..0284b7c 100644 --- a/src/CaseMap.cpp +++ b/src/CellMap.cpp @@ -1,19 +1,20 @@ #include #include #include -#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->height = height; - this->cases = cases; + this->cells = cells; 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); - return *(this->cases + offset); + return *(this->cells + offset); } \ No newline at end of file diff --git a/src/HeightMap.cpp b/src/HeightMap.cpp index d2455e1..c8d7567 100644 --- a/src/HeightMap.cpp +++ b/src/HeightMap.cpp @@ -5,7 +5,8 @@ #include "gdal/gdal_priv.h" #include -#include "HeightMap.hh" +#include "contour_creator.hh" + HeightMap::HeightMap(const char* filepath) { @@ -42,4 +43,4 @@ float HeightMap::get_pixel(int x, int y) int offset = ((this->width * y) + x); //std::cout << " offset: " << offset << " " << x << ","<< y; return *(this->data + offset); -} +} \ No newline at end of file diff --git a/src/HeightMap.hh b/src/HeightMap.hh deleted file mode 100644 index 94d2112..0000000 --- a/src/HeightMap.hh +++ /dev/null @@ -1,17 +0,0 @@ -#include -/** - @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); -}; \ No newline at end of file diff --git a/src/contour_creator.hh b/src/contour_creator.hh new file mode 100644 index 0000000..f9ca8b7 --- /dev/null +++ b/src/contour_creator.hh @@ -0,0 +1,35 @@ +#include +#include +#include + +/** + @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); +}; \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index c09b23a..cf980c1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,4 @@ -#include "HeightMap.hh" -#include "CaseMap.hh" +#include "contour_creator.hh" #include #include "ogrsf_frmts.h" #include @@ -11,10 +10,10 @@ #include #include -CaseMap produce_casemap(HeightMap* heightmap, float z) +CellMap produce_cellmap(HeightMap* heightmap, float z) { 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; iwidth-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+1)>z)*4 + (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 vector_casemap(HeightMap* heightmap, int interval) +std::vector vector_cellmap(HeightMap* heightmap, int interval) { int num_contours = (heightmap->max - heightmap->min)/interval; - std::vector vector_contours; -omp_set_num_threads(12); + std::vector vector_contours; + omp_set_num_threads(12); -#pragma omp parallel -{ - std::vector vec_private; - #pragma omp for - for (int i = 1; i <= num_contours; i++) + #pragma omp parallel { - vec_private.push_back(produce_casemap(heightmap, heightmap->min + interval*i)); - std::cout << "Execute thread " << omp_get_num_threads() << " "; + std::vector vec_private; + #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 - vector_contours.insert(vector_contours.end(), vec_private.begin(), vec_private.end()); - -} - return vector_contours; + return vector_contours; } -void write_output_file(std::vector casemaps, const char *filepath) +void write_output_file(std::vector cellmaps, const char *filepath) { const char *pszDriverName = "ESRI Shapefile"; @@ -78,7 +77,7 @@ void write_output_file(std::vector casemaps, const char *filepath) 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 ) { printf( "Layer creation failed.\n" ); @@ -94,25 +93,25 @@ void write_output_file(std::vector casemaps, const char *filepath) printf( "Creating Name field failed.\n" ); exit( 1 ); } - for (int j = 0; j < casemaps.size(); j++) + for (int j = 0; j < cellmaps.size(); j++) { - CaseMap* casemap = &casemaps[j]; - for (int i = 0; iheight*casemap->width; i++) { - if (*(casemap->cases + i) != 0 && *(casemap->cases + i) != 15) + CellMap* cellmap = &cellmaps[j]; + for (int i = 0; iheight*cellmap->width; i++) { + if (*(cellmap->cells + i) != 0 && *(cellmap->cells + i) != 15) { - int x_int = i%casemap->width; - int y_int = casemap->height*casemap->width - i/casemap->width; + int x_int = i%cellmap->width; + int y_int = cellmap->height*cellmap->width - i/cellmap->width; double x = double(x_int); double y = double(y_int); OGRFeature *poFeature; poFeature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() ); - poFeature->SetField( "Name", *(casemap->cases + i) ); + poFeature->SetField( "Name", *(cellmap->cells + i) ); //OGRSpatialReference local; - //auto poCT = OGRCreateCoordinateTransformation( &local, &casemap->reference_system ); + //auto poCT = OGRCreateCoordinateTransformation( &local, &cellmap->reference_system ); /* if( poCT == NULL || !poCT->Transform( 1, &x, &y ) ) @@ -138,7 +137,7 @@ void write_output_file(std::vector casemaps, const char *filepath) GDALClose( poDS ); /* 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*); auto feature = OGR_F_Create(OGR_L_GetLayerDefn(static_cast(layer))); @@ -155,20 +154,20 @@ int main(int argc, const char* argv[]) std::cout << "x: " << map.width << " y: " << map.height << "\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) { - std::cout << x << ","<< y << "=" << casemap.get_case(x, y) << " "; + 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 casemaps = vector_casemap(&map, 5); - write_output_file(casemaps, "out.shp"); + auto cellmaps = vector_cellmap(&map, 5); + write_output_file(cellmaps, "out.shp"); } \ No newline at end of file