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
94 [[nodiscard]] MRMESH_API Vector3f loopDirArea( EdgeId e ) const;
95
97 [[nodiscard]] MRMESH_API float totalLength() const;
98
101
103 [[nodiscard]] const AABBTreePolyline<V> * getAABBTreeNotCreate() const { return AABBTreeOwner_.get(); }
104
106 [[nodiscard]] MRMESH_API Box<V> getBoundingBox() const;
107
110 [[nodiscard]] MRMESH_API Box<V> computeBoundingBox( const AffineXf<V> * toWorld = nullptr ) const;
111
112 // computes average position of all valid polyline vertices
113 [[nodiscard]] MRMESH_API V findCenterFromPoints() const;
114
116 MRMESH_API void transform( const AffineXf<V> & xf );
117
122 MRMESH_API EdgeId splitEdge( EdgeId e, const V & newVertPos );
123
124 // same, but split given edge on two equal parts
125 EdgeId splitEdge( EdgeId e ) { return splitEdge( e, edgeCenter( e ) ); }
126
128 void invalidateCaches() { AABBTreeOwner_.reset(); };
129
133 [[nodiscard]] MRMESH_API Contours<V> contours( std::vector<std::vector<VertId>>* vertMap = nullptr ) const;
134
138 [[nodiscard]] MRMESH_API Contours2f contours2( std::vector<std::vector<VertId>>* vertMap = nullptr ) const;
139
141 template<typename U>
142 [[nodiscard]] Polyline<U> toPolyline() const;
143
146 MRMESH_API EdgeId addFromEdgePath( const Mesh& mesh, const EdgePath& path );
147
150 EdgeId addFromSurfacePath( const Mesh& mesh, const SurfacePath& path ) { return addFromGeneralSurfacePath( mesh, {}, path, {} ); }
151
154 MRMESH_API EdgeId addFromGeneralSurfacePath( const Mesh& mesh, const MeshTriPoint & start, const SurfacePath& path, const MeshTriPoint & end );
155
157 [[nodiscard]] MRMESH_API size_t heapBytes() const;
159 template <class Q = V>
160 [[nodiscard]] typename std::enable_if_t< std::is_same_v<Q, Vector3f> > mirror( const Plane3f& plane )
161 {
162 for ( auto& p : points )
163 {
164 p += 2.0f * ( plane.project( p ) - p );
165 }
166
168 }
169
170private:
171 mutable UniqueThreadSafeOwner<AABBTreePolyline<V>> AABBTreeOwner_;
172};
173
174template<typename V>
175template<typename U>
177{
178 Polyline<U> res;
179 res.topology = topology;
180 res.points.reserve( points.size() );
181 for ( size_t i = 0; i < points.size(); i++ )
182 {
183 res.points.push_back( U{ points[VertId( i )] } );
184 }
185 return res;
186}
187
188} // 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:59
void push_back(const T &t)
Definition MRMesh/MRVector.h:110
represents a 3-dimentional float-typed vector
Definition MRDotNet/MRVector3.h:8
auto end(const BitSet &)
Definition MRMesh/MRBitSet.h:265
Definition MRCameraOrientationPlugin.h:8
std::vector< MeshEdgePoint > SurfacePath
Definition MRMesh/MRMeshFwd.h:349
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:25
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:128
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:103
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:176
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:150
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 Vector3f loopDirArea(EdgeId e) const
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:160
EdgeId splitEdge(EdgeId e)
Definition MRPolyline.h:125
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