MeshLib Documentation
Loading...
Searching...
No Matches
MRFeatures.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRMesh/MRCone3.h"
5#include "MRMesh/MRLine3.h"
7#include "MRMesh/MRPlane3.h"
8#include "MRMesh/MRSphere.h"
9#include "MRMesh/MRVector3.h"
10
11#include <cassert>
12#include <optional>
13#include <variant>
14
15namespace MR
16{
17class FeatureObject;
18}
19
20namespace MR::Features
21{
22
23namespace Primitives
24{
25 struct Plane;
26 struct ConeSegment;
27
28 // ---
29
30 // Doubles as a point when the radius is zero.
32
33 struct Plane
34 {
35 Vector3f center;
36
37 // This must be normalized. The sign doesn't matter.
38 Vector3f normal = Vector3f( 1, 0, 0 );
39
40 // Returns an infinite line, with the center in a sane location.
41 [[nodiscard]] MRMESH_API ConeSegment intersectWithPlane( const Plane& other ) const;
42
43 // Intersects the plane with a line, returns a point (zero radius sphere).
44 // Only `center` and `dir` are used from `line` (so if `line` is a cone/cylinder, its axis is used,
45 // and the line is extended to infinity).
46 [[nodiscard]] MRMESH_API Sphere intersectWithLine( const ConeSegment& line ) const;
47
48 friend bool operator==( const Plane&, const Plane& ) = default;
49 };
50
55 {
56 // Sanity requirements:
57 // * `dir` must be normalized.
58 // * Both `positiveLength` and `negativeLength` should be non-negative. They can be infinite (both or individually).
59 // * If they are equal (both zero) or at least one of them is infinite, `positiveSideRadius` must be equal to `negativeSideRadius`.
60 // * Both `positiveSideRadius` and `negativeSideRadius` must be non-negative.
61
62 // Some point on the axis, but not necessarily the true center point. Use `centerPoint()` for that.
64 Vector3f dir; //< Must be normalized.
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]] inline Primitives::ConeSegment toPrimitive( const Line3f& line ) { return { .referencePoint = line.p, .dir = line.d.normalized(), .positiveLength = INFINITY, .negativeLength = INFINITY }; }
120[[nodiscard]] inline Primitives::ConeSegment toPrimitive( const LineSegm3f& segm ) { return { .referencePoint = segm.a, .dir = segm.dir().normalized(), .positiveLength = segm.length() }; }
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.
150[[nodiscard]] MRMESH_API std::optional<Primitives::Variant> primitiveFromObject( const Object& object );
151// Can return null on some primitive configurations.
152// `infiniteExtent` is how large we make "infinite" objects. Half-infinite objects divide this by 2.
153[[nodiscard]] MRMESH_API std::shared_ptr<FeatureObject> primitiveToObject( const Primitives::Variant& primitive, float infiniteExtent );
154
157{
158 enum class Status
159 {
160 ok = 0,
169 notFinite,
170 };
171
173 {
175 [[nodiscard]] operator bool() const { return status == Status::ok; }
176 };
177
179 {
180 // This is a separate field because it can be negative.
181 float distance = 0;
182
185
186 [[nodiscard]] Vector3f closestPointFor( bool b ) const { return b ? closestPointB : closestPointA; }
187 };
188 // Exact distance.
190
191 // Some approximation of the distance.
192 // For planes and lines, this expects them to be mostly parallel. For everything else, it just takes the feature center.
194
196 {
197 Vector3f pointA;
198 Vector3f pointB;
199 [[nodiscard]] Vector3f pointFor( bool b ) const { return b ? pointB : pointA; }
200
201 Vector3f dirA; // Normalized.
202 Vector3f dirB; // ^
203 [[nodiscard]] Vector3f dirFor( bool b ) const { return b ? dirB : dirA; }
204
206 bool isSurfaceNormalA = false;
207 bool isSurfaceNormalB = false;
208
209 [[nodiscard]] bool isSurfaceNormalFor( bool b ) const { return b ? isSurfaceNormalB : isSurfaceNormalA; }
210
211 [[nodiscard]] MRMESH_API float computeAngleInRadians() const;
212 };
214
215 // The primitives obtained from intersecting those two.
216 std::vector<Primitives::Variant> intersections;
217
218 // Modifies the object to swap A and B;
220};
221// `MeasureResult::Status` enum to string.
222[[nodiscard]] MRMESH_API std::string_view toString( MeasureResult::Status status );
223
225namespace Traits
226{
227
228template <typename T>
229struct Unary {};
230template <>
231struct Unary<Primitives::Sphere>
232{
233 MRMESH_API std::string name( const Primitives::Sphere& prim ) const;
234};
235template <>
236struct Unary<Primitives::ConeSegment>
237{
238 MRMESH_API std::string name( const Primitives::ConeSegment& prim ) const;
239};
240template <>
241struct Unary<Primitives::Plane>
242{
243 MRMESH_API std::string name( const Primitives::Plane& prim ) const;
244};
245
246template <typename A, typename B>
247struct Binary {};
248
251template <typename A, typename B>
252concept MeasureSupportedOneWay = requires( const Binary<A, B>& t, const A& a, const B& b )
253{
254 { t.measure( a, b ) } -> std::same_as<MeasureResult>;
255};
256
257// ?? <-> Sphere
258template <>
263template <>
264struct Binary<Primitives::ConeSegment, Primitives::Sphere>
265{
267};
268template <>
269struct Binary<Primitives::Plane, Primitives::Sphere>
270{
272};
273
274// ?? <-> Cone
275template <>
276struct Binary<Primitives::ConeSegment, Primitives::ConeSegment>
277{
279};
280template <>
285
286// ?? <-> Plane
287template <>
288struct Binary<Primitives::Plane, Primitives::Plane>
289{
291};
292
293}
294
295// Get name of a `Primitives::...` class (can depend on its parameters).
296template <typename T>
297[[nodiscard]] std::string name( const T& primitive )
298{
299 return Traits::Unary<T>{}.name( primitive );
300}
301// Same but for a variant.
302[[nodiscard]] MRMESH_API std::string name( const Primitives::Variant& var );
303
304// Whether you can measure two primitives relative to one another.
305template <typename A, typename B>
307
308// Measures stuff between two primitives. (Two types from `Primitives::...`.)
309template <typename A, typename B>
311[[nodiscard]] MeasureResult measure( const A& a, const B& b )
312{
314 {
315 MeasureResult ret = Traits::Binary<A, B>{}.measure( a, b );
316
317 for ( auto* dist : { &ret.distance, &ret.centerDistance } )
318 {
319 // Catch non-finite distance.
320 if ( *dist && ( !std::isfinite( dist->distance ) || !dist->closestPointA.isFinite() || !dist->closestPointB.isFinite() ) )
322
323 // Check that we got the correct distance here.
324 // Note that the distance is signed, so we apply `abs` to it to compare it properly.
325 assert( *dist <= ( std::abs( ( dist->closestPointB - dist->closestPointA ).length() - std::abs( dist->distance ) ) < 0.0001f ) );
326 }
327
328 // Catch non-finite angle.
329 if ( ret.angle && ( !ret.angle.pointA.isFinite() || !ret.angle.pointB.isFinite() || !ret.angle.dirA.isFinite() || !ret.angle.dirB.isFinite() ) )
331
332 // Check that the angle normals are normalized.
333 assert( ret.angle <= ( std::abs( 1 - ret.angle.dirA.length() ) < 0.0001f ) );
334 assert( ret.angle <= ( std::abs( 1 - ret.angle.dirB.length() ) < 0.0001f ) );
335
336 return ret;
337 }
338 else
339 {
340 static_assert( Traits::MeasureSupportedOneWay<B, A>, "This should never fail." );
341 MeasureResult ret = ( measure )( b, a );
342 ret.swapObjects();
343 return ret;
344 }
345}
346// Same, but with a variant as the first argument.
347template <typename B>
348[[nodiscard]] MeasureResult measure( const Primitives::Variant& a, const B& b )
349{
350 return std::visit( [&]( const auto& elem ){ return (measure)( elem, b ); }, a );
351}
352// Same, but with a variant as the second argument.
353template <typename A>
354[[nodiscard]] MeasureResult measure( const A& a, const Primitives::Variant& b )
355{
356 return std::visit( [&]( const auto& elem ){ return (measure)( a, elem ); }, b );
357}
358// Same, but with variants as both argument.
360
361}
#define MRMESH_API
Definition MRMesh/MRMeshFwd.h:46
named object in the data model
Definition MRObject.h:60
Definition MRFeatures.h:306
std::variant< Sphere, ConeSegment, Plane > Variant
Definition MRFeatures.h:111
Definition MRFeatures.h:21
std::string name(const T &primitive)
Definition MRFeatures.h:297
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:311
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::ConeSegment primitiveCone(const Vector3f &a, const Vector3f &b, float rad)
a is the center of the base, b is the pointy end.
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:261
Cone3f
Definition MRMesh/MRMeshFwd.h:266
Definition MRFeatures.h:196
Vector3f pointA
Definition MRFeatures.h:197
Vector3f dirFor(bool b) const
Definition MRFeatures.h:203
MRMESH_API float computeAngleInRadians() const
Vector3f pointB
Definition MRFeatures.h:198
Vector3f dirA
Definition MRFeatures.h:201
bool isSurfaceNormalA
Whether dir{A,B} is a surface normal or a line direction.
Definition MRFeatures.h:206
Vector3f pointFor(bool b) const
Definition MRFeatures.h:199
bool isSurfaceNormalFor(bool b) const
Definition MRFeatures.h:209
Vector3f dirB
Definition MRFeatures.h:202
bool isSurfaceNormalB
Definition MRFeatures.h:207
Definition MRFeatures.h:173
Status status
Definition MRFeatures.h:174
Definition MRFeatures.h:179
Vector3f closestPointA
Definition MRFeatures.h:183
Vector3f closestPointFor(bool b) const
Definition MRFeatures.h:186
Vector3f closestPointB
Definition MRFeatures.h:184
float distance
Definition MRFeatures.h:181
Stores the results of measuring two objects relative to one another.
Definition MRFeatures.h:157
MRMESH_API void swapObjects()
Distance centerDistance
Definition MRFeatures.h:193
Angle angle
Definition MRFeatures.h:213
Distance distance
Definition MRFeatures.h:189
Status
Definition MRFeatures.h:159
@ 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:216
Definition MRFeatures.h:55
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
Definition MRFeatures.h:63
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
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:34
Vector3f center
Definition MRFeatures.h:35
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:38
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:247
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:229
Definition MRSphere.h:9
V center
Definition MRSphere.h:12