-
Notifications
You must be signed in to change notification settings - Fork 104
Polyline smoothing #262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Polyline smoothing #262
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
bb55c15
Simple algorithm
oitel 73de577
Keep Area algorithm
oitel f96daae
Use more relevant method
oitel 27ea007
Update Visual Studio project
oitel 6cb4e99
Reuse code for 2D polylines
oitel 168350c
Use more relevant method
oitel b54a795
Remove empty struct
oitel 2653f7b
Fix lone edge check
oitel 3047b0f
Fix open polyline check
oitel 23828ec
Simplify code
oitel 55d78a5
Fix template initialization
oitel 680357c
Move common relax parameters to separate header
oitel 8463dbd
Reorganize includes
oitel 59423fe
Introduce Relax filter
oitel dc6cc08
Fix build
oitel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| #include "MRPolylineRelax.h" | ||
| #include "MRBitSetParallelFor.h" | ||
| #include "MRPolyline.h" | ||
| #include "MRTimer.h" | ||
| #include "MRVector2.h" | ||
| #include "MRWriter.h" | ||
|
|
||
| namespace MR | ||
| { | ||
|
|
||
| template<typename V> | ||
| bool relax( Polyline<V> &polyline, const RelaxParams ¶ms, ProgressCallback cb ) | ||
| { | ||
| if ( params.iterations <= 0 ) | ||
| return true; | ||
|
|
||
| MR_TIMER | ||
| MR_WRITER(polyline) | ||
|
|
||
| Vector<V, VertId> newPoints; | ||
| const auto& zone = polyline.topology.getVertIds( params.region ); | ||
|
|
||
| bool keepGoing = true; | ||
| for ( int i = 0; i < params.iterations; ++i ) | ||
| { | ||
| ProgressCallback internalCb; | ||
| if ( cb ) | ||
| { | ||
| internalCb = [&]( float p ) | ||
| { | ||
| return cb(( float( i ) + p ) / float( params.iterations )); | ||
| }; | ||
| } | ||
|
|
||
| newPoints = polyline.points; | ||
| keepGoing = BitSetParallelFor( zone, [&]( VertId v ) | ||
| { | ||
| auto e0 = polyline.topology.edgeWithOrg( v ); | ||
| assert( !polyline.topology.isLoneEdge( e0 ) ); | ||
| auto e1 = polyline.topology.next( e0 ); | ||
| if ( e0 == e1 ) | ||
| return; | ||
|
|
||
| auto mp = ( polyline.destPnt( e0 ) + polyline.destPnt( e1 ) ) / 2.f; | ||
|
|
||
| auto& np = newPoints[v]; | ||
| auto pushForce = params.force * ( mp - np ); | ||
| np += pushForce; | ||
| }, internalCb ); | ||
| polyline.points.swap( newPoints ); | ||
| if ( !keepGoing ) | ||
| break; | ||
| } | ||
| return keepGoing; | ||
| } | ||
|
|
||
| template<typename V> | ||
| bool relaxKeepArea( Polyline<V> &polyline, const RelaxParams ¶ms, ProgressCallback cb ) | ||
| { | ||
| if ( params.iterations <= 0 ) | ||
| return true; | ||
|
|
||
| MR_TIMER | ||
| MR_WRITER(polyline) | ||
|
|
||
| Vector<V, VertId> newPoints; | ||
| const auto& zone = polyline.topology.getVertIds( params.region ); | ||
| std::vector<V> vertPushForces( zone.size() ); | ||
|
|
||
| bool keepGoing = true; | ||
| for ( int i = 0; i < params.iterations; ++i ) | ||
| { | ||
| ProgressCallback internalCb1, internalCb2; | ||
| if ( cb ) | ||
| { | ||
| internalCb1 = [&] ( float p ) | ||
| { | ||
| return cb( ( float( i ) + p * 0.5f ) / float( params.iterations ) ); | ||
| }; | ||
| internalCb2 = [&] ( float p ) | ||
| { | ||
| return cb( ( float( i ) + p * 0.5f + 0.5f ) / float( params.iterations ) ); | ||
| }; | ||
| } | ||
|
|
||
| keepGoing = BitSetParallelFor( zone, [&]( VertId v ) | ||
| { | ||
| auto e0 = polyline.topology.edgeWithOrg( v ); | ||
| assert( !polyline.topology.isLoneEdge( e0 ) ); | ||
| auto e1 = polyline.topology.next( e0 ); | ||
| if ( e0 == e1 ) | ||
| return; | ||
|
|
||
| auto mp = ( polyline.destPnt( e0 ) + polyline.destPnt( e1 ) ) / 2.f; | ||
|
|
||
| vertPushForces[v] = params.force * ( mp - polyline.points[v] ); | ||
| }, internalCb1 ); | ||
| if ( !keepGoing ) | ||
| break; | ||
|
|
||
| newPoints = polyline.points; | ||
| keepGoing = BitSetParallelFor( zone, [&]( VertId v ) | ||
| { | ||
| auto e0 = polyline.topology.edgeWithOrg( v ); | ||
| assert( !polyline.topology.isLoneEdge( e0 ) ); | ||
| auto e1 = polyline.topology.next( e0 ); | ||
| if ( e0 == e1 ) | ||
| return; | ||
|
|
||
| auto& np = newPoints[v]; | ||
| np += vertPushForces[v]; | ||
| auto modifier = 1.0f / 2.0f; | ||
| np -= ( vertPushForces[polyline.topology.dest( e0 )] * modifier ); | ||
| np -= ( vertPushForces[polyline.topology.dest( e1 )] * modifier ); | ||
| }, internalCb2 ); | ||
| polyline.points.swap( newPoints ); | ||
| if ( !keepGoing ) | ||
| break; | ||
| } | ||
|
|
||
| return keepGoing; | ||
| } | ||
|
|
||
| template bool relax<>( Polyline2& polyline, const RelaxParams& params, ProgressCallback cb ); | ||
| template bool relax<>( Polyline3& polyline, const RelaxParams& params, ProgressCallback cb ); | ||
|
|
||
| template bool relaxKeepArea<>( Polyline2 &polyline, const RelaxParams ¶ms, ProgressCallback cb ); | ||
| template bool relaxKeepArea<>( Polyline3 &polyline, const RelaxParams ¶ms, ProgressCallback cb ); | ||
|
|
||
| } // namespace MR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| #pragma once | ||
| #include "MRMeshFwd.h" | ||
| #include "MRProgressCallback.h" | ||
| #include "MRRelaxParams.h" | ||
|
|
||
| namespace MR | ||
| { | ||
|
|
||
| /// \addtogroup PolylineGroup | ||
| /// \{ | ||
|
|
||
| /// applies given number of relaxation iterations to the whole polyline ( or some region if it is specified ) | ||
| /// \return true if was finished successfully, false if was interrupted by progress callback | ||
| template<typename V> | ||
| MRMESH_API bool relax( Polyline<V>& polyline, const RelaxParams& params = {}, ProgressCallback cb = {} ); | ||
|
|
||
| /// applies given number of relaxation iterations to the whole polyline ( or some region if it is specified ) | ||
| /// do not really keeps area but tries hard | ||
| /// \return true if was finished successfully, false if was interrupted by progress callback | ||
| template<typename V> | ||
| MRMESH_API bool relaxKeepArea( Polyline<V>& polyline, const RelaxParams& params = {}, ProgressCallback cb = {} ); | ||
|
|
||
| /// \} | ||
|
|
||
| } // namespace MR | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| #pragma once | ||
| #include "MRMeshFwd.h" | ||
|
|
||
| namespace MR | ||
| { | ||
|
|
||
| struct RelaxParams | ||
| { | ||
| /// number of iterations | ||
| int iterations{1}; | ||
| /// region to relax | ||
| const VertBitSet *region{nullptr}; | ||
| /// speed of relaxing, typical values (0.0, 0.5] | ||
| float force{0.5f}; | ||
| }; | ||
|
|
||
| enum class RelaxApproxType | ||
| { | ||
| Planar, | ||
| Quadric, | ||
| }; | ||
|
|
||
| } // namespace MR |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.