From 6f001d23f8a2bac1b671501152f85df889f4e46f Mon Sep 17 00:00:00 2001 From: Trygve Date: Sat, 6 Apr 2024 13:18:28 +0200 Subject: [PATCH] Moved HeightMap into h and cpp files --- CMakeLists.txt | 4 +- HeightMap.cpp | 39 +++++++++++++++ HeightMap.hh | 19 ++++++++ main.cpp | 17 +++++++ read_tiff.cpp | 130 ------------------------------------------------- 5 files changed, 77 insertions(+), 132 deletions(-) create mode 100644 HeightMap.cpp create mode 100644 HeightMap.hh create mode 100644 main.cpp delete mode 100644 read_tiff.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f5bba7a..04cbee1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,9 +3,9 @@ cmake_minimum_required(VERSION 3.0) find_package(GDAL CONFIG REQUIRED) -set(EXAMPLE_NAME read_tiff) +set(EXAMPLE_NAME marching_squares) add_executable(${EXAMPLE_NAME} - read_tiff.cpp + main.cpp HeightMap.cpp ) diff --git a/HeightMap.cpp b/HeightMap.cpp new file mode 100644 index 0000000..7de83d5 --- /dev/null +++ b/HeightMap.cpp @@ -0,0 +1,39 @@ +#include +#include "gdal/gdal_priv.h" +#include + +#include + +#include "HeightMap.hh" + +HeightMap::HeightMap(const char* filepath) +{ + // Open the file with some gdal nonsense: + const GDALAccess eAccess = GA_ReadOnly; + GDALDatasetUniquePtr file; + GDALAllRegister(); + file = GDALDatasetUniquePtr(GDALDataset::FromHandle(GDALOpen( filepath, eAccess ))); + if( !file ) + { + std::cout << "Could not open tiff file!"; // handle error + } + + // The heigthmap only has one band + auto band = file->GetBands()[0]; + // write the attrributes + this->x_size = band->GetXSize(); + this->y_size = band->GetYSize(); + + this->data = (float *) CPLMalloc(sizeof(float)*x_size*y_size); + band->RasterIO( GF_Read, 0, 0, x_size, y_size, + this->data, x_size, y_size, GDT_Float32, + 0, 0 ); + + band->FlushCache(); +} +float HeightMap::get_pixel(int x, int y) +{ + // all the pixels are in an array of floats from left to right, top to bottom + int offset = ((this->x_size * y) + x); + return *(this->data + offset); +} \ No newline at end of file diff --git a/HeightMap.hh b/HeightMap.hh new file mode 100644 index 0000000..d2cef09 --- /dev/null +++ b/HeightMap.hh @@ -0,0 +1,19 @@ +#include +#include "gdal/gdal_priv.h" +#include + +#include + +/** + @brief stores the contents of a tif file with float32 values + */ +class HeightMap +{ + public: + float* data; + int x_size; //!< x dimension in pixels + int y_size; //!< y dimension in pixels + + HeightMap(const char* filepath); + float get_pixel(int x,int y); +}; \ No newline at end of file diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..1c04dbb --- /dev/null +++ b/main.cpp @@ -0,0 +1,17 @@ +#include "HeightMap.hh" + +int main(int argc, const char* argv[]) +{ + const char* filepath = argv[1]; + HeightMap map(filepath); + std::cout << "x: " << map.x_size << " y: " << map.y_size << "\n"; + for (int y = 0; y < map.y_size; y++) + { + for (int x = 0; x < map.x_size; x++) + { + std::cout << map.get_pixel(x, y) << " "; + } + std::cout << "\n"; + } + +} \ No newline at end of file diff --git a/read_tiff.cpp b/read_tiff.cpp deleted file mode 100644 index 9869544..0000000 --- a/read_tiff.cpp +++ /dev/null @@ -1,130 +0,0 @@ -#include -#include -#include -#include "gdal/gdal_priv.h" - -#include -#include -#include - -#include -#include -#include - -class HeightMap -{ - public: - float* data; - int x_size; - int y_size; - - HeightMap(const char* filepath); - float get_pixel(int x,int y); -}; - -HeightMap::HeightMap(const char* filepath) -{ - // Open the file with some gdal nonsense: - const GDALAccess eAccess = GA_ReadOnly; - GDALDatasetUniquePtr file; - GDALAllRegister(); - file = GDALDatasetUniquePtr(GDALDataset::FromHandle(GDALOpen( filepath, eAccess ))); - if( !file ) - { - std::cout << "Could not open tiff file!"; // handle error - } - - // The heigthmap only has one band - auto band = file->GetBands()[0]; - // write the attrributes - this->x_size = band->GetXSize(); - this->y_size = band->GetYSize(); - - this->data = (float *) CPLMalloc(sizeof(GDT_Float32)*x_size*y_size); - band->RasterIO( GF_Read, 0, 0, x_size, y_size, - this->data, x_size, y_size, GDT_Float32, - 0, 0 ); -} -float HeightMap::get_pixel(int x, int y) -{ - // all the pixels are in an array of floats from left to right, top to bottom - int offset = (this->x_size * y + x) * sizeof(GDT_Float32); - return *(this->data + offset); -} - -int main(int argc, const char* argv[]) -{ - const char* filepath = argv[1]; - HeightMap map(filepath); - std::cout << map.get_pixel(0, 0); - /* - for (int i = 0; i < map.x_size*map.y_size; i++) - - { - map.data+=sizeof(GDT_Float32); - if (*map.data) - { - std::cout << *map.data<<" "<GetDriverName(); - - - double adfGeoTransform[6]; - printf( "Driver: %s/%s\n", - poDataset->GetDriver()->GetDescription(), - poDataset->GetDriver()->GetMetadataItem( GDAL_DMD_LONGNAME ) ); - printf( "Size is %dx%dx%d\n", - poDataset->GetRasterXSize(), poDataset->GetRasterYSize(), - poDataset->GetRasterCount() ); - if( poDataset->GetProjectionRef() != NULL ) - printf( "Projection is `%s'\n", poDataset->GetProjectionRef() ); - if( poDataset->GetGeoTransform( adfGeoTransform ) == CE_None ) - {poDataset - printf( "Origin = (%.6f,%.6f)\n", - adfGeoTransform[0], adfGeoTransform[3] ); - printf( "Pixel Size = (%.6f,%.6f)\n", - adfGeoTransform[1], adfGeoTransform[5] ); - } - - float *buffer; - auto poBand = poDataset->GetBands()[0]; - - int x_size = poBand->GetXSize(); - int y_size = poBand->GetYSize(); - - - buffer = (float *) CPLMalloc(sizeof(GDT_Float32)*x_size*y_size); - poBand->RasterIO( GF_Read, 0, 0, x_size, y_size, - buffer, x_size, y_size, GDT_Float32, - 0, 0 ); - - for (int i = 0; i < x_size*y_size; i++) - { - buffer+=sizeof(GDT_Float32); - if (*buffer) - { - std::cout << *buffer<<" "<