-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjarvis_march_parallel.hpp
111 lines (93 loc) · 2.74 KB
/
jarvis_march_parallel.hpp
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
#ifndef jarvis_march_parallel_hpp
#define jarvis_march_parallel_hpp
#include <vector>
#include <string>
#include <utility>
#include <omp.h>
#include "convex_hull_base.hpp"
#include "point.hpp"
#include "vector2d.hpp"
namespace csce {
template<typename T>
class jarvis_march_parallel : public csce::convex_hull_base<T> {
public:
jarvis_march_parallel(int _nthreads) : convex_hull_base<T>(_nthreads){}
std::string name() const {
return "Jarvis' March Parallel";
}
std::vector<csce::point<T>> compute_hull(std::vector<csce::point<T>>& points) {
return this->performShortestPathCalculation(points);
}
~jarvis_march_parallel() {}
private:
std::vector<csce::point<T>> performShortestPathCalculation(const std::vector<csce::point<T>>& points) const {
std::vector<csce::point<T>> resultsOfShortestPath;
int topMostPoint = 0;
int leftMostPoint = 0;
int rightMostPoint = 0;
int bottomMostPoint = 0;
int topLeft = 0;
int topRight = 0;
int bottomLeft = 0;
int bottomRight = 0;
#pragma omp parallel for num_threads(this->nthreads)
for(int i = 0; i < points.size(); i++){
if(points[i].y > points[topMostPoint].y){
topMostPoint = i;
}
if(points[i].y < points[bottomMostPoint].y) {
bottomMostPoint = i;
}
if(points[i].x > points[rightMostPoint].x) {
rightMostPoint = i;
}
if(points[i].x < points[leftMostPoint].x) {
leftMostPoint = i;
}
}
#pragma omp parallel for num_threads(this->nthreads)
for(int i = 0; i < points.size(); i++) {
if(points[i] == points[topMostPoint] + points[leftMostPoint]){
topLeft = i;
}
if(points[i] == points[topMostPoint] + points[rightMostPoint]) {
topRight = i;
}
if(points[i] == points[bottomMostPoint] + points[leftMostPoint]) {
bottomLeft = i;
}
if(points[i] == points[bottomMostPoint] + points[rightMostPoint]) {
bottomRight = i;
}
}
int tempPoint1 = topMostPoint;
int tempPoint2 = 0;
int orientationValue = 0;
do{
tempPoint2 = (tempPoint1 + 1) % points.size();
#pragma omp parallel for num_threads(this->nthreads)
for(int i = 0; i < points.size(); i++){
orientationValue = operation(points[tempPoint1],points[i],points[tempPoint2]);
if(orientationValue == 2){
tempPoint2 = i;
}
}
resultsOfShortestPath.push_back(points[tempPoint2]);
tempPoint1 = tempPoint2;
}while(tempPoint1 != topMostPoint);
return resultsOfShortestPath;
}
int operation(csce::point<T> d, csce::point<T> e, csce::point<T> f) const {
csce::vector2d<T> a(e,d);
csce::vector2d<T> b(e,f);
bool is_counterclockwise = a.ccw(b);
if(is_counterclockwise == true){
return 2;
}
else {
return 0;
}
}
};
}
#endif