-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPointRepository.cpp
607 lines (487 loc) · 15.6 KB
/
PointRepository.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
//
// Created by rkindela on 12-04-2018.
//
#include "PointRepository.h"
#include "Vector3D.h"
#include "utils.h"
#include "Matrix.h"
PointRepository* PointRepository::pointRepository = nullptr;
std::ostream& operator<<(std::ostream& os, const PointRepository* pr)
{
int count = pr->getPointCount();
long address = (long)pr;
os<<"POINT_REPOSITORY address: "<<address<<endl;
os<<"point count: "<<count<<endl;
for (auto it : pr->pointList)
{
os<<"POINT: "<<*it<<endl;
}
return os;
}
PointRepository::PointRepository() : pointList()
{
}
void PointRepository::clear() {
if (pointRepository == nullptr)
return;
int count = pointList.size();
for (int i = 0; i < count; i++)
{
if (pointList[i] != nullptr)
delete pointList[i];
pointList[i] = nullptr;
}
pointList.clear();
pointRepository = nullptr;
}
PointRepository::~PointRepository()
{
if (pointRepository == nullptr)
return;
int count = pointList.size();
for (int i = 0; i < count;i++)
delete pointList[i];
pointList.clear();
pointRepository = nullptr;
}
PointRepository* PointRepository::instance()
{
if (pointRepository == nullptr)
pointRepository = new PointRepository();
return pointRepository;
}
void PointRepository::add(POINT* p)
{
pointList.push_back(p);
}
POINT* PointRepository::getPoint(int pointPosition)
{
if (pointPosition >= 0 && pointPosition < pointList.size())
return pointList[pointPosition];
return nullptr;
}
float PointRepository::area2(int pointA, int pointB, int pointC)
{
POINT* pA = this->getPoint(pointA);
POINT* pB = this->getPoint(pointB);
POINT* pC = this->getPoint(pointC);
if (pA == nullptr || pB == nullptr || pC == nullptr)
return 0;
Vector3D A(*pA, *pB);
Vector3D B(*pA, *pC);
return (A.x*B.y - A.y*B.x);
}
int PointRepository::areaSign(int pointA, int pointB, int pointC)
{
float area = this->area2(pointA, pointB, pointC);
if(almost_equal(area, 0.00f))
return 0;
return (area > 0) ? 1 : -1;
}
int PointRepository::volume6(int pointA, int pointB, int pointC, int pointD)
{
/**
* |pAx pAy pAz 1|
* (1/3!) * |pBx pBy pBz 1| = (pB-pA)x(pC-pA)*(pD-pA)
* |pCx pCy pCz 1|
* |pDx pDy pDz 1|*/
POINT* pA = this->getPoint(pointA);
POINT* pB = this->getPoint(pointB);
POINT* pC = this->getPoint(pointC);
POINT* pD = this->getPoint(pointD);
if (pA == nullptr || pB == nullptr || pC == nullptr || pD == nullptr)
return 0;
Vector3D v1(*pA, *pB);
Vector3D v2(*pA, *pC);
Vector3D v3(*pA, *pD);
return v1.cross(v2).dot(v3);
}
// Given three colinear points p, q, r, the function checks if
// point q lies on line segment 'pr'
bool PointRepository::onSegment(int _p, int _q, int _r)
{
POINT& p = *this->getPoint(_p);
POINT& q = *this->getPoint(_q);
POINT& r = *this->getPoint(_r);
if (q.x <= max(p.x, r.x) && q.x >= min(p.x, r.x) &&
q.y <= max(p.y, r.y) && q.y >= min(p.y, r.y))
return true;
return false;
}
bool PointRepository::inCircumcircle(int pointA, int pointB, int pointC, int pointD)
{
/**
* |Ax Ay Ax^2+Ay^2 1|
* |Bx By Bx^2+By^2 1| > 0
* |Cx Cy Cx^2+Cy^2 1|
* |Dx Dy Dx^2+Dy^2 1|*/
POINT* pA = this->getPoint(pointA);
POINT* pB = this->getPoint(pointB);
POINT* pC = this->getPoint(pointC);
POINT* pD = this->getPoint(pointD);
if (pA == nullptr || pB == nullptr || pC == nullptr || pD == nullptr)
return false;
POINT& ad = (*pA) - (*pD);
POINT& bd = (*pB) - (*pD);
POINT& cd = (*pC) - (*pD);
float _det = Matrix::det(ad.x, ad.y, ad.x*ad.x+ad.y*ad.y,
bd.x, bd.y, bd.x*bd.x+bd.y*bd.y,
cd.x, cd.y, cd.x*cd.x+cd.y*cd.y);
if (almost_equal(_det, 0.000f))
return true;
return (_det > 0);
}
bool PointRepository::areIntersected(int p1, int q1, int p2, int q2)
{
int o1 = areaSign(p1, q1, p2);
int o2 = areaSign(p1, q1, q2);
int o3 = areaSign(p2, q2, p1);
int o4 = areaSign(p2, q2, q1);
if (o1 != o2 && o3 != o4)
return true;
if (o1 == 0 && onSegment(p1, p2, q1)) return true;
// p1, q1 and q2 are colinear and q2 lies on segment p1q1
if (o2 == 0 && onSegment(p1, q2, q1)) return true;
// p2, q2 and p1 are colinear and p1 lies on segment p2q2
if (o3 == 0 && onSegment(p2, p1, q2)) return true;
// p2, q2 and q1 are colinear and q1 lies on segment p2q2
if (o4 == 0 && onSegment(p2, q1, q2)) return true;
return false;
}
int PointRepository::squareMagnitude(int pointA, int pointB)
{
POINT* pA = this->getPoint(pointA);
POINT* pB = this->getPoint(pointB);
if (pA == nullptr || pB == nullptr)
return 0;
return pA->squareDistance(*pB);
}
int PointRepository::middleAngleCos(int pointA, int pointB, int pointC)
{
POINT* pA = this->getPoint(pointA);
POINT* pB = this->getPoint(pointB);
POINT* pC = this->getPoint(pointC);
if (pA == nullptr || pB == nullptr || pC == nullptr)
return 0;
Vector3D v1(*pA, *pB);
Vector3D v2(*pA, *pC);
return v1.angleCos(v2);
}
float PointRepository::angleCos(int pointA, int pointB, int pointC, int pointD)
{
POINT* pA = this->getPoint(pointA);
POINT* pB = this->getPoint(pointB);
POINT* pC = this->getPoint(pointC);
POINT* pD = this->getPoint(pointD);
if (pA == nullptr || pB == nullptr || pC == nullptr || pD == nullptr)
return 0;
Vector3D v1(*pA, *pB);
Vector3D v2(*pC, *pD);
return v1.angleCos(v2);
}
int PointRepository::getPointCount() const
{
return pointList.size();
}
int PointRepository::getCentralPointPosition(const std::vector<int> &pointCloud, int p)
{
int count = pointList.size();
if (p < count)
count = p;
int x = 0, y = 0, z = 0;
for (int i = 0; i < count; i++)
{
int idx = pointCloud[i];
POINT *initialPoint = this->getPoint(idx);
x += initialPoint->x;
y += initialPoint->y;
z += initialPoint->z;
}
if (count == 0) // for avoid division by zero exception
count = 1;
POINT *center = new POINT(x / count, y / count, z / count);
PointRepository::instance()->add(center); // we add the center point to repository for guarantee indexing
int pointPosition = PointRepository::instance()->getPointCount()-1;
return pointPosition;
}
int PointRepository::getLowerMostXCoord(const std::vector<int> &pointCloud)
{
int x = 214748364, count = pointCloud.size(), idxleftPoint = 0;
for (int idx = 0; idx < count; idx++)
{
int i = pointCloud[idx];
POINT* tempPoint = this->getPoint(i);
if (x > tempPoint->x)
{
idxleftPoint = i;
x = tempPoint->x;
}
else if(x == tempPoint->x)
{
POINT* currentPoint = this->getPoint(idxleftPoint);
if (currentPoint != nullptr)
{
if (tempPoint->y > currentPoint->y)
idxleftPoint = idx;
if (tempPoint->z > currentPoint->z)
idxleftPoint = idx;
}
}
}
return idxleftPoint;
}
int PointRepository::getLowerMostYCoord(const std::vector<int> &pointCloud)
{
int y = 214748364, count = pointCloud.size(), idxleftPoint = 0;
for (int i = 0; i < count; i++)
{
int idx = pointCloud[i];
POINT* tempPoint = this->getPoint(idx);
if (y > tempPoint->y)
{
idxleftPoint = idx;
y = tempPoint->y;
}
else if(y == tempPoint->y)
{
POINT* currentPoint = this->getPoint(idxleftPoint);
if (currentPoint != nullptr)
{
if (tempPoint->x > currentPoint->x)
idxleftPoint = idx;
if (tempPoint->z > currentPoint->z)
idxleftPoint = idx;
}
}
}
return idxleftPoint;
}
int PointRepository::getLowerMostZCoord(const std::vector<int> &pointCloud)
{
int z = 214748364, count = pointCloud.size(), idxleftPoint = 0;
for (int i = 0; i < count; i++)
{
int idx = pointCloud[i];
POINT* tempPoint = this->getPoint(idx);
if (z > tempPoint->z)
{
idxleftPoint = idx;
z = tempPoint->z;
}
else if(z == tempPoint->z)
{
POINT* currentPoint = this->getPoint(idxleftPoint);
if (currentPoint != nullptr)
{
if (tempPoint->x > currentPoint->x)
idxleftPoint = idx;
if (tempPoint->y > currentPoint->y)
idxleftPoint = idx;
}
}
}
return idxleftPoint;
}
int PointRepository::getUpperMostXCoord(const std::vector<int> &pointCloud)
{
int x = -214748364, count = pointCloud.size(), idxleftPoint = 0;
for (int i = 0; i < count; i++)
{
int idx = pointCloud[i];
POINT* tempPoint = this->getPoint(idx);
if (x < tempPoint->x)
{
idxleftPoint = idx;
x = tempPoint->x;
}
else if(x == tempPoint->x)
{
POINT* currentPoint = this->getPoint(idxleftPoint);
if (currentPoint != nullptr)
{
if (tempPoint->y < currentPoint->y)
idxleftPoint = idx;
if (tempPoint->z < currentPoint->z)
idxleftPoint = idx;
}
}
}
return idxleftPoint;
}
int PointRepository::getUpperMostYCoord(const std::vector<int> &pointCloud)
{
int y = -214748364, count = pointCloud.size(), idxleftPoint = 0;
for (int i = 0; i < count; i++)
{
int idx = pointCloud[i];
POINT* tempPoint = this->getPoint(idx);
if (y < tempPoint->y)
{
idxleftPoint = idx;
y = tempPoint->y;
}
else if(y == tempPoint->y)
{
POINT* currentPoint = this->getPoint(idxleftPoint);
if (currentPoint != nullptr)
{
if (tempPoint->x < currentPoint->x)
idxleftPoint = idx;
if (tempPoint->z < currentPoint->z)
idxleftPoint = idx;
}
}
}
return idxleftPoint;
}
int PointRepository::getUpperMostZCoord(const std::vector<int> &pointCloud)
{
int z = -214748364, count = pointCloud.size(), idxleftPoint = 0;
for (int i = 0; i < count; i++)
{
int idx = pointCloud[i];
POINT* tempPoint = this->getPoint(idx);
if (z < tempPoint->z)
{
idxleftPoint = idx;
z = tempPoint->z;
}
else if(z == tempPoint->z)
{
POINT* currentPoint = this->getPoint(idxleftPoint);
if (currentPoint != nullptr)
{
if (tempPoint->x < currentPoint->x)
idxleftPoint = idx;
if (tempPoint->y < currentPoint->y)
idxleftPoint = idx;
}
}
}
return idxleftPoint;
}
// A function used by library function qsort() to sort an array of
// points with respect to the first point
int compare(const void *vp1, const void *vp2)
{
int p0 = PointRepository::instance()->pivotePosition;
int p1 = *(int*)vp1;
int p2 = *(int*)vp2;
// Find orientation
int o = PointRepository::instance()->areaSign(p0, p1, p2);
POINT pp0 = *PointRepository::instance()->getPoint(p0);
POINT pp1 = *PointRepository::instance()->getPoint(p1);
POINT pp2 = *PointRepository::instance()->getPoint(p2);
if (o == 0) {
return (pp0.squareDistance(pp2) >= pp0.squareDistance(pp1)) ? -1 : 1;
}
return (o > 0) ? -1 : 1;
}
// A function used by library function qsort() to sort an array of
// points with respect to the first point
int compare2(const void *vp1, const void *vp2)
{
int p1 = *(int*)vp1;
int p2 = *(int*)vp2;
// Find orientation
return PointRepository::instance()->computeLexicographicOrder(p1, p2);
}
int PointRepository::computeLexicographicOrder( int a, int b)
{
POINT* p1 = PointRepository::instance()->getPoint(a);
POINT* p2 = PointRepository::instance()->getPoint(b);
// test the x-coord first
if (p1->x > p2->x) return 1;
if (p1->x < p2->x) return (-1);
// and test the y-coord second
if (p1->y > p2->y) return 1;
if (p1->y < p2->y) return (-1);
if (p1->z > p2->z) return 1;
if (p1->z < p2->z) return (-1);
// when you exclude all other possibilities, what remains is...
return 0; // they are the same point
}
std::vector<int> PointRepository::getSortedPolarAngles(std::vector<int>& pointCloud)
{
/**
*
* This method sort all points according its polar angles with point P0.
* */
int count = pointCloud.size() - 1;
pivotePosition = pointCloud[0];
qsort(&pointCloud[1], count, sizeof(int), compare);
return pointCloud;
}
std::vector<int> PointRepository::getSortedPoints(std::vector<int>& pointCloud)
{
/**
*
* This method sort all points according its polar angles with point P0.
* */
int count = pointCloud.size();
qsort(&pointCloud[0], count, sizeof(int), compare2);
return pointCloud;
}
std::vector<int> PointRepository::getIndexes()
{
std::vector<int>* indexes = new std::vector<int>();
int count = pointList.size();
for (int i = 0; i < count; i++)
{
indexes->push_back(i);
}
return *indexes;
}
int PointRepository::generalArea2(const vector<int>& pointcloud)
{
/**
* The general formulation for compute the area of any simple polygon is using the Gauss Determinant
*
* **/
bool inLeft = true;
int last = pointcloud.size(), i = 0;
int area = 0;
if (last < 3) // if polygon vertex count is less than three
return 0;
while(inLeft && i < last)
{
// we check if generalArea2 >= 0 to include the colinear case
int pos = pointcloud[i];
int pos2 = pointcloud[(i+1)%last];
POINT* pi = this->getPoint(pos);
POINT* pi1 = this->getPoint(pos2); // (i+1)%last is for check at the end the last point with first ((last-1)+1)%last = 0
area += pi->x*pi1->y - pi1->x*pi->y;
++i;
}
return area;
}
Vector3D* PointRepository::getVector3dFromPoints(int pointA, int pointB)
{
POINT *a = new POINT(), *b = new POINT();
if (pointA != -1)
a = getPoint(pointA);
if (pointB != -1)
b = getPoint(pointB);
return new Vector3D(*a, *b);
}
int PointRepository::getMaximumAreaCoord(const std::vector<int> &pointCloud, int a, int b)
{
int max_area = -214748364, count = pointCloud.size(), idxmaxPoint = 0;
for (int i = 0; i < count; i++)
{
int idx = pointCloud[i];
int area = this->area2(a, b, idx);
if (max_area < area)
{
idxmaxPoint = idx;
max_area = area;
}
}
return idxmaxPoint;
}
void PointRepository::remove(int pos)
{
if (pos < 0 || pos > pointList.size())
return;
pointList.erase(pointList.begin() + pos);
}