-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathutils.cpp
More file actions
167 lines (150 loc) · 5.27 KB
/
Copy pathutils.cpp
File metadata and controls
167 lines (150 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//
// Created by feixh on 10/25/17.
//
// unix
#include "dirent.h"
// stl
#include <iostream>
// I/O
#include "igl/readOBJ.h"
#include "igl/readPLY.h"
#include "json/json.h"
#include "vlslam.pb.h"
#include "utils.h"
namespace feh {
const std::string TermColor::red = "\033[91m";
const std::string TermColor::green = "\033[92m";
const std::string TermColor::blue = "\033[94m";
const std::string TermColor::cyan = "\033[96m";
const std::string TermColor::yellow = "\033[93m";
const std::string TermColor::magenta = "\033[95m";
const std::string TermColor::gray = "\033[90m";
const std::string TermColor::white = "\033[97m";
const std::string TermColor::bold = "\033[1m";
const std::string TermColor::end = "\033[0m";
const std::string TermColor::endl = "\033[0m\n";
std::ostream& operator<<(std::ostream& os, const Timer& obj)
{
os << "....." << std::endl;
// write obj to stream
for ( auto it = obj.look_up_table_.begin(); it != obj.look_up_table_.end(); ++it ) {
float elapsed = obj.report_average ? it->second / obj.counter_.at(it->first) : it->second;
os << "[" << TermColor::bold+TermColor::green
<< obj.module_name_
<< TermColor::end << "]"
<< TermColor::cyan
<< it->first
<< TermColor::end
<< ":" << elapsed*1e-6 << " ms" << std::endl;
}
os << "....." << std::endl;
return os;
}
bool Glob(const std::string &directory,
const std::string &extension,
std::vector<std::string> &filenames) {
std::string suffix( (extension[0] == '.' ? "":".") + extension );
std::string path(directory + "/");
DIR *dir_ptr = opendir(path.c_str());
struct dirent *entry;
if (dir_ptr) {
while ((entry = readdir(dir_ptr)) != NULL) {
std::string entry_name(entry->d_name);
if (entry_name.length() > suffix.length()
&& (entry_name.substr(entry_name.length() - suffix.length()).compare(suffix) == 0)) {
filenames.push_back(entry_name.substr(0, entry_name.length() - suffix.length()));
}
}
try {
std::sort(filenames.begin(),
filenames.end(),
[](const std::string &a, const std::string &b) { return std::stof(a) < std::stof(b); });
} catch (const std::invalid_argument &e) {
std::sort(filenames.begin(),
filenames.end());
}
for (auto &filename: filenames) filename = path + filename + suffix;
closedir(dir_ptr);
return true;
} else {
return false;
}
}
bool Glob(const std::string &directory,
const std::string &extension,
const std::string &prefix,
std::vector<std::string> &filenames) {
std::string suffix( (extension[0] == '.' ? "":".") + extension );
std::string path(directory + "/");
DIR *dir_ptr = opendir(path.c_str());
struct dirent *entry;
if (dir_ptr) {
while ((entry = readdir(dir_ptr)) != NULL) {
std::string entry_name(entry->d_name);
if (entry_name.length() > suffix.length() + prefix.length()
&& (entry_name.substr(entry_name.length() - suffix.length()).compare(suffix) == 0)
&& (entry_name.substr(0, prefix.length()).compare(prefix) == 0)) {
filenames.push_back(entry_name.substr(prefix.length(),
entry_name.length() - suffix.length() - prefix.length()));
}
}
try {
std::sort(filenames.begin(),
filenames.end(),
[](const std::string &a, const std::string &b) { return std::stof(a) < std::stof(b); });
} catch (const std::invalid_argument &e) {
std::sort(filenames.begin(),
filenames.end());
}
for (auto &filename: filenames) filename = path + prefix + filename + suffix;
closedir(dir_ptr);
return true;
} else {
return false;
}
}
std::tuple<MatXf, MatXi> LoadMesh(const std::string &file) {
MatXf V;
MatXi F;
if (!LoadMesh(file, V, F)) {
throw MeshIO();
} else {
return std::make_tuple(V, F);
}
}
bool LoadMesh(const std::string &file, MatXf &V, MatXi &F) {
bool success = false;
if (file.find(".obj") != std::string::npos) {
success = igl::readOBJ(file, V, F);
} else if (file.find(".ply") != std::string::npos) {
success = igl::readPLY(file, V, F);
}
V = V.leftCols(3);
F = F.leftCols(3);
return success;
}
void MergeJson(Json::Value &a, const Json::Value &b) {
if (!a.isObject() || !b.isObject()) return;
for (const auto &key : b.getMemberNames())
if (a[key].isObject())
MergeJson(a[key], b[key]);
else
a[key] = b[key];
}
Json::Value LoadJson(const std::string &filename) {
std::ifstream in(filename, std::ios::in);
if (in.is_open()) {
Json::Value out;
in >> out;
return out;
} else {
throw std::runtime_error(absl::StrFormat("failed to read file %s", filename));
}
}
void SaveJson(const Json::Value &j, const std::string &filename) {
std::ofstream ofs(filename, std::ios::out);
assert(ofs.is_open());
ofs << j;
ofs.close();
}
} // namespace feh