From cf72fbfefa634b422311ce44cbb2327da936e4a8 Mon Sep 17 00:00:00 2001 From: esther Date: Wed, 17 Apr 2024 11:34:54 +0200 Subject: [PATCH] Attempted parallelization of casemaps --- src/main.cpp | 314 +++++++++++++++++++++++++++------------------------ 1 file changed, 169 insertions(+), 145 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 5d33e73..0356f89 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,146 +1,170 @@ -#include "HeightMap.hh" -#include "CaseMap.hh" -#include -#include "ogrsf_frmts.h" -#include -#include -#include -#include -#include -#include "gdal/gdal_priv.h" -#include - -CaseMap produce_casemap(HeightMap* heightmap, float z) -{ - int length = (heightmap->width-1)*(heightmap->height-1); - uint8_t *cases = (uint8_t *) CPLMalloc(sizeof(uint8_t)*length); - for (int i = 0; iwidth-1); - int x = i%(heightmap->width-1); - uint8_t result = (heightmap->get_pixel(x,y)>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; - } - - return CaseMap(heightmap->width-1, heightmap->height-1, cases, heightmap->reference_system); - - -} - -void write_output_file(CaseMap* casemap, const char *filepath) -{ - - const char *pszDriverName = "ESRI Shapefile"; - GDALDriver *poDriver; - - GDALAllRegister(); - - poDriver = GetGDALDriverManager()->GetDriverByName(pszDriverName ); - if( poDriver == NULL ) - { - printf( "%s driver not available.\n", pszDriverName ); - exit( 1 ); - } - - GDALDataset *poDS; - - poDS = poDriver->Create( filepath, 0, 0, 0, GDT_Unknown, NULL ); - if( poDS == NULL ) - { - printf( "Creation of output file failed.\n" ); - exit( 1 ); - } - - OGRLayer *poLayer; - - poLayer = poDS->CreateLayer( "contours", &casemap->reference_system, wkbPoint, NULL ); - if( poLayer == NULL ) - { - printf( "Layer creation failed.\n" ); - exit( 1 ); - } - - OGRFieldDefn oField( "Name", OFTString ); - - oField.SetWidth(32); - - if( poLayer->CreateField( &oField ) != OGRERR_NONE ) - { - printf( "Creating Name field failed.\n" ); - exit( 1 ); - } - - for (int i = 0; iheight*casemap->width; i++) { - if (*(casemap->cases + i) != 0 && *(casemap->cases + i) != 15) - { - int x_int = i%casemap->width; - int y_int = casemap->height*casemap->width - i/casemap->width; - double x = double(x_int); - double y = double(y_int); - - OGRFeature *poFeature; - - poFeature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() ); - poFeature->SetField( "Name", *(casemap->cases + i) ); - - //OGRSpatialReference local; - - //auto poCT = OGRCreateCoordinateTransformation( &local, &casemap->reference_system ); - - /* - if( poCT == NULL || !poCT->Transform( 1, &x, &y ) ) - printf( "Transformation failed.\n" ); - */ - OGRPoint pt; - 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 ); - } - } - - GDALClose( poDS ); - /* - OGRDataSourceH datasource = OGR_Dr_CreateDataSource(OGRGetDriverByName("GeoJSON"), "contour.geojson", new char*); - OGRSpatialReference reference_system = casemap->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))); - - OGRGeometryH geometry = OGR_G_CreateGeometry(wkbLineString25D); - */ -} - -int main(int argc, const char* argv[]) -{ - const char* filepath = argv[1]; - HeightMap map(filepath); - - std::cout << "x: " << map.width << " y: " << map.height << "\n"; - std::cout << "max: " << map.max << " min: " << map.min << "\n"; - - auto casemap = produce_casemap(&map, 40); - /* - for (int y = 0; y < casemap.height; y++) - { - for (int x = 0; x < casemap.width; x++) - { - if (casemap.get_case(x, y) && casemap.get_case(x, y)!=15) { - std::cout << x << ","<< y << "=" << casemap.get_case(x, y) << " "; - } - } - //std::cout << "\n"; - } - */ - write_output_file(&casemap, "out.shp"); +#include "HeightMap.hh" +#include "CaseMap.hh" +#include +#include "ogrsf_frmts.h" +#include +#include +#include +#include +#include +#include "gdal/gdal_priv.h" +#include +#include + +CaseMap produce_casemap(HeightMap* heightmap, float z) +{ + int length = (heightmap->width-1)*(heightmap->height-1); + uint8_t *cases = (uint8_t *) CPLMalloc(sizeof(uint8_t)*length); + for (int i = 0; iwidth-1); + int x = i%(heightmap->width-1); + uint8_t result = (heightmap->get_pixel(x,y)>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; + } + + return CaseMap(heightmap->width-1, heightmap->height-1, cases, heightmap->reference_system); + + +} + +std::vector vector_casemap(HeightMap* heightmap, int interval) +{ + int num_contours = (heightmap->max - heightmap->min)/interval; + std::vector vector_contours; +omp_set_num_threads(8); + +#pragma omp parallel +{ + std::vector 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::cout << "Execute thread " << omp_get_num_threads << " "; + } + #pragma omp critical + vector_contours.insert(vector_contours.end(), vec_private.begin(), vec_private.end()); + +} + return vector_contours; +} + +void write_output_file(CaseMap* casemap, const char *filepath) +{ + + const char *pszDriverName = "ESRI Shapefile"; + GDALDriver *poDriver; + + GDALAllRegister(); + + poDriver = GetGDALDriverManager()->GetDriverByName(pszDriverName ); + if( poDriver == NULL ) + { + printf( "%s driver not available.\n", pszDriverName ); + exit( 1 ); + } + + GDALDataset *poDS; + + poDS = poDriver->Create( filepath, 0, 0, 0, GDT_Unknown, NULL ); + if( poDS == NULL ) + { + printf( "Creation of output file failed.\n" ); + exit( 1 ); + } + + OGRLayer *poLayer; + + poLayer = poDS->CreateLayer( "contours", &casemap->reference_system, wkbPoint, NULL ); + if( poLayer == NULL ) + { + printf( "Layer creation failed.\n" ); + exit( 1 ); + } + + OGRFieldDefn oField( "Name", OFTString ); + + oField.SetWidth(32); + + if( poLayer->CreateField( &oField ) != OGRERR_NONE ) + { + printf( "Creating Name field failed.\n" ); + exit( 1 ); + } + + for (int i = 0; iheight*casemap->width; i++) { + if (*(casemap->cases + i) != 0 && *(casemap->cases + i) != 15) + { + int x_int = i%casemap->width; + int y_int = casemap->height*casemap->width - i/casemap->width; + double x = double(x_int); + double y = double(y_int); + + OGRFeature *poFeature; + + poFeature = OGRFeature::CreateFeature( poLayer->GetLayerDefn() ); + poFeature->SetField( "Name", *(casemap->cases + i) ); + + //OGRSpatialReference local; + + //auto poCT = OGRCreateCoordinateTransformation( &local, &casemap->reference_system ); + + /* + if( poCT == NULL || !poCT->Transform( 1, &x, &y ) ) + printf( "Transformation failed.\n" ); + */ + OGRPoint pt; + 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 ); + } + } + + GDALClose( poDS ); + /* + OGRDataSourceH datasource = OGR_Dr_CreateDataSource(OGRGetDriverByName("GeoJSON"), "contour.geojson", new char*); + OGRSpatialReference reference_system = casemap->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))); + + OGRGeometryH geometry = OGR_G_CreateGeometry(wkbLineString25D); + */ +} + +int main(int argc, const char* argv[]) +{ + const char* filepath = argv[1]; + HeightMap map(filepath); + + std::cout << "x: " << map.width << " y: " << map.height << "\n"; + std::cout << "max: " << map.max << " min: " << map.min << "\n"; + + auto casemap = produce_casemap(&map, 40); + /* + for (int y = 0; y < casemap.height; y++) + { + for (int x = 0; x < casemap.width; x++) + { + if (casemap.get_case(x, y) && casemap.get_case(x, y)!=15) { + std::cout << x << ","<< y << "=" << casemap.get_case(x, y) << " "; + } + } + //std::cout << "\n"; + } + */ + write_output_file(&casemap, "out.shp"); + vector_casemap(&map, 1); } \ No newline at end of file