From d7f1dbbfaeb344538439e647568429a45b91c5ae Mon Sep 17 00:00:00 2001
From: Nicolas Vuaille <nicolas.vuaille@kitware.com>
Date: Thu, 30 Jan 2025 16:06:46 +0100
Subject: [PATCH] Add DegeneratedCell Test

With previous behavior, one boundary face was not extracted so
we saw the "interior" of the mesh. Thus coloring backface makes
the failure obvious.
---
 Filters/Geometry/Testing/CMakeLists.txt       |  1 +
 Filters/Geometry/Testing/Cxx/CMakeLists.txt   |  5 +-
 .../TestGeometryFilterDegeneratedCells.cxx    | 82 +++++++++++++++++++
 ...tGeometryFilterDegeneratedCells.png.sha512 |  1 +
 .../Data/degenerated-hexahedrons.vtu.sha512   |  1 +
 5 files changed, 89 insertions(+), 1 deletion(-)
 create mode 100644 Filters/Geometry/Testing/Cxx/TestGeometryFilterDegeneratedCells.cxx
 create mode 100644 Filters/Geometry/Testing/Data/Baseline/TestGeometryFilterDegeneratedCells.png.sha512
 create mode 100644 Testing/Data/degenerated-hexahedrons.vtu.sha512

diff --git a/Filters/Geometry/Testing/CMakeLists.txt b/Filters/Geometry/Testing/CMakeLists.txt
index aa325aa5655..46c8bca7246 100644
--- a/Filters/Geometry/Testing/CMakeLists.txt
+++ b/Filters/Geometry/Testing/CMakeLists.txt
@@ -1,4 +1,5 @@
 vtk_module_test_data(
+  Data/degenerated-hexahedrons.vtu
   Data/explicitStructuredGrid.vtu
   Data/ghost_cells.vtu
   Data/quadraticTetra01.vtu
diff --git a/Filters/Geometry/Testing/Cxx/CMakeLists.txt b/Filters/Geometry/Testing/Cxx/CMakeLists.txt
index d3f266bccda..c8dd778c71e 100644
--- a/Filters/Geometry/Testing/Cxx/CMakeLists.txt
+++ b/Filters/Geometry/Testing/Cxx/CMakeLists.txt
@@ -7,6 +7,7 @@ vtk_add_test_cxx(vtkFiltersGeometryCxxTests tests
   TestExplicitStructuredGridSurfaceFilter.cxx
   TestExtractSurfaceNonLinearSubdivision.cxx
   TestFastUnstructuredGridWithPolyDataGeometryFilter.cxx,NO_DATA
+  TestGeometryFilterDegeneratedCells.cxx
   TestImageDataToUniformGrid.cxx,NO_VALID
   TestLinearToQuadraticCellsFilter.cxx
   TestProjectSphereFilter.cxx,NO_VALID
@@ -31,4 +32,6 @@ set(all_tests
   ${tests}
   ${no_data_tests}
   )
-vtk_test_cxx_executable(vtkFiltersGeometryCxxTests all_tests)
+vtk_test_cxx_executable(vtkFiltersGeometryCxxTests all_tests
+  RENDERING_FACTORY
+)
diff --git a/Filters/Geometry/Testing/Cxx/TestGeometryFilterDegeneratedCells.cxx b/Filters/Geometry/Testing/Cxx/TestGeometryFilterDegeneratedCells.cxx
new file mode 100644
index 00000000000..5d5765ebb6e
--- /dev/null
+++ b/Filters/Geometry/Testing/Cxx/TestGeometryFilterDegeneratedCells.cxx
@@ -0,0 +1,82 @@
+// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
+// SPDX-License-Identifier: BSD-3-Clause
+
+/**
+ * Test the vtkGeometryFilter on degenerated cells.
+ *
+ * By degenerated cell we mean here a "cell that is defined using a same point several time".
+ * This was found as a community workaround to store tetrahedron as hexahedron.
+ * https://discourse.paraview.org/t/paraview-versions-greater-5-11-fail-to-display-all-mesh-elements/15810
+ *
+ * While this is not supported in VTK, the vtkGeometryFilter used to provide
+ * an acceptable output when computing the external surface, as for Rendering purpose:
+ * external faces are correctly extracted (but a lot of *inner* faces too).
+ *
+ * This test uses a dataset made of 2 tetrahedron stored as hexahedron.
+ * They are rendered with white faces and red backfaces: a missing
+ * face should make some backface visible. Also test the number of produced faces.
+ *
+ * See more discussion on https://gitlab.kitware.com/vtk/vtk/-/issues/19600
+ */
+
+#include "vtkActor.h"
+#include "vtkCamera.h"
+#include "vtkGeometryFilter.h"
+#include "vtkLogger.h"
+#include "vtkNew.h"
+#include "vtkPolyDataMapper.h"
+#include "vtkProperty.h"
+#include "vtkRenderWindow.h"
+#include "vtkRenderWindowInteractor.h"
+#include "vtkRenderer.h"
+#include "vtkTestUtilities.h"
+#include "vtkXMLUnstructuredGridReader.h"
+
+int TestGeometryFilterDegeneratedCells(int argc, char* argv[])
+{
+  char* filename =
+    vtkTestUtilities::ExpandDataFileName(argc, argv, "Data/degenerated-hexahedrons.vtu");
+
+  vtkNew<vtkXMLUnstructuredGridReader> reader;
+  reader->SetFileName(filename);
+  delete[] filename;
+
+  vtkNew<vtkGeometryFilter> geomFilter;
+  geomFilter->SetInputConnection(reader->GetOutputPort());
+  geomFilter->Update();
+
+  auto out = geomFilter->GetOutput();
+  if (out->GetNumberOfCells() != 12)
+  {
+    vtkLog(
+      ERROR, "wrong number of output cells. Has " << out->GetNumberOfCells() << " but expects 12");
+  }
+
+  vtkNew<vtkPolyDataMapper> mapper;
+  mapper->SetInputConnection(geomFilter->GetOutputPort());
+
+  vtkNew<vtkActor> actor;
+  actor->SetMapper(mapper);
+  // red backfaces to detect missing external face.
+  vtkNew<vtkProperty> backfaceProp;
+  backfaceProp->SetColor(255, 0, 0);
+  actor->SetBackfaceProperty(backfaceProp);
+
+  vtkNew<vtkRenderer> renderer;
+  renderer->AddActor(actor);
+
+  vtkNew<vtkRenderWindow> win;
+  win->AddRenderer(renderer);
+
+  vtkNew<vtkRenderWindowInteractor> interactor;
+  interactor->SetRenderWindow(win);
+  win->Render();
+
+  // orient to catch the regression from 4a46c5dd
+  vtkCamera* cam = renderer->GetActiveCamera();
+  cam->Azimuth(-90);
+
+  interactor->Start();
+
+  return EXIT_SUCCESS;
+}
diff --git a/Filters/Geometry/Testing/Data/Baseline/TestGeometryFilterDegeneratedCells.png.sha512 b/Filters/Geometry/Testing/Data/Baseline/TestGeometryFilterDegeneratedCells.png.sha512
new file mode 100644
index 00000000000..4156dd4fd63
--- /dev/null
+++ b/Filters/Geometry/Testing/Data/Baseline/TestGeometryFilterDegeneratedCells.png.sha512
@@ -0,0 +1 @@
+390f1e583d1471378e78df5575c790e1be830794c95cf35ee4c11bf277cd458aaef0f7bc41077d400c3e73cc19b5fd4d19c7a27b0851ba542aa29c7c52d29f1b
diff --git a/Testing/Data/degenerated-hexahedrons.vtu.sha512 b/Testing/Data/degenerated-hexahedrons.vtu.sha512
new file mode 100644
index 00000000000..e0ac81a544c
--- /dev/null
+++ b/Testing/Data/degenerated-hexahedrons.vtu.sha512
@@ -0,0 +1 @@
+fbd1fe5cc695ab886e88f0e8da2542cf973a09200957b27dd5c139696ce7e1f98323fb9d534613b98d885310ad9903088cb6ec64f9279a74b0994853a7da2cef
-- 
GitLab