Skip to content
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

Cancellation option is added to Triangulate, Refine and Smooth methods #52

Merged
merged 2 commits into from
Jan 6, 2025

Conversation

YasarYY
Copy link
Contributor

@YasarYY YasarYY commented Jan 6, 2025

Triangulation operations are now cancellable. This option comes handy for time-consuming operations. Option added to Triangulate and Refine methods with quality parameter, and to Smooth method.

If no cancellation token is given, which is default, these methods works uncancellable and synchronous.

I didn't implement this functionality to Viewer UI in order not to complicate it. Viewer works the same synchronous way.

A usage example snippet:

// in class:
TriangleNet.Mesh netMesh;
CancellationTokenSource cancellationTokenSource;

// in an async method:
cancellationTokenSource = new CancellationTokenSource();
try
{
	await Task.Run(() => {
		netMesh = (TriangleNet.Mesh)polygon.Triangulate(constraintOptions, qualityOptions, cancellationTokenSource.Token);
		smoother.Smooth(netMesh, 20, 0.01, cancellationTokenSource.Token);
	});
}
catch (OperationCanceledException)
{
	// triangulation is canceled
}
catch (Exception ex)
{
	// some other exception
}

// cancellation method:
public void CancelOperation()
{
	cancellationTokenSource.Cancel();
}

@wo80
Copy link
Owner

wo80 commented Jan 6, 2025

Thanks @YasarYY , cancellation support is a welcome addition!

Please remove the Windows specific projects from the solution file. This should make the tests run successfully.

@wo80
Copy link
Owner

wo80 commented Jan 6, 2025

I didn't implement this functionality to Viewer UI in order not to complicate it.

That's okay. I will look at those projects myself and update the code, if necessary.

@YasarYY
Copy link
Contributor Author

YasarYY commented Jan 6, 2025

Hi Christian,
I removed the Windows specific projects from the solution file.

@wo80
Copy link
Owner

wo80 commented Jan 6, 2025

Alright, this looks good.

Do you plan to add cancellation support to the actual triangulation algorithms or is this ready to merge? I wouldn't mind merging the PR the way it is.

@YasarYY
Copy link
Contributor Author

YasarYY commented Jan 6, 2025

I don't intend to make any further implementation, unless it is necessary for some reason. Usage of ThrowIfCancellationRequested() at lower levels may degrade performance. I used CPU profiling in an attempt to find optimal places to call ThrowIfCancellationRequested().

With current implementation, ThrowIfCancellationRequested() is called by EnforceQuality() (which is called by Triangulate() and Refine()) and by Smooth(). At my trials, I got cancellation responses below 1 second on a relatively slow machine.

So, in my opinion this is ready to merge. 😌

@wo80
Copy link
Owner

wo80 commented Jan 6, 2025

Thanks again for your contribution!

@wo80 wo80 merged commit 613ad53 into wo80:master Jan 6, 2025
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants