Project
Loading...
Searching...
No Matches
O2FlatCSG.h
Go to the documentation of this file.
1// Copyright 2019-2026 CERN and copyright holders of ALICE O2.
2// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3// All rights not expressly granted are reserved.
4//
5// This software is distributed under the terms of the GNU General Public
6// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7//
8// In applying this license CERN does not waive the privileges and immunities
9// granted to it by virtue of its status as an Intergovernmental Organization
10// or submit itself to any jurisdiction.
13
14#ifndef ALICEO2_CADSUPPORT_O2FLATCSG_
15#define ALICEO2_CADSUPPORT_O2FLATCSG_
16
17#include "TGeoBBox.h"
18
19#include <vector>
20
21namespace o2
22{
23namespace cad
24{
25
29 enum Kind : int { kQuadric = 0,
30 kTorus = 1 };
32 double sign = 1.;
33 double c[11] = {};
34};
35
38 int first = 0;
39 int count = 0;
40 double volume = 0.;
41};
42
45struct FlatCSGBox {
46 double min[3] = {};
47 double max[3] = {};
48 int cell = -1;
49 int firstActive = 0;
50 int nActive = 0;
51};
52
55class O2FlatCSG : public TGeoBBox
56{
57 public:
58 O2FlatCSG();
59 explicit O2FlatCSG(const char* name);
60 ~O2FlatCSG() override;
61
62 // The shape owns a raw `bvh::v2::Bvh` behind `fBVH`, so a compiler-written copy would hand two
63 // shapes the same BVH and then free it twice; same treatment as O2BVHAssembly.
64 O2FlatCSG(const O2FlatCSG&) = delete;
65 O2FlatCSG& operator=(const O2FlatCSG&) = delete;
66
67 // ---- building -------------------------------------------------------------------------
69 int AddQuadric(double sign, const double coeff[10]);
71 int AddTorus(double sign, const double* centre, const double* axis, double major, double minor);
73 int AddCell(int first, int count, double volume);
74
75 int GetNhalfspaces() const { return static_cast<int>(fHalfspaces.size()); }
76 int GetNcells() const { return static_cast<int>(fCells.size()); }
77 const FlatCSGHalfspace& GetHalfspace(int index) const { return fHalfspaces[index]; }
78 const FlatCSGCell& GetCell(int index) const { return fCells[index]; }
79
82 void SetCellBBox(int cell, const double* lo, const double* hi);
85 void GetCellBBox(int cell, double* lo, double* hi) const;
86
88 void CloseShape();
89 bool IsClosed() const { return fClosed; }
90
92 size_t GetBVHMemory() const;
93
94 int GetNboxes() const { return static_cast<int>(fBoxes.size()); }
95 const FlatCSGBox& GetBox(int index) const { return fBoxes[index]; }
97 int GetActive(int index) const { return fActive[index]; }
99 bool CellContains(int index, const double* point) const;
100
105 void SetMinBoxFraction(double fraction) { fMinBoxFraction = fraction; }
106
108 static double EvalHalfspace(const FlatCSGHalfspace& halfspace, const double* point);
109
112 static void HalfspaceRange(const FlatCSGHalfspace& halfspace, const double* lo, const double* hi,
113 double& rangeLo, double& rangeHi);
114
116 static int HalfspaceRoots(const FlatCSGHalfspace& halfspace, const double* origin,
117 const double* dir, double* roots);
118
121 int CellIntervals(int cell, const int* active, int nActive, const double* origin,
122 const double* dir, double tlo, double thi, double* out, int maxOut) const;
123
124 // ---- the TGeoShape contract; the accelerated queries use their `_Loop` twin until CloseShape succeeds ----
125 Bool_t Contains(const Double_t* point) const override;
126
127 Double_t DistFromOutside(const Double_t* point, const Double_t* dir, Int_t iact = 1,
128 Double_t step = TGeoShape::Big(), Double_t* safe = nullptr) const override;
129 Double_t DistFromInside(const Double_t* point, const Double_t* dir, Int_t iact = 1,
130 Double_t step = TGeoShape::Big(), Double_t* safe = nullptr) const override;
131
133 Double_t Safety(const Double_t* point, Bool_t in = kTRUE) const override;
134
136 static void ResetUnprunedRetryCounter();
137 static long long GetUnprunedRetryCount();
138
140 void ComputeBBox() override;
141
144 Double_t Capacity() const override;
145
147 void ComputeNormal(const Double_t* point, const Double_t* dir, Double_t* norm) const override;
149 Bool_t GetPointsOnSegments(Int_t npoints, Double_t* array) const override;
150
151 // ---- the reference twins ---------------------------------------------------------------
152 Bool_t Contains_Loop(const Double_t* point) const;
153
154 Double_t DistFromOutside_Loop(const Double_t* point, const Double_t* dir,
155 Double_t step = TGeoShape::Big()) const;
156 Double_t DistFromInside_Loop(const Double_t* point, const Double_t* dir,
157 Double_t step = TGeoShape::Big()) const;
159 Double_t Safety_Loop(const Double_t* point, Bool_t in = kTRUE) const;
160
161 protected:
164
166 Double_t DistFromOutsideBVH(const Double_t* point, const Double_t* dir, Double_t step) const;
167 Double_t DistFromInsideBVH(const Double_t* point, const Double_t* dir, Double_t step) const;
168
171 enum class RayBound { kNone,
172 kEntry,
173 kExit };
174
178 bool GatherRayPieces(const Double_t* point, const Double_t* dir, Double_t step,
179 std::vector<double>& pairs, std::vector<int>& cells, RayBound bound,
180 double& smallestPruned) const;
181
184 void SplitBox(int cell, const double* lo, const double* hi, const std::vector<int>& active,
185 int depth, double minSize, int cubifyBudget);
186
187 std::vector<FlatCSGHalfspace> fHalfspaces;
188 std::vector<FlatCSGCell> fCells;
189
191 std::vector<FlatCSGBox> fBoxes;
194 std::vector<int> fActive;
195 std::vector<double> fCellLo;
196 std::vector<double> fCellHi;
199 std::vector<bool> fCellBBoxSet;
201 bool fClosed = false;
204 int fSplitDepth = 4;
205 double fMinBoxFraction = 0.05;
206
208 void* fBVH = nullptr;
209
210 // Scratch buffers are thread_local statics in the .cxx, never members: shapes are shared by all navigator threads.
211
212 ClassDefOverride(O2FlatCSG, 1) // flat-DNF halfspace shape class
213};
214
215} // namespace cad
216} // namespace o2
217
218#endif
header::DataOrigin origin
size_t minSize
uint32_t minor
Definition RawData.h:6
uint32_t major
Definition RawData.h:7
void SplitBox(int cell, const double *lo, const double *hi, const std::vector< int > &active, int depth, double minSize, int cubifyBudget)
static long long GetUnprunedRetryCount()
std::vector< int > fActive
Definition O2FlatCSG.h:194
const FlatCSGBox & GetBox(int index) const
Definition O2FlatCSG.h:95
void * fBVH
The BVH over fBoxes, rebuilt by CloseShape; not streamed.
Definition O2FlatCSG.h:208
Double_t DistFromOutside(const Double_t *point, const Double_t *dir, Int_t iact=1, Double_t step=TGeoShape::Big(), Double_t *safe=nullptr) const override
int AddCell(int first, int count, double volume)
Append a cell over [first, first + count) of the halfspace array; returns its index.
Double_t DistFromOutside_Loop(const Double_t *point, const Double_t *dir, Double_t step=TGeoShape::Big()) const
std::vector< double > fCellLo
each cell's AABB low corner, 3 doubles per cell
Definition O2FlatCSG.h:195
int AddQuadric(double sign, const double coeff[10])
Append a quadric halfspace; returns its index. sign is +1 or -1, inside is sign*Q <= 0.
int CellIntervals(int cell, const int *active, int nActive, const double *origin, const double *dir, double tlo, double thi, double *out, int maxOut) const
static int HalfspaceRoots(const FlatCSGHalfspace &halfspace, const double *origin, const double *dir, double *roots)
Real roots of sign * f(origin + t*dir) = 0, unsorted, at most four; returns the count.
bool fClosed
Set by a successful CloseShape; not streamed. The #pragma read rule closes every shape ROOT reads bac...
Definition O2FlatCSG.h:201
int GetActive(int index) const
For the tests: the box structure is the thing being proved sound, so it has to be readable.
Definition O2FlatCSG.h:97
int GetNhalfspaces() const
Definition O2FlatCSG.h:75
Bool_t GetPointsOnSegments(Int_t npoints, Double_t *array) const override
Points on the solid's own boundary, for the overlap checkers; kFALSE if fewer than npoints were found...
bool GatherRayPieces(const Double_t *point, const Double_t *dir, Double_t step, std::vector< double > &pairs, std::vector< int > &cells, RayBound bound, double &smallestPruned) const
GatherRayPieces – each box's window is its own slab intersected with [0, step], never pooled across b...
void EnsureCellBBoxStorage()
Grow the per-cell bounding-box storage to the cell count.
Double_t Safety_Loop(const Double_t *point, Bool_t in=kTRUE) const
Safety's twin over all boxes: it must equal Safety and be a sound bound.
void SetSplitDepth(int depth)
Subdivision depth cap. See fSplitDepth for where the default comes from.
Definition O2FlatCSG.h:102
O2FlatCSG(const O2FlatCSG &)=delete
Double_t DistFromInside(const Double_t *point, const Double_t *dir, Int_t iact=1, Double_t step=TGeoShape::Big(), Double_t *safe=nullptr) const override
Double_t DistFromInsideBVH(const Double_t *point, const Double_t *dir, Double_t step) const
DistFromInsideBVH – the far end of the union's interval containing t = 0, merged across cells with th...
std::vector< bool > fCellBBoxSet
Definition O2FlatCSG.h:199
static double EvalHalfspace(const FlatCSGHalfspace &halfspace, const double *point)
sign * f(point); the halfspace contains the point when this is <= 0.
void CloseShape()
Build the sub-cell boxes and their BVH. Call once, after the last AddCell.
Bool_t Contains_Loop(const Double_t *point) const
Double_t Safety(const Double_t *point, Bool_t in=kTRUE) const override
A lower bound on the distance to the boundary from the box structure: outside the nearest box,...
static void HalfspaceRange(const FlatCSGHalfspace &halfspace, const double *lo, const double *hi, double &rangeLo, double &rangeHi)
void ComputeBBox() override
The union of the retained sub-cell boxes, tighter than the union of the cell AABBs.
const FlatCSGCell & GetCell(int index) const
Definition O2FlatCSG.h:78
std::vector< double > fCellHi
Definition O2FlatCSG.h:196
Double_t DistFromInside_Loop(const Double_t *point, const Double_t *dir, Double_t step=TGeoShape::Big()) const
int GetNcells() const
Definition O2FlatCSG.h:76
int AddTorus(double sign, const double *centre, const double *axis, double major, double minor)
Append a torus halfspace, inside sign * (sqrt((rho - major)^2 + z^2) - minor) <= 0 about unit axis; r...
void ComputeNormal(const Double_t *point, const Double_t *dir, Double_t *norm) const override
The normal of the halfspace nearest to equality at point, oriented along dir.
bool IsClosed() const
Definition O2FlatCSG.h:89
~O2FlatCSG() override
Bool_t Contains(const Double_t *point) const override
void GetCellBBox(int cell, double *lo, double *hi) const
std::vector< FlatCSGCell > fCells
the DNF's cells, indexing into it
Definition O2FlatCSG.h:188
void SetMinBoxFraction(double fraction)
Definition O2FlatCSG.h:105
Double_t Capacity() const override
size_t GetBVHMemory() const
Bytes held by the BVH nodes and the primitive-index permutation.
static void ResetUnprunedRetryCounter()
Per-thread count of DistFromInside queries whose pruned traversal had to be redone unpruned.
bool CellContains(int index, const double *point) const
True when every halfspace of cell index contains point.
Double_t DistFromOutsideBVH(const Double_t *point, const Double_t *dir, Double_t step) const
The accelerated DistFromOutside/DistFromInside bodies; each clips the ray to a box before using its a...
const FlatCSGHalfspace & GetHalfspace(int index) const
Definition O2FlatCSG.h:77
std::vector< FlatCSGBox > fBoxes
The sub-cell boxes, rebuilt by CloseShape; not streamed.
Definition O2FlatCSG.h:191
std::vector< FlatCSGHalfspace > fHalfspaces
the flat halfspace array
Definition O2FlatCSG.h:187
void SetCellBBox(int cell, const double *lo, const double *hi)
O2FlatCSG & operator=(const O2FlatCSG &)=delete
int GetNboxes() const
Definition O2FlatCSG.h:94
GLint GLsizei count
Definition glcorearb.h:399
GLenum array
Definition glcorearb.h:4274
GLuint index
Definition glcorearb.h:781
GLuint const GLchar * name
Definition glcorearb.h:781
GLint GLint GLsizei GLsizei GLsizei depth
Definition glcorearb.h:470
a couple of static helper functions to create timestamp values for CCDB queries or override obsolete ...
One DNF cell: [first, first + count) of the halfspace array, intersected; volume is its own volume.
Definition O2FlatCSG.h:37
std::vector< Cell > cells