Skip to content

Commit

Permalink
added ProgressCancel, use to allow cancellation of computation in Red…
Browse files Browse the repository at this point in the history
…ucer and Remesher
  • Loading branch information
rms80 committed May 22, 2018
1 parent 0822bf9 commit e569636
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
58 changes: 58 additions & 0 deletions core/ProgressCancel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;

namespace g3
{
/// <summary>
/// interface that provides a cancel function
/// </summary>
public interface ICancelSource
{
bool Cancelled();
}


/// <summary>
/// Just wraps a func<bool> as an ICancelSource
/// </summary>
public class CancelFunction : ICancelSource
{
public Func<bool> CancelF;
public CancelFunction(Func<bool> cancelF) {
CancelF = cancelF;
}
public bool Cancelled() { return CancelF(); }
}


/// <summary>
/// This class is intended to be passed to long-running computes to
/// 1) provide progress info back to caller (not implemented yet)
/// 2) allow caller to cancel the computation
/// </summary>
public class ProgressCancel
{
public ICancelSource Source;

bool WasCancelled = false; // will be set to true if CancelF() ever returns true

public ProgressCancel(ICancelSource source)
{
Source = source;
}
public ProgressCancel(Func<bool> cancelF)
{
Source = new CancelFunction(cancelF);
}

/// <summary>
/// Check if client would like to cancel
/// </summary>
public bool Cancelled()
{
if (WasCancelled)
return true;
WasCancelled = Source.Cancelled();
return WasCancelled;
}
}
}
1 change: 1 addition & 0 deletions geometry3Sharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
<Compile Include="core\Units.cs" />
<Compile Include="core\DVectorArray.cs" />
<Compile Include="core\VectorArray.cs" />
<Compile Include="core\ProgressCancel.cs" />
<Compile Include="curve\BaseCurve2.cs" />
<Compile Include="curve\BSplineBasis.cs" />
<Compile Include="curve\Circle2.cs" />
Expand Down
13 changes: 13 additions & 0 deletions mesh/MeshRefinerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ public void SetExternalConstraints(MeshConstraints cons)
}


/// <summary>
/// Set this to be able to cancel running remesher
/// </summary>
public ProgressCancel Progress = null;

/// <summary>
/// if this returns true, abort computation.
/// </summary>
protected virtual bool Cancelled() {
return (Progress == null) ? false : Progress.Cancelled();
}


protected double edge_flip_metric(ref Vector3d n0, ref Vector3d n1)
{
if (edge_flip_tol == 0) {
Expand Down
18 changes: 18 additions & 0 deletions mesh/Reducer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,14 @@ public virtual void DoReduce()

begin_setup();
Precompute();
if (Cancelled())
return;
InitializeVertexQuadrics();
if (Cancelled())
return;
InitializeQueue();
if (Cancelled())
return;
end_setup();

begin_ops();
Expand All @@ -103,6 +109,8 @@ public virtual void DoReduce()
int eid = EdgeQueue.Dequeue();
if (!mesh.IsEdge(eid))
continue;
if (Cancelled())
return;

int vKept;
ProcessResult result = CollapseEdge(eid, EdgeQuadrics[eid].collapse_pt, out vKept);
Expand All @@ -114,6 +122,9 @@ public virtual void DoReduce()
end_collapse();
end_ops();

if (Cancelled())
return;

Reproject();

end_pass();
Expand Down Expand Up @@ -170,6 +181,8 @@ public virtual void FastCollapsePass(double fMinEdgeLength)

begin_setup();
Precompute();
if (Cancelled())
return;
end_setup();

begin_ops();
Expand All @@ -183,6 +196,8 @@ public virtual void FastCollapsePass(double fMinEdgeLength)
continue;
if (mesh.IsBoundaryEdge(eid))
continue;
if (Cancelled())
return;

mesh.GetEdgeV(eid, ref va, ref vb);
if (va.DistanceSquared(ref vb) > min_sqr)
Expand All @@ -200,6 +215,9 @@ public virtual void FastCollapsePass(double fMinEdgeLength)
end_collapse();
end_ops();

if (Cancelled())
return;

Reproject();

end_pass();
Expand Down
11 changes: 11 additions & 0 deletions mesh/Remesher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,15 @@ public virtual void BasicRemeshPass() {
if (result == ProcessResult.Ok_Collapsed || result == ProcessResult.Ok_Flipped || result == ProcessResult.Ok_Split)
ModifiedEdgesLastPass++;
}
if (Cancelled()) // expensive to check every iter?
return;
cur_eid = next_edge(cur_eid, out done);
} while (done == false);
end_ops();

if (Cancelled())
return;

begin_smooth();
if (EnableSmoothing && SmoothSpeedT > 0) {
if (EnableSmoothInPlace)
Expand All @@ -185,13 +190,19 @@ public virtual void BasicRemeshPass() {
}
end_smooth();

if (Cancelled())
return;

begin_project();
if (target != null && ProjectionMode == TargetProjectionMode.AfterRefinement) {
FullProjectionPass();
DoDebugChecks();
}
end_project();

if (Cancelled())
return;

end_pass();
}

Expand Down

0 comments on commit e569636

Please sign in to comment.