From ffbe4bd9b48c6e5cc5beacd00bb1c5cda07311c2 Mon Sep 17 00:00:00 2001
From: Thomas Galland <thomas.galland@kitware.com>
Date: Tue, 5 Nov 2024 11:13:00 +0100
Subject: [PATCH] vtkHyperTreeGridAxisCut: fix floating point number comparison

Fix a bug where the cut plane wasn't considered as intersecting HTG cells due to precision
loss during floating point comparison.

(cherry picked from commit 08a5f4aab16629e7fe4313bba4a5a16dc3d017c1)
---
 Filters/HyperTree/vtkHyperTreeGridAxisCut.cxx | 7 +++++--
 Filters/HyperTree/vtkHyperTreeGridAxisCut.h   | 8 ++++++--
 2 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/Filters/HyperTree/vtkHyperTreeGridAxisCut.cxx b/Filters/HyperTree/vtkHyperTreeGridAxisCut.cxx
index 9de431ae615..f7289202585 100644
--- a/Filters/HyperTree/vtkHyperTreeGridAxisCut.cxx
+++ b/Filters/HyperTree/vtkHyperTreeGridAxisCut.cxx
@@ -11,6 +11,7 @@
 #include "vtkIdTypeArray.h"
 #include "vtkInformation.h"
 #include "vtkInformationVector.h"
+#include "vtkMathUtilities.h"
 #include "vtkNew.h"
 #include "vtkObjectFactory.h"
 #include "vtkUniformHyperTreeGrid.h"
@@ -167,7 +168,8 @@ int vtkHyperTreeGridAxisCut::ProcessTrees(vtkHyperTreeGrid* input, vtkDataObject
     const double* _size = inCursor->GetSize();
 
     // Check whether root cell is intersected by plane
-    if (origin[axis] < inter && (origin[axis] + _size[axis] >= inter))
+    if ((origin[axis] < inter && (origin[axis] + _size[axis] > inter)) ||
+      vtkMathUtilities::FuzzyCompare(origin[axis] + _size[axis], inter))
     {
       // Root is intersected by plane, descend into current child
       input->GetLevelZeroCoordinatesFromIndex(inIndex, i, j, k);
@@ -260,7 +262,8 @@ void vtkHyperTreeGridAxisCut::RecursivelyProcessTree(
       const double* size = inCursor->GetSize();
 
       // Check whether child is intersected by plane
-      if (origin[axis] < inter && (origin[axis] + size[axis] >= inter))
+      if ((origin[axis] < inter && (origin[axis] + size[axis] > inter)) ||
+        vtkMathUtilities::FuzzyCompare(origin[axis] + size[axis], inter))
       {
         // Child is intersected by plane, descend into current child
         outCursor->ToChild(outChild);
diff --git a/Filters/HyperTree/vtkHyperTreeGridAxisCut.h b/Filters/HyperTree/vtkHyperTreeGridAxisCut.h
index 9a3c54d949f..a184578506a 100644
--- a/Filters/HyperTree/vtkHyperTreeGridAxisCut.h
+++ b/Filters/HyperTree/vtkHyperTreeGridAxisCut.h
@@ -4,9 +4,13 @@
  * @class   vtkHyperTreeGridAxisCut
  * @brief   Axis aligned hyper tree grid cut
  *
- *
  * Cut an hyper tree grid along an axis aligned plane and output a hyper
- * tree grid lower dimensionality. Only works for 3D grids as inputs
+ * tree grid lower dimensionality. Only works for 3D HTGs as input.
+ *
+ * @note This filter uses fuzzy comparison to test if a plane cuts the
+ * HTG (used epsilon is DBL_EPSILON). It prevents having no cut generated
+ * inside the HTG (when the is being coincident to cell faces) or bugs
+ * related to floating point comparison.
  *
  * NB: This new (2014-16) version of the class is not to be confused with
  * earlier (2012-13) version that produced a vtkPolyData output composed of
-- 
GitLab