5#include <igl/list_to_matrix.h>
19 bool read_matrix(
const std::string &path, Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> &mat)
21 std::string extension = std::filesystem::path(path).extension().string();
22 std::transform(extension.begin(), extension.end(), extension.begin(),
23 [](
unsigned char c) { return std::tolower(c); });
25 if (extension ==
".txt")
29 else if (extension ==
".bin")
42 template <
typename Mat>
45 std::string extension = std::filesystem::path(path).extension().string();
46 std::transform(extension.begin(), extension.end(), extension.begin(),
47 [](
unsigned char c) { return std::tolower(c); });
49 if (extension ==
".txt")
53 else if (extension ==
".bin")
59 logger().warn(
"Uknown output matrix format (\"{}\"). Using ASCII format.");
64 template <
typename Mat>
65 bool write_matrix(
const std::string &path,
const std::string &key,
const Mat &mat,
const bool replace)
67 h5pp::File hdf5_file(path, replace ? h5pp::FileAccess::REPLACE : h5pp::FileAccess::READWRITE);
68 hdf5_file.writeDataset(mat, key);
73 template <
typename Mat>
74 bool read_matrix(
const std::string &path,
const std::string &key, Mat &mat)
76 h5pp::File hdf5_file(path, h5pp::FileAccess::READONLY);
77 if (!hdf5_file.linkExists(key))
79 mat = hdf5_file.readDataset<Mat>(key);
84 bool read_matrix_ascii(
const std::string &path, Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> &mat)
86 std::ifstream file(path);
90 logger().error(
"Failed to open file: {}", path);
97 std::vector<std::vector<T>> matrix;
99 while (getline(file, s))
103 std::stringstream input(s);
105 matrix.emplace_back();
107 std::vector<T> ¤tLine = matrix.back();
114 if (!(input >> temp))
116 currentLine.push_back(temp);
118 if (currentLine.empty())
124 if (!igl::list_to_matrix(matrix, mat))
126 logger().error(
"list to matrix error");
135 template <
typename Mat>
138 std::ofstream out(path);
141 logger().error(
"Failed to write to file: {}", path);
154 template <
typename T>
155 bool read_matrix_binary(
const std::string &path, Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> &mat)
157 typedef typename Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>::Index Index;
159 std::ifstream in(path, std::ios::in | std::ios::binary);
162 logger().error(
"Failed to open file: {}", path);
168 Index rows = 0, cols = 0;
169 in.read((
char *)(&rows),
sizeof(Index));
170 in.read((
char *)(&cols),
sizeof(Index));
171 if (!in || rows < 0 || cols < 0
172 || (cols && rows > std::numeric_limits<Index>::max() / cols))
174 const uintmax_t count = uintmax_t(rows) * uintmax_t(cols);
175 if (count > (uintmax_t(std::numeric_limits<std::streamsize>::max()) - 2 *
sizeof(Index)) /
sizeof(T))
177 in.seekg(0, std::ios::end);
178 if (in.tellg() != std::streamoff(2 *
sizeof(Index) + count *
sizeof(T)))
180 in.seekg(2 *
sizeof(Index));
182 mat.resize(rows, cols);
183 in.read((
char *)mat.data(), rows * cols *
sizeof(T));
184 const bool success = bool(in);
190 template <
typename Mat>
193 typedef typename Mat::Index Index;
194 typedef typename Mat::Scalar Scalar;
195 std::ofstream out(path, std::ios::out | std::ios::binary);
199 logger().error(
"Failed to write to file: {}", path);
205 const Index rows = mat.rows(), cols = mat.cols();
206 out.write((
const char *)(&rows),
sizeof(Index));
207 out.write((
const char *)(&cols),
sizeof(Index));
208 out.write((
const char *)mat.data(), rows * cols *
sizeof(Scalar));
216 std::ofstream csv(path, std::ios::out);
220 logger().error(
"Failed to write to file: {}", path);
226 csv << std::setprecision(std::numeric_limits<long double>::digits10 + 2);
228 csv << fmt::format(
"shape,{},{}\n", mat.rows(), mat.cols());
229 csv <<
"Row,Col,Val\n";
230 for (
int k = 0; k < mat.outerSize(); ++k)
232 for (Eigen::SparseMatrix<double>::InnerIterator it(mat, k); it; ++it)
234 csv << it.row() <<
","
236 << it.value() <<
"\n";
244 template <
typename T>
246 const std::string &path,
const json &
import, Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> &mat)
249 if (
import.contains(
"offset"))
251 const int offset =
import[
"offset"];
253 Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic> tmp;
257 assert(mat.rows() >= offset && mat.cols() >= 1);
258 mat.block(0, 0, offset, 1) = tmp.block(0, 0, offset, 1);
269 template bool read_matrix<int>(
const std::string &, Eigen::Matrix<int, Eigen::Dynamic, Eigen::Dynamic> &);
270 template bool read_matrix<double>(
const std::string &, Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> &);
271 template bool read_matrix<long>(
const std::string &, Eigen::Matrix<long, Eigen::Dynamic, Eigen::Dynamic> &);
template bool import_matrix< double >(const std::string &path, const json &import, Eigen::MatrixXd &mat)
template bool write_matrix< Eigen::VectorXd >(const std::string &, const Eigen::VectorXd &)
template bool read_matrix_binary< int >(const std::string &, Eigen::Matrix< int, Eigen::Dynamic, Eigen::Dynamic > &)
template bool write_matrix< Eigen::VectorXf >(const std::string &, const Eigen::VectorXf &)
bool read_matrix(const std::string &path, Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &mat)
Reads a matrix from a file. Determines the file format based on the path's extension.
template bool write_matrix_ascii< Eigen::VectorXd >(const std::string &, const Eigen::VectorXd &)
bool read_matrix_binary(const std::string &path, Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &mat)
template bool read_matrix_ascii< long >(const std::string &, Eigen::Matrix< long, Eigen::Dynamic, Eigen::Dynamic > &)
template bool import_matrix< int >(const std::string &path, const json &import, Eigen::MatrixXi &mat)
template bool read_matrix< long >(const std::string &, Eigen::Matrix< long, Eigen::Dynamic, Eigen::Dynamic > &)
template bool write_matrix_binary< Eigen::MatrixXd >(const std::string &, const Eigen::MatrixXd &)
template bool write_matrix_binary< Eigen::MatrixXf >(const std::string &, const Eigen::MatrixXf &)
bool import_matrix(const std::string &path, const json &import, Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &mat)
template bool write_matrix_ascii< Eigen::MatrixXf >(const std::string &, const Eigen::MatrixXf &)
template bool write_matrix_binary< Eigen::VectorXd >(const std::string &, const Eigen::VectorXd &)
template bool write_matrix< Eigen::MatrixXf >(const std::string &, const Eigen::MatrixXf &)
bool write_matrix_binary(const std::string &path, const Mat &mat)
bool read_matrix_ascii(const std::string &path, Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &mat)
template bool write_matrix< Eigen::MatrixXd >(const std::string &, const Eigen::MatrixXd &)
bool write_matrix_ascii(const std::string &path, const Mat &mat)
template bool read_matrix_binary< double >(const std::string &, Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > &)
template bool read_matrix_binary< long >(const std::string &, Eigen::Matrix< long, Eigen::Dynamic, Eigen::Dynamic > &)
template bool read_matrix< Eigen::MatrixXd >(const std::string &, const std::string &, Eigen::MatrixXd &)
bool write_sparse_matrix_csv(const std::string &path, const Eigen::SparseMatrix< double > &mat)
template bool read_matrix< int >(const std::string &, Eigen::Matrix< int, Eigen::Dynamic, Eigen::Dynamic > &)
template bool read_matrix< Eigen::MatrixXi >(const std::string &, const std::string &, Eigen::MatrixXi &)
template bool write_matrix_binary< Eigen::VectorXf >(const std::string &, const Eigen::VectorXf &)
template bool read_matrix< double >(const std::string &, Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > &)
template bool write_matrix_ascii< Eigen::VectorXf >(const std::string &, const Eigen::VectorXf &)
template bool read_matrix_ascii< double >(const std::string &, Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > &)
template bool read_matrix_ascii< int >(const std::string &, Eigen::Matrix< int, Eigen::Dynamic, Eigen::Dynamic > &)
template bool write_matrix_ascii< Eigen::MatrixXd >(const std::string &, const Eigen::MatrixXd &)
bool write_matrix(const std::string &path, const Mat &mat)
Writes a matrix to a file. Determines the file format based on the path's extension.
spdlog::logger & logger()
Retrieves the current logger.