-
Notifications
You must be signed in to change notification settings - Fork 233
Expand file tree
/
Copy pathutilities.cpp
More file actions
135 lines (108 loc) · 3.16 KB
/
utilities.cpp
File metadata and controls
135 lines (108 loc) · 3.16 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
// Copyright 2017-2023, Nicholas Sharp and the Polyscope contributors. https://polyscope.run
#include "polyscope/utilities.h"
#include <cmath>
#include <vector>
#include "imgui.h"
#include "polyscope/messages.h"
namespace polyscope {
// Globals for random utilities
std::random_device util_random_device;
std::mt19937 util_mersenne_twister(util_random_device());
std::string guessNiceNameFromPath(std::string fullname) {
size_t startInd = 0;
for (std::string sep : {"/", "\\"}) {
size_t pos = fullname.rfind(sep);
if (pos != std::string::npos) {
startInd = std::max(startInd, pos + 1);
}
}
size_t endInd = fullname.size();
for (std::string sep : {"."}) {
size_t pos = fullname.rfind(sep);
if (pos != std::string::npos) {
endInd = std::min(endInd, pos);
}
}
if (startInd >= endInd) {
return fullname;
}
std::string niceName = fullname.substr(startInd, endInd - startInd);
return niceName;
}
void validateName(const std::string& name) {
if (name == "") exception("name must not be the empty string");
if (name.find("#") != std::string::npos) exception("name must not contain '#' characters");
}
std::tuple<std::string, std::string> splitExt(std::string f) {
auto p = f.find_last_of(".");
return std::tuple<std::string, std::string>{f.substr(0, p), f.substr(p, std::string::npos)};
}
void splitTransform(const glm::mat4& trans, glm::mat3x4& R, glm::vec3& T) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
R[i][j] = trans[i][j];
}
T[i] = trans[3][i];
}
}
glm::mat4 buildTransform(const glm::mat3x4& R, const glm::vec3& T) {
glm::mat4 trans(1.0);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
trans[i][j] = R[i][j];
}
trans[3][i] = T[i];
}
return trans;
}
std::string prettyPrintCount(size_t count) {
int nDigits = 1;
if (count > 0) {
nDigits = 1 + std::floor(std::log10(count));
}
// Print small values exactly
if (nDigits <= 4) {
return std::to_string(count);
}
std::vector<std::string> postFixes = {"", "K", "M", "B", "T"};
size_t iPostfix = 0;
size_t iPow = 0;
double countD = count;
while (nDigits > 3) {
count /= 1000;
countD /= 1000;
iPostfix++;
iPow += 3;
nDigits -= 3;
}
// Get a postfix, either as a predefined character or scientific notation
std::string postfix;
if (iPostfix < postFixes.size()) {
postfix = postFixes[iPostfix];
} else {
postfix = "*10^" + std::to_string(iPow);
}
// Build the actual string
char buf[50];
if (nDigits == 1) {
snprintf(buf, 50, "%2.2f%s", countD, postfix.c_str());
return std::string(buf);
} else if (nDigits == 2) {
snprintf(buf, 50, "%2.1f%s", countD, postfix.c_str());
return std::string(buf);
} else /*(nDigits == 3) */ {
snprintf(buf, 50, "%2.0f%s", countD, postfix.c_str());
return std::string(buf);
}
}
void ImGuiHelperMarker(const char* text) {
ImGui::TextDisabled("(?)");
if (ImGui::IsItemHovered()) {
ImGui::BeginTooltip();
ImGui::PushTextWrapPos(ImGui::GetFontSize() * 35.0f);
ImGui::TextUnformatted(text);
ImGui::PopTextWrapPos();
ImGui::EndTooltip();
}
}
} // namespace polyscope