MeshLib Documentation
Loading...
Searching...
No Matches
MRDistanceMap.h
Go to the documentation of this file.
1#pragma once
2
3#include "MRRectIndexer.h"
4#include "MRBitSet.h"
5#include "MRAffineXf3.h"
7#include "MRPolyline.h"
8#include "MRHeapBytes.h"
9#include "MRImage.h"
10#include "MRExpected.h"
11#include <cfloat>
12#include <filesystem>
13#include <vector>
14
15namespace MR
16{
17
20
23class [[nodiscard]] DistanceMap : public RectIndexer
24{
25public:
26 DistanceMap() = default;
27
30 [[nodiscard]] MRMESH_API DistanceMap( size_t resX, size_t resY );
31
33 [[nodiscard]] MRMESH_API DistanceMap( const MR::Matrix<float>& m );
34
36 [[nodiscard]] MRMESH_API bool isValid( size_t x, size_t y ) const;
38 [[nodiscard]] MRMESH_API bool isValid( size_t i ) const;
39
41 [[nodiscard]] bool isInBounds( size_t x, size_t y ) const { return x < size_t( dims_.x ) && y < size_t( dims_.y ); }
43 [[nodiscard]] bool isInBounds( size_t i ) const { return i < size_; }
44
46 [[nodiscard]] MRMESH_API std::optional<float> get( size_t x, size_t y ) const;
48 [[nodiscard]] MRMESH_API std::optional<float> get( size_t i ) const;
51 [[nodiscard]] float& getValue( size_t x, size_t y )
52 {
53 return data_[toIndex( { int( x ), int( y ) } )];
54 }
55 [[nodiscard]] float getValue( size_t x, size_t y ) const
56 {
57 return data_[toIndex( { int( x ), int( y ) } )];
58 }
59 [[nodiscard]] float& getValue( size_t i )
60 {
61 return data_[i];
62 }
63 [[nodiscard]] float getValue( size_t i ) const
64 {
65 return data_[i];
66 }
67
68 float* data() { return data_.data(); }
69 const float* data() const { return data_.data(); }
70
80 [[nodiscard]] MRMESH_API std::optional<float> getInterpolated( float x, float y ) const;
81
85 [[nodiscard]] MRMESH_API std::optional<Vector3f> unproject( size_t x, size_t y, const AffineXf3f& toWorld ) const;
86
95 [[nodiscard]] MRMESH_API std::optional<Vector3f> unprojectInterpolated( float x, float y, const AffineXf3f& toWorld ) const;
96
99
102 [[nodiscard]] MRMESH_API DistanceMap max( const DistanceMap& rhs ) const;
106 [[nodiscard]] MRMESH_API DistanceMap min( const DistanceMap& rhs ) const;
110 [[nodiscard]] MRMESH_API DistanceMap operator- ( const DistanceMap& rhs ) const;
113
115 MRMESH_API void set( size_t x, size_t y, float val );
117 MRMESH_API void set( size_t i, float val );
119 MRMESH_API void set( std::vector<float> data );
121 MRMESH_API void unset( size_t x, size_t y );
123 MRMESH_API void unset( size_t i );
124
129
133 [[nodiscard]] MRMESH_API std::pair< DistanceMap, DistanceMap > getXYDerivativeMaps() const;
134
136 [[nodiscard]] MRMESH_API std::vector< std::pair<size_t, size_t> > getLocalMaximums() const;
137
139 [[nodiscard]] size_t resX() const
140 {
141 return dims_.x;
142 }
144 [[nodiscard]] size_t resY() const
145 {
146 return dims_.y;
147 }
148
150 [[nodiscard]] size_t numPoints() const
151 {
152 return size();
153 }
154
157 [[nodiscard]] MRMESH_API std::pair<float, float> getMinMaxValues() const;
160 [[nodiscard]] MRMESH_API std::pair<size_t, size_t> getMinIndex() const;
163 [[nodiscard]] MRMESH_API std::pair<size_t, size_t> getMaxIndex() const;
164
166 [[nodiscard]] size_t heapBytes() const
167 {
168 return MR::heapBytes( data_ );
169 }
170
171private:
172 std::vector<float> data_;
173};
174
176MRMESH_API DistanceMap combineXYderivativeMaps( std::pair<DistanceMap, DistanceMap> XYderivativeMaps );
177
181 ProgressCallback cb = {}, std::vector<MeshTriPoint> * outSamples = nullptr );
182
186 ProgressCallback cb = {}, std::vector<MeshTriPoint> * outSamples = nullptr );
187
189struct [[nodiscard]] ContoursDistanceMapOffset
190{
193 enum class OffsetType
194 {
195 Normal,
196 Shell
197 } type{ OffsetType::Shell };
198};
199
200struct [[nodiscard]] ContoursDistanceMapOptions
201{
204 {
212 } signMethod{ ContourOrientation };
214 const ContoursDistanceMapOffset* offsetParameters{ nullptr };
216 const PixelBitSet* region{ nullptr };
218 std::vector<UndirectedEdgeId>* outClosestEdges{ nullptr };
220 float minDist{ 0 };
222 float maxDist{ FLT_MAX };
223};
224
229[[nodiscard]] MRMESH_API DistanceMap distanceMapFromContours( const Polyline2& contours, const ContourToDistanceMapParams& params,
230 const ContoursDistanceMapOptions& options = {} );
231
237MRMESH_API void distanceMapFromContours( DistanceMap & distMap, const Polyline2& polyline, const ContourToDistanceMapParams& params,
238 const ContoursDistanceMapOptions& options = {} );
239
243[[nodiscard]] MRMESH_API std::vector<Vector3f> edgePointsFromContours( const Polyline2& polyline, float pixelSize, float threshold );
244
247[[nodiscard]] MRMESH_API Polyline2 distanceMapTo2DIsoPolyline( const DistanceMap& distMap, float isoValue );
248
250[[nodiscard]] MRMESH_API Polyline2 distanceMapTo2DIsoPolyline( const DistanceMap& distMap,
251 const ContourToDistanceMapParams& params, float isoValue );
252
256[[nodiscard]] MRMESH_API std::pair<Polyline2, AffineXf3f> distanceMapTo2DIsoPolyline( const DistanceMap& distMap,
257 const AffineXf3f& xf, float isoValue, bool useDepth = false );
258[[nodiscard]] MRMESH_API Polyline2 distanceMapTo2DIsoPolyline( const DistanceMap& distMap, float pixelSize, float isoValue );
259
261[[nodiscard]] MRMESH_API Polyline2 polylineOffset( const Polyline2& polyline, float pixelSize, float offset );
262
270[[nodiscard]] MRMESH_API Polyline2 contourUnion( const Polyline2& contoursA, const Polyline2& contoursB,
271 const ContourToDistanceMapParams& params, float offsetInside = 0 );
272
280[[nodiscard]] MRMESH_API Polyline2 contourIntersection( const Polyline2& contoursA, const Polyline2& contoursB,
281 const ContourToDistanceMapParams& params, float offsetInside = 0.f );
282
290[[nodiscard]] MRMESH_API Polyline2 contourSubtract( const Polyline2& contoursA, const Polyline2& contoursB,
291 const ContourToDistanceMapParams& params, float offsetInside = 0.f );
292
294[[nodiscard]] MRMESH_API Expected<Mesh> distanceMapToMesh( const DistanceMap& distMap, const AffineXf3f& toWorld, ProgressCallback cb = {} );
295
298[[nodiscard]] MRMESH_API Image convertDistanceMapToImage( const DistanceMap& distMap, float threshold = 1.f / 255 );
299
302[[nodiscard]] MRMESH_API Expected<DistanceMap> convertImageToDistanceMap( const Image& image, float threshold = 1.f / 255 );
303
305
306} // namespace MR
constexpr A operator-(A a)
Definition MRImGuiVectorOperators.h:98
constexpr A & operator-=(A &a, B b)
Definition MRImGuiVectorOperators.h:113
#define MRMESH_API
Definition MRMesh/MRMeshFwd.h:46
Definition MRDistanceMap.h:24
MRMESH_API void set(size_t i, float val)
sets value in index element (the coord must be valid, UB otherwise)
MRMESH_API std::optional< float > get(size_t x, size_t y) const
returns value in (X,Y) element, returns nullopt if not valid (see isValid()), UB if out of bounds.
MRMESH_API bool isValid(size_t x, size_t y) const
checks if X,Y element is valid (i.e. not std::numeric_limits<float>::lowest(); passing invalid coords...
float getValue(size_t i) const
Definition MRDistanceMap.h:63
float & getValue(size_t i)
Definition MRDistanceMap.h:59
float * data()
Definition MRDistanceMap.h:68
MRMESH_API void negate()
replaces every valid element in the map with its negative value
const float * data() const
Definition MRDistanceMap.h:69
DistanceMap()=default
float getValue(size_t x, size_t y) const
Definition MRDistanceMap.h:55
MRMESH_API std::pair< size_t, size_t > getMaxIndex() const
MRMESH_API DistanceMap min(const DistanceMap &rhs) const
returns new Distance Map with cell-wise minimum values. Invalid values remain only if both correspond...
MRMESH_API const DistanceMap & mergeMax(const DistanceMap &rhs)
replaces values with cell-wise maximum values. Invalid values remain only if both corresponding cells...
MRMESH_API void set(std::vector< float > data)
sets all values at one time
MRMESH_API DistanceMap(size_t resX, size_t resY)
MRMESH_API DistanceMap getDerivativeMap() const
returns new derivatives map without directions
MRMESH_API DistanceMap max(const DistanceMap &rhs) const
MRMESH_API std::pair< float, float > getMinMaxValues() const
size_t resX() const
returns X resolution
Definition MRDistanceMap.h:139
MRMESH_API std::optional< Vector3f > unproject(size_t x, size_t y, const AffineXf3f &toWorld) const
MRMESH_API std::optional< float > get(size_t i) const
returns value of index element, returns nullopt if not valid (see isValid()), UB if out of bounds.
MRMESH_API std::vector< std::pair< size_t, size_t > > getLocalMaximums() const
computes single derivative map from XY spaces combined. Returns local maximums then
MRMESH_API const DistanceMap & mergeMin(const DistanceMap &rhs)
replaces values with cell-wise minimum values. Invalid values remain only if both corresponding cells...
MRMESH_API std::optional< float > getInterpolated(float x, float y) const
finds interpolated value.
size_t heapBytes() const
returns the amount of memory this object occupies on heap
Definition MRDistanceMap.h:166
MRMESH_API DistanceMap(const MR::Matrix< float > &m)
make from 2d array
float & getValue(size_t x, size_t y)
Definition MRDistanceMap.h:51
MRMESH_API std::pair< DistanceMap, DistanceMap > getXYDerivativeMaps() const
returns new derivative maps with X and Y axes direction
MRMESH_API std::pair< size_t, size_t > getMinIndex() const
size_t resY() const
returns Y resolution
Definition MRDistanceMap.h:144
bool isInBounds(size_t x, size_t y) const
Returns true if (X,Y) coordinates are in bounds.
Definition MRDistanceMap.h:41
MRMESH_API void unset(size_t x, size_t y)
invalidates value in (X,Y) element (the coords must be valid, UB otherwise)
MRMESH_API void invalidateAll()
invalidates all elements
MRMESH_API void set(size_t x, size_t y, float val)
sets value in (X,Y) element (the coords must be valid, UB otherwise)
MRMESH_API void unset(size_t i)
invalidates value in index element (the coord must be valid, UB otherwise)
MRMESH_API std::optional< Vector3f > unprojectInterpolated(float x, float y, const AffineXf3f &toWorld) const
finds 3d coordinates of the Point on the model surface for the (x,y) interpolated value
size_t numPoints() const
returns the number of pixels
Definition MRDistanceMap.h:150
MRMESH_API void clear()
clears data, sets resolutions to zero
MRMESH_API bool isValid(size_t i) const
checks if index element is valid (i.e. not std::numeric_limits<float>::lowest(); passing an invalid c...
bool isInBounds(size_t i) const
Returns true if a flattened coordinate is in bounds.
Definition MRDistanceMap.h:43
a class for converting 2D integer coordinates into 1D linear coordinates and backward
Definition MRRectIndexer.h:38
std::vector<T>-like container that requires specific indexing type,
Definition MRMesh/MRVector.h:20
size_t heapBytes(const std::vector< T > &vec)
returns the amount of memory given vector occupies on heap
Definition MRHeapBytes.h:15
std::function< bool(float)> ProgressCallback
Definition MRMesh/MRMeshFwd.h:576
MRMESH_API std::vector< Vector3f > edgePointsFromContours(const Polyline2 &polyline, float pixelSize, float threshold)
MRMESH_API Expected< Mesh > distanceMapToMesh(const DistanceMap &distMap, const AffineXf3f &toWorld, ProgressCallback cb={})
converts distance map into mesh and applies a transformation to all points
MRMESH_API DistanceMap combineXYderivativeMaps(std::pair< DistanceMap, DistanceMap > XYderivativeMaps)
fill another distance map pair with gradients across X and Y axes of the argument map
MRMESH_API Polyline2 contourUnion(const Polyline2 &contoursA, const Polyline2 &contoursB, const ContourToDistanceMapParams &params, float offsetInside=0)
computes the union of the shapes bounded by input 2d contours
MRMESH_API DistanceMap distanceMapFromContours(const Polyline2 &contours, const ContourToDistanceMapParams &params, const ContoursDistanceMapOptions &options={})
Computes distance of 2d contours according ContourToDistanceMapParams.
MRMESH_API Polyline2 contourIntersection(const Polyline2 &contoursA, const Polyline2 &contoursB, const ContourToDistanceMapParams &params, float offsetInside=0.f)
computes the intersection of the shapes bounded by input 2d contours
MRMESH_API DistanceMap computeDistanceMapD(const MeshPart &mp, const MeshToDistanceMapParams &params, ProgressCallback cb={}, std::vector< MeshTriPoint > *outSamples=nullptr)
MRMESH_API Polyline2 distanceMapTo2DIsoPolyline(const DistanceMap &distMap, float isoValue)
MRMESH_API Polyline2 contourSubtract(const Polyline2 &contoursA, const Polyline2 &contoursB, const ContourToDistanceMapParams &params, float offsetInside=0.f)
computes the difference between the shapes bounded by contoursA and the shapes bounded by contoursB
MRMESH_API Expected< DistanceMap > convertImageToDistanceMap(const Image &image, float threshold=1.f/255)
MRMESH_API Image convertDistanceMapToImage(const DistanceMap &distMap, float threshold=1.f/255)
MRMESH_API Polyline2 polylineOffset(const Polyline2 &polyline, float pixelSize, float offset)
constructs an offset contour for given polyline
MRMESH_API DistanceMap computeDistanceMap(const MeshPart &mp, const MeshToDistanceMapParams &params, ProgressCallback cb={}, std::vector< MeshTriPoint > *outSamples=nullptr)
Definition MRCameraOrientationPlugin.h:8
@ WindingRule
ray intersection counter, significantly slower than ProjectionNormal and does not support holes in me...
ImVec2 size(const ViewportRectangle &rect)
Definition MRViewport.h:32
tl::expected< T, E > Expected
Definition MRExpected.h:58
Structure with parameters to generate DistanceMap by Contours.
Definition MRDistanceMapParams.h:94
Structure with parameters for optional offset in distanceMapFromContours function.
Definition MRDistanceMap.h:190
const Vector< float, UndirectedEdgeId > & perEdgeOffset
offset values for each undirected edge of given polyline
Definition MRDistanceMap.h:192
OffsetType
Definition MRDistanceMap.h:194
Definition MRDistanceMap.h:201
SignedDetectionMethod
method to calculate sign
Definition MRDistanceMap.h:204
@ ContourOrientation
Definition MRDistanceMap.h:208
Definition MRImage.h:15
Definition MRMatrix.h:16
Definition MRDistanceMapParams.h:17