-
Notifications
You must be signed in to change notification settings - Fork 2
/
triangle.cpp
61 lines (45 loc) · 1.47 KB
/
triangle.cpp
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
#include "triangle.hpp"
#include <vec/vec.hpp>
void triangle::generate_flat_normals()
{
vec3f normal = generate_flat_normal(xyz_to_vec(vertices[0].get_pos()), xyz_to_vec(vertices[1].get_pos()), xyz_to_vec(vertices[2].get_pos()));
for(int i=0; i<3; i++)
{
vertices[i].set_normal({normal.x(), normal.y(), normal.z()});
}
}
std::pair<vec3f, vec3f> triangle::get_min_max()
{
vec3f minv = {FLT_MAX, FLT_MAX, FLT_MAX};
vec3f maxv = {-FLT_MAX, -FLT_MAX, -FLT_MAX};
for(auto& i : vertices)
{
cl_float4 pos = i.get_pos();
minv = min(minv, (vec3f){pos.x, pos.y, pos.z});
maxv = max(maxv, (vec3f){pos.x, pos.y, pos.z});
}
return {minv, maxv};
}
std::array<cl_float4, 6> quad::decompose()
{
std::vector<vec3f> points;
points.push_back(xyz_to_vec(p1));
points.push_back(xyz_to_vec(p2));
points.push_back(xyz_to_vec(p3));
points.push_back(xyz_to_vec(p4));
vec3f avg = {0.f,0.f,0.f};
for(auto& i : points)
{
avg += i;
}
avg = avg / (float)points.size();
auto sorted = sort_anticlockwise(points, avg);
cl_float4 conv[4];
for(int i=0; i<4; i++)
{
conv[i] = {sorted[i].v[0], sorted[i].v[1], sorted[i].v[2]};
}
///sorted anticlockwise, now return both halves
//return {conv[0], conv[2], conv[1], conv[1], conv[2], conv[3]};
return {conv[0], conv[2], conv[1], conv[0], conv[3], conv[2]};
}