File indexing completed on 2026-09-18 09:32:41
0001
0002
0003
0004
0005 #ifndef VECGEOM_BASE_UTILS3D_H_
0006 #define VECGEOM_BASE_UTILS3D_H_
0007
0008 #include "VecGeom/base/Vector3D.h"
0009 #include "VecGeom/base/Transformation3D.h"
0010
0011 #ifndef VECCORE_CUDA
0012 #include <vector>
0013 #else
0014 #include "VecGeom/base/Vector.h"
0015 #endif
0016
0017
0018 namespace vecgeom {
0019 inline namespace VECGEOM_IMPL_NAMESPACE {
0020
0021 namespace Utils3D {
0022
0023 using Vec_t = Vector3D<Precision>;
0024
0025 template <typename T>
0026 #ifndef VECCORE_CUDA
0027 using vector_t = std::vector<T>;
0028 #else
0029 using vector_t = vecgeom::Vector<T>;
0030 #endif
0031
0032 enum EPlaneXing_t { kParallel = 0, kIdentical, kIntersecting };
0033
0034 enum EBodyXing_t { kDisjoint = 0, kTouching, kOverlapping };
0035
0036
0037 struct Plane {
0038 Vector3D<Precision> fNorm;
0039 Precision fDist = 0.;
0040
0041 Plane() : fNorm() {}
0042
0043 Plane(Vector3D<Precision> const &norm, Precision dist)
0044 {
0045 fNorm = norm;
0046 fDist = dist;
0047 }
0048
0049
0050 void Transform(Transformation3D const &tr);
0051 };
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082 struct LineIntersection {
0083 enum { fParallel, fOverlap, fIntersect, fNoIntersect } fType;
0084 Precision fA;
0085 Precision fB;
0086 };
0087
0088
0089 struct Line {
0090 Vector3D<Precision> fPts[2];
0091
0092
0093 LineIntersection *Intersect(const Line &lB);
0094
0095
0096 bool IsPointOnLine(const Vec_t &p);
0097 };
0098
0099
0100
0101
0102 struct Polygon {
0103 size_t fN = 0;
0104 bool fConvex = false;
0105 bool fHasNorm = false;
0106 bool fValid = false;
0107 Precision fDist = 0.;
0108 Vec_t fNorm;
0109 vector_t<Vec_t> *fVert{nullptr};
0110 vector_t<size_t> fInd;
0111 vector_t<Vec_t> fSides;
0112
0113 Polygon() = default;
0114
0115 VECCORE_ATT_HOST_DEVICE
0116 Polygon(size_t n, vector_t<Vec_t> &vertices, bool convex = false);
0117
0118
0119 VECCORE_ATT_HOST_DEVICE
0120 Polygon(size_t n, vector_t<Vec_t> &vertices, Vec_t const &norm);
0121
0122
0123 Polygon(size_t n, vector_t<Vec_t> &vertices, vector_t<size_t> const &indices, bool convex);
0124
0125
0126 Polygon(const Polygon &other) = default;
0127
0128
0129
0130 Polygon &operator=(const Polygon &other) = default;
0131
0132
0133 VECGEOM_FORCE_INLINE
0134 void SetVertex(size_t ind, size_t ivert) { fInd[ind] = ivert; }
0135
0136
0137 VECGEOM_FORCE_INLINE
0138 Vec_t const &GetVertex(size_t i) const { return (*fVert)[fInd[i]]; }
0139
0140
0141 template <typename T>
0142 void SetVertices(vector_t<T> indices)
0143 {
0144 for (size_t i = 0; i < fN; ++i)
0145 fInd[i] = size_t(indices[i]);
0146 }
0147
0148
0149 void Init();
0150
0151
0152 void Transform(Transformation3D const &tr);
0153
0154
0155
0156 void CheckAndFixDegenerate();
0157
0158
0159
0160 void TriangulatePolygon(vector_t<Polygon> &polys) const;
0161
0162
0163
0164 bool isConvexVertex(size_t i0, size_t i1, size_t i2) const;
0165
0166
0167
0168 bool isPointInsideTriangle(const Vec_t &point, size_t i0, size_t i1, size_t i2) const;
0169
0170
0171
0172 bool IsPointInside(const Vec_t &p) const;
0173
0174 #ifndef VECCORE_CUDA
0175
0176 struct PolygonIntersection *Intersect(const Polygon &clipper);
0177 #endif
0178
0179
0180
0181
0182
0183 void Extent(Precision x[2], Precision y[2], Precision z[2]);
0184 };
0185
0186
0187 struct Polyhedron {
0188 vector_t<Vec_t> fVert;
0189 vector_t<Polygon> fPolys;
0190
0191
0192 Polyhedron() = default;
0193 Polyhedron(size_t nvert, size_t npolys)
0194 {
0195 fVert.reserve(nvert);
0196 fPolys.reserve(npolys);
0197 }
0198
0199
0200 VECGEOM_FORCE_INLINE
0201 void Reset(size_t nvert, size_t npolys)
0202 {
0203 fVert.reserve(nvert);
0204 fVert.clear();
0205 fPolys.reserve(npolys);
0206 fPolys.clear();
0207 }
0208
0209
0210 VECGEOM_FORCE_INLINE
0211 size_t GetNvertices() const { return fVert.size(); }
0212
0213
0214 VECGEOM_FORCE_INLINE
0215 size_t GetNpolygons() const { return fPolys.size(); }
0216
0217
0218 VECGEOM_FORCE_INLINE
0219 Vec_t const &GetVertex(size_t i) const { return fVert[i]; }
0220
0221
0222 VECGEOM_FORCE_INLINE
0223 Polygon const &GetPolygon(size_t i) const { return fPolys[i]; }
0224
0225
0226 void Transform(Transformation3D const &tr);
0227
0228
0229 void AddPolygon(Polygon &poly, bool triangulate);
0230 };
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242
0243
0244 struct PolygonIntersection {
0245 vector_t<Vec_t> fPoints;
0246 vector_t<Line> fLines;
0247 vector_t<Polygon> fPolygons;
0248 vector_t<Vec_t> fVertices;
0249 };
0250
0251 #ifndef VECCORE_CUDA
0252
0253 std::ostream &operator<<(std::ostream &os, Plane const &hpl);
0254 std::ostream &operator<<(std::ostream &os, Polygon const &poly);
0255 std::ostream &operator<<(std::ostream &os, Polyhedron const &polyh);
0256 #endif
0257
0258
0259 EPlaneXing_t PlaneXing(Plane const &pl1, Plane const &pl2, Vector3D<Precision> &point, Vector3D<Precision> &direction);
0260
0261
0262 EBodyXing_t PolygonXing(Polygon const &poly1, Polygon const &poly2, Line *line = nullptr);
0263
0264
0265 EBodyXing_t PolyhedronXing(Polyhedron const &poly1, Polyhedron const &poly2, vector_t<Line> &lines);
0266
0267
0268
0269
0270 EBodyXing_t BoxCollision(Vector3D<Precision> const &box1, Transformation3D const &tr1, Vector3D<Precision> const &box2,
0271 Transformation3D const &tr2);
0272
0273
0274 void FillBoxPolyhedron(Vec_t const &dimensions, Polyhedron &polyh);
0275
0276 }
0277 }
0278 }
0279 #endif