Compare commits

...

2 Commits

Author SHA1 Message Date
789f5aa84e Update readme 2024-04-22 16:13:06 +02:00
8084db97a5 Put all classes in one header and rename case to cell 2024-04-22 16:10:02 +02:00
8 changed files with 86 additions and 84 deletions

View File

@ -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)

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
- [ ] Run the marching squares algorithm and produce a "casemap"
- [ ] Use a lookuptable to produce a vector file from the "casemap"
- [x] Run the marching squares algorithm and produce a "cellmap"
- [ ] Use a lookuptable to produce a vector file from the "cellmap"
## Dependencies
- GDAL
- OpenMP

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 <gdal/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->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);
}

View File

@ -5,7 +5,8 @@
#include "gdal/gdal_priv.h"
#include <gdal/gdal_frmts.h>
#include "HeightMap.hh"
#include "contour_creator.hh"
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 "CaseMap.hh"
#include "contour_creator.hh"
#include <gdal/ogr_api.h>
#include "ogrsf_frmts.h"
#include <iostream>
@ -11,10 +10,10 @@
#include <gdal/gdal_frmts.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);
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++) {
int y = i/(heightmap->width-1);
int x = i%(heightmap->width-1);
@ -22,27 +21,27 @@ 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<CaseMap> vector_casemap(HeightMap* heightmap, int interval)
std::vector<CellMap> vector_cellmap(HeightMap* heightmap, int interval)
{
int num_contours = (heightmap->max - heightmap->min)/interval;
std::vector<CaseMap> vector_contours;
std::vector<CellMap> vector_contours;
omp_set_num_threads(12);
#pragma omp parallel
{
std::vector<CaseMap> vec_private;
std::vector<CellMap> vec_private;
#pragma omp for
for (int i = 1; i <= num_contours; i++)
{
vec_private.push_back(produce_casemap(heightmap, heightmap->min + interval*i));
vec_private.push_back(produce_cellmap(heightmap, heightmap->min + interval*i));
std::cout << "Execute thread " << omp_get_num_threads() << " ";
}
#pragma omp critical
@ -52,7 +51,7 @@ omp_set_num_threads(12);
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";
@ -78,7 +77,7 @@ void write_output_file(std::vector<CaseMap> 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<CaseMap> 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; i<casemap->height*casemap->width; i++) {
if (*(casemap->cases + i) != 0 && *(casemap->cases + i) != 15)
CellMap* cellmap = &cellmaps[j];
for (int i = 0; i<cellmap->height*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<CaseMap> 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<OGRLayerH>(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");
}