Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Detectors/Base/include/DetectorsBase/O2Tessellated.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ class O2Tessellated : public TGeoBBox
template <bool closest_facet = false>
Double_t SafetyKernel(const Double_t* point, bool in, int* closest_facet_id = nullptr) const;

// cached values for safety
mutable float mLast_x;
mutable float mLast_y;
mutable float mLast_z;
mutable float mCachedSafety;
mutable size_t cached_counter = 0;
mutable size_t call_counter = 0;

ClassDefOverride(O2Tessellated, 1) // tessellated shape class
};

Expand Down
26 changes: 24 additions & 2 deletions Detectors/Base/src/O2Tessellated.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1349,8 +1349,30 @@ Double_t O2Tessellated::Safety(const Double_t* point, Bool_t in) const
// we could use some caching here (in future) since queries to the solid will likely
// be made with some locality

if (in) {
call_counter++;
// distance to last known evaluation
const auto xd = float(point[0]) - mLast_x;
const auto yd = float(point[1]) - mLast_y;
const auto zd = float(point[2]) - mLast_z;
const auto d2 = xd * xd + yd * yd + zd * zd;

if (d2 < mCachedSafety * mCachedSafety) {
// we moved less than known safety
cached_counter++;
return mCachedSafety - std::sqrt(d2);
}
}

// fall-back to precise safety kernel
return SafetyKernel<false>(point, in);
const auto safety = SafetyKernel<false>(point, in);
if (in) {
mLast_x = point[0];
mLast_y = point[1];
mLast_z = point[2];
mCachedSafety = safety;
}
return safety;
}

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1506,4 +1528,4 @@ void O2Tessellated::CalculateNormals()
}
}

// NOLINTEND
// NOLINTEND