2024-03-29 16:12:42 +00:00
|
|
|
#include <array>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <gdal/gdal.h>
|
|
|
|
#include "gdal/gdal_priv.h"
|
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include <iterator>
|
|
|
|
|
|
|
|
#include <stdfloat>
|
|
|
|
#include <thread>
|
|
|
|
#include <vector>
|
2024-04-02 09:50:33 +00:00
|
|
|
|
2024-03-29 16:12:42 +00:00
|
|
|
class HeightMap
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
float* data;
|
|
|
|
int x_size;
|
|
|
|
int y_size;
|
|
|
|
|
2024-04-02 09:50:33 +00:00
|
|
|
HeightMap(const char* filepath);
|
|
|
|
float get_pixel(int x,int y);
|
2024-03-29 16:12:42 +00:00
|
|
|
};
|
|
|
|
|
2024-04-02 09:50:33 +00:00
|
|
|
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();
|
|
|
|
|
2024-03-29 16:12:42 +00:00
|
|
|
this->data = (float *) CPLMalloc(sizeof(GDT_Float32)*x_size*y_size);
|
2024-04-02 09:50:33 +00:00
|
|
|
band->RasterIO( GF_Read, 0, 0, x_size, y_size,
|
|
|
|
this->data, x_size, y_size, GDT_Float32,
|
|
|
|
0, 0 );
|
2024-03-29 16:12:42 +00:00
|
|
|
}
|
2024-04-02 09:50:33 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2024-03-29 16:12:42 +00:00
|
|
|
int main(int argc, const char* argv[])
|
2024-04-02 09:50:33 +00:00
|
|
|
{
|
|
|
|
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<<" "<<i<<"\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
int oldmain(int argc, const char* argv[])
|
2024-03-29 16:12:42 +00:00
|
|
|
{
|
|
|
|
if (argc != 2) {
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
const char* pszFilename = argv[1];
|
|
|
|
|
|
|
|
GDALDatasetUniquePtr poDataset;
|
|
|
|
GDALAllRegister();
|
|
|
|
const GDALAccess eAccess = GA_ReadOnly;
|
|
|
|
poDataset = GDALDatasetUniquePtr(GDALDataset::FromHandle(GDALOpen( pszFilename, eAccess )));
|
|
|
|
if( !poDataset )
|
|
|
|
{
|
|
|
|
std::cout << "Error :/"; // handle error
|
|
|
|
}
|
|
|
|
std::cout << poDataset->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 )
|
2024-04-02 09:50:33 +00:00
|
|
|
{poDataset
|
2024-03-29 16:12:42 +00:00
|
|
|
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<<" "<<i<<"\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::cout << "🤡"<<*buffer;
|
|
|
|
}
|
2024-04-02 09:50:33 +00:00
|
|
|
*/
|