-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPolyhedron.cpp
374 lines (298 loc) · 8.52 KB
/
Polyhedron.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
//
// Created by rkindela on 19-07-18.
//
#include "Polyhedron.h"
Polyhedron::Polyhedron():faces(), vertexes(), edges()
{
}
vector<int>* Polyhedron::getLink(int point)
{
Vertex* v = this->getVertex(point);
vector<Vertex*>* link = v->getLink();
std::vector<Vertex*>::iterator begin = link->begin();
std::vector<Vertex*>::iterator end = link->end();
vector<int>* linkIndexes = new vector<int>();
for(auto i = begin; i != end; ++i)
{
linkIndexes->push_back((*i)->pointIndex);
}
delete link;
return linkIndexes;
}
int Polyhedron::addVertex(int point)
{
Vertex* v = new Vertex();
v->pointIndex = point;
this->vertexes.push_back(v);
return this->vertexes.size()-1;
}
int Polyhedron::addFace()
{
Face* f = new Face();
this->faces.push_back(f);
return this->faces.size()-1;
}
int Polyhedron::addEdge(int init, int final)
{
HalfEdge* edge = new HalfEdge();
edge->vertex = this->getVertex(init);
edge->vertex->edge = edge;
edge->face = this->getFace(final);
edges.push_back(edge);
return edges.size()-1;
}
Vertex* Polyhedron::getVertex(int pos)
{
if (pos < 0 || pos > vertexes.size())
return nullptr;
return vertexes[pos];
}
Vertex* Polyhedron::getVertex(int pos) const
{
if (pos < 0 || pos > vertexes.size())
return nullptr;
return vertexes[pos];
}
POINT* Polyhedron::getPoint(int pos)
{
Vertex* v = this->getVertex(pos);
if(v == nullptr)
return nullptr;
return PointRepository::instance()->getPoint(v->pointIndex);
}
POINT* Polyhedron::getPoint(int pos) const
{
Vertex* v = this->getVertex(pos);
if(v == nullptr)
return nullptr;
return PointRepository::instance()->getPoint(v->pointIndex);
}
Face* Polyhedron::getFace(int pos)
{
if(0 > pos || faces.size() < pos)
return nullptr;
return faces[pos];
}
Face* Polyhedron::getFace(int pos) const
{
if(0 > pos || faces.size() < pos)
return nullptr;
return faces[pos];
}
HalfEdge* Polyhedron::getEdge(int pos)
{
if(0 > pos || edges.size() < pos)
return nullptr;
return edges[pos];
}
HalfEdge* Polyhedron::getEdge(int pos) const
{
if(0 > pos || edges.size() < pos)
return nullptr;
return edges[pos];
}
int Polyhedron::getPointCount() const
{
return vertexes.size();
}
int Polyhedron::getFaceCount() const
{
return faces.size();
}
int Polyhedron::getEdgeCount() const
{
return edges.size();
}
bool Polyhedron::intersect(const Polyhedron& newPoly)
{
/**
*
Idea basica para generalizar el algoritmo de interseccion de poligonos:
Supongamos que las caras P y Q
estan orientados ccw y que A y B son los vectores normales de P y Q, respectivamente.
El algoritmo tiene a A y B "cazandose" uno a otro
respectivamente, ajustando sus velocidades de tal forma
de encontrarse cada vez que P y Q se intersectan.
A y B se representan como vectores y lo clave aqui son las
reglas para avanzar A y B.
La estructura basica del algoritmo es la siguiente:
Se avanzan las caras P y Q segun condiciones geometricas. y cuando se detecte una interseccion se termina.
al final es necesario chequear que P no contenga a Q y viceversa
* */
Face* P = faces[0];
Face* Q = newPoly.faces[0];
int itP = 0, itQ = 0;
int n = faces.size(), m = newPoly.faces.size(); /** indices en P y Q */
Vector3D *A, *B; // aristas orientadas en P y Q
int cross; // AxB
bool bHA, aHB; // b in H(A), a in H(B)
/**Inicializacion de variables*/
do
{
/**Calculo de variables claves*/
A = P->getNormal();
B = Q->getNormal();
cross = A->detFromCross(*B); // en 2D AxB = C y C tiene la forma (0,0, Cz), por tanto AxB == Cz
bHA = P->inLeft(Q); // leftOn
aHB = Q->inLeft(P); // leftOn
if (P->areIntersected(Q))
{
return true;
}
/**Advances Rules*/
if (cross >= 0)
{
if (bHA) {
P = faces[(++itP)%n];
}
else{
Q = newPoly.faces[(++itQ)%m];
}
} else
{
if (aHB) {
Q = newPoly.faces[(++itQ)%m];
}
else{
P = faces[(++itP)%n];
}
}
}
while((itP < n) || (itQ < m));
/**Casos especiales*/
return this->contains(newPoly) || newPoly.contains(*this);
}
bool Polyhedron::isRegular()
{
int nedges = edges.size();
if(nedges == 0)
return false;
Vector3D* v = edges[0]->getOrientedVector3D();
int side = v->squareNorm(), oldside = side;
for (int i = 1; i < nedges; i++)
{
v = edges[i]->getOrientedVector3D();
side = v->squareNorm();
if (side != oldside)
return false;
oldside = side;
}
return true;
}
bool Polyhedron::isSimple()
{
//check simplicity of polyhedron using Euler characteristics
// V - F + E = 2
return true;
}
bool Polyhedron::isConvex()
{
int count = vertexes.size();
int numfaces = faces.size();
if (numfaces == 0 || count == 0)
return false;
POINT* centroid = new POINT();
for(int i = 0; i<count;i++) // find the centroid of polyhedron
{
Vertex* v = vertexes[i];
POINT* p = PointRepository::instance()->getPoint(v->pointIndex);
centroid->x += p->x;
centroid->y += p->y;
centroid->z += p->z;
}
int div = count == 0 ? 1 : count;
centroid->x /= div;
centroid->y /= div;
centroid->z /= div;
PointRepository::instance()->add(centroid); // add to repository and count is it index because centroid is the last point
bool sign = true, oldsign = true;
Face* f = faces[0]; // as all face is oriented, we just need the first three vertex to check
int a = f->edge->vertex->pointIndex;
int b = f->edge->next->vertex->pointIndex;
int c = f->edge->next->next->vertex->pointIndex;
int d = count;
sign = (PointRepository::instance()->volume6(a, b, c, d) >= 0);
oldsign = sign;
for(int i = 1; i < numfaces;i++) // for any face we check the volume orientation all of this will be the same sign
{
Face* f = faces[i]; // as all face is oriented, we just need the first three vertex to check
int a = f->edge->vertex->pointIndex;
int b = f->edge->next->vertex->pointIndex;
int c = f->edge->next->next->vertex->pointIndex;
sign = PointRepository::instance()->volume6(a, b, c, d) >= 0;
if (sign != oldsign) {
PointRepository::instance()->remove(d);
return false;
}
oldsign = sign;
}
return true;
}
bool Polyhedron::inside(int npoint)
{
return pointIntersections(npoint) % 2 != 0;
}
int Polyhedron::pointIntersections(int npoint)
{
/**
* 1) Draw a horizontal line from npoint to infinity
1) Count the number of times the line intersects with polyhedron faces.
2) A point is inside the polyhedron if either count of intersections is odd or
point lies on a face of polyhedron. If none of the conditions is true, then
point lies outside.polyhedron
*/
int nfaces = faces.size();
if (nfaces == 0)
return false;
POINT* point = this->getPoint(npoint);
PointRepository::instance()->add(new POINT(2147483647, point->y));
int end = PointRepository::instance()->getPointCount()-1;
int intersectionsNumber = 0;
for (int i = 0; i < nfaces; i++)
{
Face* f = faces[i];
if (f->intersectWithSegment(npoint, end))
++intersectionsNumber;
}
return intersectionsNumber;
}
BoundingSphereOctree* Polyhedron::getBoundingSphere() const
{
return nullptr;
}
bool Polyhedron::contains(const Polyhedron& p) const
{
return this->getBoundingSphere()->hasInside(p.getBoundingSphere());
}
bool Polyhedron::left(int npoint)
{
return (pointIntersections(npoint) % 2) == 0;
}
bool Polyhedron::rigth(int npoint)
{
return pointIntersections(npoint) == 0;
}
float Polyhedron::area2()
{
}
std::vector<Polyhedron*> Polyhedron::triangulate()
{
}
vector<Polyhedron*>* Polyhedron::getConvexHull()
{
}
vector<Polyhedron*>* Polyhedron::giftWrapping(std::vector<int> pointcloud)
{
}
vector<Polyhedron*>* Polyhedron::grahamScan(std::vector<int> pointcloud)
{
}
std::ostream& operator<<(std::ostream& os, const Polyhedron* poly)
{
}
std::ostream& operator<<(std::ostream& os, const Polyhedron& poly)
{
}
bool Polyhedron::isEqual(Polyhedron& polyB)
{
}