Implementerte HeightMap

This commit is contained in:
Trygve 2024-04-02 11:50:33 +02:00
parent 5778f2fd8e
commit b0e9910e93

View File

@ -10,7 +10,7 @@
#include <stdfloat> #include <stdfloat>
#include <thread> #include <thread>
#include <vector> #include <vector>
/*
class HeightMap class HeightMap
{ {
public: public:
@ -18,18 +18,59 @@ class HeightMap
int x_size; int x_size;
int y_size; int y_size;
HeightMap(int x_size, int y_size); HeightMap(const char* filepath);
float get_pixel(int x,int y);
}; };
HeightMap::HeightMap(int x_size, int y_size, char* filepath) { HeightMap::HeightMap(const char* filepath)
this->x_size = x_size; {
this->y_size = y_size; // 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); 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<<" "<<i<<"\n";
}
} }
*/ */
int main(int argc, const char* argv[]) }
/*
int oldmain(int argc, const char* argv[])
{ {
if (argc != 2) { if (argc != 2) {
return EINVAL; return EINVAL;
@ -57,7 +98,7 @@ int main(int argc, const char* argv[])
if( poDataset->GetProjectionRef() != NULL ) if( poDataset->GetProjectionRef() != NULL )
printf( "Projection is `%s'\n", poDataset->GetProjectionRef() ); printf( "Projection is `%s'\n", poDataset->GetProjectionRef() );
if( poDataset->GetGeoTransform( adfGeoTransform ) == CE_None ) if( poDataset->GetGeoTransform( adfGeoTransform ) == CE_None )
{ {poDataset
printf( "Origin = (%.6f,%.6f)\n", printf( "Origin = (%.6f,%.6f)\n",
adfGeoTransform[0], adfGeoTransform[3] ); adfGeoTransform[0], adfGeoTransform[3] );
printf( "Pixel Size = (%.6f,%.6f)\n", printf( "Pixel Size = (%.6f,%.6f)\n",
@ -86,3 +127,4 @@ int main(int argc, const char* argv[])
} }
std::cout << "🤡"<<*buffer; std::cout << "🤡"<<*buffer;
} }
*/