MeshLib C++ Docs
Loading...
Searching...
No Matches
MRMeshOrPoints.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRMeshPart.h"
4#include "MRPointCloudPart.h"
5#include "MRAffineXf3.h"
6#include "MRId.h"
7#include <cfloat>
8#include <functional>
9#include <optional>
10#include <variant>
11
12namespace MR
13{
16
17
21{
22public:
23 MeshOrPoints( const MeshPart & mp ) : var_( mp ) { }
24 MeshOrPoints( const PointCloudPart & pcp ) : var_( pcp ) { }
25
27 MeshOrPoints( const Mesh & mesh ) : var_( MeshPart( mesh ) ) { }
28 MeshOrPoints( const PointCloud & pc ) : var_( PointCloudPart( pc ) ) { }
29
31 [[nodiscard]] const MeshPart* asMeshPart() const;
32
34 [[nodiscard]] const PointCloudPart* asPointCloudPart() const;
35
38 [[nodiscard]] MRMESH_API Box3f getObjBoundingBox() const;
39
41 MRMESH_API void cacheAABBTree() const;
42
45 [[nodiscard]] MRMESH_API Box3f computeBoundingBox( const AffineXf3f * toWorld = nullptr ) const;
46
48 MRMESH_API void accumulate( PointAccumulator& accum, const AffineXf3f* xf = nullptr ) const;
49
55 [[nodiscard]] MRMESH_API std::optional<VertBitSet> pointsGridSampling( float voxelSize, size_t maxVoxels = 500000,
56 const ProgressCallback & cb = {} ) const;
57
59 [[nodiscard]] MRMESH_API const VertCoords & points() const;
60
62 [[nodiscard]] MRMESH_API const VertBitSet& validPoints() const;
63
65 [[nodiscard]] MRMESH_API std::function<Vector3f(VertId)> normals() const;
66
69 [[nodiscard]] MRMESH_API std::function<float(VertId)> weights() const;
70
72 {
74 Vector3f point;
75
78 std::optional<Vector3f> normal;
79
81 bool isBd = false;
82
84 float distSq = FLT_MAX;
85
89
90 [[nodiscard]] explicit operator bool() const { return valid(); }
91 [[nodiscard]] bool valid() const { return distSq < FLT_MAX; }
92 };
93
95 [[nodiscard]] MRMESH_API std::function<ProjectionResult( const Vector3f & )> projector() const;
96
97 using LimitedProjectorFunc = std::function<bool( const Vector3f& p, ProjectionResult& res )>;
101 [[nodiscard]] MRMESH_API LimitedProjectorFunc limitedProjector() const;
102
103private:
104 std::variant<MeshPart, PointCloudPart> var_;
105};
106
109{
111 AffineXf3f xf;
112
114 [[nodiscard]] MRMESH_API std::function<MeshOrPoints::ProjectionResult( const Vector3f& )> projector() const;
115
118 [[nodiscard]] MRMESH_API MeshOrPoints::LimitedProjectorFunc limitedProjector() const;
119};
120
122[[nodiscard]] MRMESH_API std::optional<MeshOrPoints> getMeshOrPoints( const Object * obj );
123[[nodiscard]] MRMESH_API std::optional<MeshOrPointsXf> getMeshOrPointsXf( const Object * obj );
124
126using ProjectOnAllCallback = std::function<void( ObjId, MeshOrPoints::ProjectionResult )>;
127
129MRMESH_API void projectOnAll(
130 const Vector3f& pt,
131 const AABBTreeObjects & tree,
132 float upDistLimitSq,
133 const ProjectOnAllCallback & callback,
134 ObjId skipObjId = {} );
135
137[[nodiscard]] inline MeshOrPoints::ProjectionResult projectWorldPointOntoObject( const Vector3f& p, const Object& obj )
138{
139 auto mop = getMeshOrPointsXf( &obj );
140 if ( !mop )
141 return {};
142 return mop->projector()( p );
143}
144
150 const Vector3f& p,
151 const Object* root = nullptr,
152 std::function<bool( const Object& )> projectPred = nullptr,
153 std::function<bool( const Object& )> recursePred = nullptr
154);
155
156inline const MeshPart* MeshOrPoints::asMeshPart() const
157{
158 return std::visit( overloaded{
159 []( const MeshPart & mp ) { return &mp; },
160 []( const PointCloudPart & ) { return (const MeshPart*)nullptr; }
161 }, var_ );
162}
163
165{
166 return std::visit( overloaded{
167 []( const MeshPart & ) { return (const PointCloudPart *)nullptr; },
168 []( const PointCloudPart & pcp ) { return &pcp; }
169 }, var_ );
170}
171
172}
Definition MRAABBTreeObjects.h:22
Definition MRMeshOrPoints.h:21
named object in the data model
Definition MRObject.h:62
Class to accumulate points and make best line / plane approximation.
Definition MRBestFit.h:20
MRMESH_API std::function< ProjectionResult(const Vector3f &)> projector() const
returns a function that finds projection (closest) points on this: Vector3f->ProjectionResult
MRMESH_API void projectOnAll(const Vector3f &pt, const AABBTreeObjects &tree, float upDistLimitSq, const ProjectOnAllCallback &callback, ObjId skipObjId={})
finds closest point on every object within given distance
const MeshPart * asMeshPart() const
if this object holds a mesh part then returns pointer on it, otherwise returns nullptr
Definition MRMeshOrPoints.h:156
std::function< bool(const Vector3f &p, ProjectionResult &res)> LimitedProjectorFunc
Definition MRMeshOrPoints.h:97
MeshOrPoints::ProjectionResult projectWorldPointOntoObject(const Vector3f &p, const Object &obj)
Projects a point onto an object, in world space. Returns .valid() == false if this object type isn't ...
Definition MRMeshOrPoints.h:137
std::optional< Vector3f > normal
Definition MRMeshOrPoints.h:78
MRMESH_API std::function< float(VertId)> weights() const
MeshOrPoints(const PointCloudPart &pcp)
Definition MRMeshOrPoints.h:24
MRMESH_API std::function< MeshOrPoints::ProjectionResult(const Vector3f &)> projector() const
returns a function that finds projection (closest) points on this: Vector3f->ProjectionResult
const PointCloudPart * asPointCloudPart() const
if this object holds a point cloud part then returns pointer on it, otherwise returns nullptr
Definition MRMeshOrPoints.h:164
MRMESH_API std::optional< MeshOrPoints > getMeshOrPoints(const Object *obj)
constructs MeshOrPoints from ObjectMesh or ObjectPoints, otherwise returns nullopt
MeshOrPoints(const MeshPart &mp)
Definition MRMeshOrPoints.h:23
std::function< void(ObjId, MeshOrPoints::ProjectionResult)> ProjectOnAllCallback
to receive object id + projection result on it
Definition MRMeshOrPoints.h:126
MRMESH_API MeshOrPoints::ProjectionResult projectWorldPointOntoObjectsRecursive(const Vector3f &p, const Object *root=nullptr, std::function< bool(const Object &)> projectPred=nullptr, std::function< bool(const Object &)> recursePred=nullptr)
MeshOrPoints obj
Definition MRMeshOrPoints.h:110
MRMESH_API const VertCoords & points() const
gives access to points-vector (which can include invalid points as well)
MRMESH_API Box3f getObjBoundingBox() const
bool valid() const
Definition MRMeshOrPoints.h:91
MRMESH_API const VertBitSet & validPoints() const
gives access to bit set of valid points
MRMESH_API std::function< Vector3f(VertId)> normals() const
returns normals generating function: VertId->normal (or empty for point cloud without normals)
Vector3f point
found closest point
Definition MRMeshOrPoints.h:74
MRMESH_API std::optional< VertBitSet > pointsGridSampling(float voxelSize, size_t maxVoxels=500000, const ProgressCallback &cb={}) const
float distSq
squared distance from query point to the closest point
Definition MRMeshOrPoints.h:84
MRMESH_API void accumulate(PointAccumulator &accum, const AffineXf3f *xf=nullptr) const
Adds in existing PointAccumulator the elements of the contained object.
MeshOrPoints(const PointCloud &pc)
Definition MRMeshOrPoints.h:28
bool isBd
can be true only for meshes, if the closest point is located on the boundary of the mesh (or the curr...
Definition MRMeshOrPoints.h:81
VertId closestVert
Definition MRMeshOrPoints.h:88
MRMESH_API std::optional< MeshOrPointsXf > getMeshOrPointsXf(const Object *obj)
MRMESH_API LimitedProjectorFunc limitedProjector() const
MRMESH_API void cacheAABBTree() const
if AABBTree is already built does nothing otherwise builds and caches it
MRMESH_API Box3f computeBoundingBox(const AffineXf3f *toWorld=nullptr) const
MeshOrPoints(const Mesh &mesh)
these constructors are redundant for C++, but important for python bindings
Definition MRMeshOrPoints.h:27
MRMESH_API MeshOrPoints::LimitedProjectorFunc limitedProjector() const
AffineXf3f xf
Definition MRMeshOrPoints.h:111
only for bindings generation
Definition MRCameraOrientationPlugin.h:8
an object and its transformation to global space with other objects
Definition MRMeshOrPoints.h:109
Definition MRMeshOrPoints.h:72
Definition MRMesh.h:23
represents full point cloud (if region is nullptr) or some portion of point cloud (if region pointer ...
Definition MRPointCloudPart.h:13
Definition MRPointCloud.h:17