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

SubMesh::RecalculateNormals improvement #609

Merged
merged 10 commits into from
Jul 25, 2024

Conversation

ntfshard
Copy link
Contributor

@ntfshard ntfshard commented May 12, 2024

🦟 Bug fix

Fixes #N/A

Summary

Previous version of RecalculateNormals member function has a O(n^2) complexity because inner loop where we are looking for normal elements. But we already know indexes of this elements. So we can get rid of inner loop.
And seems we should check indices.size() < 3u || indices.size() % 3u != 0 due to if it will be 4, main loop will start and on second iteration it will start reading out of array.
Also seems condition in the begin should check amount of indexes/vector, but not normals, due to we are resizing it later.
Maybe this function also should have a check of enum PrimitiveType of data, seems attempt to call it for POINTS and LINES is not legit (for rest 4 types maybe it's ok, not sure)

Checklist

  • Signed all commits for DCO
  • Added tests
  • Updated documentation (as needed)
  • Updated migration guide (as needed)
  • Consider updating Python bindings (if the library has them)
  • codecheck passed (See contributing)
  • All tests passed (See test coverage)
  • While waiting for a review on your PR, please help review another open pull request to support the maintainers

Note to maintainers: Remember to use Squash-Merge and edit the commit message to match the pull request summary while retaining Signed-off-by messages.

@ntfshard ntfshard requested a review from marcoag as a code owner May 12, 2024 18:12
@github-actions github-actions bot added 🌱 garden Ignition Garden 🎵 harmonic Gazebo Harmonic labels May 12, 2024
@ntfshard
Copy link
Contributor Author

I force-pushed commit due to previous one was not signed and I made boundary check more strict

@iche033
Copy link
Contributor

iche033 commented May 13, 2024

looks like there is still an issue with DCO, can you try fix it again?

@ntfshard ntfshard closed this May 13, 2024
@ntfshard ntfshard reopened this May 13, 2024
@iche033
Copy link
Contributor

iche033 commented May 14, 2024

I was testing this with a capsule shape and I noticed that a seam is now visible after the changes:

Before:
cylinder_normal_before

After:
cylinder_normal_after

The RecaculateNormals function was ported from gazebo-classic so I went through it's history and found this:
gazebosim/gazebo-classic@ea54b56

From the commit msg, the loop was actually implemented on purpose to make objects appear smoother. The effect of that loop is that more normals are updated instead of limiting to 3 normals. Not efficient but produces smoother results. I'm leaning towards keeping the current code as it was intentional.

@ntfshard
Copy link
Contributor Author

I see. Thank you for testing and clarification. The main problem with current code that it could work about a minute if polygon has quite a lot of triangles even on a modern hardware. Maybe I can try to re-implement initial behaviour with unordered_map. I guess it should be ok to exchange execution time to a memory consumption spike

@iche033
Copy link
Contributor

iche033 commented May 15, 2024

Maybe I can try to re-implement initial behaviour with unordered_map. I guess it should be ok to exchange execution time to a memory consumption spike

yes that would be good to try, thanks

ntfshard added 5 commits July 20, 2024 19:56
Signed-off-by: Maksim Derbasov <[email protected]>
Signed-off-by: Maksim Derbasov <[email protected]>
Signed-off-by: Maksim Derbasov <[email protected]>
Signed-off-by: Maksim Derbasov <[email protected]>
ntfshard and others added 2 commits July 20, 2024 23:55
Co-authored-by: Dmitry Skorobogaty <[email protected]>
Signed-off-by: Maksim Derbasov <[email protected]>
Signed-off-by: Maksim Derbasov <[email protected]>
@ntfshard
Copy link
Contributor Author

ntfshard commented Jul 20, 2024

Sorry for a delay. Here is an another solution. We are grouping all points by x coordinate and check only points with a same x coordinate (and adjacent groups, due to operator== for Vector3d is actually treat equal a coordinate in a plus/minus Epsilon range.)

For a relatively small sub-meshes it works slower(on my hardware, around 8 milliseconds) than previous one. It looks like a minimal running time constant. Not sure how critical is it. If it's a showstopper, I can dispatch algorithms according to a input size, but it will make code more complex(for a future readers), that's why I came with this version first.

Also not sure what with Jenkins CI, I tried to run this binaries locally and they seems working fine
UPD: seems no problem, I'm just a stupid

And the last thing, I don't have a full gazebo setup in current environment, can rely only on unit tests (w/o visual manual check)

@azeey azeey requested a review from iche033 July 22, 2024 18:59
ntfshard added 2 commits July 23, 2024 10:22
Signed-off-by: Maksim Derbasov <[email protected]>
Signed-off-by: Maksim Derbasov <[email protected]>
Comment on lines 579 to 627
if (this->dataPtr->normals.size() < 3u)
if (this->dataPtr->indices.empty()
|| this->dataPtr->indices.size() % 3u != 0)
return;

Copy link
Contributor Author

@ntfshard ntfshard Jul 23, 2024

Choose a reason for hiding this comment

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

It feels like it also should check: || this->dataPtr->primitiveType != SubMesh::TRIANGLES but not 100% sure that it's only one primitiveType

Copy link
Contributor

Choose a reason for hiding this comment

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

+1, I think it's good to be explicit and check that it's a TRIANGLES primitive type. I think other primitive types don't quite work yet but this is just in case better support is implemented for other types in the future.

Copy link
Contributor

@iche033 iche033 left a comment

Choose a reason for hiding this comment

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

The new approach looks fine to me. I tested the code and the capsule renders fine without the seam. Just one minor comment on adding some doc to explain the logic.

Comment on lines 579 to 627
if (this->dataPtr->normals.size() < 3u)
if (this->dataPtr->indices.empty()
|| this->dataPtr->indices.size() % 3u != 0)
return;

Copy link
Contributor

Choose a reason for hiding this comment

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

+1, I think it's good to be explicit and check that it's a TRIANGLES primitive type. I think other primitive types don't quite work yet but this is just in case better support is implemented for other types in the future.

}

template<typename Visitor>
void Visit(const gz::math::Vector3d &_point, Visitor _v) const
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add some documentation to explain the logic in this function? I think it's to find and include all indices of vertices that are within the tolerance of the math::equal checks, so that their normals all count towards the final calculation?

Signed-off-by: Maksim Derbasov <[email protected]>
@ntfshard ntfshard requested a review from iche033 July 25, 2024 03:00
Copy link
Contributor

@iche033 iche033 left a comment

Choose a reason for hiding this comment

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

looks good, thanks for iterating

@iche033 iche033 merged commit ad1d5ad into gazebosim:gz-common5 Jul 25, 2024
12 checks passed
@ntfshard ntfshard deleted the submesh-normals branch July 25, 2024 17:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🌱 garden Ignition Garden 🎵 harmonic Gazebo Harmonic
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

3 participants