Go to the documentation of this file.
17 #ifndef IGNITION_MATH_LINE3_HH_
18 #define IGNITION_MATH_LINE3_HH_
22 #include <ignition/math/config.hh>
29 inline namespace IGNITION_MATH_VERSION_NAMESPACE {
38 public:
Line3() =
default;
44 this->pts[0] = _line[0];
45 this->pts[1] = _line[1];
53 this->Set(_ptA, _ptB);
61 public:
Line3(
const double _x1,
const double _y1,
62 const double _x2,
const double _y2)
64 this->Set(_x1, _y1, _x2, _y2);
74 public:
Line3(
const double _x1,
const double _y1,
75 const double _z1,
const double _x2,
76 const double _y2,
const double _z2)
78 this->Set(_x1, _y1, _z1, _x2, _y2, _z2);
113 public:
void Set(
const double _x1,
const double _y1,
114 const double _x2,
const double _y2,
117 this->pts[0].Set(_x1, _y1, _z);
118 this->pts[1].Set(_x2, _y2, _z);
128 public:
void Set(
const double _x1,
const double _y1,
129 const double _z1,
const double _x2,
130 const double _y2,
const double _z2)
132 this->pts[0].Set(_x1, _y1, _z1);
133 this->pts[1].Set(_x2, _y2, _z2);
140 return (this->pts[1] - this->pts[0]).Normalize();
147 return this->pts[0].Distance(this->pts[1]);
161 const double _epsilon = 1e-6)
const
166 if (std::abs(p43.
X()) < _epsilon && std::abs(p43.
Y()) < _epsilon &&
167 std::abs(p43.
Z()) < _epsilon)
174 if (std::abs(p21.
X()) < _epsilon && std::abs(p21.
Y()) < _epsilon &&
175 std::abs(p21.
Z()) < _epsilon)
180 double d1343 = p13.
Dot(p43);
181 double d4321 = p43.
Dot(p21);
182 double d1321 = p13.
Dot(p21);
183 double d4343 = p43.
Dot(p43);
184 double d2121 = p21.
Dot(p21);
186 double denom = d2121 * d4343 - d4321 * d4321;
190 if (std::abs(denom) < _epsilon)
192 double d1 = this->pts[0].Distance(_line[0]);
193 double d2 = this->pts[0].Distance(_line[1]);
195 double d3 = this->pts[1].Distance(_line[0]);
196 double d4 = this->pts[1].Distance(_line[1]);
198 if (d1 <= d2 && d1 <= d3 && d1 <= d4)
200 _result.
SetA(this->pts[0]);
201 _result.
SetB(_line[0]);
203 else if (d2 <= d3 && d2 <= d4)
205 _result.
SetA(this->pts[0]);
206 _result.
SetB(_line[1]);
210 _result.
SetA(this->pts[1]);
211 _result.
SetB(_line[0]);
215 _result.
SetA(this->pts[1]);
216 _result.
SetB(_line[1]);
222 double numer = d1343 * d4321 - d1321 * d4343;
224 double mua =
clamp(numer / denom, 0.0, 1.0);
225 double mub =
clamp((d1343 + d4321 * mua) / d4343, 0.0, 1.0);
227 _result.
Set(this->pts[0] + (p21 * mua), _line[0] + (p43 * mub));
237 auto line = this->pts[1] - this->pts[0];
238 auto ptTo0 = _pt - this->pts[0];
239 auto ptTo1 = _pt - this->pts[1];
242 if (ptTo0.Dot(line) <= 0.0)
248 if (ptTo1.Dot(line) >= 0.0)
250 return ptTo1.Length();
256 auto d = ptTo0.Cross(line);
257 auto lineLength = line.Length();
258 assert(lineLength > 0);
259 return d.Length() / lineLength;
268 double _epsilon = 1e-6)
const
271 return this->Intersect(_line, ignore, _epsilon);
280 const double _epsilon = 1e-6)
const
282 return std::abs((_line[0] - this->pts[0]).Dot(
283 (this->pts[1] - this->pts[0]).Cross(_line[1] - _line[0])))
293 const double _epsilon = 1e-6)
const
295 return (this->pts[1] - this->pts[0]).Cross(
296 _line[1] - _line[0]).Length() <= _epsilon;
308 double _epsilon = 1e-6)
const
311 if (this->Parallel(_line, _epsilon))
314 if (this->Within(_line[0], _epsilon))
320 else if (this->Within(_line[1], _epsilon))
332 this->Distance(_line, distLine, _epsilon);
336 if (distLine.
Length() < _epsilon)
352 double _epsilon = 1e-6)
const
354 return _pt.
X() <=
std::max(this->pts[0].X(),
355 this->pts[1].X()) + _epsilon &&
357 this->pts[1].X()) - _epsilon &&
359 this->pts[1].Y()) + _epsilon &&
361 this->pts[1].Y()) - _epsilon &&
363 this->pts[1].Z()) + _epsilon &&
365 this->pts[1].Z()) - _epsilon;
373 return this->pts[0] == _line[0] && this->pts[1] == _line[1];
381 return !(*
this == _line);
399 _out << _line[0] <<
" " << _line[1];
408 this->pts[0] = _line[0];
409 this->pts[1] = _line[1];
Definition: AdditivelySeparableScalarField3.hh:27
Line3(const double _x1, const double _y1, const double _z1, const double _x2, const double _y2, const double _z2)
Constructor.
Definition: Line3.hh:74
bool Distance(const Line3< T > &_line, Line3< T > &_result, const double _epsilon=1e-6) const
Get the shortest line between this line and the provided line.
Definition: Line3.hh:160
T Length() const
Returns the length (magnitude) of the vector.
Definition: Vector3.hh:122
Line3(const math::Vector3< T > &_ptA, const math::Vector3< T > &_ptB)
Constructor.
Definition: Line3.hh:51
T Distance(const Vector3< T > &_pt)
Calculate shortest distance between line and point.
Definition: Line3.hh:235
T X() const
Get the x value.
Definition: Vector3.hh:654
bool Within(const math::Vector3< T > &_pt, double _epsilon=1e-6) const
Check if the given point is between the start and end points of the line segment.
Definition: Line3.hh:351
void Set(const double _x1, const double _y1, const double _x2, const double _y2, const double _z=0)
Set the start and end point of the line segment, assuming that both points have the same height.
Definition: Line3.hh:113
Line3(const double _x1, const double _y1, const double _x2, const double _y2)
2D Constructor where Z coordinates are 0
Definition: Line3.hh:61
T Z() const
Get the z value.
Definition: Vector3.hh:668
bool Parallel(const Line3< T > &_line, const double _epsilon=1e-6) const
Test if this line and the given line are parallel.
Definition: Line3.hh:292
void Set(const math::Vector3< T > &_ptA, const math::Vector3< T > &_ptB)
Set the start and end point of the line segment.
Definition: Line3.hh:84
Line3 & operator=(const Line3< T > &_line)
Assignment operator.
Definition: Line3.hh:406
T clamp(T _v, T _min, T _max)
Simple clamping function.
Definition: Helpers.hh:406
The Vector3 class represents the generic vector containing 3 elements. Since it's commonly used to ke...
Definition: Vector3.hh:41
T Length() const
Get the length of the line.
Definition: Line3.hh:145
bool Intersect(const Line3< T > &_line, math::Vector3< T > &_pt, double _epsilon=1e-6) const
Check if this line intersects the given line segment. The point of intersection is returned in the _p...
Definition: Line3.hh:307
void SetA(const math::Vector3< T > &_ptA)
Set the start point of the line segment.
Definition: Line3.hh:93
Line3< float > Line3f
Definition: Line3.hh:420
Line3()=default
Line Constructor.
Line3< int > Line3i
Definition: Line3.hh:418
bool Coplanar(const Line3< T > &_line, const double _epsilon=1e-6) const
Test if this line and the given line are coplanar.
Definition: Line3.hh:279
A three dimensional line segment. The line is defined by a start and end point.
Definition: Line3.hh:35
bool operator!=(const Line3< T > &_line) const
Inequality operator.
Definition: Line3.hh:379
void SetB(const math::Vector3< T > &_ptB)
Set the end point of the line segment.
Definition: Line3.hh:100
Line3< double > Line3d
Definition: Line3.hh:419
Line3(const Line3< T > &_line)
Copy constructor.
Definition: Line3.hh:42
math::Vector3< T > operator[](const size_t _index) const
Get the start or end point.
Definition: Line3.hh:387
void Set(const double _x1, const double _y1, const double _z1, const double _x2, const double _y2, const double _z2)
Set the start and end point of the line segment.
Definition: Line3.hh:128
math::Vector3< T > Direction() const
Get the direction of the line.
Definition: Line3.hh:138
friend std::ostream & operator<<(std::ostream &_out, const Line3< T > &_line)
Stream extraction operator.
Definition: Line3.hh:396
static const size_t IGN_ZERO_SIZE_T
size_t type with a value of 0
Definition: Helpers.hh:227
bool Intersect(const Line3< T > &_line, double _epsilon=1e-6) const
Check if this line intersects the given line segment.
Definition: Line3.hh:267
static const size_t IGN_ONE_SIZE_T
size_t type with a value of 1
Definition: Helpers.hh:230
T Dot(const Vector3< T > &_v) const
Return the dot product of this vector and another vector.
Definition: Vector3.hh:205
bool operator==(const Line3< T > &_line) const
Equality operator.
Definition: Line3.hh:371
T Y() const
Get the y value.
Definition: Vector3.hh:661