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

Question about refinement #134

Open
ivan-trackman opened this issue May 29, 2024 · 5 comments
Open

Question about refinement #134

ivan-trackman opened this issue May 29, 2024 · 5 comments

Comments

@ivan-trackman
Copy link

Hi,

I have a question regarding the refinement process, for some reason (and I think this is by design), the refinement process adds points to the constraint edge, but we want to preserve it as is. Is there any way to avoid doing that, and not split the constraint edges in refinement?

We apply catmullrom on the constraint edge later in compute shader and it will be not the same with new points.

P.S. thank you for this amazing project!

@andywiecko
Copy link
Owner

Hi @ivan-trackman,

Many thanks for reporting the issue!


Unfortunately, the current refinement algorithm, terminator, requires splitting the segment first before inserting points into triangles. This is essential to prevent adding points outside the mesh. To resolve your issue, I have thought of two solutions:

  1. Run two passes of triangulation. First, schedule triangulation with mesh refinement, and in the second run, use the output positions from the first pass, filter out points that are inserted in constraints, and disable refinement. This solution may result in strange triangles for "very long" constraints, but overall the mesh should have similar triangle quality. Sketch:
using var positions = new NativeArray<float2>(..., Allocator.Persistent);
using var constraints = new NativeArray<int>(..., Allocator.Persistent);
using var holes = new NativeArray<float2>(..., Allocator.Persistent); // if required

using var triangulator = new Triangulator(Allocator.Persistent)
{
   Input = { Positions = positions, ConstraintEdges = constraints, HoleSeeds = holes },
   Settings = { RefineMesh = true, RefinementThresholds = { ... }, RestoreBoundary = true  
};


triangulator.Run();

using var filteredPositions = new NativeList<float2>(Allocator.Persistent);

// Filter out positions
filteredPositions.CopyFrom(positions);
for(int i = positions.Length; i < triangulator.Output.Positions.Length; i++)
{
   var p = triangulator.Output.Positions[i];
   if(i is not constraint)
   {
      filteredPositions.Add(p);
   }
}

triangulator.Settings.RefineMesh = false;
triangulator.Input.Positions = filteredPositions;

triangulator.Run();

Implementing the check "if(i is not constraint)" can be tricky but is doable. To make it easier, note that all points inserted during refinement have i >= positions.Length. The Triangulator internally has a buffer NativeList<bool> constrainedHalfedges which has a true element if a given halfedge is a constraint. This can be used to filter out which points should be removed.

  1. Implement a different refinement algorithm and use Triangulator only for constraint triangulation.

In about two weeks, I will push development further. I'm going to merge some stuff, implement some enhancements, and probably release a new version. I could make this constrainedHalfedges public to make it easier and implement the full if(i is not constraint) part (output halfedges have already been opened here #131). I reported this issue on the project board: https://github.com/users/andywiecko/projects/1/views/1?pane=issue&itemId=65089038

If you have any questions, let me know.

Best,
Andrzej Więckowski

@andywiecko
Copy link
Owner

PS
In the future, I plan to add a low-level API, NativeTriangulator, which will include many utilities to customize options for cases like this issue.

@ivan-trackman
Copy link
Author

@andywiecko thanks a lot for this blazingly fast reply! We are currently trying the 2nd approach with ability to add some custom lists to store the information that will help us restore the original triangles (or so my teammate said)
We will post our findings here if you don't mind and maybe make a PR later

@andywiecko
Copy link
Owner

@ivan-trackman Interesting! I'm looking forward to hearing from you about your findings!
Contributions are always welcome 🙂

Best,
Andrzej

@andywiecko
Copy link
Owner

Hi @ivan-trackman,
I just wanted to inform you that I've recently released v3.1. You can find the full release notes here). It is already available on OpenUPM.

This release includes Output.ConstrainedHalfedges, which I mentioned in the thread, and supports native use cases that I believe may be beneficial for you.

Best,
Andrzej

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

No branches or pull requests

2 participants