/
Util.h
168 lines (132 loc) · 3.96 KB
/
Util.h
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
167
168
#ifndef UTIL_H
#define UTIL_H
#include "Scan3D.h"
class Util
{
public:
/* string operations */
static vector<string> split(string str, string sep) {
// char* cstr = const_cast<char*>(str.c_str());
char cstr[str.size()];
str.copy(cstr, str.size());
char* current;
vector<string> arr;
current = strtok(cstr, sep.c_str());
while(current != NULL)
{
arr.push_back(current);
current = strtok(NULL, sep.c_str());
}
return arr;
}
static string join(vector<string> parts, string glue = "") {
string frankenstein;
for (int i = 0; i < parts.size()-1; ++i)
{
frankenstein += parts[i] + glue;
}
return frankenstein + parts.back();
}
static vector<string> split(string str, char delimiter) {
vector<string> internal;
stringstream ss(str);
string tok;
while(getline(ss, tok, delimiter)) {
internal.push_back(tok);
}
return internal;
}
static string getFileExtension(string fileName) {
return split(fileName, '.').back();
}
static string removeFileExtension(string fileName) {
// vector<string> temp = split(fileName, ".");
Tokenizer tok(fileName, '.');
vector<string> temp = tok.remaining();
string nameWithoutExtension;
for (int i = 0; i < temp.size()-1; ++i)
{
nameWithoutExtension = nameWithoutExtension + temp[i];
}
return nameWithoutExtension;
}
static string addSuffix(string str, string suffix) {
return str + suffix;
}
static string changeSuffix(string filePath, string suffix) {
// vector<string> path = Util::split(filePath, "/");
Tokenizer tok(filePath, '/');
vector<string> path = tok.remaining();
string fileNameRaw = Util::removeFileExtension(path.back());
string newFileName = Util::addSuffix(fileNameRaw, suffix);
if (path.size() > 1)
{
vector<string> previousPath(path.begin(), path.end() - 1);
return join(previousPath, "/") + "/" + newFileName;
}
return newFileName;
}
/* other */
// Get current date/time, format is YYYY-MM-DD.HH:mm:ss
static string currentDateTime() {
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
// Visit http://en.cppreference.com/w/cpp/chrono/c/strftime
// for more information about date/time format
strftime(buf, sizeof(buf), "%Y-%m-%d_%T", &tstruct);
return buf;
}
// desenha pontos de facetracker
static void Draw(cv::Mat &image, cv::Mat &shape, cv::Mat &visi) {
int n = shape.rows/2;
cv::Point p1;
cv::Scalar c;
//draw points
// int j = 0;
for(int i = 0; i < n; i++) {
if (visi.at<int>(i,0) == 0) continue;
p1 = cv::Point(shape.at<double>(i,0), shape.at<double>(i+n,0));
c = cv::Scalar(0, 255, 0);
cv::circle(image, p1, 2, c);
// ostringstream ss;
// ss << i;
// putText(image, ss.str(), p1, cv::FONT_HERSHEY_SIMPLEX, .3, cv::Scalar(0, 0, 255));
}
}
// distancia entre dois pontos (de verdade)
static float euclideanDistance(float x1, float y1, float z1, float x2, float y2, float z2) {
return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2) + pow(z1 - z2, 2));
}
// distancia entre dois pontos (apenas para descobrir o ponto mais próximo)
static float euclideanDistance2(float x1, float y1, float z1, float x2, float y2, float z2) {
return pow(x1 - x2, 2) + pow(y1 - y2, 2) + pow(z1 - z2, 2);
}
// cria vector inicializado com valores vindos de um array
static vector<int> initVector(int array[]) {
int arrLength = sizeof(array) / sizeof(array[0]);
vector<int> result(array, array + arrLength);
return result;
}
static cv::Point findMinimumXY(std::vector<cv::Point> points) {
cv::Point minimumXY(9999, 9999);
for (int i = 0; i < points.size(); ++i)
{
cv::Point cvp = points[i];
if (cvp.x < minimumXY.x) minimumXY.x = cvp.x;
if (cvp.y < minimumXY.y) minimumXY.y = cvp.y;
}
return minimumXY;
}
static void printPoints(std::vector<cv::Point> points) {
cout << "[ ";
for (int i = 0; i < points.size(); ++i)
{
cv::Point cvp = points[i];
cout << "(" << cvp.x << "," << cvp.y << ") ";
}
cout << "]" << endl;
}
};
#endif