Compare commits

..

No commits in common. "789f5aa84e724de5189705b572c1bbb1c76955a0" and "040e011a66ca7d9bb46ab96a1e1a3a554b2654b0" have entirely different histories.

8 changed files with 84 additions and 86 deletions

View File

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

View File

@ -3,8 +3,8 @@ This program takes a tiff heightmap and produces vector contours.
This is our project for the INF205: Resource-efficient programming course
## Status
- [x] Read .tif file into memory using gdal
- [x] Run the marching squares algorithm and produce a "cellmap"
- [ ] Use a lookuptable to produce a vector file from the "cellmap"
- [ ] Run the marching squares algorithm and produce a "casemap"
- [ ] Use a lookuptable to produce a vector file from the "casemap"
## Dependencies
- GDAL
- OpenMP

View File

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

17
src/CaseMap.hh Normal file
View File

@ -0,0 +1,17 @@
#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

@ -5,8 +5,7 @@
#include "gdal/gdal_priv.h"
#include <gdal/gdal_frmts.h>
#include "contour_creator.hh"
#include "HeightMap.hh"
HeightMap::HeightMap(const char* filepath)
{
@ -43,4 +42,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);
}
}

17
src/HeightMap.hh Normal file
View File

@ -0,0 +1,17 @@
#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);
};

View File

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