Skip to content

FIX: Avoid potential division by zero in FilterDecimateAdaptive#48

Merged
jlblancoc merged 2 commits intodevelopfrom
fix/filter-adaptive-avoid-fpe
Mar 6, 2026
Merged

FIX: Avoid potential division by zero in FilterDecimateAdaptive#48
jlblancoc merged 2 commits intodevelopfrom
fix/filter-adaptive-avoid-fpe

Conversation

@jlblancoc
Copy link
Copy Markdown
Member

@jlblancoc jlblancoc commented Mar 5, 2026

Summary by CodeRabbit

  • Bug Fixes

    • Added validation for decimation parameters to prevent invalid inputs.
    • Improved edge-case handling to avoid processing errors when no data is present.
  • Refactor

    • Simplified version-compatibility logic for more reliable behavior across builds.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 5, 2026

📝 Walkthrough

Walkthrough

Added runtime parameter validation and an early exit to avoid division by zero; removed MRPT-version conditional compilation for insertPointFrom. MRPT minimum required version in CMake was raised from 2.11.5 to 2.15.0.

Changes

Cohort / File(s) Summary
Adaptive Decimation Logic
mp2p_icp_filters/src/FilterDecimateAdaptive.cpp
Added sanity checks for desired_output_point_count > 0 and voxel_size > 0. Early return when there are zero occupied voxels to avoid division by zero. Replaced MRPT version-dependent insertPointFrom branching with a single unconditional call.
Build Configuration
CMakeLists.txt
Bumped MRPT required version from 2.11.5 to 2.15.0 in find_package (same COMPONENTS list retained).

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐇 I nibble code and tidy trails,
I check the counts and mend the rails;
Old branches pruned, one path is true,
MRPT updated — hoppity-doo! 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main fix: preventing division by zero in FilterDecimateAdaptive by adding sanity checks and early exit logic.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/filter-adaptive-avoid-fpe

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
mp2p_icp_filters/src/FilterDecimateAdaptive.cpp (1)

191-195: Refine the warning text to match actual condition.

At Line 193, the message says “No occupied voxels”, but nVoxels here means “no voxels passed minimum_input_points_per_voxel”. This can mislead debugging.

Suggested log-message tweak
-        MRPT_LOG_WARN("FilterDecimateAdaptive: No occupied voxels. Output will be empty.");
+        MRPT_LOG_WARN(
+            "FilterDecimateAdaptive: No eligible voxels after minimum_input_points_per_voxel "
+            "filtering. No points will be inserted.");
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@mp2p_icp_filters/src/FilterDecimateAdaptive.cpp` around lines 191 - 195, The
warning message logged in FilterDecimateAdaptive when nVoxels == 0 is
misleading—nVoxels here means "no voxels passed minimum_input_points_per_voxel"
not simply "no occupied voxels"; update the MRPT_LOG_WARN call to clearly state
that no voxels met minimum_input_points_per_voxel (include the variable name or
value if desirable) so the message reflects the actual condition checked (refer
to nVoxels and minimum_input_points_per_voxel in FilterDecimateAdaptive).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@mp2p_icp_filters/src/FilterDecimateAdaptive.cpp`:
- Around line 182-183: Wrap the MRPT-2.15+ API calls with preprocessor guards:
surround registerPointFieldsFrom and prepareForInsertPointsFrom (used to obtain
mrpt::maps::CPointsMap::InsertCtx) with `#if` MRPT_VERSION >= 0x020f00 / `#endif`;
for insertPointFrom use the newer no-source signature guarded with `#if`
MRPT_VERSION >= 0x020f03, and provide an `#elif` MRPT_VERSION >= 0x020f00 fallback
to the three-argument insertPointFrom version; ensure you reference the same
symbols (registerPointFieldsFrom, prepareForInsertPointsFrom,
mrpt::maps::CPointsMap::InsertCtx, insertPointFrom) so the code compiles on MRPT
2.11.5–2.15.x.

---

Nitpick comments:
In `@mp2p_icp_filters/src/FilterDecimateAdaptive.cpp`:
- Around line 191-195: The warning message logged in FilterDecimateAdaptive when
nVoxels == 0 is misleading—nVoxels here means "no voxels passed
minimum_input_points_per_voxel" not simply "no occupied voxels"; update the
MRPT_LOG_WARN call to clearly state that no voxels met
minimum_input_points_per_voxel (include the variable name or value if desirable)
so the message reflects the actual condition checked (refer to nVoxels and
minimum_input_points_per_voxel in FilterDecimateAdaptive).

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 392f3478-e5a9-4634-9937-bee1eed472b2

📥 Commits

Reviewing files that changed from the base of the PR and between 2956344 and 4289cb7.

📒 Files selected for processing (1)
  • mp2p_icp_filters/src/FilterDecimateAdaptive.cpp

Comment on lines 182 to 183
outPc->registerPointFieldsFrom(pc);
mrpt::maps::CPointsMap::InsertCtx ctx = outPc->prepareForInsertPointsFrom(pc);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail

echo "== MRPT version constraints in CMake files =="
git ls-files | grep -i cmake | head -20

echo
echo "== Finding CMakeLists.txt files =="
fd '^CMakeLists\.txt$' | head -10

Repository: MOLAorg/mp2p_icp

Length of output: 957


🏁 Script executed:

#!/bin/bash
# Check main CMakeLists.txt for MRPT version requirements
cat CMakeLists.txt | grep -i "mrpt\|find_package" | head -30

Repository: MOLAorg/mp2p_icp

Length of output: 468


🏁 Script executed:

#!/bin/bash
# Read FilterDecimateAdaptive.cpp around lines 182-183 and 235
wc -l mp2p_icp_filters/src/FilterDecimateAdaptive.cpp
echo "=== Lines 180-190 ==="
sed -n '180,190p' mp2p_icp_filters/src/FilterDecimateAdaptive.cpp
echo
echo "=== Lines 233-237 ==="
sed -n '233,237p' mp2p_icp_filters/src/FilterDecimateAdaptive.cpp

Repository: MOLAorg/mp2p_icp

Length of output: 582


🏁 Script executed:

#!/bin/bash
# Search for MRPT_VERSION guards and insertPointFrom usage in the codebase
echo "=== Existing MRPT_VERSION guards in source ==="
rg -n 'MRPT_VERSION\s*[<>=]' mp2p_icp_filters/src --type cpp

echo
echo "=== All insertPointFrom calls ==="
rg -n 'insertPointFrom' mp2p_icp_filters/src --type cpp -B2 -A2

echo
echo "=== All prepareForInsertPointsFrom calls ==="
rg -n 'prepareForInsertPointsFrom' mp2p_icp_filters/src --type cpp -B2 -A2

echo
echo "=== All registerPointFieldsFrom calls ==="
rg -n 'registerPointFieldsFrom' mp2p_icp_filters/src --type cpp -B2 -A2

Repository: MOLAorg/mp2p_icp

Length of output: 50372


🌐 Web query:

MRPT library mrpt::maps::CPointsMap insertPointFrom API signature MRPT 2.11.5 2.15.0 2.15.3 overload prepareForInsertPointsFrom registerPointFieldsFrom

💡 Result:

MRPT mrpt::maps::CPointsMap point-copy API signatures

MRPT 2.11.5

void insertPointFrom(const mrpt::maps::CPointsMap& source, size_t sourcePointIndex);

Copies XYZ plus the built-in optional fields (intensity, ring, timestamp, color channels) if they exist in both maps. (raw.githubusercontent.com)


MRPT 2.15.0

New “generic fields” workflow: first register fields, then build an insertion context, then insert points.

bool registerPointFieldsFrom(const mrpt::maps::CPointsMap& source);

struct InsertCtx { /* ... */ };

InsertCtx prepareForInsertPointsFrom(const CPointsMap& source) const;

void insertPointFrom(
    const CPointsMap& source, size_t sourcePointIndex, const InsertCtx& ctx);

(raw.githubusercontent.com)


MRPT 2.15.3

insertPointFrom() drops the redundant source argument (the source buffers are already inside InsertCtx). Also registerPointFieldsFrom() expands to cover more field types.

bool registerPointFieldsFrom(const mrpt::maps::CPointsMap& source); // now includes double, uint8, etc.

struct InsertCtx { /* ... */ };

[[nodiscard]] InsertCtx prepareForInsertPointsFrom(const CPointsMap& source) const;

void insertPointFrom(size_t sourcePointIndex, const InsertCtx& ctx);

(raw.githubusercontent.com)

Citations:


Add MRPT-version guards for InsertCtx / insertPointFrom / registerPointFieldsFrom calls.

Lines 182–183 and 235 use APIs introduced in MRPT 2.15.0 without version guards. The project requires MRPT ≥ 2.11.5 (see CMakeLists.txt), so these calls will cause build failures on 2.11.5–2.15.2.

The codebase pattern is already established in FilterDecimate.cpp and nearly every other filter. Add:

  • #if MRPT_VERSION >= 0x020f00 around registerPointFieldsFrom and prepareForInsertPointsFrom (lines 182–183)
  • #if MRPT_VERSION >= 0x020f03 for the insertPointFrom signature without source (line 235), with #elif MRPT_VERSION >= 0x020f00 fallback for the 3-argument form
Compatibility-safe patch pattern
-    outPc->registerPointFieldsFrom(pc);
-    mrpt::maps::CPointsMap::InsertCtx ctx = outPc->prepareForInsertPointsFrom(pc);
+#if MRPT_VERSION >= 0x020f00  // 2.15.0
+    outPc->registerPointFieldsFrom(pc);
+    mrpt::maps::CPointsMap::InsertCtx ctx = outPc->prepareForInsertPointsFrom(pc);
+#endif
@@
-            outPc->insertPointFrom(ptIdx, ctx);
+#if MRPT_VERSION >= 0x020f03  // 2.15.3
+            outPc->insertPointFrom(ptIdx, ctx);
+#elif MRPT_VERSION >= 0x020f00  // 2.15.0
+            outPc->insertPointFrom(pc, ptIdx, ctx);
+#else
+            outPc->insertPointFrom(pc, ptIdx);
+#endif
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@mp2p_icp_filters/src/FilterDecimateAdaptive.cpp` around lines 182 - 183, Wrap
the MRPT-2.15+ API calls with preprocessor guards: surround
registerPointFieldsFrom and prepareForInsertPointsFrom (used to obtain
mrpt::maps::CPointsMap::InsertCtx) with `#if` MRPT_VERSION >= 0x020f00 / `#endif`;
for insertPointFrom use the newer no-source signature guarded with `#if`
MRPT_VERSION >= 0x020f03, and provide an `#elif` MRPT_VERSION >= 0x020f00 fallback
to the three-argument insertPointFrom version; ensure you reference the same
symbols (registerPointFieldsFrom, prepareForInsertPointsFrom,
mrpt::maps::CPointsMap::InsertCtx, insertPointFrom) so the code compiles on MRPT
2.11.5–2.15.x.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
CMakeLists.txt (1)

58-64: ⚠️ Potential issue | 🟠 Major

Bump declared MRPT minimum to 2.15.4 to match actual code requirements.

CMakeLists.txt line 58 declares MRPT 2.15.0, but multiple filters require MRPT 2.15.4: FilterSOR.cpp explicitly throws at runtime if MRPT_VERSION < 0x020f04, and FilterByRing.cpp, FilterByExpression.cpp, and FilterAbsoluteTimestamp.cpp all guard on >= 0x020f04 (2.15.4). This allows a build to succeed but fail at runtime on the older version.

Proposed fix
-find_package(MRPT 2.15.0 REQUIRED COMPONENTS
+find_package(MRPT 2.15.4 REQUIRED COMPONENTS
   containers
   tfest
   maps
   gui
   topography
 )
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@CMakeLists.txt` around lines 58 - 64, Update the MRPT minimum version
declared in CMakeLists.txt to 2.15.4 so the configured build requirement matches
runtime checks; specifically change the version in the find_package(MRPT ...)
call to 2.15.4. This aligns CMake with the version guards and runtime throw in
FilterSOR.cpp and the preprocessor/version checks in FilterByRing.cpp,
FilterByExpression.cpp, and FilterAbsoluteTimestamp.cpp that require MRPT >=
2.15.4.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Outside diff comments:
In `@CMakeLists.txt`:
- Around line 58-64: Update the MRPT minimum version declared in CMakeLists.txt
to 2.15.4 so the configured build requirement matches runtime checks;
specifically change the version in the find_package(MRPT ...) call to 2.15.4.
This aligns CMake with the version guards and runtime throw in FilterSOR.cpp and
the preprocessor/version checks in FilterByRing.cpp, FilterByExpression.cpp, and
FilterAbsoluteTimestamp.cpp that require MRPT >= 2.15.4.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 93f5aba8-7119-4f74-9b08-2b66811a2c7b

📥 Commits

Reviewing files that changed from the base of the PR and between 4289cb7 and 8bcb772.

📒 Files selected for processing (1)
  • CMakeLists.txt

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 5, 2026

Codecov Report

❌ Patch coverage is 71.42857% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 78.59%. Comparing base (2956344) to head (8bcb772).
⚠️ Report is 3 commits behind head on develop.

Files with missing lines Patch % Lines
mp2p_icp_filters/src/FilterDecimateAdaptive.cpp 71.42% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop      #48      +/-   ##
===========================================
- Coverage    78.60%   78.59%   -0.01%     
===========================================
  Files          190      190              
  Lines        10564    10569       +5     
  Branches       979      980       +1     
===========================================
+ Hits          8304     8307       +3     
- Misses        2260     2262       +2     
Files with missing lines Coverage Δ
mp2p_icp_filters/src/FilterDecimateAdaptive.cpp 91.01% <71.42%> (-1.85%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jlblancoc jlblancoc merged commit ac1448d into develop Mar 6, 2026
14 of 15 checks passed
@jlblancoc jlblancoc deleted the fix/filter-adaptive-avoid-fpe branch March 6, 2026 09:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant