From 63aa094d04be4fb8a9a572107194e4378c4dd6e2 Mon Sep 17 00:00:00 2001
From: Markus Frank <Markus.Frank@cern.ch>
Date: Wed, 17 Jan 2024 14:12:42 +0100
Subject: [PATCH] Fix creation of tessellated shapes from CAD files.

---
 DDCAD/src/ASSIMPReader.cpp | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)

diff --git a/DDCAD/src/ASSIMPReader.cpp b/DDCAD/src/ASSIMPReader.cpp
index 5a22c5b3b..78e9e7f5c 100644
--- a/DDCAD/src/ASSIMPReader.cpp
+++ b/DDCAD/src/ASSIMPReader.cpp
@@ -111,16 +111,27 @@ ASSIMPReader::readVolumes(const std::string& source, double unit_length)  const
         name = _toString(result.size(), "tessellated_%ld");
       }
 
+      /// NOTE: IMPORTANT!
+      ///       ALWAYS add facets using the physical vertices!
+      ///       TGeoTessellated takes care that the vertex map is unique and
+      ///       assigns the proper indices to the facet.
       for(unsigned int i=0; i < mesh->mNumFaces; i++)  {
         const unsigned int* idx  = mesh->mFaces[i].mIndices;
         bool degenerated = dd4hep::cad::facetIsDegenerated({vertices[idx[0]], vertices[idx[1]], vertices[idx[2]]});
-        if ( !degenerated )   {
-          shape->AddFacet(idx[0], idx[1], idx[2]);
-          continue;
+        if ( degenerated )   {
+          printout(DEBUG, "ASSIMPReader", "+++ %s: Drop degenerated facet: %d %d %d",
+                   name.c_str(), idx[0], idx[1], idx[2]);
+        }
+        else if ( mesh->mFaces[i].mNumIndices == 3 )   {
+          shape->AddFacet(vertices[idx[0]], vertices[idx[1]], vertices[idx[2]]);
+        }
+        else if ( mesh->mFaces[i].mNumIndices == 4 )   {
+          shape->AddFacet(vertices[idx[0]], vertices[idx[1]], vertices[idx[2]], vertices[idx[3]]);
+        }
+        else  {
+          printout(INFO, "ASSIMPReader", "+++ %s: Fancy facet with %d indices.",
+                   name.c_str(), mesh->mFaces[i].mNumIndices);
         }
-        printout(INFO, "ASSIMPReader", "+++ %s: Drop degenerated facet: %d %d %d",
-                 name.c_str(), idx[0], idx[1], idx[2]);
-
       }
       if ( shape->GetNfacets() > 2 )   {
         std::string mat_name;
-- 
GitLab