ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
FeatureIO.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include "FeatureIO.h"
9 
10 #include <FileSystem.h>
11 #include <Logging.h>
12 
13 namespace cloudViewer {
14 namespace {
15 using namespace io;
16 
17 bool ReadMatrixXdFromBINFile(FILE *file, Eigen::MatrixXd &mat) {
18  uint32_t rows, cols;
19  if (fread(&rows, sizeof(uint32_t), 1, file) < 1) {
20  cloudViewer::utility::LogWarning("Read BIN failed: unexpected EOF.");
21  return false;
22  }
23  if (fread(&cols, sizeof(uint32_t), 1, file) < 1) {
24  cloudViewer::utility::LogWarning("Read BIN failed: unexpected EOF.");
25  return false;
26  }
27  mat.resize(rows, cols);
28  if (fread(mat.data(), sizeof(double), rows * cols, file) < rows * cols) {
29  cloudViewer::utility::LogWarning("Read BIN failed: unexpected EOF.");
30  return false;
31  }
32  return true;
33 }
34 
35 bool WriteMatrixXdToBINFile(FILE *file, const Eigen::MatrixXd &mat) {
36  uint32_t rows = (uint32_t)mat.rows();
37  uint32_t cols = (uint32_t)mat.cols();
38  if (fwrite(&rows, sizeof(uint32_t), 1, file) < 1) {
39  cloudViewer::utility::LogWarning("Write BIN failed: unexpected error.");
40  return false;
41  }
42  if (fwrite(&cols, sizeof(uint32_t), 1, file) < 1) {
43  cloudViewer::utility::LogWarning("Write BIN failed: unexpected error.");
44  return false;
45  }
46  if (fwrite(mat.data(), sizeof(double), rows * cols, file) < rows * cols) {
47  cloudViewer::utility::LogWarning("Write BIN failed: unexpected error.");
48  return false;
49  }
50  return true;
51 }
52 
53 } // unnamed namespace
54 
55 namespace io {
56 
57 bool ReadFeature(const std::string &filename, utility::Feature &feature) {
58  return ReadFeatureFromBIN(filename, feature);
59 }
60 
61 bool WriteFeature(const std::string &filename,
62  const utility::Feature &feature) {
63  return WriteFeatureToBIN(filename, feature);
64 }
65 
66 bool ReadFeatureFromBIN(const std::string &filename,
67  utility::Feature &feature) {
69  if (fid == NULL) {
71  "Read BIN failed: unable to open file: {}", filename);
72  return false;
73  }
74  bool success = ReadMatrixXdFromBINFile(fid, feature.data_);
75  fclose(fid);
76  return success;
77 }
78 
79 bool WriteFeatureToBIN(const std::string &filename,
80  const utility::Feature &feature) {
82  if (fid == NULL) {
84  "Write BIN failed: unable to open file: {}", filename);
85  return false;
86  }
87  bool success = WriteMatrixXdToBINFile(fid, feature.data_);
88  fclose(fid);
89  return success;
90 }
91 
92 } // namespace io
93 } // namespace cloudViewer
std::string filename
#define NULL
Class to store featrues for registration.
Definition: ecvFeature.h:29
Eigen::MatrixXd data_
Data buffer storing features.
Definition: ecvFeature.h:54
#define LogWarning(...)
Definition: Logging.h:72
bool ReadFeature(const std::string &filename, utility::Feature &feature)
Definition: FeatureIO.cpp:57
bool WriteFeatureToBIN(const std::string &filename, const utility::Feature &feature)
Definition: FeatureIO.cpp:79
bool WriteFeature(const std::string &filename, const utility::Feature &feature)
Definition: FeatureIO.cpp:61
bool ReadFeatureFromBIN(const std::string &filename, utility::Feature &feature)
Definition: FeatureIO.cpp:66
FILE * FOpen(const std::string &filename, const std::string &mode)
Definition: FileSystem.cpp:609
Generic file read and write utility for python interface.