mirror of
https://gitlab.com/Trygve/contour-creator.git
synced 2026-03-14 16:04:04 +00:00
Moved code into src
This commit is contained in:
39
src/HeightMap.cpp
Normal file
39
src/HeightMap.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#include <gdal/gdal.h>
|
||||
#include "gdal/gdal_priv.h"
|
||||
#include <iostream>
|
||||
|
||||
#include <stdfloat>
|
||||
|
||||
#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);
|
||||
}
|
||||
19
src/HeightMap.hh
Normal file
19
src/HeightMap.hh
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <gdal/gdal.h>
|
||||
#include "gdal/gdal_priv.h"
|
||||
#include <iostream>
|
||||
|
||||
#include <stdfloat>
|
||||
|
||||
/**
|
||||
@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);
|
||||
};
|
||||
17
src/main.cpp
Normal file
17
src/main.cpp
Normal file
@@ -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";
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user