Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
Polygon2D.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_Polygon2D_H_
9#define model_Polygon2D_H_
10
11#include <cfloat>
12#include <tuple>
13#include <map>
14#include <Eigen/Dense>
15#include <assert.h>
16
17namespace model
18{
19
20 struct Polygon2D
21 {
22
23
24 template<typename PointType>
25 static double area(const std::vector<PointType>& points)
26 {
27 double A(0.0);
28 for(size_t k=0;k<points.size();++k)
29 {
30 const size_t k1(k<points.size()-1? k+1 : 0);
31 A+=(points[k](0)-points[0](0))*(points[k1](1)-points[k](1))-(points[k](1)-points[0](1))*(points[k1](0)-points[k](0));
32 }
33 return 0.5*A;
34 }
35
36// template<typename PointType>
37// static double inside(const std::vector<PointType>& points, const PointType& test)
38// {
39// double A(area(points));
40// for(size_t k=0;k<points.size();++k)
41// {
42// const size_t k1(k<points.size()-1? k+1 : 0);
43// A+=(points[k](0)-points[0](0))*(points[k1](1)-points[k](1))-(points[k](1)-points[0](1))*(points[k1](0)-points[k](0));
44// }
45// return 0.5*A;
46// }
47
48 template<typename PointType>
49 static double isLeft(const PointType& P0, const PointType& P1, const PointType& P2 )
50 {// return type changed from int to double http://geomalgorithms.com/a03-_inclusion.html
51 return ( (P1(0) - P0(0)) * (P2(1) - P0(1))
52 - (P2(0) - P0(0)) * (P1(1) - P0(1)) );
53 }
54
55 template<typename PointType>
56 static int windingNumber(const PointType& P, const std::vector<PointType>& V)
57 {// http://geomalgorithms.com/a03-_inclusion.html
58 int wn = 0; // the winding number counter
59
60 // loop through all edges of the polygon
61 for (size_t i=0; i<V.size(); i++) { // edge from V[i] to V[i+1]
62 const size_t j(i<V.size()-1? i+1 : 0);
63 if (V[i](1) <= P(1)) { // start y <= P(1)
64 if (V[j](1) > P(1)) // an upward crossing
65 if (isLeft( V[i], V[j], P) > 0) // P left of edge
66 ++wn; // have a valid up intersect
67 }
68 else { // start y > P(1) (no test needed)
69 if (V[j](1) <= P(1)) // a downward crossing
70 if (isLeft( V[i], V[j], P) < 0) // P right of edge
71 --wn; // have a valid down intersect
72 }
73 }
74 return wn;
75 }
76
77 };
78
79} /* namespace model */
80#endif
Eigen::Vector3d V
function infiniteDisplacement syms Ax Ay Az Bx By Bz qx qy qz u L x y z real A
static int windingNumber(const PointType &P, const std::vector< PointType > &V)
Definition Polygon2D.h:56
static double isLeft(const PointType &P0, const PointType &P1, const PointType &P2)
Definition Polygon2D.h:49
static double area(const std::vector< PointType > &points)
Definition Polygon2D.h:25
int j
int k
int i