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
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions graphics/include/gz/common/SubMesh.hh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include <memory>
#include <optional>
#include <string>
#include <vector>

#include <gz/math/Vector3.hh>
#include <gz/math/Vector2.hh>
Expand Down Expand Up @@ -158,7 +157,7 @@ namespace gz
public: gz::math::Vector3d Vertex(const unsigned int _index) const;

/// \brief Get the raw vertex pointer. This is unsafe, it is the
/// caller's responsability to ensure it's not indexed out of bounds.
/// caller's responsibility to ensure it's not indexed out of bounds.
/// The valid range is [0; VertexCount())
/// \return Raw vertices
public: const gz::math::Vector3d* VertexPtr() const;
Expand Down Expand Up @@ -224,7 +223,7 @@ namespace gz
public: int Index(const unsigned int _index) const;

/// \brief Get the raw index pointer. This is unsafe, it is the
/// caller's responsability to ensure it's not indexed out of bounds.
/// caller's responsibility to ensure it's not indexed out of bounds.
/// The valid range is [0; IndexCount())
/// \return Raw indices
public: const unsigned int* IndexPtr() const;
Expand Down Expand Up @@ -416,7 +415,7 @@ namespace gz
GZ_UTILS_IMPL_PTR(dataPtr)
};

/// \brief Vertex to node weighted assignement for skeleton animation
/// \brief Vertex to node weighted assignment for skeleton animation
/// visualization
class GZ_COMMON_GRAPHICS_VISIBLE NodeAssignment
{
Expand Down
63 changes: 54 additions & 9 deletions graphics/src/SubMesh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
#include <algorithm>
#include <limits>
#include <map>
#include <optional>
#include <string>
#include <vector>

#include "gz/math/Helpers.hh"

Expand Down Expand Up @@ -573,10 +573,56 @@ void SubMesh::FillArrays(double **_vertArr, int **_indArr) const
}
}

namespace {
// Simple way to find neighbors by grouping all vertices
// by X coordinate with (ordered) map. KD-tree maybe better
// but not sure about construction overhead
struct Neighbors
{
Neighbors(const std::vector<unsigned int> &_indices,
const std::vector<gz::math::Vector3d> &_vertices)
: vertices(_vertices)
{
for (unsigned int i = 0; i < _indices.size(); ++i)
{
const auto index = _indices[i];
this->neighbors[_vertices[index].X()].push_back(index);
}
}

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?

{
auto it = this->neighbors.find(_point.X());
// find smaller acceptable value
while (it != this->neighbors.begin())
{
auto prev = it;
--prev;
if (!gz::math::equal(prev->first, _point.X()))
break;
it = prev;
}
while (it != this->neighbors.end()
&& gz::math::equal(it->first, _point.X()))
{
for (const auto index : it->second)
if (this->vertices[index] == _point)
_v(index);
++it;
}
}

private: std::map<double, std::vector<unsigned int>> neighbors;
private: const std::vector<gz::math::Vector3d> &vertices;
};
} // namespace

//////////////////////////////////////////////////
void SubMesh::RecalculateNormals()
{
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.

// Reset all the normals
Expand All @@ -586,6 +632,8 @@ void SubMesh::RecalculateNormals()
if (this->dataPtr->normals.size() != this->dataPtr->vertices.size())
this->dataPtr->normals.resize(this->dataPtr->vertices.size());

Neighbors neighbors(this->dataPtr->indices, this->dataPtr->vertices);

// For each face, which is defined by three indices, calculate the normals
for (unsigned int i = 0; i < this->dataPtr->indices.size(); i+= 3)
{
Expand All @@ -597,14 +645,11 @@ void SubMesh::RecalculateNormals()
this->dataPtr->vertices[this->dataPtr->indices[i+2]];
gz::math::Vector3d n = gz::math::Vector3d::Normal(v1, v2, v3);

for (unsigned int j = 0; j < this->dataPtr->vertices.size(); ++j)
{
gz::math::Vector3d v = this->dataPtr->vertices[j];
if (v == v1 || v == v2 || v == v3)
for (const auto &point : {v1, v2, v3})
neighbors.Visit(point, [&](const unsigned int index)
{
this->dataPtr->normals[j] += n;
}
}
this->dataPtr->normals[index] += n;
});
}

// Normalize the results
Expand Down
29 changes: 29 additions & 0 deletions graphics/src/SubMesh_TEST.cc
Original file line number Diff line number Diff line change
Expand Up @@ -585,3 +585,32 @@ TEST_F(SubMeshTest, Volume)
boxSub.AddIndex(1);
EXPECT_DOUBLE_EQ(0.0, boxSub.Volume());
}

/////////////////////////////////////////////////
TEST_F(SubMeshTest, NormalsRecalculation)
{
auto submesh = std::make_shared<common::SubMesh>();
submesh->SetPrimitiveType(common::SubMesh::TRIANGLES);

constexpr unsigned int triangles = 16384;
for (unsigned int i = 0; i < triangles; ++i) {
// sub to X less than _epsilon from even triangles
// expect that the 2nd vertex should be matched with
// the 1st of next triangle
const auto jitter = i % 2 ? 1e-7 : 0.0;
submesh->AddVertex(i-jitter, i, i);
submesh->AddVertex(i+1, i+1, i+1);
submesh->AddVertex(i, i, -static_cast<double>(i));

submesh->AddIndex(3*i);
submesh->AddIndex(3*i+1);
submesh->AddIndex(3*i+2);
}

ASSERT_EQ(submesh->IndexCount() % 3, 0u);
submesh->RecalculateNormals();
ASSERT_EQ(submesh->NormalCount(), submesh->VertexCount());
// Same triangle, but different normals
// because of neighbour vertex
ASSERT_NE(submesh->Normal(0), submesh->Normal(1));
}
Loading