Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions source/MRMesh/MRPolyline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,22 @@ EdgePoint Polyline<V>::toEdgePoint( EdgeId e, const V & p ) const
return { e, dt / edgeLenSq };
}

template<typename V>
Vector3f MR::Polyline<V>::loopDirArea( EdgeId e0 ) const
{
Vector3f area;
auto e = e0;
for ( ;; )
{
area += cross( Vector3f( orgPnt( e ) ), Vector3f( destPnt( e ) ) );
e = topology.next( e.sym() );
if ( e == e0 )
return area;
else if ( e == e0.sym() )
return Vector3f( 0.0f, 0.0f, FLT_MAX );
}
}

template<typename V>
float Polyline<V>::totalLength() const
{
Expand Down Expand Up @@ -400,6 +416,24 @@ TEST( MRMesh, Polyline2 )
}
}

TEST( MRMesh, Polyline2LoopDir )
{
Contour2f cont;
cont.push_back( Vector2f( 0.f, 0.f ) );
cont.push_back( Vector2f( 1.f, 0.f ) );
cont.push_back( Vector2f( 1.f, 1.f ) );
cont.push_back( Vector2f( 0.f, 1.f ) );

Polyline2 plNotClosed( { cont } );
EXPECT_TRUE( plNotClosed.loopDirArea( 0_e ).z == FLT_MAX );

cont.push_back( Vector2f( 0.f, 0.f ) );

Polyline2 plClosed( { cont } );
EXPECT_TRUE( plClosed.loopDirArea( 0_e ).z > 0.0f );
EXPECT_TRUE( plClosed.loopDirArea( 1_e ).z < 0.0f );
}

TEST( MRMesh, Polyline3 )
{
Contour2f cont;
Expand Down
4 changes: 4 additions & 0 deletions source/MRMesh/MRPolyline.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ struct Polyline
/// returns squared Euclidean length of the edge (faster to compute than length)
[[nodiscard]] float edgeLengthSq( EdgeId e ) const { return edgeVector( e ).lengthSq(); }

/// calculates directed loop area if iterating in `e` direction
/// .z = FLT_MAX if `e` does not represent a loop
[[nodiscard]] MRMESH_API Vector3f loopDirArea( EdgeId e ) const;

/// returns total length of the polyline
[[nodiscard]] MRMESH_API float totalLength() const;

Expand Down