Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
ConvexHull.h
Go to the documentation of this file.
1/* This file is part of MoDELib, the Mechanics Of Defects Evolution Library.
2 *
3 *
4 * MoDELib is distributed without any warranty under the
5 * GNU General Public License (GPL) v2 <http://www.gnu.org/licenses/>.
6 */
7
8#ifndef model_ConvexHull_H_
9#define model_ConvexHull_H_
10
11#include <algorithm>
12#include <array>
13#include <map>
14#include <set>
15#include <vector>
16#include <cfloat>
17
18namespace model
19{
20
21
22 template <typename T>
24 {
25 const T &x;
26
27 public:
28 reverse_range(T &x) : x(x) {}
29
30 auto begin() const -> decltype(this->x.rbegin())
31 {
32 return x.rbegin();
33 }
34
35 auto end() const -> decltype(this->x.rend())
36 {
37 return x.rend();
38 }
39 };
40
41 template <typename T>
46
47 template<int dim,typename T>
48 struct HullPoint : public std::array<double,dim>
49 {
50 const T* const t ;
51
53 /* init */t(nullptr)
54 {
55
56 }
57
58 HullPoint(const std::array<double,dim>& p_in,
59 const T* const t_in) :
60 /* init */ std::array<double,dim>(p_in)
61 /* init */,t(t_in)
62 {
63
64 }
65
66// HullPoint(const HullPoint& other)
67// {}
68
69 };
70
71
72 template <int dim,typename T>
74 {
75
76 static_assert("ConvexHull not implemented for thid dimension");
77 };
78
79 template <typename T>
80 struct ConvexHull<2,T> : public std::set<HullPoint<2,T>>
81 {
85 typedef std::vector<HullPointType> HullPointContainerType;
86 static constexpr int dim=2;
87 static constexpr double tol=FLT_EPSILON;
88
89 private:
90
91 static double cross(const HullPointType& O, const HullPointType& A, const HullPointType& B)
92 {
93 return (A[0] - O[0]) * (B[1] - O[1]) - (A[1] - O[1]) * (B[0] - O[0]);
94 }
95
96 public:
97
99 {
101 if(this->size()<=3)
102 {
103 for(const auto& pt : *this)
104 {
105 H.push_back(pt);
106 }
107 }
108 else
109 {
110 // Build lower hull
111 for (const auto& pt : *this)
112 {
113 while(H.size()>=2 && cross(H[H.size()-2], H[H.size()-1], pt)<= tol)
114 {
115 H.pop_back();
116 }
117 H.push_back(pt);
118 }
119 H.pop_back();
120
121
122 const size_t lowerSize(H.size());
123
124 for (const auto& pt : reverse_iterate(*this))
125 {
126 while(H.size()-lowerSize>=2 && cross(H[H.size()-2], H[H.size()-1], pt)<= tol)
127 {
128 H.pop_back();
129 }
130 H.push_back(pt);
131 }
132 H.pop_back();
133
134 }
135 return H;
136 }
137
138
139
140 };
141
142}
143#endif
144
auto begin() const -> decltype(this->x.rbegin())
Definition ConvexHull.h:30
auto end() const -> decltype(this->x.rend())
Definition ConvexHull.h:35
initial point of the source segment B
function infiniteDisplacement syms Ax Ay Az Bx By Bz qx qy qz u L x y z real A
x
Definition int_R.m:12
reverse_range< T > reverse_iterate(T &x)
Definition ConvexHull.h:42
static constexpr double tol
Definition ConvexHull.h:87
static constexpr int dim
Definition ConvexHull.h:86
HullPointContainerType getPoints()
Definition ConvexHull.h:98
HullPoint< 2, T > HullPointType
Definition ConvexHull.h:84
std::vector< HullPointType > HullPointContainerType
Definition ConvexHull.h:85
HullPoint(const std::array< double, dim > &p_in, const T *const t_in)
Definition ConvexHull.h:58