Skip to content

Commit

Permalink
Add read array from numpy file and write array to numpy file
Browse files Browse the repository at this point in the history
  • Loading branch information
otto-link committed Aug 16, 2024
1 parent d3a589c commit b0bf663
Show file tree
Hide file tree
Showing 10 changed files with 113 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,6 @@
[submodule "external/Noise"]
path = external/Noise
url = git@github.com:otto-link/Noise.git
[submodule "external/libnpy"]
path = external/libnpy
url = git@github.com:llohse/libnpy.git
1 change: 1 addition & 0 deletions HighMap/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ target_link_libraries(
FastNoiseLite::FastNoiseLite
hmm::hmm
libInterpolate::libInterpolate
libnpy::libnpy
macro-logger::macro-logger
NoiseLib
OpenMP::OpenMP_CXX
Expand Down
20 changes: 20 additions & 0 deletions HighMap/include/highmap/array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,16 @@ class Array
*/
void from_file(std::string fname);

/**
* @brief Import array data from a numpy binary file.
*
* @param fname The name of the file to import data from.
*
* **Example**
* @include ex_from_numpy.cpp
*/
void from_numpy(std::string fname);

/**
* @brief Display various information about the array.
*
Expand Down Expand Up @@ -865,6 +875,16 @@ class Array
*/
void to_file(std::string fname);

/**
* @brief Export the array to a numpy binary file.
*
* @param fname The name of the file to which the array data will be written.
*
* **Example**
* @include ex_to_numpy.cpp
*/
void to_numpy(std::string fname);

/**
* @brief Export the array as a PNG image file with a specified colormap and
* hillshading.
Expand Down
40 changes: 40 additions & 0 deletions HighMap/src/array/array_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <fstream>

#include "macrologger.h"
#include "npy.hpp"

#include "highmap/array.hpp"
#include "highmap/io.hpp"
Expand All @@ -22,6 +23,35 @@ void Array::from_file(std::string fname)
f.close();
}

void Array::from_numpy(std::string fname)
{
npy::npy_data d = npy::read_npy<float>(fname);

// update array shape
Vec2<int> new_shape = {(int)d.shape[0], (int)d.shape[1]};
this->set_shape(new_shape);

// copy the data
if (d.fortran_order)
{
for (int i = 0; i < this->shape.x; i++)
for (int j = 0; j < this->shape.y; j++)
{
int k = j * this->shape.x + i;
(*this)(i, j) = d.data[k];
}
}
else
{
for (int i = 0; i < this->shape.x; i++)
for (int j = 0; j < this->shape.y; j++)
{
int k = i * this->shape.y + j;
(*this)(i, j) = d.data[k];
}
}
}

void Array::infos(std::string msg) const
{
std::cout << "Array: " << msg << " ";
Expand Down Expand Up @@ -58,6 +88,16 @@ void Array::to_file(std::string fname)
f.close();
}

void Array::to_numpy(std::string fname)
{
npy::npy_data_ptr<float> d;
d.data_ptr = this->vector.data();
d.shape = {(uint)this->shape.x, (uint)this->shape.y};
d.fortran_order = false;

npy::write_npy(fname, d);
}

void Array::to_png(std::string fname, int cmap, bool hillshading)
{
std::vector<uint8_t> img(3 * this->shape.x * this->shape.y);
Expand Down
2 changes: 2 additions & 0 deletions examples/ex_from_numpy/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_executable(ex_from_numpy ex_from_numpy.cpp)
target_link_libraries(ex_from_numpy highmap)
19 changes: 19 additions & 0 deletions examples/ex_from_numpy/ex_from_numpy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <iostream>

#include "highmap.hpp"

int main(void)
{
hmap::Vec2<int> shape = {512, 256};
hmap::Vec2<float> res = {4.f, 2.f};
int seed = 1;

hmap::Array z1 = hmap::noise_fbm(hmap::NoiseType::PERLIN, shape, res, seed);

z1.to_numpy("out.npy");

hmap::Array z2;
z2.from_numpy("out.npy");

hmap::export_banner_png("ex_from_numpy.png", {z1, z2}, hmap::cmap::inferno);
}
2 changes: 2 additions & 0 deletions examples/ex_to_numpy/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
add_executable(ex_to_numpy ex_to_numpy.cpp)
target_link_libraries(ex_to_numpy highmap)
24 changes: 24 additions & 0 deletions examples/ex_to_numpy/ex_to_numpy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <iostream>

#include "highmap.hpp"

int main(void)
{
hmap::Vec2<int> shape = {512, 256};
hmap::Vec2<float> res = {4.f, 2.f};
int seed = 1;

hmap::Array z = hmap::noise_fbm(hmap::NoiseType::PERLIN, shape, res, seed);

std::cout << z(10, 12) << std::endl;

z.to_numpy("out.npy");
z.to_png("ex_to_numpy.png", hmap::cmap::jet);

// --- python script to check this

// import numpy as np
// z = np.load('out.npy')
// print(z.shape)
// print(z[10, 12])
}
1 change: 1 addition & 0 deletions external/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ include(dkm.cmake)
include(FastNoiseLite.cmake)
include(hmm.cmake)
include(libInterpolate.cmake)
include(libnpy.cmake)
include(macro-logger.cmake)
include(xsimd.cmake)

Expand Down
1 change: 1 addition & 0 deletions external/libnpy
Submodule libnpy added at 471fe4

0 comments on commit b0bf663

Please sign in to comment.