G4OCCT 0.1.0
Geant4 interface to Open CASCADE Technology (OCCT) geometry definitions
Loading...
Searching...
No Matches
G4OCCTSolidKernel.hh
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later
2// Copyright (C) 2026 G4OCCT Contributors
3
6
7#ifndef G4OCCT_src_G4OCCTSolidKernel_hh
8#define G4OCCT_src_G4OCCTSolidKernel_hh
9
10#include <G4ThreeVector.hh>
11#include <geomdefs.hh>
12
13#include <BRepAdaptor_Surface.hxx>
14#include <BRepClass3d_SolidClassifier.hxx>
15#include <BRepExtrema_TriangleSet.hxx>
16#include <Bnd_Box.hxx>
17#include <IntCurvesFace_Intersector.hxx>
18#include <TopoDS_Face.hxx>
19#include <TopoDS_Shape.hxx>
20#include <gp_Pln.hxx>
21#include <gp_Pnt2d.hxx>
22
23#include <atomic>
24#include <condition_variable>
25#include <cstdint>
26#include <limits>
27#include <memory>
28#include <mutex>
29#include <optional>
30#include <utility>
31#include <vector>
32
33namespace g4occt::detail {
34
39inline constexpr Standard_Real kOCCTRelativeDeflection = 0.01;
40
57public:
59 static G4double Infinity() { return ::kInfinity; }
60
63
66 G4ThreeVector min;
67 G4ThreeVector max;
68 };
69
77 std::uint64_t generation{std::numeric_limits<std::uint64_t>::max()};
78 std::optional<BRepClass3d_SolidClassifier> classifier;
79 };
80
87 std::uint64_t generation{std::numeric_limits<std::uint64_t>::max()};
88 std::vector<std::unique_ptr<IntCurvesFace_Intersector>> faceIntersectors;
89 std::vector<Bnd_Box> expandedBoxes;
90 };
91
94 G4ThreeVector centre;
95 G4double radius;
96 };
97
102 std::vector<InscribedSphere> spheres;
103 std::uint64_t generation{std::numeric_limits<std::uint64_t>::max()};
104 };
105
107 static constexpr std::size_t kMaxInscribedSpheres = 64;
108
111 G4ThreeVector p1, p2, p3;
112 std::uint32_t faceIndex;
113 };
114
122 std::vector<TopoDS_Face> faces;
123 std::vector<SurfaceTriangle> triangles;
124 std::vector<G4double> cumulativeAreas;
125 G4double totalArea{0.0};
126 };
127
135 explicit G4OCCTSolidKernel(const TopoDS_Shape& shape);
136
146 void SetShape(const TopoDS_Shape& shape);
147
149 const TopoDS_Shape& Shape() const { return fShape; }
150
152 std::uint64_t ShapeGeneration() const { return fShapeGeneration.load(std::memory_order_acquire); }
153
155 const AxisAlignedBounds& Bounds() const { return fCachedBounds; }
156
158 PointClassification ClassifyPoint(const G4ThreeVector& p, ClassifierCache& classifierCache,
159 IntersectorCache& intersectorCache,
160 SphereCacheData& sphereCache) const;
161
163 G4ThreeVector SurfaceNormal(const G4ThreeVector& p) const;
164
166 G4double DistanceToIn(const G4ThreeVector& p, const G4ThreeVector& v,
167 IntersectorCache& intersectorCache) const;
168
170 G4double DistanceToIn(const G4ThreeVector& p, ClassifierCache& classifierCache) const;
171
173 G4double DistanceToOut(const G4ThreeVector& p, const G4ThreeVector& v,
174 IntersectorCache& intersectorCache, const G4bool calcNorm = false,
175 G4bool* validNorm = nullptr, G4ThreeVector* n = nullptr) const;
176
178 G4double DistanceToOut(const G4ThreeVector& p, SphereCacheData& sphereCache) const;
179
181 G4double ExactDistanceToIn(const G4ThreeVector& p, ClassifierCache& classifierCache) const;
182
184 G4double ExactDistanceToOut(const G4ThreeVector& p) const;
185
187 G4double GetCubicVolume();
188
190 G4double GetSurfaceArea();
191
198 G4ThreeVector GetPointOnSurface(const char* diagnosticName = nullptr) const;
199
201 const SurfaceSamplingCache& GetOrBuildSurfaceCache() const;
202
203private:
205 struct FaceBounds {
206 TopoDS_Face face;
207 Bnd_Box box;
208 BRepAdaptor_Surface adaptor;
209 std::optional<gp_Pln> plane;
210 std::vector<gp_Pnt2d> uvPolygon;
211 std::optional<G4ThreeVector> outwardNormal;
212 };
213
215 struct ClosestFaceMatch {
216 TopoDS_Face face;
217 G4double distance{Infinity()};
218 std::size_t faceIndex{0};
219 std::optional<std::pair<Standard_Real, Standard_Real>> uv;
220 };
221
223 BRepClass3d_SolidClassifier& GetOrCreateClassifier(ClassifierCache& cache) const;
224
226 IntersectorCache& GetOrCreateIntersector(IntersectorCache& cache) const;
227
229 SphereCacheData& GetOrInitSphereCache(SphereCacheData& cache) const;
230
232 void TryInsertSphere(SphereCacheData& cache, const G4ThreeVector& centre, G4double d) const;
233
235 void ComputeBounds();
236
238 void ComputeInitialSpheres();
239
241 G4double AABBLowerBound(const G4ThreeVector& p) const;
242
244 G4double BVHLowerBoundDistance(const G4ThreeVector& p) const;
245
247 G4double PlanarFaceLowerBoundDistance(const G4ThreeVector& p) const;
248
250 static std::optional<ClosestFaceMatch>
251 TryFindClosestFace(const std::vector<FaceBounds>& faceBoundsCache, const G4ThreeVector& point,
252 G4double maxDistance = Infinity());
253
255 TopoDS_Shape fShape;
257 AxisAlignedBounds fCachedBounds;
259 std::vector<FaceBounds> fFaceBoundsCache;
261 std::vector<InscribedSphere> fInitialSpheres;
263 Handle(BRepExtrema_TriangleSet) fTriangleSet;
265 G4double fBVHDeflection{0.0};
267 std::vector<G4double> fFaceDeflections;
269 bool fAllFacesPlanar{false};
271 std::atomic<std::uint64_t> fShapeGeneration{0};
273 mutable std::optional<G4double> fCachedVolume;
275 mutable std::optional<G4double> fCachedSurfaceArea;
276 mutable std::mutex fVolumeAreaMutex;
278 mutable std::optional<SurfaceSamplingCache> fSurfaceCache;
280 mutable std::uint64_t fSurfaceCacheGeneration{std::numeric_limits<std::uint64_t>::max()};
282 mutable bool fSurfaceCacheBuilding{false};
283 mutable std::mutex fSurfaceCacheMutex;
285 mutable std::condition_variable fSurfaceCacheCV;
286};
287
288} // namespace g4occt::detail
289
290#endif // G4OCCT_src_G4OCCTSolidKernel_hh
Shared OCCT-backed query kernel used by adapter frontends.
G4double DistanceToOut(const G4ThreeVector &p, const G4ThreeVector &v, IntersectorCache &intersectorCache, const G4bool calcNorm=false, G4bool *validNorm=nullptr, G4ThreeVector *n=nullptr) const
Exact ray distance from an interior point to the first exit intersection.
void SetShape(const TopoDS_Shape &shape)
G4double GetCubicVolume()
Compute and cache the solid volume for the current shape generation.
G4double GetSurfaceArea()
Compute and cache the solid surface area for the current shape generation.
static constexpr std::size_t kMaxInscribedSpheres
Maximum number of inscribed spheres retained in a per-thread sphere cache.
G4ThreeVector GetPointOnSurface(const char *diagnosticName=nullptr) const
G4double DistanceToIn(const G4ThreeVector &p, const G4ThreeVector &v, IntersectorCache &intersectorCache) const
Exact ray distance from an exterior point to the first entry intersection.
PointClassification
Classification result for point-in-solid queries.
G4ThreeVector SurfaceNormal(const G4ThreeVector &p) const
Return the outward surface normal at the face nearest point p.
static G4double Infinity()
Return the Geant4 navigation infinity sentinel used by the kernel.
G4double ExactDistanceToOut(const G4ThreeVector &p) const
Exact shortest distance from a point to the surface.
std::uint64_t ShapeGeneration() const
Monotonic generation counter incremented after successful SetShape().
const TopoDS_Shape & Shape() const
Read-only access to the currently wrapped OCCT shape.
PointClassification ClassifyPoint(const G4ThreeVector &p, ClassifierCache &classifierCache, IntersectorCache &intersectorCache, SphereCacheData &sphereCache) const
Classify a point as inside, on, or outside the solid.
G4double ExactDistanceToIn(const G4ThreeVector &p, ClassifierCache &classifierCache) const
Exact shortest distance from an exterior point to the surface.
const AxisAlignedBounds & Bounds() const
Return the cached axis-aligned bounds for the current shape.
const SurfaceSamplingCache & GetOrBuildSurfaceCache() const
Build or return the shared surface-sampling cache for the current shape.
constexpr Standard_Real kOCCTRelativeDeflection
Axis-aligned bounds in the imported OCCT coordinate system.
std::optional< BRepClass3d_SolidClassifier > classifier
Cached inscribed sphere used to accelerate deep-interior classifications.
std::vector< std::unique_ptr< IntCurvesFace_Intersector > > faceIntersectors
Tessellated triangle entry used for random surface-point sampling.