MeshLib
 
Loading...
Searching...
No Matches
MRPolyline.h
Go to the documentation of this file.
1#pragma once
2
5#include "MRPlane3.h"
6#include "MRLineSegm.h"
7#include "MREdgePoint.h"
8
9namespace MR
10{
11
13
16template<typename V>
18{
19public:
22
23 Polyline() = default;
24
27
30
34 MRMESH_API Polyline( const std::vector<VertId> & comp2firstVert, Vector<V, VertId> ps );
35
39 MRMESH_API EdgeId addFromPoints( const V * vs, size_t num, bool closed );
40
44 MRMESH_API EdgeId addFromPoints( const V * vs, size_t num );
45
48 MRMESH_API void addPart( const Polyline<V>& from,
49 VertMap * outVmap = nullptr, WholeEdgeMap * outEmap = nullptr );
50
52 MRMESH_API void addPartByMask( const Polyline<V>& from, const UndirectedEdgeBitSet& mask,
53 VertMap* outVmap = nullptr, EdgeMap* outEmap = nullptr );
54
57 MRMESH_API void pack( VertMap * outVmap = nullptr, WholeEdgeMap * outEmap = nullptr );
58
60 [[nodiscard]] V orgPnt( EdgeId e ) const { return points[ topology.org( e ) ]; }
61
63 [[nodiscard]] V destPnt( EdgeId e ) const { return points[ topology.dest( e ) ]; }
64
66 [[nodiscard]] V edgePoint( EdgeId e, float f ) const { return f * destPnt( e ) + ( 1 - f ) * orgPnt( e ); }
67
69 [[nodiscard]] V edgePoint( const EdgePoint & ep ) const { return edgePoint( ep.e, ep.a ); }
70
72 [[nodiscard]] V edgeCenter( EdgeId e ) const { return edgePoint( e, 0.5f ); }
73
75 [[nodiscard]] V edgeVector( EdgeId e ) const { return destPnt( e ) - orgPnt( e ); }
76
78 [[nodiscard]] LineSegm<V> edgeSegment( EdgeId e ) const { return LineSegm<V>( orgPnt( e ), destPnt( e ) ); }
79
81 [[nodiscard]] EdgePoint toEdgePoint( VertId v ) const { return EdgePoint( topology, v ); }
82
84 [[nodiscard]] MRMESH_API EdgePoint toEdgePoint( EdgeId e, const V & p ) const;
85
87 [[nodiscard]] float edgeLength( EdgeId e ) const { return edgeVector( e ).length(); }
88
90 [[nodiscard]] float edgeLengthSq( EdgeId e ) const { return edgeVector( e ).lengthSq(); }
91
93 [[nodiscard]] MRMESH_API float totalLength() const;
94
97
99 [[nodiscard]] const AABBTreePolyline<V> * getAABBTreeNotCreate() const { return AABBTreeOwner_.get(); }
100
102 [[nodiscard]] MRMESH_API Box<V> getBoundingBox() const;
103
106 [[nodiscard]] MRMESH_API Box<V> computeBoundingBox( const AffineXf<V> * toWorld = nullptr ) const;
107
108 // computes average position of all valid polyline vertices
109 [[nodiscard]] MRMESH_API V findCenterFromPoints() const;
110
112 MRMESH_API void transform( const AffineXf<V> & xf );
113
118 MRMESH_API EdgeId splitEdge( EdgeId e, const V & newVertPos );
119
120 // same, but split given edge on two equal parts
121 EdgeId splitEdge( EdgeId e ) { return splitEdge( e, edgeCenter( e ) ); }
122
124 void invalidateCaches() { AABBTreeOwner_.reset(); };
125
129 [[nodiscard]] MRMESH_API Contours<V> contours( std::vector<std::vector<VertId>>* vertMap = nullptr ) const;
130
134 [[nodiscard]] MRMESH_API Contours2f contours2( std::vector<std::vector<VertId>>* vertMap = nullptr ) const;
135
137 template<typename U>
138 [[nodiscard]] Polyline<U> toPolyline() const;
139
142 MRMESH_API EdgeId addFromEdgePath( const Mesh& mesh, const EdgePath& path );
143
146 EdgeId addFromSurfacePath( const Mesh& mesh, const SurfacePath& path ) { return addFromGeneralSurfacePath( mesh, {}, path, {} ); }
147
150 MRMESH_API EdgeId addFromGeneralSurfacePath( const Mesh& mesh, const MeshTriPoint & start, const SurfacePath& path, const MeshTriPoint & end );
151
153 [[nodiscard]] MRMESH_API size_t heapBytes() const;
155 template <class Q = V>
156 [[nodiscard]] typename std::enable_if_t< std::is_same_v<Q, Vector3f> > mirror( const Plane3f& plane )
157 {
158 for ( auto& p : points )
159 {
160 p += 2.0f * ( plane.project( p ) - p );
161 }
162
164 }
165
166private:
167 mutable UniqueThreadSafeOwner<AABBTreePolyline<V>> AABBTreeOwner_;
168};
169
170template<typename V>
171template<typename U>
173{
174 Polyline<U> res;
175 res.topology = topology;
176 res.points.reserve( points.size() );
177 for ( size_t i = 0; i < points.size(); i++ )
178 {
179 res.points.push_back( U{ points[VertId( i )] } );
180 }
181 return res;
182}
183
184} // namespace MR
int VertId
Definition MRDotNet/MRMeshFwd.h:51
List< VertId > VertMap
Definition MRDotNet/MRMeshFwd.h:84
int EdgeId
Definition MRDotNet/MRMeshFwd.h:52
#define MRMESH_API
Definition MRMesh/MRMeshFwd.h:46
bounding volume hierarchy for line segments
Definition MRAABBTreePolyline.h:30
Definition MRPolylineTopology.h:15
VertId org(EdgeId he) const
returns origin vertex of half-edge
Definition MRPolylineTopology.h:66
VertId dest(EdgeId he) const
returns destination vertex of half-edge
Definition MRPolylineTopology.h:68
Definition MRUniqueThreadSafeOwner.h:20
std::vector<T>-like container that requires specific indexing type,
Definition MRMesh/MRVector.h:19
void reserve(size_t capacity)
Definition MRMesh/MRVector.h:58
void push_back(const T &t)
Definition MRMesh/MRVector.h:109
auto end(const BitSet &)
Definition MRMesh/MRBitSet.h:265
Definition MRCameraOrientationPlugin.h:7
std::vector< MeshEdgePoint > SurfacePath
Definition MRMesh/MRMeshFwd.h:340
Contours2< float > Contours2f
Definition MRMesh/MRMeshFwd.h:282
std::vector< EdgeId > EdgePath
Definition MRMesh/MRMeshFwd.h:98
Contours3< float > Contours3f
Definition MRMesh/MRMeshFwd.h:284
std::vector< Contour< V > > Contours
Definition MRMesh/MRMeshFwd.h:278
Definition MRMesh/MRAffineXf.h:14
Box given by its min- and max- corners.
Definition MRMesh/MRBox.h:23
encodes a point on an edge of mesh or of polyline
Definition MREdgePoint.h:11
SegmPointf a
a in [0,1], a=0 => point is in org( e ), a=1 => point is in dest( e )
Definition MREdgePoint.h:13
EdgeId e
Definition MREdgePoint.h:12
Definition MRLineSegm.h:11
Definition MRMesh/MRMeshTriPoint.h:23
Definition MRMesh/MRMesh.h:23
Definition MRPolyline.h:18
V edgePoint(const EdgePoint &ep) const
computes coordinates of point given as edge and relative position on it
Definition MRPolyline.h:69
MRMESH_API EdgePoint toEdgePoint(EdgeId e, const V &p) const
converts edge and point's coordinates into edge-point representation
V edgePoint(EdgeId e, float f) const
returns a point on the edge: origin point for f=0 and destination point for f=1
Definition MRPolyline.h:66
MRMESH_API EdgeId splitEdge(EdgeId e, const V &newVertPos)
MRMESH_API EdgeId addFromEdgePath(const Mesh &mesh, const EdgePath &path)
V destPnt(EdgeId e) const
returns coordinates of the edge destination
Definition MRPolyline.h:63
MRMESH_API float totalLength() const
returns total length of the polyline
float edgeLength(EdgeId e) const
returns Euclidean length of the edge
Definition MRPolyline.h:87
void invalidateCaches()
Invalidates caches (e.g. aabb-tree) after a change in polyline.
Definition MRPolyline.h:124
const AABBTreePolyline< V > * getAABBTreeNotCreate() const
returns cached aabb-tree for this polyline, but does not create it if it did not exist
Definition MRPolyline.h:99
MRMESH_API void addPartByMask(const Polyline< V > &from, const UndirectedEdgeBitSet &mask, VertMap *outVmap=nullptr, EdgeMap *outEmap=nullptr)
appends polyline (from) in addition to this polyline: creates new edges, verts and points
MRMESH_API Box< V > getBoundingBox() const
returns the minimal bounding box containing all valid vertices (implemented via getAABBTree())
V orgPnt(EdgeId e) const
returns coordinates of the edge origin
Definition MRPolyline.h:60
Polyline< U > toPolyline() const
convert Polyline3 to Polyline2 or vice versa
Definition MRPolyline.h:172
MRMESH_API Polyline(const Contours3f &contours)
creates polyline from 3D contours, 2D polyline will lose z-component
MRMESH_API void addPart(const Polyline< V > &from, VertMap *outVmap=nullptr, WholeEdgeMap *outEmap=nullptr)
Polyline()=default
MRMESH_API V findCenterFromPoints() const
MRMESH_API void transform(const AffineXf< V > &xf)
applies given transformation to all valid polyline vertices
MRMESH_API void pack(VertMap *outVmap=nullptr, WholeEdgeMap *outEmap=nullptr)
Vector< V, VertId > points
Definition MRPolyline.h:21
MRMESH_API Contours< V > contours(std::vector< std::vector< VertId > > *vertMap=nullptr) const
PolylineTopology topology
Definition MRPolyline.h:20
EdgeId addFromSurfacePath(const Mesh &mesh, const SurfacePath &path)
Definition MRPolyline.h:146
MRMESH_API Polyline(const std::vector< VertId > &comp2firstVert, Vector< V, VertId > ps)
MRMESH_API EdgeId addFromPoints(const V *vs, size_t num, bool closed)
float edgeLengthSq(EdgeId e) const
returns squared Euclidean length of the edge (faster to compute than length)
Definition MRPolyline.h:90
MRMESH_API Contours2f contours2(std::vector< std::vector< VertId > > *vertMap=nullptr) const
V edgeVector(EdgeId e) const
returns vector equal to edge destination point minus edge origin point
Definition MRPolyline.h:75
V edgeCenter(EdgeId e) const
returns edge's centroid
Definition MRPolyline.h:72
MRMESH_API EdgeId addFromPoints(const V *vs, size_t num)
MRMESH_API size_t heapBytes() const
returns the amount of memory this object occupies on heap
EdgePoint toEdgePoint(VertId v) const
converts vertex into edge-point representation
Definition MRPolyline.h:81
MRMESH_API const AABBTreePolyline< V > & getAABBTree() const
returns cached aabb-tree for this polyline, creating it if it did not exist in a thread-safe manner
LineSegm< V > edgeSegment(EdgeId e) const
returns line segment of given edge
Definition MRPolyline.h:78
std::enable_if_t< std::is_same_v< Q, Vector3f > > mirror(const Plane3f &plane)
reflects the polyline from a given plane. Enabled only for Polyline3f
Definition MRPolyline.h:156
EdgeId splitEdge(EdgeId e)
Definition MRPolyline.h:121
MRMESH_API Box< V > computeBoundingBox(const AffineXf< V > *toWorld=nullptr) const
MRMESH_API EdgeId addFromGeneralSurfacePath(const Mesh &mesh, const MeshTriPoint &start, const SurfacePath &path, const MeshTriPoint &end)
MRMESH_API Polyline(const Contours2f &contours)
creates polyline from 2D contours, 3D polyline will get zero z-component