File indexing completed on 2025-01-18 10:13:55
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;
0110 vector_t<size_t> fInd;
0111 vector_t<Vec_t> fSides;
0112
0113
0114 VECCORE_ATT_HOST_DEVICE
0115 Polygon(size_t n, vector_t<Vec_t> &vertices, bool convex = false);
0116
0117
0118 VECCORE_ATT_HOST_DEVICE
0119 Polygon(size_t n, vector_t<Vec_t> &vertices, Vec_t const &norm);
0120
0121
0122 Polygon(size_t n, vector_t<Vec_t> &vertices, vector_t<size_t> const &indices, bool convex);
0123
0124
0125 VECCORE_ATT_HOST_DEVICE
0126 Polygon(const Polygon &other)
0127 : fN(other.fN), fConvex(other.fConvex), fHasNorm(other.fHasNorm), fValid(other.fValid), fDist(other.fDist),
0128 fNorm(other.fNorm), fVert(other.fVert), fInd(other.fInd), fSides(other.fSides)
0129 {
0130 }
0131
0132
0133 VECGEOM_FORCE_INLINE
0134 Polygon &operator=(const Polygon &other)
0135 {
0136 if (&other == this) return *this;
0137 fN = other.fN;
0138 fConvex = other.fConvex;
0139 fHasNorm = other.fHasNorm;
0140 fValid = other.fValid;
0141 fDist = other.fDist;
0142 fNorm = other.fNorm;
0143 fVert = other.fVert;
0144 fInd = other.fInd;
0145 fSides = other.fSides;
0146 return *this;
0147 }
0148
0149
0150 VECGEOM_FORCE_INLINE
0151 void SetVertex(size_t ind, size_t ivert) { fInd[ind] = ivert; }
0152
0153
0154 VECGEOM_FORCE_INLINE
0155 Vec_t const &GetVertex(size_t i) const { return fVert[fInd[i]]; }
0156
0157
0158 template <typename T>
0159 void SetVertices(vector_t<T> indices)
0160 {
0161 for (size_t i = 0; i < fN; ++i)
0162 fInd[i] = size_t(indices[i]);
0163 }
0164
0165
0166 void Init();
0167
0168
0169 void Transform(Transformation3D const &tr);
0170
0171
0172
0173 void CheckAndFixDegenerate();
0174
0175
0176
0177 void TriangulatePolygon(vector_t<Polygon> &polys) const;
0178
0179
0180
0181 bool isConvexVertex(size_t i0, size_t i1, size_t i2) const;
0182
0183
0184
0185 bool isPointInsideTriangle(const Vec_t &point, size_t i0, size_t i1, size_t i2) const;
0186
0187
0188
0189 bool IsPointInside(const Vec_t &p) const;
0190
0191 #ifndef VECCORE_CUDA
0192
0193 struct PolygonIntersection *Intersect(const Polygon &clipper);
0194 #endif
0195
0196
0197
0198
0199
0200 void Extent(Precision x[2], Precision y[2], Precision z[2]);
0201 };
0202
0203
0204 struct Polyhedron {
0205 vector_t<Vec_t> fVert;
0206 vector_t<Polygon> fPolys;
0207
0208
0209 Polyhedron(){};
0210 Polyhedron(size_t nvert, size_t npolys)
0211 {
0212 fVert.reserve(nvert);
0213 fPolys.reserve(npolys);
0214 }
0215
0216
0217 VECGEOM_FORCE_INLINE
0218 void Reset(size_t nvert, size_t npolys)
0219 {
0220 fVert.reserve(nvert);
0221 fVert.clear();
0222 fPolys.reserve(npolys);
0223 fPolys.clear();
0224 }
0225
0226
0227 VECGEOM_FORCE_INLINE
0228 size_t GetNvertices() const { return fVert.size(); }
0229
0230
0231 VECGEOM_FORCE_INLINE
0232 size_t GetNpolygons() const { return fPolys.size(); }
0233
0234
0235 VECGEOM_FORCE_INLINE
0236 Vec_t const &GetVertex(size_t i) const { return fVert[i]; }
0237
0238
0239 VECGEOM_FORCE_INLINE
0240 Polygon const &GetPolygon(size_t i) const { return fPolys[i]; }
0241
0242
0243 void Transform(Transformation3D const &tr);
0244
0245
0246 void AddPolygon(Polygon &poly, bool triangulate);
0247 };
0248
0249
0250
0251
0252
0253
0254
0255
0256
0257
0258
0259
0260
0261 struct PolygonIntersection {
0262 vector_t<Vec_t> fPoints;
0263 vector_t<Line> fLines;
0264 vector_t<Polygon> fPolygons;
0265 vector_t<Vec_t> fVertices;
0266 };
0267
0268 #ifndef VECCORE_CUDA
0269
0270 std::ostream &operator<<(std::ostream &os, Plane const &hpl);
0271 std::ostream &operator<<(std::ostream &os, Polygon const &poly);
0272 std::ostream &operator<<(std::ostream &os, Polyhedron const &polyh);
0273 #endif
0274
0275
0276 EPlaneXing_t PlaneXing(Plane const &pl1, Plane const &pl2, Vector3D<Precision> &point, Vector3D<Precision> &direction);
0277
0278
0279 EBodyXing_t PolygonXing(Polygon const &poly1, Polygon const &poly2, Line *line = nullptr);
0280
0281
0282 EBodyXing_t PolyhedronXing(Polyhedron const &poly1, Polyhedron const &poly2, vector_t<Line> &lines);
0283
0284
0285
0286
0287 EBodyXing_t BoxCollision(Vector3D<Precision> const &box1, Transformation3D const &tr1, Vector3D<Precision> const &box2,
0288 Transformation3D const &tr2);
0289
0290
0291 void FillBoxPolyhedron(Vec_t const &dimensions, Polyhedron &polyh);
0292
0293 }
0294 }
0295 }
0296 #endif