-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathpolygon.cpp
More file actions
171 lines (160 loc) · 4.18 KB
/
polygon.cpp
File metadata and controls
171 lines (160 loc) · 4.18 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
167
168
169
170
171
#include "polygon.h"
#include "types.h"
#include <algorithm>
#include <glm/glm.hpp>
Polygon::Polygon(vector<shared_ptr<Vertex>> const &vertices)
: vertices(vertices)
{
if (vertices.size() >= 3)
{
calculateProperties();
}
else
{
normal = nullptr;
w = 0;
}
}
void Polygon::calculateProperties()
{
auto const &a = vertices.at(0);
auto const &b = vertices.at(1);
auto const &c = vertices.at(2);
glm::vec3 const e1 = b->position - a->position;
glm::vec3 const e2 = c->position - a->position;
glm::vec3 const n = glm::normalize(glm::cross(e1, e2));
this->normal = std::make_shared<Vertex>(Vertex(n, glm::vec3(), glm::vec2()));
this->w = glm::dot(n, a->position);
}
shared_ptr<Polygon> Polygon::clone() const
{
auto polygon = make_shared<Polygon>(Polygon());
polygon->vertices.reserve(vertices.size());
for (auto const &vertex : vertices)
polygon->vertices.push_back(vertex->clone());
polygon->calculateProperties();
return polygon;
}
shared_ptr<Polygon> Polygon::flip()
{
this->normal = this->normal->multiplyScalar(-1);
this->w *= -1;
std::reverse(this->vertices.begin(), this->vertices.end());
return shared_from_this();
}
CLASSIFICATION Polygon::classifyVertex(shared_ptr<Vertex> const &vertex)
{
float const side_value = glm::dot(this->normal->position, vertex->position) - this->w;
if (side_value < -EPSILON)
{
return BACK;
}
if (side_value > EPSILON)
{
return FRONT;
}
return COPLANAR;
}
CLASSIFICATION Polygon::classifySide(shared_ptr<Polygon> const &polygon)
{
int num_positive{0};
int num_negative{0};
for (auto const &vertex : polygon->vertices)
{
auto classification = classifyVertex(vertex);
if (classification == FRONT)
{
num_positive++;
}
else if (classification == BACK)
{
num_negative++;
}
}
if (num_positive > 0 && num_negative == 0)
{
return FRONT;
}
if (num_positive == 0 && num_negative > 0)
{
return BACK;
}
if (num_positive == 0 && num_negative == 0)
{
return COPLANAR;
}
return SPANNING;
}
void Polygon::splitPolygon(shared_ptr<Polygon> const &polygon, vector<shared_ptr<Polygon>> &coplanar_front, vector<shared_ptr<Polygon>> &coplanar_back, vector<shared_ptr<Polygon>> &front, vector<shared_ptr<Polygon>> &back)
{
auto classification = classifySide(polygon);
if (classification == COPLANAR)
{
if (this->normal->dot(polygon->normal) > 0)
{
coplanar_front.push_back(polygon);
}
else
{
coplanar_back.push_back(polygon);
}
}
else if (classification == FRONT)
{
front.push_back(polygon);
}
else if (classification == BACK)
{
back.push_back(polygon);
}
else
{
vector<shared_ptr<Vertex>> front_vertices;
vector<shared_ptr<Vertex>> back_vertices;
size_t const n = polygon->vertices.size();
front_vertices.reserve(n + 2);
back_vertices.reserve(n + 2);
glm::vec3 const planeN = this->normal->position;
for (size_t i{0}; i < n; i++)
{
size_t const j = (i + 1) % n;
auto const &vi = polygon->vertices.at(i);
auto const &vj = polygon->vertices.at(j);
auto ti = this->classifyVertex(vi);
auto tj = this->classifyVertex(vj);
if (ti != BACK)
{
front_vertices.push_back(vi);
}
if (ti != FRONT)
{
back_vertices.push_back(vi);
}
if ((ti | tj) == CLASSIFICATION::SPANNING)
{
glm::vec3 const edge = vj->position - vi->position;
float const denom = glm::dot(planeN, edge);
/** Edge parallel to cutting plane: skip bogus intersection (avoids holes / spikes). */
if (std::abs(denom) < 1e-8f)
continue;
float t = (this->w - glm::dot(planeN, vi->position)) / denom;
t = glm::clamp(t, 0.0f, 1.0f);
auto v = vi->interpolate(vj, t);
front_vertices.push_back(v);
back_vertices.push_back(v);
}
}
if (front_vertices.size() >= 3)
{
auto p = make_shared<Polygon>(front_vertices);
// p->calculateProperties();
front.push_back(p);
}
if (back_vertices.size() >= 3)
{
auto p = make_shared<Polygon>(back_vertices);
// p->calculateProperties();
back.push_back(p);
}
}
}