Skip to content

Commit

Permalink
Clear cache manager when node is dirty (#214)
Browse files Browse the repository at this point in the history
* Clear cache manager when node is dirty

* Add test

* Reformat code
  • Loading branch information
msz-rai authored Nov 13, 2023
1 parent eea3102 commit 828f8cd
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 0 deletions.
4 changes: 4 additions & 0 deletions extensions/pcl/src/graph/DownSamplePointsNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ void DownSamplePointsNode::validateImpl()
auto msg = fmt::format("{} requires XYZ to be present", getName());
throw InvalidPipeline(msg);
}
// Needed to clear cache because fields in the pipeline may have changed
// In fact, the cache manager is no longer useful here
// To be kept/removed in some future refactor (when resolving comment in the `enqueueExecImpl`)
cacheManager.clear();
}

void DownSamplePointsNode::enqueueExecImpl()
Expand Down
6 changes: 6 additions & 0 deletions src/CacheManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ struct CacheManager
return result;
}

void clear()
{
cache.clear();
cacheAge.clear();
}

void setUpdated(Key key) { cacheAge.at(key) = 0; }
bool isLatest(Key key) const { return cacheAge.at(key) == 0; }
bool contains(Key key) const { return cache.contains(key); }
Expand Down
8 changes: 8 additions & 0 deletions src/graph/CompactPointsNode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
#include <repr.hpp>
#include <graph/GraphRunCtx.hpp>

void CompactPointsNode::validateImpl()
{
IPointsNodeSingleInput::validateImpl();
// Needed to clear cache because fields in the pipeline may have changed
// In fact, the cache manager is no longer useful here
// To be kept/removed in some future refactor (when resolving comment in the `enqueueExecImpl`)
cacheManager.clear();
}

void CompactPointsNode::enqueueExecImpl()
{
Expand Down
1 change: 1 addition & 0 deletions src/graph/NodesCore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ struct CompactPointsNode : IPointsNodeSingleInput
void setParameters() {}

// Node
void validateImpl() override;
void enqueueExecImpl() override;

// Node requirements
Expand Down
42 changes: 42 additions & 0 deletions test/src/graph/graphCaseTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,48 @@ TEST_F(GraphCase, NoInputNodesDoNotRequireInput)
EXPECT_RGL_SUCCESS(rgl_graph_run(pointsFromArray));
}

TEST_F(GraphCase, ChangingRequiredFieldsBetweenRuns)
{
spawnCubeOnScene(Mat3x4f::identity());

rgl_node_t useRays = nullptr, raytrace = nullptr, compact = nullptr, format = nullptr;

std::vector<std::vector<rgl_field_t>> subsequentFormatFields = {
{RGL_FIELD_XYZ_VEC3_F32},
{RGL_FIELD_XYZ_VEC3_F32, RGL_FIELD_INTENSITY_F32, RGL_FIELD_ENTITY_ID_I32},
{RGL_FIELD_XYZ_VEC3_F32, RGL_FIELD_INTENSITY_F32},
{RGL_FIELD_XYZ_VEC3_F32, RGL_FIELD_INTENSITY_F32, RGL_FIELD_DISTANCE_F32, RGL_FIELD_IS_HIT_I32},
{RGL_FIELD_XYZ_VEC3_F32, RGL_FIELD_DISTANCE_F32},
};

std::vector<rgl_mat3x4f> rays = makeLidar3dRays(360, 180, 0.36, 0.18);

ASSERT_RGL_SUCCESS(rgl_node_rays_from_mat3x4f(&useRays, rays.data(), rays.size()));
ASSERT_RGL_SUCCESS(rgl_node_raytrace(&raytrace, nullptr));
ASSERT_RGL_SUCCESS(rgl_node_points_compact(&compact));
ASSERT_RGL_SUCCESS(rgl_node_points_format(&format, subsequentFormatFields[0].data(), subsequentFormatFields[0].size()));

ASSERT_RGL_SUCCESS(rgl_graph_node_add_child(useRays, raytrace));
ASSERT_RGL_SUCCESS(rgl_graph_node_add_child(raytrace, compact));
ASSERT_RGL_SUCCESS(rgl_graph_node_add_child(compact, format));

// 1st method of changing required fields: update node with fields as a parameter
for (auto&& formatFields : subsequentFormatFields) {
ASSERT_RGL_SUCCESS(rgl_node_points_format(&format, formatFields.data(), formatFields.size()));
ASSERT_RGL_SUCCESS(rgl_graph_run(raytrace)); // Run with dirty nodes
ASSERT_RGL_SUCCESS(rgl_graph_run(raytrace)); // Run with valid nodes
}

// 2nd method of changing required fields: remove child node with user-defined fields requirements
for (auto&& formatFields : subsequentFormatFields) {
ASSERT_RGL_SUCCESS(rgl_node_points_format(&format, formatFields.data(), formatFields.size()));
ASSERT_RGL_SUCCESS(rgl_graph_run(raytrace));
ASSERT_RGL_SUCCESS(rgl_graph_node_remove_child(compact, format)); // Remove node with extra fields in the pipeline
ASSERT_RGL_SUCCESS(rgl_graph_run(raytrace));
ASSERT_RGL_SUCCESS(rgl_graph_node_add_child(compact, format)); // Restore connection for the next loop iteration
}
}

// Test for rgl_node_points_visualize. May not work inside docker (graphical environment needed). Uncomment to use.
//#if RGL_BUILD_PCL_EXTENSION
//#include <rgl/api/extensions/pcl.h>
Expand Down

0 comments on commit 828f8cd

Please sign in to comment.