#include <iostream>
#include <visp3/core/vpConfig.h>
#include <visp3/core/vpEndian.h>
#if defined(VISP_HAVE_CATCH2) && \
(defined(_WIN32) || (defined(__unix__) || defined(__unix) || (defined(__APPLE__) && defined(__MACH__)))) && \
defined(VISP_LITTLE_ENDIAN) && defined(VISP_HAVE_MINIZ) && defined(VISP_HAVE_WORKING_REGEX)
#include <catch_amalgamated.hpp>
#include <type_traits>
#include <complex>
#include <visp3/core/vpIoTools.h>
#include <visp3/core/vpImage.h>
#ifdef ENABLE_VISP_NAMESPACE
#endif
namespace
{
std::string createTmpDir()
{
return directory_filename;
}
}
TEST_CASE("Test visp::cnpy::npy_load/npz_save", "[visp::cnpy I/O]")
{
std::string directory_filename = createTmpDir();
std::string npz_filename = directory_filename + "/test_npz_read_write.npz";
SECTION("Read/Save string data")
{
const std::string save_string = "Open Source Visual Servoing Platform";
std::vector<char> vec_save_string(save_string.begin(), save_string.end());
const std::string identifier = "String";
visp::cnpy::npz_save(npz_filename, identifier, &vec_save_string[0], { vec_save_string.size() },
"w");
REQUIRE(npz_data.find(identifier) != npz_data.end());
std::vector<char> vec_arr_string_data = arr_string_data.
as_vec<
char>();
const std::string read_string = std::string(vec_arr_string_data.begin(), vec_arr_string_data.end());
CHECK(save_string == read_string);
}
SECTION("Read/Save multi-dimensional array")
{
const std::string identifier = "Array";
size_t height = 5, width = 7, channels = 3;
std::vector<int> save_vec_copy;
{
std::vector<int> save_vec;
save_vec.reserve(height*width*channels);
for (int i = 0; i < static_cast<int>(height*width*channels); ++i) {
save_vec.push_back(i);
}
save_vec_copy = save_vec;
}
{
REQUIRE(npz_data.find(identifier) != npz_data.end());
std::vector<int> read_vec = arr_vec_data.
as_vec<
int>();
REQUIRE(save_vec_copy.size() == read_vec.size());
for (size_t i = 0; i < read_vec.size(); ++i) {
CHECK(save_vec_copy[i] == read_vec[i]);
}
}
}
SECTION("Read/Save vpImage<vpRGBa>")
{
CHECK(
sizeof(
vpRGBa) == (4 *
sizeof(
unsigned char)));
const std::string identifier = "vpImage<vpRGBa>";
{
for (unsigned int i = 0; i < I_save.getRows(); i++) {
for (unsigned int j = 0; j < I_save.getCols(); j++) {
I_save[i][j].R = 4 * (i*I_save.getCols() + j) + 0;
I_save[i][j].G = 4 * (i*I_save.getCols() + j) + 1;
I_save[i][j].B = 4 * (i*I_save.getCols() + j) + 2;
I_save[i][j].A = 4 * (i*I_save.getCols() + j) + 3;
}
}
visp::cnpy::npz_save(npz_filename, identifier, &I_save.bitmap[0], { I_save.getRows(), I_save.getCols() },
"a");
I_save_copy = I_save;
}
{
REQUIRE(npz_data.find(identifier) != npz_data.end());
const bool copy_data = false;
static_cast<unsigned int>(arr_vec_data.
shape[1]), copy_data);
CHECK(I_save_copy.
getSize() == I_read.getSize());
CHECK(I_save_copy == I_read);
}
}
SECTION("Read/Save std::complex<double>")
{
#if (VISP_CXX_STANDARD > VISP_CXX_STANDARD_11)
CHECK(std::is_trivially_copyable<std::complex<double>>::value == true);
#endif
const std::string identifier = "std::complex<double>";
std::complex<double> complex_data_copy;
{
std::complex<double> complex_data(99, 3.14);
complex_data_copy = complex_data;
}
{
REQUIRE(npz_data.find(identifier) != npz_data.end());
std::complex<double> complex_data_read = *arr_vec_data.
data<std::complex<double>>();
CHECK(complex_data_copy.real() == complex_data_read.real());
CHECK(complex_data_copy.imag() == complex_data_read.imag());
}
}
SECTION("Read/Save std::vector<std::complex<double>>")
{
const std::string identifier = "std::vector<std::complex<double>>";
std::vector<std::complex<double>> vec_complex_data_copy;
{
std::vector<std::complex<double>> vec_complex_data;
std::complex<double> complex_data(99, 3.14);
vec_complex_data.push_back(complex_data);
complex_data.real(-77.12);
complex_data.imag(-100.95);
vec_complex_data.push_back(complex_data);
visp::cnpy::npz_save(npz_filename, identifier, &vec_complex_data[0], { vec_complex_data.size() },
"a");
vec_complex_data_copy = vec_complex_data;
}
{
REQUIRE(npz_data.find(identifier) != npz_data.end());
std::vector<std::complex<double>> vec_complex_data_read = arr_vec_data.
as_vec<std::complex<double>>();
REQUIRE(vec_complex_data_copy.size() == vec_complex_data_read.size());
for (size_t i = 0; i < vec_complex_data_copy.size(); i++) {
CHECK(vec_complex_data_copy[i].real() == vec_complex_data_read[i].real());
CHECK(vec_complex_data_copy[i].imag() == vec_complex_data_read[i].imag());
}
}
}
SECTION("Read/Save vpHomogeneousMatrix")
{
const std::string identifier = "vpHomogeneousMatrix";
{
visp::cnpy::npz_save(npz_filename, identifier, &cMo_save.data[0], { cMo_save.getRows(), cMo_save.getCols() },
"a");
cMo_save_copy = cMo_save;
}
{
REQUIRE(npz_data.find(identifier) != npz_data.end());
CHECK(cMo_save_copy == cMo_read);
}
}
SECTION("Read/Save std::vector<vpHomogeneousMatrix>")
{
const std::string identifier = "std::vector<vpHomogeneousMatrix>";
std::vector<vpHomogeneousMatrix> vec_cMo_save_copy;
{
std::vector<double> vec_cMo_save;
for (size_t i = 0; i < 5; i++) {
vec_cMo_save_copy.push_back(cMo_save);
vec_cMo_save.insert(vec_cMo_save.end(), cMo_save.data, cMo_save.data+cMo_save.size());
}
visp::cnpy::npz_save(npz_filename, identifier, &vec_cMo_save[0], { vec_cMo_save.size()/16, 16 },
"a");
}
{
REQUIRE(npz_data.find(identifier) != npz_data.end());
std::vector<double> vec_cMo_read = arr_vec_data.
as_vec<
double>();
REQUIRE(vec_cMo_save_copy.size() == arr_vec_data.
shape[0]);
for (
size_t i = 0; i < arr_vec_data.
shape[0]; i++) {
std::vector<double>::const_iterator first = vec_cMo_read.begin() + i*arr_vec_data.
shape[1];
std::vector<double>::const_iterator last = first + arr_vec_data.
shape[1];
std::vector<double> subvec_cMo_read(first, last);
CHECK(vec_cMo_save_copy[i] == cMo_read);
}
}
}
}
using BasicTypes = std::tuple<uint8_t, int8_t, uint16_t, int16_t, uint32_t, int32_t, uint64_t, int64_t, float, double>;
TEMPLATE_LIST_TEST_CASE("Test visp::cnpy::npy_load/npz_save", "[BasicTypes][list]", BasicTypes)
{
std::string directory_filename = createTmpDir();
std::string npz_filename = directory_filename + "/test_npz_read_write.npz";
const std::string identifier = "data";
TestType save_data_copy;
{
TestType save_data = std::numeric_limits<TestType>::min();
save_data_copy = save_data;
}
{
REQUIRE(npz_data.find(identifier) != npz_data.end());
TestType read_data = *arr_data.
data<TestType>();
CHECK(save_data_copy == read_data);
}
{
TestType save_data = std::numeric_limits<TestType>::max();
save_data_copy = save_data;
}
{
REQUIRE(npz_data.find(identifier) != npz_data.end());
TestType read_data = *arr_data.
data<TestType>();
CHECK(save_data_copy == read_data);
}
}
int main(int argc, char *argv[])
{
Catch::Session session;
session.applyCommandLine(argc, argv);
int numFailed = session.run();
return numFailed;
}
#else
int main() { return EXIT_SUCCESS; }
#endif
Implementation of an homogeneous matrix and operations on such kind of matrices.
unsigned int getSize() const
Implementation of a rotation vector as axis-angle minimal representation.
Class that consider the case of a translation vector.
void npz_save(std::string zipname, std::string fname, const T *data, const std::vector< size_t > &shape, std::string mode="w")
VISP_EXPORT npz_t npz_load(std::string fname)
std::map< std::string, NpyArray > npz_t
std::vector< size_t > shape
std::vector< T > as_vec() const