Skip to content

Commit

Permalink
[types] bugfix on dereferencePtr function (returned object was by value)
Browse files Browse the repository at this point in the history
  • Loading branch information
alemuntoni committed Feb 18, 2025
1 parent bf723aa commit 8f871e0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
28 changes: 23 additions & 5 deletions tests/core/021-grid-query/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,15 @@ using Meshesf = std::tuple<vcl::TriMeshf, vcl::PolyMeshf>;
using MeshesIndexed = std::tuple<vcl::TriMeshIndexed, vcl::PolyMeshIndexed>;
using MeshesIndexedf = std::tuple<vcl::TriMeshIndexedf, vcl::PolyMeshIndexedf>;

static const vcl::uint N_POINTS_TEST = 200;

TEMPLATE_TEST_CASE("Closest faces to points...", "", Meshes)
static const vcl::uint N_POINTS_TEST = 50;

TEMPLATE_TEST_CASE(
"Closest faces to points...",
"",
Meshes,
Meshesf,
MeshesIndexed,
MeshesIndexedf)
{
using TriMesh = std::tuple_element_t<0, TestType>;
using PolyMesh = std::tuple_element_t<1, TestType>;
Expand Down Expand Up @@ -87,7 +93,13 @@ TEMPLATE_TEST_CASE("Closest faces to points...", "", Meshes)
}
}

TEMPLATE_TEST_CASE("K nearest faces to points...", "", Meshes)
TEMPLATE_TEST_CASE(
"K nearest faces to points...",
"",
Meshes,
Meshesf,
MeshesIndexed,
MeshesIndexedf)
{
using TriMesh = std::tuple_element_t<0, TestType>;
using PolyMesh = std::tuple_element_t<1, TestType>;
Expand Down Expand Up @@ -147,7 +159,13 @@ TEMPLATE_TEST_CASE("K nearest faces to points...", "", Meshes)
}
}

TEMPLATE_TEST_CASE("Faces in spheres...", "", Meshes)
TEMPLATE_TEST_CASE(
"Faces in spheres...",
"",
Meshes,
Meshesf,
MeshesIndexed,
MeshesIndexedf)
{
using TriMesh = std::tuple_element_t<0, TestType>;
using PolyMesh = std::tuple_element_t<1, TestType>;
Expand Down
10 changes: 6 additions & 4 deletions vclib/core/include/vclib/space/complex/grid/abstract_grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -789,16 +789,18 @@ class AbstractGrid : public GridType
const Iterator& it,
const Sphere<typename GridType::ScalarType>& s) const
{
using PointType = GridType::PointType;

const VT* vv = addressOfObj(it->second);

bool test = false;
if constexpr (PointConcept<VT> || VertexConcept<VT>) {
typename GridType::PointType p;
const PointType* p;
if constexpr (PointConcept<VT>)
p = *vv;
p = vv;
else
p = vv->coord();
test = vv && s.isInside(p);
p = &(vv->coord());
test = vv && s.isInside(*p);
}
else { // check if the bbox of the value intersects the sphere
test = vv && s.intersects(boundingBox(*vv));
Expand Down
14 changes: 8 additions & 6 deletions vclib/core/include/vclib/types/pointers.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,19 @@ using RemoveConstFromPointer = std::conditional_t<

/**
* @brief Utility function that applies the unary operator '*' to the argument
* only if the object is a pointer.
* only if the object is a pointer, and returns a reference to the object
* itself.
*
* @param obj
* @return obj if it is not a pointer, or *obj if it is a pointer.
* @param[in] obj: The object to dereference.
* @return a reference to obj if it is not a pointer, or a reference to *obj if
* it is a pointer.
*
* @ingroup types
*/
template<typename T>
auto dereferencePtr(T&& obj)
auto& dereferencePtr(T&& obj)
{
if constexpr (std::is_pointer_v<std::remove_reference_t<T>>) {
if constexpr (std::is_pointer_v<RemoveRef<T>>) {
return *obj;
}
else {
Expand All @@ -113,7 +115,7 @@ auto dereferencePtr(T&& obj)
template<typename T>
auto addressOfObj(T& obj)
{
if constexpr (std::is_pointer_v<std::remove_reference_t<T>>) {
if constexpr (std::is_pointer_v<RemoveRef<T>>) {
return obj;
}
else {
Expand Down

0 comments on commit 8f871e0

Please sign in to comment.