Skip to content

Commit

Permalink
add update normal functions
Browse files Browse the repository at this point in the history
  • Loading branch information
alemuntoni committed Jan 24, 2024
1 parent 2311f69 commit dcacc0c
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 29 deletions.
71 changes: 48 additions & 23 deletions include/vclib/algorithms/update/normal.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,35 +25,46 @@

#include <vclib/mesh/requirements.h>
#include <vclib/misc/logger.h>
#include <vclib/misc/parallel.h>
#include <vclib/space/matrix.h>

#include "../polygon.h"

namespace vcl {

namespace detail {

template<uint ELEM_ID, LoggerConcept LogType>
void normalizeNoThrow(auto& elem, LogType& log)
{
try {
elem.normal().normalize();
}
catch (const std::exception& e) {
log.log(
LogType::WARNING,
elementEnumString<ELEM_ID>() + " " +
std::to_string(elem.index()) + ": " + e.what());
}
}

} // namespace vcl::detail

template<uint ELEM_ID, MeshConcept MeshType, LoggerConcept LogType = NullLogger>
void normalizePerElementNormals(
MeshType& mesh,
bool noThrow = true,
LogType& log = nullLogger)
{
vcl::requirePerElementComponent<ELEM_ID, NORMAL>(mesh);

log.log(0, "Normalizing per-" + elementEnumString<ELEM_ID>() + " normals");
log.log(0, "Normalizing per-" + elementEnumString<ELEM_ID>() + " normals.");

for (auto& elem : mesh.template elements<ELEM_ID>()) {
try {
elem.normal().normalize();
}
catch (const std::exception& e) {
if (noThrow) {
log.log(LogType::WARNING, e.what());
}
else {
throw e;
}
}
}
// define a lambda that, for each element, normalizes the normal
auto normalize = [&](auto& elem) {
detail::normalizeNoThrow<ELEM_ID>(elem, log);
};

vcl::parallelFor(mesh.template elements<ELEM_ID>(), normalize);

log.log(
100, "Per-" + elementEnumString<ELEM_ID>() + " normals normalized.");
Expand Down Expand Up @@ -160,6 +171,22 @@ void normalizePerVertexNormals(MeshType& m)
normalizePerElementNormals<VERTEX>(m);
}

/**
* @brief Normalizes the length of normals the referenced vertices.
*
* @param m
* @param log
*/
template<FaceMeshConcept MeshType, LoggerConcept LogType = NullLogger>
void normalizePerReferencedVertexNormals(MeshType& m, LogType& log = nullLogger)
{
for (auto& f : m.faces()) {
for (auto* v : f.vertices()) {
detail::normalizeNoThrow<VERTEX>(*v, log);
}
}
}

/**
* @brief Normalizes the length of the face normals.
*
Expand Down Expand Up @@ -289,24 +316,22 @@ void clearPerReferencedVertexNormals(MeshType& m)
* @param[in] normalize: if true (default), normals are normalized after
* computation.
*/
template<FaceMeshConcept MeshType>
void updatePerVertexNormals(MeshType& m, bool normalize = true)
void updatePerVertexNormals(FaceMeshConcept auto& m, bool normalize = true)
{
clearPerReferencedVertexNormals(m);

using MeshType = std::remove_reference_t<decltype(m)>;
using VertexType = MeshType::VertexType;
using FaceType = MeshType::FaceType;
using NormalType = VertexType::NormalType;
using NScalar = NormalType::ScalarType;

for (FaceType& f : m.faces()) {
NormalType n =
faceNormal(f).template cast<typename NormalType::ScalarType>();
for (auto& f : m.faces()) {
for (VertexType* v : f.vertices()) {
v->normal() += n;
v->normal() += faceNormal(f).template cast<NScalar>();
}
}
if (normalize)
normalizePerVertexNormals(m);
normalizePerReferencedVertexNormals(m);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion include/vclib/space/plane.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class Plane
}

template<typename S>
Plane<S, NORM> cast() const
auto cast() const
{
if constexpr (std::is_same<Scalar, S>::value) {
return *this;
Expand Down
2 changes: 1 addition & 1 deletion include/vclib/space/point.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class Point
* object, but with each scalar value casted to a different type.
*/
template<typename S>
Point<S, N> cast() const
auto cast() const
{
if constexpr (std::is_same_v<Scalar, S>) {
return *this;
Expand Down
2 changes: 1 addition & 1 deletion include/vclib/space/principal_curvature.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class PrincipalCurvature
* a different type.
*/
template<typename S>
PrincipalCurvature<S> cast() const
auto cast() const
{
if constexpr (std::is_same<Scalar, S>::value) {
return *this;
Expand Down
2 changes: 1 addition & 1 deletion include/vclib/space/quaternion.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class Quaternion
* different type.
*/
template<typename S>
const Quaternion<S> cast() const
auto cast() const
{
if constexpr (std::is_same_v<Scalar, S>) {
return *this;
Expand Down
2 changes: 1 addition & 1 deletion include/vclib/space/sphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Sphere
Scalar& radius() { return r; }

template<typename S>
Sphere<S> cast() const
auto cast() const
{
if constexpr (std::is_same_v<Scalar, S>) {
return *this;
Expand Down
2 changes: 1 addition & 1 deletion include/vclib/space/tex_coord.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class TexCoord
TexCoord(const Point2<Scalar>& p) : coord(p) {}

template<typename S>
TexCoord<S> cast() const
auto cast() const
{
if constexpr (std::is_same<Scalar, S>::value) {
return *this;
Expand Down

0 comments on commit dcacc0c

Please sign in to comment.