14#include "numeric/roundtrip_string.h"
15#include "boost/algorithm/string.hpp"
22 using tabled = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
24 inline tabled read_table(
const std::string& filename,
const char* delimiters =
" \t,") {
25 std::ifstream ifs(filename);
27 throw std::runtime_error(
"Failed to open file '" + filename +
"'.");
30 std::vector<double> buffer;
33 auto n_cols = index_t{0};
36 while(std::getline(ifs, line)) {
39 if (boost::starts_with(line,
"#")) {
43 std::vector<std::string> row;
44 boost::split(row, line, boost::is_any_of(delimiters));
46 auto row_size =
static_cast<index_t
>(row.size());
50 }
else if(row_size != n_cols) {
51 std::cerr <<
"Skipping line " << line_no <<
" with a different number of columns." << std::endl;
55 for(
const auto& cell : row) {
56 buffer.push_back(numeric::to_double(boost::trim_copy(cell)));
61 throw std::runtime_error(
"File '" + filename +
"' is empty.");
64 auto n_rows =
static_cast<index_t
>(buffer.size()) / n_cols;
65 return tabled::Map(buffer.data(), n_rows, n_cols);
68 template<
class Derived>
69 void write_table(
const std::string& filename,
const Eigen::MatrixBase<Derived>& table,
char delimiter =
' ') {
70 std::ofstream ofs(filename);
72 throw std::runtime_error(
"Failed to open file '" + filename +
"'.");
75 auto n_cols = table.cols();
76 for(
auto row : table.rowwise()) {
77 for(index_t i = 0; i < n_cols; i ++) {
78 ofs << numeric::to_string(row(i));
本系统的主命名空间,包含了common, examples, fmm, geometry, numeric, point_cloud等子命名空间