MeshLib C++ Docs
Loading...
Searching...
No Matches
MRFeatures.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRMesh/MRAffineXf.h"
4#include "MRMesh/MRCone3.h"
6#include "MRMesh/MRPlane3.h"
7#include "MRMesh/MRSphere.h"
8#include "MRMesh/MRVector3.h"
9
10#include <cassert>
11#include <optional>
12#include <variant>
13
14namespace MR
15{
16class FeatureObject;
17}
18
19namespace MR::Features
20{
21
22namespace Primitives
23{
24 struct Plane;
25 struct ConeSegment;
26
27 // ---
28
29 // Doubles as a point when the radius is zero.
31
32 struct Plane
33 {
34 Vector3f center;
35
36 // This must be normalized. The sign doesn't matter.
37 Vector3f normal = Vector3f( 1, 0, 0 );
38
39 // Returns an infinite line, with the center in a sane location.
40 [[nodiscard]] MRMESH_API ConeSegment intersectWithPlane( const Plane& other ) const;
41
42 // Intersects the plane with a line, returns a point (zero radius sphere).
43 // Only `center` and `dir` are used from `line` (so if `line` is a cone/cylinder, its axis is used,
44 // and the line is extended to infinity).
45 [[nodiscard]] MRMESH_API Sphere intersectWithLine( const ConeSegment& line ) const;
46
47 friend bool operator==( const Plane&, const Plane& ) = default;
48 };
49
54 {
55 // Sanity requirements:
56 // * `dir` must be normalized.
57 // * Both `positiveLength` and `negativeLength` should be non-negative. They can be infinite (both or individually).
58 // * If they are equal (both zero) or at least one of them is infinite, `positiveSideRadius` must be equal to `negativeSideRadius`.
59 // * Both `positiveSideRadius` and `negativeSideRadius` must be non-negative.
60
64 Vector3f dir;
65
70
72 float positiveLength = 0;
74 float negativeLength = 0;
75
76 // If true, the cone has no caps and no volume, and all distances (to the conical surface, that is) are positive.
77 bool hollow = false;
78
79 friend bool operator==( const ConeSegment&, const ConeSegment& ) = default;
80
81 [[nodiscard]] bool isZeroRadius() const { return positiveSideRadius == 0 && negativeSideRadius == 0; }
82 [[nodiscard]] bool isCircle() const { return positiveLength == -negativeLength && std::isfinite( positiveLength ); }
83
84 // Returns the length. Can be infinite.
85 [[nodiscard]] float length() const { return positiveLength + negativeLength; }
86
87 // Returns the center point (unlike `referencePoint`, which can actually be off-center).
88 // For half-infinite objects, returns the finite end.
89 [[nodiscard]] MRMESH_API Sphere centerPoint() const;
90
91 // Extends the object to infinity in one direction. The radius in the extended direction becomes equal to the radius in the opposite direction.
92 [[nodiscard]] MRMESH_API ConeSegment extendToInfinity( bool negative ) const;
93 // Extends the object to infinity in both directions. This is equivalent to `.extendToInfinity(false).extendToInfinity(true)`,
94 // except that calling it with `positiveSideRadius != negativeSideRadius` is illegal and triggers an assertion.
96
97 // Untruncates a truncated cone. If it's not a cone at all, returns the object unchanged and triggers an assertion.
98 [[nodiscard]] MRMESH_API ConeSegment untruncateCone() const;
99
100 // Returns a finite axis. For circles, you might want to immediately `extendToInfinity()` it.
101 [[nodiscard]] MRMESH_API ConeSegment axis() const;
102
103 // Returns a center of one of the two base circles.
104 [[nodiscard]] MRMESH_API Sphere basePoint( bool negative ) const;
105 // Returns one of the two base planes.
106 [[nodiscard]] MRMESH_API Plane basePlane( bool negative ) const;
107 // Returns one of the two base circles.
108 [[nodiscard]] MRMESH_API ConeSegment baseCircle( bool negative ) const;
109 };
110
111 using Variant = std::variant<Sphere, ConeSegment, Plane>;
112}
113
114// Those map various MR types to our primitives. Some of those are identity functions.
115
116[[nodiscard]] inline Primitives::Sphere toPrimitive( const Vector3f& point ) { return { point, 0 }; }
117[[nodiscard]] inline Primitives::Sphere toPrimitive( const Sphere3f& sphere ) { return sphere; }
118
119[[nodiscard]] MRMESH_API Primitives::ConeSegment toPrimitive( const Line3f& line );
120[[nodiscard]] MRMESH_API Primitives::ConeSegment toPrimitive( const LineSegm3f& segm );
121
122[[nodiscard]] inline Primitives::ConeSegment toPrimitive( const Cylinder3f& cyl )
123{
124 float halfLen = cyl.length / 2;
125 return{
126 .referencePoint = cyl.center(),
127 .dir = cyl.direction().normalized(),
128 .positiveSideRadius = cyl.radius, .negativeSideRadius = cyl.radius,
129 .positiveLength = halfLen, .negativeLength = halfLen,
130 };
131}
132[[nodiscard]] inline Primitives::ConeSegment toPrimitive( const Cone3f& cone )
133{
134 return{
135 .referencePoint = cone.center(),
136 .dir = cone.direction().normalized(),
137 .positiveSideRadius = std::tan( cone.angle ) * cone.height, .negativeSideRadius = 0,
138 .positiveLength = cone.height, .negativeLength = 0,
139 };
140}
141
143[[nodiscard]] MRMESH_API Primitives::ConeSegment primitiveCircle( const Vector3f& point, const Vector3f& normal, float rad );
145[[nodiscard]] MRMESH_API Primitives::ConeSegment primitiveCylinder( const Vector3f& a, const Vector3f& b, float rad );
147[[nodiscard]] MRMESH_API Primitives::ConeSegment primitiveCone( const Vector3f& a, const Vector3f& b, float rad );
148
149// Returns null if the object type is unknown. This overload ignores the parent xf.
150[[nodiscard]] MRMESH_API std::optional<Primitives::Variant> primitiveFromObject( const Object& object );
151// Returns null if the object type is unknown. This overload respects the parent's `worldXf()`.
152[[nodiscard]] MRMESH_API std::optional<Primitives::Variant> primitiveFromObjectWithWorldXf( const Object& object );
153// Can return null on some primitive configurations.
154// `infiniteExtent` is how large we make "infinite" objects. Half-infinite objects divide this by 2.
155[[nodiscard]] MRMESH_API std::shared_ptr<FeatureObject> primitiveToObject( const Primitives::Variant& primitive, float infiniteExtent );
156
157// Transform a primitive by an xf.
158// Non-uniform scaling and skewing are not supported.
159[[nodiscard]] MRMESH_API Primitives::Sphere transformPrimitive( const AffineXf3f& xf, const Primitives::Sphere& primitive );
160[[nodiscard]] MRMESH_API Primitives::Plane transformPrimitive( const AffineXf3f& xf, const Primitives::Plane& primitive );
161[[nodiscard]] MRMESH_API Primitives::ConeSegment transformPrimitive( const AffineXf3f& xf, const Primitives::ConeSegment& primitive );
162[[nodiscard]] MRMESH_API Primitives::Variant transformPrimitive( const AffineXf3f& xf, const Primitives::Variant& primitive );
163
166{
167 enum class Status
168 {
169 ok = 0,
178 notFinite,
179 };
180
182 {
184 [[nodiscard]] operator bool() const { return status == Status::ok; }
185 };
186
188 {
189 // This is a separate field because it can be negative.
190 float distance = 0;
191
194
195 [[nodiscard]] Vector3f closestPointFor( bool b ) const { return b ? closestPointB : closestPointA; }
196 };
197 // Exact distance.
199
200 // Some approximation of the distance.
201 // For planes and lines, this expects them to be mostly parallel. For everything else, it just takes the feature center.
203
205 {
206 Vector3f pointA;
207 Vector3f pointB;
208 [[nodiscard]] Vector3f pointFor( bool b ) const { return b ? pointB : pointA; }
209
210 Vector3f dirA; // Normalized.
211 Vector3f dirB; // ^
212 [[nodiscard]] Vector3f dirFor( bool b ) const { return b ? dirB : dirA; }
213
215 bool isSurfaceNormalA = false;
216 bool isSurfaceNormalB = false;
217
218 [[nodiscard]] bool isSurfaceNormalFor( bool b ) const { return b ? isSurfaceNormalB : isSurfaceNormalA; }
219
220 [[nodiscard]] MRMESH_API float computeAngleInRadians() const;
221 };
223
224 // The primitives obtained from intersecting those two.
225 std::vector<Primitives::Variant> intersections;
226
227 // Modifies the object to swap A and B;
229};
230// `MeasureResult::Status` enum to string.
231[[nodiscard]] MRMESH_API std::string_view toString( MeasureResult::Status status );
232
234namespace Traits
235{
236
237template <typename T>
238struct Unary {};
239template <>
240struct Unary<Primitives::Sphere>
241{
242 MRMESH_API std::string name( const Primitives::Sphere& prim ) const;
243};
244template <>
245struct Unary<Primitives::ConeSegment>
246{
247 MRMESH_API std::string name( const Primitives::ConeSegment& prim ) const;
248};
249template <>
250struct Unary<Primitives::Plane>
251{
252 MRMESH_API std::string name( const Primitives::Plane& prim ) const;
253};
254
255template <typename A, typename B>
256struct Binary {};
257
260template <typename A, typename B>
261concept MeasureSupportedOneWay = requires( const Binary<A, B>& t, const A& a, const B& b )
262{
263 { t.measure( a, b ) } -> std::same_as<MeasureResult>;
264};
265
266// ?? <-> Sphere
267template <>
272template <>
273struct Binary<Primitives::ConeSegment, Primitives::Sphere>
274{
276};
277template <>
278struct Binary<Primitives::Plane, Primitives::Sphere>
279{
281};
282
283// ?? <-> Cone
284template <>
285struct Binary<Primitives::ConeSegment, Primitives::ConeSegment>
286{
288};
289template <>
294
295// ?? <-> Plane
296template <>
297struct Binary<Primitives::Plane, Primitives::Plane>
298{
300};
301
302}
303
304// Get name of a `Primitives::...` class (can depend on its parameters).
305template <typename T>
306[[nodiscard]] std::string name( const T& primitive )
307{
308 return Traits::Unary<T>{}.name( primitive );
309}
310// Same but for a variant.
311[[nodiscard]] MRMESH_API std::string name( const Primitives::Variant& var );
312
313// Whether you can measure two primitives relative to one another.
314template <typename A, typename B>
316
317// Measures stuff between two primitives. (Two types from `Primitives::...`.)
318template <typename A, typename B>
320[[nodiscard]] MeasureResult measure( const A& a, const B& b )
321{
323 {
324 MeasureResult ret = Traits::Binary<A, B>{}.measure( a, b );
325
326 for ( auto* dist : { &ret.distance, &ret.centerDistance } )
327 {
328 // Catch non-finite distance.
329 if ( *dist && ( !std::isfinite( dist->distance ) || !dist->closestPointA.isFinite() || !dist->closestPointB.isFinite() ) )
331
332 // Check that we got the correct distance here.
333 // Note that the distance is signed, so we apply `abs` to it to compare it properly.
334 if ( *dist )
335 {
336 assert( [&]{
337 float a = ( dist->closestPointB - dist->closestPointA ).length();
338 float b = std::abs( dist->distance );
339 return std::abs( a - b ) <= std::max( std::min( a, b ), 0.01f ) * 0.001f;
340 }() );
341 }
342 }
343
344 // Catch non-finite angle.
345 if ( ret.angle && ( !ret.angle.pointA.isFinite() || !ret.angle.pointB.isFinite() || !ret.angle.dirA.isFinite() || !ret.angle.dirB.isFinite() ) )
347
348 // Check that the angle normals are normalized.
349 assert( ret.angle <= ( std::abs( 1 - ret.angle.dirA.length() ) < 0.0001f ) );
350 assert( ret.angle <= ( std::abs( 1 - ret.angle.dirB.length() ) < 0.0001f ) );
351
352 return ret;
353 }
354 else
355 {
356 static_assert( Traits::MeasureSupportedOneWay<B, A>, "This should never fail." );
357 MeasureResult ret = ( measure )( b, a );
358 ret.swapObjects();
359 return ret;
360 }
361}
362// Same, but with a variant as the first argument.
363template <typename B>
364[[nodiscard]] MeasureResult measure( const Primitives::Variant& a, const B& b )
365{
366 return std::visit( [&]( const auto& elem ){ return (measure)( elem, b ); }, a );
367}
368// Same, but with a variant as the second argument.
369template <typename A>
370[[nodiscard]] MeasureResult measure( const A& a, const Primitives::Variant& b )
371{
372 return std::visit( [&]( const auto& elem ){ return (measure)( a, elem ); }, b );
373}
374// Same, but with variants as both argument.
376
377}
#define MRMESH_API
Definition MRMesh/MRMeshFwd.h:79
length
Definition MRObjectDimensionsEnum.h:14
named object in the data model
Definition MRObject.h:60
Definition MRFeatures.h:315
std::variant< Sphere, ConeSegment, Plane > Variant
Definition MRFeatures.h:111
Definition MRFeatures.h:20
std::string name(const T &primitive)
Definition MRFeatures.h:306
MRMESH_API Primitives::ConeSegment primitiveCylinder(const Vector3f &a, const Vector3f &b, float rad)
a and b are centers of the sides.
MeasureResult measure(const A &a, const B &b)
Definition MRFeatures.h:320
MRMESH_API std::shared_ptr< FeatureObject > primitiveToObject(const Primitives::Variant &primitive, float infiniteExtent)
MRMESH_API Primitives::ConeSegment primitiveCircle(const Vector3f &point, const Vector3f &normal, float rad)
normal doesn't need to be normalized.
MRMESH_API std::string_view toString(MeasureResult::Status status)
MRMESH_API Primitives::Sphere transformPrimitive(const AffineXf3f &xf, const Primitives::Sphere &primitive)
MRMESH_API Primitives::ConeSegment primitiveCone(const Vector3f &a, const Vector3f &b, float rad)
a is the center of the base, b is the pointy end.
MRMESH_API std::optional< Primitives::Variant > primitiveFromObjectWithWorldXf(const Object &object)
Primitives::Sphere toPrimitive(const Vector3f &point)
Definition MRFeatures.h:116
MRMESH_API std::optional< Primitives::Variant > primitiveFromObject(const Object &object)
Definition MRCameraOrientationPlugin.h:8
Cylinder3f
Definition MRMesh/MRMeshFwd.h:301
Cone3f
Definition MRMesh/MRMeshFwd.h:306
Definition MRFeatures.h:205
Vector3f pointA
Definition MRFeatures.h:206
Vector3f dirFor(bool b) const
Definition MRFeatures.h:212
MRMESH_API float computeAngleInRadians() const
Vector3f pointB
Definition MRFeatures.h:207
Vector3f dirA
Definition MRFeatures.h:210
bool isSurfaceNormalA
Whether dir{A,B} is a surface normal or a line direction.
Definition MRFeatures.h:215
Vector3f pointFor(bool b) const
Definition MRFeatures.h:208
bool isSurfaceNormalFor(bool b) const
Definition MRFeatures.h:218
Vector3f dirB
Definition MRFeatures.h:211
bool isSurfaceNormalB
Definition MRFeatures.h:216
Definition MRFeatures.h:182
Status status
Definition MRFeatures.h:183
Definition MRFeatures.h:188
Vector3f closestPointA
Definition MRFeatures.h:192
Vector3f closestPointFor(bool b) const
Definition MRFeatures.h:195
Vector3f closestPointB
Definition MRFeatures.h:193
float distance
Definition MRFeatures.h:190
Stores the results of measuring two objects relative to one another.
Definition MRFeatures.h:166
MRMESH_API void swapObjects()
Distance centerDistance
Definition MRFeatures.h:202
Angle angle
Definition MRFeatures.h:222
Distance distance
Definition MRFeatures.h:198
Status
Definition MRFeatures.h:168
@ badRelativeLocation
Can't be computed because of how the objects are located relative to each other.
@ notImplemented
Algorithms set this if this when something isn't yet implemented.
@ notFinite
The result was not finite. This is set automatically if you return non-finite values,...
std::vector< Primitives::Variant > intersections
Definition MRFeatures.h:225
Definition MRFeatures.h:54
friend bool operator==(const ConeSegment &, const ConeSegment &)=default
float length() const
Definition MRFeatures.h:85
MRMESH_API Sphere basePoint(bool negative) const
MRMESH_API ConeSegment extendToInfinity() const
Vector3f referencePoint
Some point on the axis, but not necessarily the true center point. Use centerPoint() for that.
Definition MRFeatures.h:62
MRMESH_API ConeSegment baseCircle(bool negative) const
MRMESH_API ConeSegment untruncateCone() const
float positiveSideRadius
Cap radius in the dir direction.
Definition MRFeatures.h:67
Vector3f dir
The axis direction. Must be normalized.
Definition MRFeatures.h:64
MRMESH_API Sphere centerPoint() const
float negativeLength
Distance from the center to the cap in the direction opposite to dir.
Definition MRFeatures.h:74
MRMESH_API Plane basePlane(bool negative) const
float negativeSideRadius
Cap radius in the direction opposite to dir.
Definition MRFeatures.h:69
MRMESH_API ConeSegment axis() const
float positiveLength
Distance from the center to the cap in the dir direction.
Definition MRFeatures.h:72
bool isZeroRadius() const
Definition MRFeatures.h:81
bool hollow
Definition MRFeatures.h:77
MRMESH_API ConeSegment extendToInfinity(bool negative) const
bool isCircle() const
Definition MRFeatures.h:82
Definition MRFeatures.h:33
Vector3f center
Definition MRFeatures.h:34
MRMESH_API Sphere intersectWithLine(const ConeSegment &line) const
MRMESH_API ConeSegment intersectWithPlane(const Plane &other) const
friend bool operator==(const Plane &, const Plane &)=default
Vector3f normal
Definition MRFeatures.h:37
MRMESH_API MeasureResult measure(const Primitives::ConeSegment &a, const Primitives::ConeSegment &b) const
MRMESH_API MeasureResult measure(const Primitives::ConeSegment &a, const Primitives::Sphere &b) const
MRMESH_API MeasureResult measure(const Primitives::Plane &a, const Primitives::ConeSegment &b) const
MRMESH_API MeasureResult measure(const Primitives::Plane &a, const Primitives::Plane &b) const
MRMESH_API MeasureResult measure(const Primitives::Plane &a, const Primitives::Sphere &b) const
MRMESH_API MeasureResult measure(const Primitives::Sphere &a, const Primitives::Sphere &b) const
Definition MRFeatures.h:256
MRMESH_API std::string name(const Primitives::ConeSegment &prim) const
MRMESH_API std::string name(const Primitives::Plane &prim) const
MRMESH_API std::string name(const Primitives::Sphere &prim) const
Definition MRFeatures.h:238
Definition MRSphere.h:9
V center
Definition MRSphere.h:12