Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
PlaneSegmentIntersection.cpp
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_PlaneSegmentIntersection_CPP_
9#define model_PlaneSegmentIntersection_CPP_
10
11#include <tuple>
12#include <Eigen/Dense>
13#include <Plane.h>
14#include <FiniteLineSegment.h>
15
17
18namespace model
19{
20
21 /**********************************************************************/
22 template <int dim>
24 const VectorDimD& Normal,
25 const VectorDimD& v0,
26 const VectorDimD& v1)
27 {
28 const double nNorm(Normal.norm());
29 assert(nNorm>FLT_EPSILON && "PLANE MUST HAVE NON-ZERO NORMAL");
30 const VectorDimD n(Normal/nNorm);
31
32 // check intersection of v0->v1 with plane
33 // x=v0+u(v1-v0)
34 // (x-P0).n=0
35 // (v0+u(v1-v0)-P0).n=0
36 // u=(P0-v0).n/(v1-v0).n;
37 const double edgeNorm=(v1-v0).norm();
38 if(edgeNorm<FLT_EPSILON)
39 {// segment is degenerate (point)
40 const VectorDimD vM(0.5*(v0+v1));
41 if(fabs((P0-vM).dot(n))<FLT_EPSILON)
42 {
43 return std::make_tuple(INCIDENT,vM,vM);
44 }
45 else
46 {
47 return std::make_tuple(OFFSET,VectorDimD::Zero(),VectorDimD::Zero());
48 }
49 }
50 else
51 {
52 const double den=(v1-v0).dot(n);
53 const double num=(P0-v0).dot(n);
54
55 if (fabs(den/edgeNorm)>FLT_EPSILON)
56 {
57 // edge intersects plane
58 const double u=num/den;
59
60 if(fabs(u)<FLT_EPSILON)
61 {
62 return std::make_tuple(INCIDENT,v0,v0);
63 }
64 else if (u>=FLT_EPSILON && u<=1.0-FLT_EPSILON)
65 {
66 const VectorDimD x((1.0-u)*v0 + u*v1);
67 return std::make_tuple(INCIDENT,x,x);
68 }
69 else if (fabs(1.0-u)<FLT_EPSILON)
70 {
71 return std::make_tuple(INCIDENT,v1,v1);
72 }
73 else
74 {// no roots
75 return std::make_tuple(OFFSET,VectorDimD::Zero(),VectorDimD::Zero());
76 }
77
78 }
79 else
80 {
81 const double P0v0norm=(P0-v0).norm();
82 const double numCheck= (P0v0norm<FLT_EPSILON)? 0.0 : num/P0v0norm;
83 if (fabs(numCheck)>FLT_EPSILON)
84 {// edge is parallel to plane, no intersection
85 return std::make_tuple(PARALLEL,VectorDimD::Zero(),VectorDimD::Zero());
86
87 }
88 else
89 {// edge is coplanar
90 return std::make_tuple(COINCIDENT,v0,v1);
91 }
92 }
93 }
94 }
95
96 /**********************************************************************/
97 template <int dim>
99 const VectorDimD& N,
100 const VectorDimD& v0,
101 const VectorDimD& v1) :
102 /* init */ sol(findIntersection(P0,N,v0,v1)),
103 /* init */ type(std::get<0>(sol)),
104 /* init */ x0(std::get<1>(sol)),
105 /* init */ x1(std::get<2>(sol))
106 {
107
108 }
109
110 /**********************************************************************/
111 template <int dim>
113 const FiniteLineSegment<dim>& seg) :
114 /* init */ sol(findIntersection(plane.P,plane.unitNormal,seg.P0,seg.P1)),
115 /* init */ type(std::get<0>(sol)),
116 /* init */ x0(std::get<1>(sol)),
117 /* init */ x1(std::get<2>(sol))
118 {
119
120 }
121
122 template struct PlaneSegmentIntersection<1>;
123 template struct PlaneSegmentIntersection<2>;
124 template struct PlaneSegmentIntersection<3>;
125}
126#endif
x
Definition int_R.m:12
Eigen::Matrix< double, dim, 1 > VectorDimD
static SolutionType findIntersection(const VectorDimD &P0, const VectorDimD &Normal, const VectorDimD &v0, const VectorDimD &v1)
PlaneSegmentIntersection(const VectorDimD &P0, const VectorDimD &N, const VectorDimD &v0, const VectorDimD &v1)
std::tuple< IntersectionType, VectorDimD, VectorDimD > SolutionType
REAL u[4]