|
23 | 23 |
|
24 | 24 | #include <geode/model/helpers/ray_tracing.hpp> |
25 | 25 |
|
26 | | -#include <mutex> |
| 26 | +#include <absl/base/call_once.h> |
27 | 27 |
|
28 | 28 | #include <geode/basic/pimpl_impl.hpp> |
29 | 29 |
|
@@ -237,7 +237,15 @@ namespace geode |
237 | 237 | class BRepRayTracing::Impl |
238 | 238 | { |
239 | 239 | public: |
240 | | - explicit Impl( const BRep& brep ) : brep_( brep ) {} |
| 240 | + explicit Impl( const BRep& brep ) : brep_( brep ) |
| 241 | + { |
| 242 | + aabb_trees_.reserve( brep.nb_surfaces() ); |
| 243 | + for( const auto& surface : brep.surfaces() ) |
| 244 | + { |
| 245 | + aabb_trees_.emplace( |
| 246 | + surface.id(), std::make_unique< CachedTree >() ); |
| 247 | + } |
| 248 | + } |
241 | 249 |
|
242 | 250 | BoundarySurfaceIntersections find_intersections_with_boundaries( |
243 | 251 | const InfiniteLine3D& infinite_line, const Block3D& block ) |
@@ -298,26 +306,24 @@ namespace geode |
298 | 306 | } |
299 | 307 |
|
300 | 308 | private: |
| 309 | + struct CachedTree |
| 310 | + { |
| 311 | + absl::once_flag built; |
| 312 | + AABBTree3D tree; |
| 313 | + }; |
| 314 | + |
301 | 315 | const AABBTree3D& surface_aabb( const Surface3D& surface ) |
302 | 316 | { |
303 | | - std::lock_guard lock{ mutex_ }; |
304 | | - { |
305 | | - const auto it = aabb_trees_.find( surface.id() ); |
306 | | - if( it != aabb_trees_.end() ) |
307 | | - { |
308 | | - return *it->second; |
309 | | - } |
310 | | - } |
311 | | - const auto [it, inserted] = aabb_trees_.emplace( |
312 | | - surface.id(), std::make_unique< AABBTree3D >( |
313 | | - create_aabb_tree( surface.mesh() ) ) ); |
314 | | - return *it->second; |
| 317 | + auto& entry = *aabb_trees_.at( surface.id() ); |
| 318 | + absl::call_once( entry.built, [&surface, &entry] { |
| 319 | + entry.tree = create_aabb_tree( surface.mesh() ); |
| 320 | + } ); |
| 321 | + return entry.tree; |
315 | 322 | } |
316 | 323 |
|
317 | 324 | private: |
318 | 325 | const BRep& brep_; |
319 | | - absl::flat_hash_map< uuid, std::unique_ptr< AABBTree3D > > aabb_trees_; |
320 | | - std::mutex mutex_; |
| 326 | + absl::flat_hash_map< uuid, std::unique_ptr< CachedTree > > aabb_trees_; |
321 | 327 | }; |
322 | 328 |
|
323 | 329 | BRepRayTracing::BRepRayTracing( const BRep& brep ) : impl_{ brep } {} |
|
0 commit comments