23 T errorLimit = std::numeric_limits<T>::epsilon() * T( 20 ) )
25 const auto crossDir = cross( plane1.
n, plane2.
n );
27 if ( crossDir.lengthSq() < errorLimit * errorLimit )
31 const auto point = matrix.inverse() *
Vector3<T>( plane1.
d, plane2.
d, 0 );
33 return Line3<T>( point, crossDir.normalized() );
41 T errorLimit = std::numeric_limits<T>::epsilon() * T( 20 ) )
43 const auto den = dot( plane.
n, line.d );
44 if ( std::abs(den) < errorLimit )
46 return line.p + ( plane.
d - dot( plane.
n, line.p ) ) / den * line.d;
54 T errorLimit = std::numeric_limits<T>::epsilon() * T( 20 ) )
56 const auto crossDir = cross( line1.
d, line2.
d );
57 if ( crossDir.lengthSq() < errorLimit * errorLimit )
60 const auto p1 = dot( crossDir, line1.
p );
61 const auto p2 = dot( crossDir, line2.
p );
62 if ( std::abs( p1 - p2 ) >= errorLimit )
65 const auto n2 = cross( line2.
d, crossDir );
66 const T den = dot( line1.
d, n2 );
69 return line1.
p + dot( ( line2.
p - line1.
p ), n2 ) / den * line1.
d;
74inline std::optional<Vector2f>
intersection(
const LineSegm2f& segm1,
const LineSegm2f& segm2 )
76 auto avec = segm1.b - segm1.a;
77 if ( cross( avec, segm2.a - segm1.a ) * cross( segm2.b - segm1.a, avec ) <= 0 )
79 auto bvec = segm2.b - segm2.a;
80 auto cda = cross( bvec, segm1.a - segm2.a );
81 auto cbd = cross( segm1.b - segm2.a, bvec );
84 return ( segm1.b * cda + segm1.a * cbd ) / ( cda + cbd );
91 T errorLimit = std::numeric_limits<T>::epsilon() * T( 20 ) )
93 const auto crossDir = cross( plane1.
n, plane2.
n );
95 if ( crossDir.lengthSq() >= errorLimit * errorLimit )
98 return ( plane2.
n * plane2.
d - plane1.
n * plane1.
d ).lengthSq();
105 T errorLimit = std::numeric_limits<T>::epsilon() * T( 20 ) )
107 std::optional<T> res =
distanceSq( plane1, plane2, errorLimit );
109 *res = std::sqrt( *res );
117 T errorLimit = std::numeric_limits<T>::epsilon() * T( 20 ) )
119 const auto den = dot( plane.
n, line.d );
120 if ( std::abs( den ) >= errorLimit )
123 return std::abs( dot( line.p, plane.
n ) - plane.
d );
132 const auto d11 = line1.
d.lengthSq();
133 const auto d12 = dot( line1.
d, line2.
d );
134 const auto d22 = line2.
d.lengthSq();
135 const auto det = d12 * d12 - d11 * d22;
139 return { line1.
p, line2.
project( line1.
p ) };
142 const auto dp = line2.
p - line1.
p;
143 const auto x = dot( dp, line1.
d ) / det;
144 const auto y = dot( dp, line2.
d ) / det;
145 const auto a = d12 * y - d22 * x;
146 const auto b = d11 * y - d12 * x;
147 return { line1( a ), line2( b ) };
156 const auto d11 = ln.
d.lengthSq();
157 const auto d12 = dot( ln.
d, ls.
dir() );
159 const auto det = d12 * d12 - d11 * d22;
163 const auto dp = ls.
a - ln.
p;
164 const auto x = dot( dp, ln.
d ) / det;
165 const auto y = dot( dp, ls.
dir() ) / det;
166 const auto b = d11 * y - d12 * x;
171 const auto a = d12 * y - d22 * x;
172 return { ln( a ), ls( b ) };
180 const auto dd = line.d.lengthSq();
187 const auto rdd = 1 / dd;
189 T bestDistSq = std::numeric_limits<T>::max();
191 static constexpr int otherDir[3][2] = { { 1, 2 }, { 2, 0 }, { 0, 1 } };
192 for (
int iDir = 0; iDir < 3; ++iDir )
197 const int iDir1 = otherDir[iDir][0];
198 const int iDir2 = otherDir[iDir][1];
200 q[1][iDir2] = box.
max[iDir2];
202 q[2][iDir1] = box.
max[iDir1];
203 q[2][iDir2] = box.
max[iDir2];
205 q[3][iDir1] = box.
max[iDir1];
208 const auto e = box.
max[iDir] - box.
min[iDir];
209 const auto ee = e * e;
210 const auto db = line.d[iDir] * e;
211 const auto denom = dd * ee - db * db;
212 const bool par = denom <= 0;
213 const auto rdenom = par ? 0 : 1 / denom;
214 for (
int j = 0; j < 4; ++j )
220 cand.
a[iDir] = q[j][iDir];
225 const auto s = q[j] - line.p;
226 const auto dt = dot( line.d, s );
227 const auto bt = s[iDir] * e;
230 const auto t = ( dt * ee - bt * db ) * rdenom;
231 assert( !std::isnan( t ) );
234 const auto u = ( t * db - bt ) / ee;
235 assert( !std::isnan( u ) );
239 cand.
a = line( dt * rdd );
244 cand.
a = line( ( db + dt ) * rdd );
246 cand.
b[iDir] = box.
max[iDir];
252 cand.
b[iDir] += e * u;
255 const auto distSq = cand.
lengthSq();
256 if ( distSq < bestDistSq )
271 using T =
typename V::ValueType;
272 std::optional<std::pair<T,T>> res;
273 const auto p = line.p - sphere.
center;
274 const auto d = line.d;
275 const auto dd = dot( d, d );
276 const auto pd = dot( p, d );
277 const auto des4 =
sqr( pd ) - dd * ( dot( p, p ) -
sqr( sphere.
radius ) );
280 const auto sqrtDes4 = std::sqrt( des4 );
282 res->first = ( -sqrtDes4 - pd ) / dd;
283 res->second = ( sqrtDes4 - pd ) / dd;
std::optional< Line3< T > > intersection(const Plane3< T > &plane1, const Plane3< T > &plane2, T errorLimit=std::numeric_limits< T >::epsilon() *T(20))
Definition MRIntersection.h:22
std::optional< T > distanceSq(const Plane3< T > &plane1, const Plane3< T > &plane2, T errorLimit=std::numeric_limits< T >::epsilon() *T(20))
Definition MRIntersection.h:90
std::optional< T > distance(const Plane3< T > &plane1, const Plane3< T > &plane2, T errorLimit=std::numeric_limits< T >::epsilon() *T(20))
Definition MRIntersection.h:104
LineSegm3< T > closestPoints(const Line3< T > &line1, const Line3< T > &line2)
Definition MRIntersection.h:130
Definition MRCameraOrientationPlugin.h:8
constexpr T sqr(T x) noexcept
Definition MRMesh/MRMeshFwd.h:600
Line< Vector3< T > > Line3
Definition MRMesh/MRMeshFwd.h:239
MRMESH_CLASS Vector3
Definition MRMesh/MRMeshFwd.h:137
Box given by its min- and max- corners.
Definition MRMesh/MRBox.h:25
V getBoxClosestPointTo(const V &pt) const
returns closest point in the box to given point
Definition MRMesh/MRBox.h:111
V max
Definition MRMesh/MRBox.h:31
V min
Definition MRMesh/MRBox.h:31
Definition MRLineSegm.h:11
V a
Definition MRLineSegm.h:13
T lengthSq() const
returns squared length of this line segment
Definition MRLineSegm.h:22
V dir() const
returns directional vector of the line
Definition MRLineSegm.h:20
V b
Definition MRLineSegm.h:13
V d
Definition MRLine.h:15
V p
Definition MRLine.h:15
V project(const V &x) const
finds the closest point on line
Definition MRLine.h:37
Definition MRMesh/MRMatrix3.h:13
Vector3< T > n
Definition MRPlane3.h:13
T d
Definition MRPlane3.h:14
V center
Definition MRSphere.h:12
T radius
Definition MRSphere.h:13
Definition MRMesh/MRVector3.h:19