-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cpp
More file actions
77 lines (64 loc) · 1.97 KB
/
Copy pathUtils.cpp
File metadata and controls
77 lines (64 loc) · 1.97 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
#include "Utils.h"
#include "Model.h"
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <fstream>
void dump_stream(std::istream &stream, std::string path) {
std::ofstream output;
int size, pos;
char* buffer;
output.open(path);
pos = stream.tellg();
stream.seekg(0, std::ios::end);
size = stream.tellg();
stream.seekg(0, std::ios::beg);
buffer = new char[size];
stream.read(buffer, size);
output.write(buffer, size);
output.close();
stream.seekg(pos, std::ios::beg);
delete[] buffer;
}
vector_streambuf::vector_streambuf(std::vector<uint8_t> &vec) {
setg((char*)(&vec[0]), (char*)(&vec[0]), (char*)(&vec[0]+vec.size()));
}
static void split(const std::string &s, char delim, std::vector<std::string> &elems) {
std::stringstream stream;
stream.str(s);
std::string item;
while (std::getline(stream, item, delim)) {
elems.push_back(item);
}
}
std::vector<std::string> split(const std::string &s, char delim) {
std::vector<std::string> elems;
split(s, delim, elems);
return elems;
}
Vector3f Barycentric(const Vector3f &a, const Vector3f &b, const Vector3f &c, const Vector3f &point) {
// We get these by solving for P = (1 - u - v)A + uAB + vAC
// 0 = (1 - u - v)PA + uAB + vAC
// actually taking the cross product of the x and y vectors of AC, AB and PA is enough.
Vector3f xCoords;
xCoords <<
c.x() - a.x(), // AC
b.x() - a.x(), // AB
a.x() - point.x(); // PA
Vector3f yCoords;
yCoords <<
c.y() - a.y(),
b.y() - a.y(),
a.y() - point.y();
// (u, v, area)
Vector3f orthoVector = xCoords.cross(yCoords);
// Actually the area of the parallelogram, not the triangle.
float double_area = orthoVector.z();
if (std::abs(double_area) < 1e-2) {return Vector3f(-1.0f, 1.0f, 1.0f);}
return Vector3f(
1.0f - (orthoVector.x() + orthoVector.y())/double_area, // (1 - u - v)
orthoVector.y() / double_area, // v
orthoVector.x() / double_area // u
);
}