17 #ifndef IGNITION_MATH_LINE3_HH_ 18 #define IGNITION_MATH_LINE3_HH_ 34 public:
Line3() =
default;
40 this->pts[0] = _line[0];
41 this->pts[1] = _line[1];
49 this->
Set(_ptA, _ptB);
57 public:
Line3(
const double _x1,
const double _y1,
58 const double _x2,
const double _y2)
60 this->
Set(_x1, _y1, _x2, _y2);
70 public:
Line3(
const double _x1,
const double _y1,
71 const double _z1,
const double _x2,
72 const double _y2,
const double _z2)
74 this->
Set(_x1, _y1, _z1, _x2, _y2, _z2);
109 public:
void Set(
const double _x1,
const double _y1,
110 const double _x2,
const double _y2,
113 this->pts[0].Set(_x1, _y1, _z);
114 this->pts[1].Set(_x2, _y2, _z);
124 public:
void Set(
const double _x1,
const double _y1,
125 const double _z1,
const double _x2,
126 const double _y2,
const double _z2)
128 this->pts[0].Set(_x1, _y1, _z1);
129 this->pts[1].Set(_x2, _y2, _z2);
136 return (this->pts[1] - this->pts[0]).Normalize();
143 return this->pts[0].Distance(this->pts[1]);
156 const double _epsilon = 1e-6)
const 161 if (std::abs(p43.
X()) < _epsilon && std::abs(p43.
Y()) < _epsilon &&
162 std::abs(p43.
Z()) < _epsilon)
169 if (std::abs(p21.
X()) < _epsilon && std::abs(p21.
Y()) < _epsilon &&
170 std::abs(p21.
Z()) < _epsilon)
175 double d1343 = p13.
Dot(p43);
176 double d4321 = p43.
Dot(p21);
177 double d1321 = p13.
Dot(p21);
178 double d4343 = p43.
Dot(p43);
179 double d2121 = p21.
Dot(p21);
181 double denom = d2121 * d4343 - d4321 * d4321;
185 if (std::abs(denom) < _epsilon)
187 double d1 = this->pts[0].Distance(_line[0]);
188 double d2 = this->pts[0].Distance(_line[1]);
190 double d3 = this->pts[1].Distance(_line[0]);
191 double d4 = this->pts[1].Distance(_line[1]);
193 if (d1 <= d2 && d1 <= d3 && d1 <= d4)
195 _result.
SetA(this->pts[0]);
196 _result.
SetB(_line[0]);
198 else if (d2 <= d3 && d2 <= d4)
200 _result.
SetA(this->pts[0]);
201 _result.
SetB(_line[1]);
205 _result.
SetA(this->pts[1]);
206 _result.
SetB(_line[0]);
210 _result.
SetA(this->pts[1]);
211 _result.
SetB(_line[1]);
217 double numer = d1343 * d4321 - d1321 * d4343;
219 double mua =
clamp(numer / denom, 0.0, 1.0);
220 double mub =
clamp((d1343 + d4321 * mua) / d4343, 0.0, 1.0);
222 _result.
Set(this->pts[0] + (p21 * mua), _line[0] + (p43 * mub));
233 double _epsilon = 1e-6)
const 236 return this->
Intersect(_line, ignore, _epsilon);
245 const double _epsilon = 1e-6)
const 247 return std::abs((_line[0] - this->pts[0]).Dot(
248 (this->pts[1] - this->pts[0]).Cross(_line[1] - _line[0])))
258 const double _epsilon = 1e-6)
const 260 return (this->pts[1] - this->pts[0]).Cross(
261 _line[1] - _line[0]).Length() <= _epsilon;
273 double _epsilon = 1e-6)
const 276 if (this->
Parallel(_line, _epsilon))
279 if (this->
Within(_line[0], _epsilon))
285 else if (this->
Within(_line[1], _epsilon))
297 this->
Distance(_line, distLine, _epsilon);
301 if (distLine.
Length() < _epsilon)
317 double _epsilon = 1e-6)
const 319 return _pt.
X() <=
std::max(this->pts[0].X(),
320 this->pts[1].X()) + _epsilon &&
322 this->pts[1].X()) - _epsilon &&
324 this->pts[1].Y()) + _epsilon &&
326 this->pts[1].Y()) - _epsilon &&
328 this->pts[1].Z()) + _epsilon &&
330 this->pts[1].Z()) - _epsilon;
338 return this->pts[0] == _line[0] && this->pts[1] == _line[1];
346 return !(*
this == _line);
362 std::ostream &_out,
const Line3<T> &_line)
364 _out << _line[0] <<
" " << _line[1];
373 this->pts[0] = _line[0];
374 this->pts[1] = _line[1];
T X() const
Get the x value.
Definition: Vector3.hh:635
math::Vector3< T > operator[](const size_t _index) const
Get the start or end point.
Definition: Line3.hh:352
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:124
static const size_t IGN_ONE_SIZE_T
size_t type with a value of 1
Definition: Helpers.hh:216
void SetA(const math::Vector3< T > &_ptA)
Set the start point of the line segment.
Definition: Line3.hh:89
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:316
T Length() const
Get the length of the line.
Definition: Line3.hh:141
static const size_t IGN_ZERO_SIZE_T
size_t type with a value of 0
Definition: Helpers.hh:213
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:244
Line3(const Line3< T > &_line)
Copy constructor.
Definition: Line3.hh:38
bool Intersect(const Line3< T > &_line, double _epsilon=1e-6) const
Check if this line intersects the given line segment.
Definition: Line3.hh:232
T max(const std::vector< T > &_values)
get the maximum value of vector of values
Definition: Helpers.hh:515
Line3< int > Line3i
Definition: Line3.hh:383
Line3()=default
Line Constructor.
Line3< float > Line3f
Definition: Line3.hh:385
bool operator==(const Line3< T > &_line) const
Equality operator.
Definition: Line3.hh:336
friend std::ostream & operator<<(std::ostream &_out, const Line3< T > &_line)
Stream extraction operator.
Definition: Line3.hh:361
void SetB(const math::Vector3< T > &_ptB)
Set the end point of the line segment.
Definition: Line3.hh:96
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:80
T Dot(const Vector3< T > &_v) const
Return the dot product of this vector and another vector.
Definition: Vector3.hh:195
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:155
T Y() const
Get the y value.
Definition: Vector3.hh:642
bool operator!=(const Line3< T > &_line) const
Inequality operator.
Definition: Line3.hh:344
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:257
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:109
Line3(const math::Vector3< T > &_ptA, const math::Vector3< T > &_ptB)
Constructor.
Definition: Line3.hh:47
The Vector3 class represents the generic vector containing 3 elements.
Definition: Vector3.hh:36
math::Vector3< T > Direction() const
Get the direction of the line.
Definition: Line3.hh:134
T Z() const
Get the z value.
Definition: Vector3.hh:649
Line3 & operator=(const Line3< T > &_line)
Assignment operator.
Definition: Line3.hh:371
A three dimensional line segment.
Definition: Line3.hh:31
bool Intersect(const Line3< T > &_line, math::Vector3< T > &_pt, double _epsilon=1e-6) const
Check if this line intersects the given line segment.
Definition: Line3.hh:272
Line3< double > Line3d
Definition: Line3.hh:384
Line3(const double _x1, const double _y1, const double _z1, const double _x2, const double _y2, const double _z2)
Constructor.
Definition: Line3.hh:70
Line3(const double _x1, const double _y1, const double _x2, const double _y2)
2D Constructor where Z coordinates are 0
Definition: Line3.hh:57
T min(const std::vector< T > &_values)
get the minimum value of vector of values
Definition: Helpers.hh:528
T clamp(T _v, T _min, T _max)
Simple clamping function.
Definition: Helpers.hh:392