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

added test function MeshClosestPoint #1

Merged
merged 19 commits into from
Jul 20, 2023
Merged

Conversation

Sophielelerre
Copy link
Collaborator

@Sophielelerre Sophielelerre commented Jul 3, 2023

What type of PR is this? (check all applicable)

  • ♻️ Refactor
  • ✨ Feature
  • 🐛 Bug Fix
  • 👷 Optimization
  • 📝 Documentation Update
  • 🔖 Release
  • 🚩 Other

Description

This PR adds a ClosestPoint() implementation for meshes. The method takes a mesh and a test point and returns the closest point to the test point on the mesh. The algorithm finds the closest point to all the faces, then the absolute closest. This point can be either on a mesh vertex, mesh edge, or inside a mesh face. The method can be used with triangle meshes, quad meshes, and Ngon meshes. Any Ngons faces are triangulated using vertices' centroid and consecutive vertices. Quads are treated as Ngons. I did not work on the tests yet, I'd like some feedback first.

Related Tickets & Documents

Please use this format link issue numbers: Fixes GSharker#416
GSharker#416 (comment)

Added tests?

  • 👍 yes
  • 🙅 no, because they aren't needed
  • 🙋 no, because I need help

Added to documentation?

  • 📓 docs
  • 🙅 no documentation needed

@Sophielelerre Sophielelerre requested a review from iltabe July 3, 2023 14:07
Copy link
Collaborator

@iltabe iltabe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Sophielelerre!

I started to look at the code. Some comments are inline, here some general comments

  1. Docstring
    Just type /// 3 times on top of a method and Visual studio (VS) will create a default structure to fill.
  2. Comments should always have a space after // (so you can distinguish between comments and commented code:
//Console.Writeline("ciao")  // is a commented code
// This is a comment
  1. Variables names
    Use camelCase and not PascalCase

Nice to see that the project is getting shaped! :)
Speak tomorrow,
-g

src/GShark/Geometry/Mesh.cs Outdated Show resolved Hide resolved
src/GShark/Geometry/Mesh.cs Outdated Show resolved Hide resolved
src/GShark/Geometry/Mesh.cs Outdated Show resolved Hide resolved
src/GShark/Geometry/Mesh.cs Outdated Show resolved Hide resolved
src/GShark/Geometry/Mesh.cs Outdated Show resolved Hide resolved
src/GShark/Geometry/Mesh.cs Outdated Show resolved Hide resolved
src/GShark/Geometry/Mesh.cs Outdated Show resolved Hide resolved
2) Added a loop in the CloudClosestPoint instead of the IndexOf(Min()) method
3) Added an if statement to distiguish triangular faces and/or quad faces in the Mesh class
@Sophielelerre Sophielelerre requested a review from iltabe July 6, 2023 14:42
Copy link
Collaborator

@iltabe iltabe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Sophie!

I've looked at the code and made suggestions on how to simplify and make the code more performative.
Try to copy-paste/implement what I did and let me know if you have questions. Pay attention on the fact that MeshVertex is a child class of Point3. So you don't need to make a conversion (allocating more memory on large meshes) but with minor tweaks at the input type of methods (using IEnumerable over List) the code is getting simplified a lot.

I'm still not convinced about the quad solution. We make 4 triangles (permutations of all possible triangles with 4 points) because we don't know which one of the two is actually the solution that is closer to the real face geometry. But we don't know at all the face geometry! So the point we find could be either ok or either wrong.
For sure if the face is planar our closest point is good BUT we do the calculation on 4 triangles when we could do just with 2.
We could threat the quad and ngons at the same way: finding the average point and then triangulate.

OR

we could check with the same strategy if the point is inside a generic flat polygon and raise an error if a mesh face is not planar. Unsure.

Sorry for this attack late Friday!
Feel free to just look at it on Monday!

src/GShark/Geometry/Point3.cs Outdated Show resolved Hide resolved
src/GShark/Geometry/Point3.cs Outdated Show resolved Hide resolved
src/GShark/Geometry/Mesh.cs Outdated Show resolved Hide resolved
src/GShark/Geometry/Mesh.cs Outdated Show resolved Hide resolved
src/GShark/Geometry/Mesh.cs Outdated Show resolved Hide resolved
src/GShark/Geometry/Point3.cs Outdated Show resolved Hide resolved
src/GShark/Geometry/Mesh.cs Outdated Show resolved Hide resolved
src/GShark/Geometry/Mesh.cs Outdated Show resolved Hide resolved
- from lists to arrays
- code refinements
- addition of polygon option (fan strategy)
Ngon doesn't get detected
@Sophielelerre Sophielelerre marked this pull request as ready for review July 13, 2023 13:19
@Sophielelerre
Copy link
Collaborator Author

Sophielelerre commented Jul 13, 2023

Main things to notice:

  • given the unreliable result of checking just the faces adjacent to the closer vertex now the code loops through all of the faces
  • Rhino defines Ngons as a collection of triangles and quads so the mesh.Faces will return Ngons as triangles and quads. Using the method rhinoMesh.GetNgonAndFacesEnumerable() will return Ngon without dividing them. However, Ngons have a weird central vertex part of their definition so using var indices = face.BoundaryVertexIndexList(); will return vertices that crash the function in the RhinoCommand that converts rhino mesh > G Shark mesh.
  • It could be failing because of an "isolated vertex" (this is being called on the creation of the GShark mesh HasIsolatedVertices())
  • The solution I adopted is using the BoundaryVertexIndexList() method to detect the Ngon and print out a message but then use the Rhino way of having Ngons as triangles and quads so that the code doesn't crash. In terms of the GShark library, I am just returning an error for Ngons. But because of the reasons above, I haven't found a way to check that the path goes there.

src/GShark/Geometry/Mesh.cs Outdated Show resolved Hide resolved
Copy link
Collaborator

@iltabe iltabe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Sophielelerre.
Great work. I left few comments, mainly regarding formatting the code. Nothing big to change.

After going through my comments, the only thing left is to fix the docstings. Let's chat about this.

src/GShark/Geometry/Point3.cs Outdated Show resolved Hide resolved
src/GShark/Geometry/Mesh.cs Show resolved Hide resolved
src/GShark/Geometry/Mesh.cs Outdated Show resolved Hide resolved
src/GShark/Geometry/Mesh.cs Outdated Show resolved Hide resolved
src/GShark/Geometry/Mesh.cs Outdated Show resolved Hide resolved
src/GShark/Geometry/Mesh.cs Outdated Show resolved Hide resolved
src/GShark/Geometry/Mesh.cs Outdated Show resolved Hide resolved
{
//throw new Exception("Ngon detected");
// Stellate method (from Ngon to triangles)
List<Point3> GSVerticesPoints = GSVertices.ConvertAll(v => (Point3)v);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as before: no need to convert. Let's not convert and then see what G-Shark people think about it.

src/GShark/Geometry/Mesh.cs Outdated Show resolved Hide resolved
src/GShark/Geometry/Mesh.cs Outdated Show resolved Hide resolved
Copy link
Collaborator

@iltabe iltabe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job!
Just a minor suggestion into a docstring.

Can you please fill in the pull request description? The template you see is from G-Shark people. Make an exaustive description and reference the g.shark issue I've created (its in g-shark repository). When this is done and reviewed you can open the PR that will ask to merge what you did into the real G-Shark repo.

Hurrà!

src/GShark/Geometry/Mesh.cs Outdated Show resolved Hide resolved
Co-authored-by: iltabe <64929000+iltabe@users.noreply.github.com>
@Sophielelerre Sophielelerre merged commit 1d15d48 into master Jul 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants