MeshLib
 
Loading...
Searching...
No Matches
MRLineSegm.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRMeshFwd.h"
4
5namespace MR
6{
7
8// a segment of straight dimensional line
9template <typename V>
11{
12 using T = typename V::ValueType;
13 V a, b;
14
15 [[nodiscard]] constexpr LineSegm() noexcept = default;
16 [[nodiscard]] constexpr LineSegm( const V & a, const V & b ) noexcept : a( a ), b( b ) { }
17 template <typename U>
18 [[nodiscard]] constexpr explicit LineSegm( const LineSegm<U> & p ) noexcept : a( p.a ), b( p.b ) { }
20 [[nodiscard]] V dir() const { return b - a; }
22 [[nodiscard]] T lengthSq() const { return dir().lengthSq(); }
24 [[nodiscard]] T length() const { return dir().length(); }
26 [[nodiscard]] V operator()( T param ) const { return ( 1 - param ) * a + param * b; }
27};
28
29template <typename V>
30[[nodiscard]] inline bool operator == ( const LineSegm<V> & a, const LineSegm<V> & b )
31{
32 return a.a == b.a && a.b == b.b;
33}
34
35template <typename V>
36[[nodiscard]] V closestPointOnLineSegm( const V& pt, const LineSegm<V> & l )
37{
38 auto ab = l.b - l.a;
39 auto dt = dot( pt - l.a, ab );
40 auto abLengthSq = ab.lengthSq();
41 if ( dt <= 0 )
42 return l.a;
43 if ( dt >= abLengthSq )
44 return l.b;
45 auto ratio = dt / abLengthSq;
46 return l.a * ( 1 - ratio ) + l.b * ratio;
47}
48
51template <typename V>
52[[nodiscard]] bool doSegmentsIntersect( const LineSegm<V> & x, const LineSegm<V> & y,
53 typename V::ValueType * xPos = nullptr, typename V::ValueType * yPos = nullptr )
54{
55 // check whether infinite line x intersect segment y
56 const auto xvec = x.b - x.a;
57 const auto ya = cross( xvec, y.a - x.a );
58 const auto yb = cross( xvec, y.b - x.a );
59 if ( ya * yb > 0 )
60 return false;
61
62 // check whether infinite line y intersect segment x
63 const auto yvec = y.b - y.a;
64 const auto xa = cross( yvec, x.a - y.a );
65 const auto xb = cross( yvec, x.b - y.a );
66 if ( xa * xb > 0 )
67 return false;
68
69 if ( xPos )
70 {
71 // calculates intersection position on segment x
72 const auto denom = xa - xb;
73 *xPos = denom == 0 ? 0 : xa / denom;
74 }
75 if ( yPos )
76 {
77 // calculates intersection position on segment y
78 const auto denom = ya - yb;
79 *yPos = denom == 0 ? 0 : ya / denom;
80 }
81 return true;
82}
83
86template <typename V>
87[[nodiscard]] bool doSegmentLineIntersect( const LineSegm<V> & x, const Line<V> & y,
88 typename V::ValueType * xPos = nullptr, typename V::ValueType * yPos = nullptr )
89{
90 // check whether infinite line y intersect segment x
91 const auto xa = cross( y.d, x.a - y.p );
92 const auto xb = cross( y.d, x.b - y.p );
93 if ( xa * xb > 0 )
94 return false;
95
96 if ( xPos )
97 {
98 // calculates intersection position on segment x
99 const auto denom = xa - xb;
100 *xPos = denom == 0 ? 0 : xa / denom;
101 }
102 if ( yPos )
103 {
104 // calculates intersection position on line y
105 const auto xvec = x.b - x.a;
106 const auto ya = cross( xvec, y.p - x.a );
107 const auto yb = cross( xvec, y.p + y.d - x.a );
108
109 const auto denom = ya - yb;
110 *yPos = denom == 0 ? 0 : ya / denom;
111 }
112 return true;
113}
114
115} //namespace MR
MRMESH_API bool operator==(const BitSet &a, const BitSet &b)
compare that two bit sets have the same set bits (they can be equal even if sizes are distinct but la...
Definition MRCameraOrientationPlugin.h:7
V closestPointOnLineSegm(const V &pt, const LineSegm< V > &l)
Definition MRLineSegm.h:36
bool doSegmentLineIntersect(const LineSegm< V > &x, const Line< V > &y, typename V::ValueType *xPos=nullptr, typename V::ValueType *yPos=nullptr)
Definition MRLineSegm.h:87
bool doSegmentsIntersect(const LineSegm< V > &x, const LineSegm< V > &y, typename V::ValueType *xPos=nullptr, typename V::ValueType *yPos=nullptr)
Definition MRLineSegm.h:52
Definition MRLineSegm.h:11
constexpr LineSegm() noexcept=default
T length() const
returns the length of this line segment
Definition MRLineSegm.h:24
typename V::ValueType T
Definition MRLineSegm.h:12
V a
Definition MRLineSegm.h:13
constexpr LineSegm(const LineSegm< U > &p) noexcept
Definition MRLineSegm.h:18
T lengthSq() const
returns squared length of this line segment
Definition MRLineSegm.h:22
V operator()(T param) const
returns point on the line, where param=0 returns a and param=1 returns b
Definition MRLineSegm.h:26
V dir() const
returns directional vector of the line
Definition MRLineSegm.h:20
V b
Definition MRLineSegm.h:13
Definition MRLine.h:12
V d
Definition MRLine.h:15
V p
Definition MRLine.h:15