www.mooseframework.org
LineSegment.C
Go to the documentation of this file.
1 /****************************************************************/
2 /* DO NOT MODIFY THIS HEADER */
3 /* MOOSE - Multiphysics Object Oriented Simulation Environment */
4 /* */
5 /* (c) 2010 Battelle Energy Alliance, LLC */
6 /* ALL RIGHTS RESERVED */
7 /* */
8 /* Prepared by Battelle Energy Alliance, LLC */
9 /* Under Contract No. DE-AC07-05ID14517 */
10 /* With the U. S. Department of Energy */
11 /* */
12 /* See COPYRIGHT for full restrictions */
13 /****************************************************************/
14 
15 #include "LineSegment.h"
16 
17 #include "libmesh/plane.h"
18 #include "libmesh/vector_value.h"
19 
20 LineSegment::LineSegment(const Point & p0, const Point & p1) : _p0(p0), _p1(p1) {}
21 
22 bool
23 LineSegment::closest_point(const Point & p, bool clamp_to_segment, Point & closest_p) const
24 {
25  Point p0_p = p - _p0;
26  Point p0_p1 = _p1 - _p0;
27  Real p0_p1_2 = p0_p1.norm_sq();
28  Real perp = p0_p(0) * p0_p1(0) + p0_p(1) * p0_p1(1) + p0_p(2) * p0_p1(2);
29  Real t = perp / p0_p1_2;
30  bool on_segment = true;
31 
32  if (t < 0.0 || t > 1.0)
33  on_segment = false;
34 
35  if (clamp_to_segment)
36  {
37  if (t < 0.0)
38  t = 0.0;
39  else if (t > 1.0)
40  t = 1.0;
41  }
42 
43  closest_p = _p0 + p0_p1 * t;
44  return on_segment;
45 }
46 
47 Point
48 LineSegment::closest_point(const Point & p) const
49 {
50  Point closest_p;
51  closest_point(p, true, closest_p);
52  return closest_p;
53 }
54 
55 bool
56 LineSegment::closest_normal_point(const Point & p, Point & closest_p) const
57 {
58  return closest_point(p, false, closest_p);
59 }
60 
61 bool
62 LineSegment::contains_point(const Point & p) const
63 {
64  Point closest_p;
65  return closest_point(p, false, closest_p) && closest_p.absolute_fuzzy_equals(p);
66 }
67 
68 bool
69 LineSegment::intersect(const Plane & pl, Point & intersect_p) const
70 {
85  Point pl0 = pl.get_planar_point();
86  RealVectorValue N = pl.unit_normal(_p0);
87  RealVectorValue I = (_p1 - _p0).unit();
88 
89  Real numerator = (pl0 - _p0) * N;
90  Real denominator = I * N;
91 
92  // The Line is parallel to the plane
93  if (std::abs(denominator) < 1.e-10)
94  {
95  // The Line is on the plane
96  if (std::abs(numerator) < 1.e-10)
97  {
98  // The solution is not unique so we'll just pick an end point for now
99  intersect_p = _p0;
100  return true;
101  }
102  return false;
103  }
104 
105  Real d = numerator / denominator;
106 
107  // Make sure we haven't moved off the line segment!
108  if (d + libMesh::TOLERANCE < 0 || d - libMesh::TOLERANCE > (_p1 - _p0).norm())
109  return false;
110 
111  intersect_p = d * I + _p0;
112 
113  return true;
114 }
115 
116 bool
117 LineSegment::intersect(const LineSegment & l, Point & intersect_p) const
118 {
140  RealVectorValue a = _p1 - _p0;
141  RealVectorValue b = l._p1 - l._p0;
142  RealVectorValue c = l._p0 - _p0;
143 
144  RealVectorValue v = a.cross(b);
145 
146  // Check for parallel lines
147  if (std::abs(v.norm()) < 1.e-10 && std::abs(c.cross(a).norm()) < 1.e-10)
148  {
149  // TODO: The lines are co-linear but more work is needed to determine and intersection point
150  // it could be the case that the line segments don't lie on the same line or overlap only
151  // a bit
152  return true;
153  }
154 
155  // Check that the lines are coplanar
156  Real concur = c * (a.cross(b));
157  if (std::abs(concur) > 1.e-10)
158  return false;
159 
160  Real s = (c.cross(b) * v) / (v * v);
161  Real t = (c.cross(a) * v) / (v * v);
162 
163  // if s and t are between 0 and 1 then the Line Segments intersect
164  // TODO: We could handle other case of clamping to the end of Line
165  // Segements if we want to here
166 
167  if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
168  {
169  intersect_p = _p0 + s * a;
170  return true;
171  }
172  return false;
173 
229 }
Point closest_point(const Point &p) const
Returns the closest point on the LineSegment to the passed in point.
Definition: LineSegment.C:48
VectorValue< Real > RealVectorValue
Definition: Assembly.h:40
bool closest_normal_point(const Point &p, Point &closest_p) const
Finds the closest point on the Line determined by the Line Segments.
Definition: LineSegment.C:56
PetscInt N
The LineSegment class is used by the LineMaterialSamplerBase class and for some ray tracing stuff...
Definition: LineSegment.h:33
bool contains_point(const Point &p) const
Determines whether a point is in a line segment or not.
Definition: LineSegment.C:62
bool intersect(const Plane &pl, Point &intersect_p) const
Definition: LineSegment.C:69
LineSegment(const Point &p0, const Point &p1)
Definition: LineSegment.C:20