MeshLib C++ Docs
Loading...
Searching...
No Matches
MRInSphere.h
Go to the documentation of this file.
1#pragma once
2
4#include "MRHighPrecision.h"
5#include "MRPch/MRBindingMacros.h"
6#include <cassert>
7#include <type_traits>
8
9namespace MR
10{
13
14
17
33
40[[nodiscard]] MRMESH_API InSphereResult inSphere( const Vector3i & a, const Vector3i & b, const Vector3i & c,
41 const Vector3i & d, std::int64_t rSq );
42
62[[nodiscard]] MRMESH_API InSphereResult inSphere( const std::array<PreciseVertCoords, 4> & vs, std::int64_t rSq );
63
73template <typename T>
75{
76 static_assert( std::is_floating_point_v<T> );
77public:
83 bool reset( const Vector3<T> & va, const Vector3<T> & vb, const Vector3<T> & vc, T sqRadius )
84 {
85 a = va;
86 rSq = sqRadius;
87 E = -1;
88 u = vb - va;
89 v = vc - va;
90
93 const T uu = u.lengthSq();
94 const T vv = v.lengthSq();
95 if ( uu > 4 * rSq || vv > 4 * rSq || ( v - u ).lengthSq() > 4 * rSq )
96 return false;
97
98 w = cross( u, v );
99 W = w.lengthSq();
100 if ( W <= 0 )
101 return false;
102
104 const T uv = dot( u, v );
105 M = ( vv * ( uu - uv ) ) * u + ( uu * ( vv - uv ) ) * v;
106
108 E = 4 * rSq * W * W - M.lengthSq();
109 return E >= 0;
110 }
111
114 [[nodiscard]] InSphereResult operator()( const Vector3<T> & d ) const
115 {
116 assert( E >= 0 );
117 const auto q = d - a;
118
120 const T qq = q.lengthSq();
121 if ( qq > 4 * rSq )
123
124 const T A = W * qq - dot( q, M );
125 const T t = dot( q, w );
126
128 if ( A < 0 && t >= 0 )
130 if ( A >= 0 && t <= 0 )
131 return ( A == 0 && ( t == 0 || E == 0 ) ) ? InSphereResult::OnSphere : InSphereResult::Outside;
132 const T lhs = A * A * W;
133 const T rhs = E * t * t;
134 if ( lhs == rhs )
136 return ( A < 0 ) == ( lhs > rhs ) ? InSphereResult::Inside : InSphereResult::Outside;
137 }
138
139private:
140 Vector3<T> a;
141 Vector3<T> u, v;
142 Vector3<T> w;
143 T W = 0;
144 Vector3<T> M;
145 T E = -1;
146 T rSq = 0;
147};
148
149
152template <>
153class InSphereTester<int>
154{
155public:
161 MRMESH_API bool reset( const Vector3i & a, const Vector3i & b, const Vector3i & c, std::int64_t rSq );
162
165 [[nodiscard]] MRMESH_API InSphereResult operator()( const Vector3i & d ) const;
166
172 [[nodiscard]] MRMESH_API InSphereResult operator()( const std::array<PreciseVertCoords, 4> & vs ) const;
173
174private:
175 Vector3i a;
176 Vector3i64 u, v;
177 Vector3i64 w;
178 Int256 W;
179 Vector3i256 M;
180 Int512 E = -1;
181 std::int64_t rSq = 0;
182};
183
187
191template <typename T>
192[[nodiscard]] std::enable_if_t<std::is_floating_point_v<T>, InSphereResult> inSphere( const Vector3<T> & a, const Vector3<T> & b, const Vector3<T> & c,
193 const Vector3<T> & d, T rSq )
194{
195 InSphereTester<T> tester;
196 if ( !tester.reset( a, b, c, rSq ) )
198 return tester( d );
199}
200
201MR_BIND_TEMPLATE( InSphereResult inSphere( const Vector3f & a, const Vector3f & b, const Vector3f & c, const Vector3f & d, float rSq ) );
202MR_BIND_TEMPLATE( InSphereResult inSphere( const Vector3d & a, const Vector3d & b, const Vector3d & c, const Vector3d & d, double rSq ) );
203
205
206}
#define MRMESH_API
Definition MRMeshFwd.h:82
#define M(T)
Definition MRInSphere.h:75
bool reset(const Vector3i &a, const Vector3i &b, const Vector3i &c, std::int64_t rSq)
auto dot(const Matrix2< T > &a, const Matrix2< T > &b) -> decltype(dot(a.x, b.x))
double-dot product: x = a : b
Definition MRMatrix2.h:142
InSphereResult operator()(const std::array< PreciseVertCoords, 4 > &vs) const
InSphereResult operator()(const Vector3i &d) const
bool reset(const Vector3< T > &va, const Vector3< T > &vb, const Vector3< T > &vc, T sqRadius)
Definition MRInSphere.h:83
InSphereResult operator()(const Vector3< T > &d) const
Definition MRInSphere.h:114
MR_BIND_TEMPLATE(std::pair< Vector3f, TriPointf > closestPointInTriangle(const Vector3f &p, const Vector3f &a, const Vector3f &b, const Vector3f &c))
T lengthSq() const
Definition MRVector3.h:68
boost::multiprecision::int512_t Int512
Definition MRHighPrecision.h:28
boost::multiprecision::int256_t Int256
Definition MRHighPrecision.h:27
Vector3< Int256 > Vector3i256
Definition MRHighPrecision.h:41
InSphereTester< int > InSphereTesteri
Definition MRInSphere.h:186
InSphereTester< double > InSphereTesterd
Definition MRInSphere.h:185
InSphereTester< float > InSphereTesterf
Definition MRInSphere.h:184
InSphereResult inSphere(const Vector3i &a, const Vector3i &b, const Vector3i &c, const Vector3i &d, std::int64_t rSq)
InSphereResult
the result of inSphere predicates
Definition MRInSphere.h:20
@ NoSphere
Definition MRInSphere.h:23
@ Outside
the sphere exists, and the point D is strictly outside
Definition MRInSphere.h:25
@ Inside
the sphere exists, and the point D is strictly inside
Definition MRInSphere.h:31
@ OnSphere
Definition MRInSphere.h:29
only for bindings generation
Definition MRCameraOrientationPlugin.h:8
T cross(const Vector2< T > &a, const Vector2< T > &b)
cross product
Definition MRVector2.h:160
Definition MRVector3.h:33