Skip to content

Commit

Permalink
Show how to save a multidimensional array.
Browse files Browse the repository at this point in the history
  • Loading branch information
s-trinh committed Nov 30, 2023
1 parent 9fec911 commit 3b1282c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ int main(int argc, char **argv)
int nb_data = *arr_nb_data.data<int>();
std::cout << "Number of images: " << nb_data << std::endl;

// Load all the poses
visp::cnpy::NpyArray arr_vec_poses = npz_data["vec_poses"];
double* vec_poses_ptr = arr_vec_poses.data<double>();

for (int iter = 0; iter < nb_data; iter++) {
std::string img_data_str = toString("png_image_%06d", iter);
visp::cnpy::NpyArray arr_nb_data = npz_data[img_data_str];
Expand Down Expand Up @@ -81,10 +85,13 @@ int main(int argc, char **argv)
}
}

const std::string vec_pose_str = toString("vec_pose_%06d", iter);
visp::cnpy::NpyArray arr_vec_pose = npz_data[vec_pose_str];
vpHomogeneousMatrix cMo(vpTranslationVector(arr_vec_pose.data<double>()[3], arr_vec_pose.data<double>()[4], arr_vec_pose.data<double>()[5]),
vpThetaUVector(arr_vec_pose.data<double>()[0], arr_vec_pose.data<double>()[1], arr_vec_pose.data<double>()[2])
// const std::string vec_pose_str = toString("vec_pose_%06d", iter);
// visp::cnpy::NpyArray arr_vec_pose = npz_data[vec_pose_str];
// vpHomogeneousMatrix cMo(vpTranslationVector(arr_vec_pose.data<double>()[3], arr_vec_pose.data<double>()[4], arr_vec_pose.data<double>()[5]),
// vpThetaUVector(arr_vec_pose.data<double>()[0], arr_vec_pose.data<double>()[1], arr_vec_pose.data<double>()[2])
// );
vpHomogeneousMatrix cMo(vpTranslationVector(vec_poses_ptr[6*iter + 3], vec_poses_ptr[6*iter + 4], vec_poses_ptr[6*iter + 5]),
vpThetaUVector(vec_poses_ptr[6*iter], vec_poses_ptr[6*iter + 1], vec_poses_ptr[6*iter + 2])
);
std::cout << "\ncMo:\n" << cMo << std::endl;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ int main(int argc, char **argv)
<< camera_name.c_str()[i] << " ; " << int(camera_name.c_str()[i]) << std::endl;
}

visp::cnpy::npz_save(npz_filename, "camera_name", &vec_camera_name[0], { vec_camera_name.size() }, "a");
visp::cnpy::npz_save(npz_filename, "height", &height, { 1 }, "a");
visp::cnpy::npz_save(npz_filename, "camera_name", &vec_camera_name[0], { vec_camera_name.size() }, "w"); // overwrite
visp::cnpy::npz_save(npz_filename, "height", &height, { 1 }, "a"); // append
visp::cnpy::npz_save(npz_filename, "width", &width, { 1 }, "a");
visp::cnpy::npz_save(npz_filename, "channel", &channel, { 1 }, "a");

Expand All @@ -86,6 +86,9 @@ int main(int argc, char **argv)
visp::cnpy::npz_save(npz_filename, "cam_u0", &cam_u0, { 1 }, "a");
visp::cnpy::npz_save(npz_filename, "cam_v0", &cam_v0, { 1 }, "a");

std::vector<double> vec_poses;
vec_poses.reserve(g.getLastFrameIndex() * 6);

int iter = 0;
while (!g.end()) {
g.acquire(I);
Expand All @@ -98,7 +101,9 @@ int main(int argc, char **argv)
visp::cnpy::npz_save(npz_filename, toString("png_image_%06d", iter), &img_buffer[0], { (unsigned int) last_pos }, "a");

std::vector<double> vec_pose = poseToVec(cMo);
visp::cnpy::npz_save(npz_filename, toString("vec_pose_%06d", iter), &vec_pose[0], { vec_pose.size() }, "a");
vec_poses.insert(vec_poses.end(), vec_pose.begin(), vec_pose.end());
// Commented, use instead a "multidimensional" array
// visp::cnpy::npz_save(npz_filename, toString("vec_pose_%06d", iter), &vec_pose[0], { vec_pose.size() }, "a");

std::map<std::string, std::vector<std::vector<double> > > mapOfModels;
std::map<std::string, unsigned int> mapOfW;
Expand Down Expand Up @@ -130,6 +135,9 @@ int main(int argc, char **argv)
iter++;
}

// Show how to save a "multidimensional" array
visp::cnpy::npz_save(npz_filename, "vec_poses", &vec_poses[0], { static_cast<size_t>(iter), 6 }, "a");

visp::cnpy::npz_save(npz_filename, "nb_data", &iter, { 1 }, "a");

return EXIT_SUCCESS;
Expand Down

0 comments on commit 3b1282c

Please sign in to comment.