Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-24 10:41:57

0001 /// @file ExtrudedStruct.h
0002 /// @author Mihaela Gheata (mihaela.gheata@cern.ch)
0003 
0004 #ifndef VECGEOM_EXTRUDED_STRUCT_H
0005 #define VECGEOM_EXTRUDED_STRUCT_H
0006 
0007 #include "VecGeom/base/Config.h"
0008 #include "VecGeom/base/Triangulation.h"
0009 
0010 #include "VecGeom/volumes/PlanarPolygonUtilities.h"
0011 #include "VecGeom/volumes/PolygonalShell.h"
0012 #include "VecGeom/volumes/TessellatedSection.h"
0013 #include "VecGeom/volumes/TessellatedStruct.h"
0014 #include <vector>
0015 
0016 namespace vecgeom {
0017 
0018 VECGEOM_DEVICE_FORWARD_DECLARE(class ExtrudedStruct;);
0019 VECGEOM_DEVICE_DECLARE_CONV(class, ExtrudedStruct);
0020 VECGEOM_DEVICE_DECLARE_CONV(struct, XtruVertex2);
0021 VECGEOM_DEVICE_DECLARE_CONV(struct, XtruSection);
0022 
0023 inline namespace VECGEOM_IMPL_NAMESPACE {
0024 
0025 // Structure wrapping either a polygonal shell helper in case of two
0026 // extruded sections or a tessellated structure in case of more
0027 
0028 struct XtruVertex2 {
0029   Precision x;
0030   Precision y;
0031 };
0032 
0033 struct XtruSection {
0034   Vector3D<Precision> fOrigin; // Origin of the section
0035   Precision fScale;
0036 };
0037 
0038 class ExtrudedStruct {
0039 
0040   // template <typename U>
0041   // using vector_t = vecgeom::Vector<U>;
0042   template <typename U>
0043   using vector_t = vecgeom::Vector<U>;
0044 
0045   struct FacetInd {
0046     size_t ind1{0}, ind2{0}, ind3{0};
0047 
0048     FacetInd() = default;
0049     VECCORE_ATT_HOST_DEVICE
0050     FacetInd(size_t i1, size_t i2, size_t i3) : ind1(i1), ind2(i2), ind3(i3) {}
0051   };
0052 
0053   /// @brief Reuse whole-mesh support flags in the section accelerator.
0054   void CacheExitFacetSupport(size_t firstLateralFacet)
0055   {
0056     auto &runtime = fTslRuntimeHelper;
0057     // Global caps always support the solid, independently of the bounded mesh
0058     // certification. Lateral planes reuse that cache, avoiding a vertex scan
0059     // for each side when an extrusion has many sections.
0060     const size_t lateralEnd = firstLateralFacet + 2 * GetNSegments() * GetNVertices();
0061     for (size_t facetIndex = 0; facetIndex < firstLateralFacet; ++facetIndex)
0062       runtime.fFacetSupports[facetIndex] = 1;
0063     for (size_t facetIndex = lateralEnd; facetIndex < runtime.fNFacets; ++facetIndex)
0064       runtime.fFacetSupports[facetIndex] = 1;
0065 
0066     // The section accelerator already keeps one convexity byte per quad. No
0067     // section query consumes its construction-local value, so promote it to
0068     // the global support result used by the public exit-normal contract.
0069     for (size_t section = 0; section < fTslSections.size(); ++section) {
0070       auto *sectionHelper = fTslSections[section];
0071       for (size_t facet = 0; facet < sectionHelper->GetNfacets(); ++facet) {
0072         const size_t triangle = firstLateralFacet + 2 * (section * sectionHelper->GetNfacets() + facet);
0073         sectionHelper->SetFacetSupportsSolid(facet, runtime.FacetSupports(triangle));
0074       }
0075     }
0076   }
0077 
0078   template <typename Facets_t>
0079   void TriangulatePolygon(Facets_t &facets) const
0080   {
0081     const bool triangulated = TriangulateEarClipping(
0082         GetNVertices(), facets, [](size_t i1, size_t i2, size_t i3) { return FacetInd(i1, i2, i3); },
0083         [this](size_t i1, size_t i2, size_t i3) { return IsConvexSide(i1, i2, i3); },
0084         [this](size_t i1, size_t i2, size_t i3, size_t index) { return IsPointInside(i1, i2, i3, index); });
0085     VECGEOM_VALIDATE(triangulated, << "Triangulation failed");
0086     (void)triangulated;
0087   }
0088 
0089 public:
0090   struct DeprecatedTslHelper {
0091     // TODO(VecGeom-release-transition): remove these public compatibility fields
0092     // after a VecGeom release has given Geant4 time to switch to GetMeshHelper().
0093     // They emulate the old ExtrudedStruct::fTslHelper.fVertices/fFacets[*]->fIndices
0094     // export layout only; navigation must use fTslRuntimeHelper instead.
0095     vector_t<Vector3D<Precision>> fVertices;
0096     vector_t<TriangleFacet<Precision>> fFacetStorage;
0097     vector_t<TriangleFacet<Precision> *> fFacets;
0098 
0099     // TODO(VecGeom-release-transition): remove together with the compatibility
0100     // fields above. New visualization/export code should use GetMeshHelper().
0101     void FillFrom(TessellatedStruct<3, Precision> const &tsl)
0102     {
0103       fVertices.clear();
0104       fFacetStorage.clear();
0105       fFacets.clear();
0106 
0107       fVertices.reserve(tsl.fVertices.size());
0108       for (size_t i = 0; i < tsl.fVertices.size(); ++i)
0109         fVertices.push_back(tsl.fVertices[i]);
0110 
0111       fFacetStorage.reserve(tsl.fFacets.size());
0112       fFacets.reserve(tsl.fFacets.size());
0113       for (size_t i = 0; i < tsl.fFacets.size(); ++i) {
0114         const auto *facet = tsl.fFacets[i];
0115         fFacetStorage.push_back(*facet);
0116         fFacets.push_back(&fFacetStorage[fFacetStorage.size() - 1]);
0117       }
0118     }
0119   };
0120 
0121   class ExtrudedMeshHelper {
0122   private:
0123     ExtrudedStruct const &fXtru;
0124     std::vector<FacetInd> fCapFacets;
0125 
0126     size_t MeshVertexIndex(size_t ivert, size_t isect) const;
0127 
0128   public:
0129     // Temporary API view for visualization/export code; navigation keeps using fTslRuntimeHelper.
0130     explicit ExtrudedMeshHelper(ExtrudedStruct const &xtru);
0131 
0132     size_t GetNvertices() const;
0133 
0134     Vector3D<Precision> GetVertex(size_t index) const;
0135 
0136     size_t GetNfacets() const;
0137 
0138     void GetFacetVertices(size_t ifacet, size_t (&indices)[3]) const;
0139   };
0140 
0141   bool fIsSxtru                  = false;                ///< Flag for sxtru representation
0142   bool fInitialized              = false;                ///< Flag for initialization
0143   Precision *fZPlanes            = nullptr;              ///< Z position of planes
0144   mutable Precision fCubicVolume = 0.;                   ///< Cubic volume
0145   mutable Precision fSurfaceArea = 0.;                   ///< Surface area
0146   PolygonalShell fSxtruHelper;                           ///< Sxtru helper
0147   TessellatedRuntimeStruct<Precision> fTslRuntimeHelper; ///< The tessellated helper for navigation
0148   // TODO(VecGeom-release-transition): remove this old-layout export shim after
0149   // a VecGeom release. Geant4 and other visualization/export users should use
0150   // GetMeshHelper(), not the internal tessellated navigation representation.
0151   DeprecatedTslHelper fTslHelper;
0152   bool fUseTslSections = false;                           ///< Use tessellated section helper
0153   vector_t<TessellatedSection<Precision> *> fTslSections; ///< Tessellated sections
0154   vector_t<XtruVertex2> fVertices;                        ///< Polygon vertices
0155   vector_t<XtruSection> fSections;                        ///< Vector of sections
0156   PlanarPolygon fPolygon;                                 ///< Planar polygon
0157   Precision fProjectionSafetySqr  = 0.; ///< Squared polygon separation required for projection; zero disables it.
0158   Precision fProjectionZTolerance = 0.; ///< Terminal-cap exclusion for the projected classifier.
0159 
0160 public:
0161   /** @brief Dummy constructor */
0162   VECCORE_ATT_HOST_DEVICE
0163   ExtrudedStruct() {}
0164 
0165   /** @brief Constructor wrapping pre-copied runtime data without allocation */
0166   VECCORE_ATT_HOST_DEVICE
0167   ExtrudedStruct(size_t ntriangles, TriangularTile<Precision> *triangles, FacetBVH_t *bvh, Precision *zplanes,
0168                  XtruSection *sections, size_t nsections, TessellatedSection<Precision> **tslSections,
0169                  size_t ntslSections, bool useTslSections, unsigned char *facetSupports)
0170       : fIsSxtru(false), fInitialized(true), fZPlanes(zplanes),
0171         fTslRuntimeHelper(ntriangles, triangles, bvh, facetSupports), fUseTslSections(useTslSections),
0172         fTslSections(tslSections, static_cast<int>(ntslSections)), fSections(sections, static_cast<int>(nsections))
0173   {
0174   }
0175 
0176   /** @brief Constructor providing polygon vertices and sections */
0177   ExtrudedStruct(int nvertices, XtruVertex2 const *vertices, int nsections, XtruSection const *sections)
0178   {
0179     Initialize(nvertices, vertices, nsections, sections);
0180   }
0181 
0182   // Constructor used during Specialization for nsections == 2
0183   ExtrudedStruct(size_t nvertices, const Precision *x, const Precision *y, Precision zmin, Precision zmax)
0184   {
0185     XtruVertex2 *vertices = new XtruVertex2[nvertices];
0186     XtruSection *sections = new XtruSection[2];
0187     for (size_t i = 0; i < nvertices; ++i) {
0188       vertices[i].x = x[i];
0189       vertices[i].y = y[i];
0190     }
0191 
0192     sections[0].fScale = 1.;
0193     sections[0].fOrigin.Set(0., 0., zmin);
0194 
0195     sections[1].fScale = 1.;
0196     sections[1].fOrigin.Set(0., 0., zmax);
0197 
0198     Initialize(nvertices, vertices, 2, sections);
0199     delete[] vertices;
0200     delete[] sections;
0201   }
0202 
0203   VECGEOM_FORCE_INLINE
0204   VECCORE_ATT_HOST_DEVICE
0205   int FindZSegment(Precision const &pointZ) const
0206   {
0207     int index              = -1;
0208     Precision const *begin = fZPlanes;
0209     Precision const *end   = fZPlanes + fSections.size();
0210     while (begin < end - 1 && pointZ - kTolerance > *begin) {
0211       ++index;
0212       ++begin;
0213     }
0214     if (begin == end) return index;
0215     if (pointZ + kTolerance > *begin) return (index + 1);
0216     return index;
0217   }
0218 
0219   /** @brief Initialize based on vertices and sections */
0220   void Initialize(int nvertices, XtruVertex2 const *vertices, int nsections, XtruSection const *sections)
0221   {
0222     if (fInitialized) return;
0223     VECGEOM_ASSERT(nsections > 1 && nvertices > 2);
0224     fZPlanes         = new Precision[nsections];
0225     fZPlanes[0]      = sections[0].fOrigin.z();
0226     bool degenerated = false;
0227     for (size_t i = 1; i < (size_t)nsections; ++i) {
0228       fZPlanes[i] = sections[i].fOrigin.z();
0229       // Make sure sections are defined in increasing order
0230       VECGEOM_VALIDATE(fZPlanes[i] >= fZPlanes[i - 1], << "Extruded sections not defined in increasing Z order");
0231       if (fZPlanes[i] - fZPlanes[i - 1] < kTolerance) degenerated = true;
0232     }
0233     if (!degenerated) fUseTslSections = true;
0234     // Check if this is an SXtru
0235     if (nsections == 2 && (sections[0].fOrigin - sections[1].fOrigin).Perp2() < kTolerance &&
0236         Abs(sections[0].fScale - sections[1].fScale) < kTolerance)
0237       fIsSxtru = true;
0238     if (fIsSxtru) {
0239       // Put vertices in arrays
0240       Precision *x = new Precision[nvertices];
0241       Precision *y = new Precision[nvertices];
0242       for (size_t i = 0; i < (size_t)nvertices; ++i) {
0243         x[i] = sections[0].fOrigin.x() + sections[0].fScale * vertices[i].x;
0244         y[i] = sections[0].fOrigin.y() + sections[0].fScale * vertices[i].y;
0245       }
0246       fSxtruHelper.Init(nvertices, x, y, sections[0].fOrigin[2], sections[1].fOrigin[2]);
0247       delete[] x;
0248       delete[] y;
0249     }
0250     // Create the tessellated structure in all cases
0251     CreateTessellated(nvertices, vertices, nsections, sections);
0252     // Only a single positive-scale section has one unambiguous inverse projection.
0253     // Keep ill-conditioned polygons and section stacks on the tessellated path.
0254     if (!fIsSxtru && !fPolygon.IsConvex() && !degenerated && nsections == 2 && sections[0].fScale > 0. &&
0255         sections[1].fScale > 0.) {
0256       const auto shift           = sections[1].fOrigin - sections[0].fOrigin;
0257       const Precision deltaScale = sections[1].fScale - sections[0].fScale;
0258       Precision maxVelocitySqr = 0., polygonSize = 0., slopeRounding = 0.;
0259       bool resolved = std::isfinite(shift.z()) && std::isfinite(deltaScale) &&
0260                       fTslRuntimeHelper.fNFacets == 4 * size_t(nvertices) - 4;
0261       for (int i = 0; i < nvertices; ++i) {
0262         const Precision vx          = (shift.x() + deltaScale * vertices[i].x) / shift.z();
0263         const Precision vy          = (shift.y() + deltaScale * vertices[i].y) / shift.z();
0264         const Precision velocitySqr = vx * vx + vy * vy;
0265         resolved &= std::isfinite(velocitySqr);
0266         maxVelocitySqr = Max(maxVelocitySqr, velocitySqr);
0267         polygonSize    = Max(polygonSize, Max(Abs(vertices[i].x), Abs(vertices[i].y)));
0268       }
0269       // Reserve seven binary guard bits for accumulated roundoff in vertex
0270       // construction, section interpolation and finite-edge projection. The
0271       // factor 128 is an engineering allowance, not a tight error bound; it
0272       // rejects poorly resolved edges and widens the tessellated fallback band
0273       // without changing the geometric surface tolerance.
0274       const Precision projectionRoundoff = 128. * kEpsilonT<Precision>;
0275       for (int i = 0; i < nvertices; ++i) {
0276         const int j        = (i + 1) % nvertices;
0277         const Precision dx = vertices[j].x - vertices[i].x, dy = vertices[j].y - vertices[i].y;
0278         const Precision lengthSqr = dx * dx + dy * dy;
0279         const Precision edgeError = projectionRoundoff * polygonSize;
0280         resolved &= std::isfinite(lengthSqr) && std::isfinite(1. / lengthSqr) && lengthSqr > edgeError * edgeError;
0281         // Contains stores slopes with NonZero(dy); cover its horizontal crossing
0282         // displacement as well as ordinary arithmetic roundoff.
0283         const Precision slopeError = dy == 0. ? 0. : Abs(dx) * Abs(NonZero(dy) / dy - 1.);
0284         resolved &= std::isfinite(slopeError);
0285         slopeRounding = Max(slopeRounding, slopeError);
0286       }
0287       // Include pre-cancellation coordinates: a large polygon and opposite section
0288       // offset can produce small facets while projection still loses large-coordinate bits.
0289       const Precision coordinateSize =
0290           Max(Precision(1.), polygonSize * Max(sections[0].fScale, sections[1].fScale) +
0291                                  Max(sections[0].fOrigin.Abs().Max(), sections[1].fOrigin.Abs().Max()));
0292       const Precision rounding = projectionRoundoff * coordinateSize;
0293       const Precision projectedBand =
0294           (kTolerance * Sqrt(1. + maxVelocitySqr) + rounding * (1. + Sqrt(maxVelocitySqr))) /
0295               Min(sections[0].fScale, sections[1].fScale) +
0296           slopeRounding;
0297       if (resolved && std::isfinite(projectedBand * projectedBand) && projectedBand > 0.) {
0298         fProjectionSafetySqr  = projectedBand * projectedBand;
0299         fProjectionZTolerance = kTolerance + rounding;
0300       }
0301     }
0302     fInitialized = true;
0303   }
0304 
0305   /** @brief Construct facets based on vertices and sections */
0306   void CreateTessellated(size_t nvertices, XtruVertex2 const *vertices, size_t nsections, XtruSection const *sections)
0307   {
0308     TessellatedStruct<3, Precision> tsl_builder_struct;
0309 
0310     // Store sections
0311     for (size_t isect = 0; isect < nsections; ++isect)
0312       fSections.push_back(sections[isect]);
0313 
0314     // Create the polygon
0315     Precision *vx = new Precision[nvertices];
0316     Precision *vy = new Precision[nvertices];
0317     for (size_t i = 0; i < nvertices; ++i) {
0318       vx[i] = vertices[i].x;
0319       vy[i] = vertices[i].y;
0320     }
0321     fPolygon.Init(nvertices, vx, vy);
0322     fUseTslSections &= fPolygon.IsConvex();
0323     if (fUseTslSections) {
0324       // Create tessellated sections
0325       fTslSections.reserve(nsections - 1);
0326       for (size_t i = 0; i < nsections - 1; ++i) {
0327         fTslSections.push_back(
0328             new TessellatedSection<Precision>(nvertices, sections[i].fOrigin.z(), sections[i + 1].fOrigin.z()));
0329       }
0330     }
0331     VectorBase<FacetInd> facets(nvertices);
0332     // TRIANGULATE POLYGON
0333     TriangulatePolygon(facets);
0334     // We have all index facets, create now the real facets
0335     // Bottom (normals pointing down)
0336     for (size_t i = 0; i < facets.size(); ++i) {
0337       size_t i1 = facets[i].ind1;
0338       size_t i2 = facets[i].ind2;
0339       size_t i3 = facets[i].ind3;
0340       tsl_builder_struct.AddTriangularFacet(VertexToSection(i1, 0), VertexToSection(i2, 0), VertexToSection(i3, 0));
0341     }
0342     const size_t firstLateralFacet = tsl_builder_struct.fFacets.size();
0343     // Sections
0344     for (size_t isect = 0; isect < nsections - 1; ++isect) {
0345       for (size_t i = 0; i < (size_t)nvertices; ++i) {
0346         size_t j = (i + 1) % nvertices;
0347         // Quadrilateral isect:(j, i)  isect+1: (i, j)
0348         tsl_builder_struct.AddQuadrilateralFacet(VertexToSection(j, isect), VertexToSection(i, isect),
0349                                                  VertexToSection(i, isect + 1), VertexToSection(j, isect + 1));
0350         if (fUseTslSections)
0351           fTslSections[isect]->AddQuadrilateralFacet(VertexToSection(j, isect), VertexToSection(i, isect),
0352                                                      VertexToSection(i, isect + 1), VertexToSection(j, isect + 1));
0353       }
0354     }
0355     // Top (normals pointing up)
0356     for (size_t i = 0; i < facets.size(); ++i) {
0357       size_t i1 = facets[i].ind1;
0358       size_t i2 = facets[i].ind2;
0359       size_t i3 = facets[i].ind3;
0360       tsl_builder_struct.AddTriangularFacet(VertexToSection(i1, nsections - 1), VertexToSection(i3, nsections - 1),
0361                                             VertexToSection(i2, nsections - 1));
0362     }
0363     // Now close the tessellated structure
0364     tsl_builder_struct.Close();
0365     fTslHelper.FillFrom(tsl_builder_struct);
0366     fTslRuntimeHelper.InitFrom(tsl_builder_struct, !fIsSxtru);
0367     // General Extruded navigation stores its conservative support classification in
0368     // the Tessellated per-triangle array. Simple extrusions already cache the
0369     // same information by side in PolygonalShell.
0370     if (!fIsSxtru) CacheExitFacetSupport(firstLateralFacet);
0371 #ifndef VECCORE_CUDA
0372     if (getenv("NOTSLSECTIONS")) {
0373       // convenience mode to compare tsl sections against pure tessellated
0374       fUseTslSections = false;
0375     }
0376 #endif
0377   }
0378 
0379   /** @brief Get the number of sections */
0380   VECCORE_ATT_HOST_DEVICE
0381   VECGEOM_FORCE_INLINE
0382   size_t GetNSections() const { return fSections.size(); }
0383 
0384   /** @brief Get the number of planes */
0385   VECCORE_ATT_HOST_DEVICE
0386   VECGEOM_FORCE_INLINE
0387   size_t GetNSegments() const { return (fSections.size() - 1); }
0388 
0389   /** @brief Get section i */
0390   VECCORE_ATT_HOST_DEVICE
0391   VECGEOM_FORCE_INLINE
0392   XtruSection GetSection(int i) const { return fSections[i]; }
0393 
0394   /** @brief Get the number of vertices */
0395   VECCORE_ATT_HOST_DEVICE
0396   VECGEOM_FORCE_INLINE
0397   size_t GetNVertices() const { return fPolygon.GetNVertices(); }
0398 
0399   ExtrudedMeshHelper GetMeshHelper() const { return ExtrudedMeshHelper(*this); }
0400 
0401   /** @brief Get the polygon vertex i */
0402   VECCORE_ATT_HOST_DEVICE
0403   VECGEOM_FORCE_INLINE
0404   void GetVertex(int i, Precision &x, Precision &y) const
0405   {
0406     x = fPolygon.GetVertices().x()[i];
0407     y = fPolygon.GetVertices().y()[i];
0408   }
0409 
0410   /** Return true if i is on the line through i1, i2 */
0411   VECGEOM_FORCE_INLINE
0412   bool IsSameLine(size_t i, size_t i1, size_t i2) const
0413   {
0414     return PlanarPolygonUtilities::IsSameLine(fPolygon, i, i1, i2);
0415   }
0416 
0417   /** @brief Return true if point i is on the line through i1, i2 and lies between i1 and i2 */
0418   VECGEOM_FORCE_INLINE
0419   bool IsSameLineSegment(size_t i, size_t i1, size_t i2) const
0420   {
0421     return PlanarPolygonUtilities::IsSameLineSegment(fPolygon, i, i1, i2);
0422   }
0423 
0424   /** @brief Return true if i and j are on the same side of the line through i1, i2 */
0425   VECGEOM_FORCE_INLINE
0426   bool IsSameSide(size_t i, size_t j, size_t i1, size_t i2) const
0427   {
0428     return PlanarPolygonUtilities::IsSameSide(fPolygon, i, j, i1, i2);
0429   }
0430 
0431   /** Return true if i is inside of triangle (i1, i2, i3) or on its edges, else returns false */
0432   VECGEOM_FORCE_INLINE
0433   bool IsPointInside(size_t i1, size_t i2, size_t i3, size_t i) const
0434   {
0435     return PlanarPolygonUtilities::IsPointInsideTriangle(fPolygon, i1, i2, i3, i);
0436   }
0437 
0438   /** @brief Check if the polygon segments (i0, i1) and (i1, i2) make a convex side */
0439   VECGEOM_FORCE_INLINE
0440   bool IsConvexSide(size_t i0, size_t i1, size_t i2) const
0441   {
0442     return PlanarPolygonUtilities::IsConvexCorner(fPolygon, i0, i1, i2);
0443   }
0444 
0445   /** @brief Returns convexity of polygon */
0446   VECCORE_ATT_HOST_DEVICE
0447   VECGEOM_FORCE_INLINE
0448   bool IsConvexPolygon() const { return fPolygon.IsConvex(); }
0449 
0450   /** @brief Returns the coordinates for a given vertex index at a given section */
0451   VECCORE_ATT_HOST_DEVICE
0452   VECGEOM_FORCE_INLINE
0453   Vector3D<Precision> VertexToSection(size_t ivert, size_t isect) const
0454   {
0455     const Precision *x = fPolygon.GetVertices().x();
0456     const Precision *y = fPolygon.GetVertices().y();
0457     Vector3D<Precision> vert(fSections[isect].fOrigin[0] + fSections[isect].fScale * x[ivert],
0458                              fSections[isect].fOrigin[1] + fSections[isect].fScale * y[ivert],
0459                              fSections[isect].fOrigin[2]);
0460     return vert;
0461   }
0462 };
0463 
0464 inline size_t ExtrudedStruct::ExtrudedMeshHelper::MeshVertexIndex(size_t ivert, size_t isect) const
0465 {
0466   return isect * fXtru.GetNVertices() + ivert;
0467 }
0468 
0469 inline ExtrudedStruct::ExtrudedMeshHelper::ExtrudedMeshHelper(ExtrudedStruct const &xtru) : fXtru(xtru)
0470 {
0471   const size_t nvertices = fXtru.GetNVertices();
0472   // Rebuild only the 2D cap triangulation; 3D vertices are redirected through VertexToSection().
0473   VectorBase<FacetInd> capFacets(nvertices);
0474   fXtru.TriangulatePolygon(capFacets);
0475   fCapFacets.reserve(capFacets.size());
0476   for (size_t i = 0; i < capFacets.size(); ++i)
0477     fCapFacets.push_back(capFacets[i]);
0478 }
0479 
0480 inline size_t ExtrudedStruct::ExtrudedMeshHelper::GetNvertices() const
0481 {
0482   return fXtru.GetNVertices() * fXtru.GetNSections();
0483 }
0484 
0485 inline Vector3D<Precision> ExtrudedStruct::ExtrudedMeshHelper::GetVertex(size_t index) const
0486 {
0487   const size_t nvertices = fXtru.GetNVertices();
0488   return fXtru.VertexToSection(index % nvertices, index / nvertices);
0489 }
0490 
0491 inline size_t ExtrudedStruct::ExtrudedMeshHelper::GetNfacets() const
0492 {
0493   const size_t nsideFacets = 2 * fXtru.GetNVertices() * (fXtru.GetNSections() - 1);
0494   return 2 * fCapFacets.size() + nsideFacets;
0495 }
0496 
0497 inline void ExtrudedStruct::ExtrudedMeshHelper::GetFacetVertices(size_t ifacet, size_t (&indices)[3]) const
0498 {
0499   const size_t nvertices     = fXtru.GetNVertices();
0500   const size_t nsections     = fXtru.GetNSections();
0501   const size_t ncapFacets    = fCapFacets.size();
0502   const size_t nsideFacets   = 2 * nvertices * (nsections - 1);
0503   const size_t firstTopFacet = ncapFacets + nsideFacets;
0504 
0505   if (ifacet < ncapFacets) {
0506     const auto &facet = fCapFacets[ifacet];
0507     indices[0]        = MeshVertexIndex(facet.ind1, 0);
0508     indices[1]        = MeshVertexIndex(facet.ind2, 0);
0509     indices[2]        = MeshVertexIndex(facet.ind3, 0);
0510     return;
0511   }
0512 
0513   if (ifacet < firstTopFacet) {
0514     // Match the side-face split used by the navigation tessellation: (j,i,i+1) and (j,i+1,j+1).
0515     const size_t sideFacet = ifacet - ncapFacets;
0516     const size_t isect     = sideFacet / (2 * nvertices);
0517     const size_t rem       = sideFacet % (2 * nvertices);
0518     const size_t i         = rem / 2;
0519     const size_t j         = (i + 1) % nvertices;
0520     const bool secondTri   = (rem % 2) != 0;
0521 
0522     indices[0] = MeshVertexIndex(j, isect);
0523     indices[1] = secondTri ? MeshVertexIndex(i, isect + 1) : MeshVertexIndex(i, isect);
0524     indices[2] = secondTri ? MeshVertexIndex(j, isect + 1) : MeshVertexIndex(i, isect + 1);
0525     return;
0526   }
0527 
0528   const auto &facet = fCapFacets[ifacet - firstTopFacet];
0529   indices[0]        = MeshVertexIndex(facet.ind1, nsections - 1);
0530   indices[1]        = MeshVertexIndex(facet.ind3, nsections - 1);
0531   indices[2]        = MeshVertexIndex(facet.ind2, nsections - 1);
0532 }
0533 
0534 } // namespace VECGEOM_IMPL_NAMESPACE
0535 } // namespace vecgeom
0536 
0537 #endif