MeshLib Documentation
Loading...
Searching...
No Matches
MRSceneCache.h
Go to the documentation of this file.
1#pragma once
2#include "MRMesh/MRMeshFwd.h"
5#include "exports.h"
6#include <unordered_map>
7
8namespace MR
9{
10
11// class to cached scene objects data
12class MRVIEWER_CLASS SceneCache
13{
14public:
15 // invalidate all cached data
16 // call it in the beginning each frame
17 MRVIEWER_API static void invalidateAll();
18
19 template <typename ObjectType>
20 using ObjectList = std::vector<std::shared_ptr<ObjectType>>;
21 // analog getAllObjectsInTree<ObjectType>( &SceneRoot::get(), SelectivityType ) but use cached data
22 // reference copy is valid until invalidateAll() is called
23 template <typename ObjectType, ObjectSelectivityType SelectivityType>
24 static const ObjectList<ObjectType>& getAllObjects();
25
26private:
27 MRVIEWER_API static SceneCache& instance_();
28 SceneCache() = default;
29
30 struct BasicVectorHolder
31 {
32 BasicVectorHolder() = default;
33 BasicVectorHolder( const BasicVectorHolder& ) = default;
34 BasicVectorHolder( BasicVectorHolder&& ) = default;
35 virtual ~BasicVectorHolder() = default;
36 };
37 template <typename ObjectType, ObjectSelectivityType SelectivityType>
38 struct VectorHolder : BasicVectorHolder
39 {
40 ObjectList<ObjectType> value;
41 };
42 std::unordered_map<std::type_index, std::shared_ptr<BasicVectorHolder>> cachedData_;
43};
44
45template <typename ObjectType, ObjectSelectivityType SelectivityType>
47{
48 using ResultType = VectorHolder<ObjectType, SelectivityType>;
49 const auto typeIndex = std::type_index( typeid( ResultType ) );
50 auto& cachedData = instance_().cachedData_;
51 if ( !cachedData.contains( typeIndex ) || !cachedData[typeIndex] )
52 {
53 ResultType newData;
54 newData.value = getAllObjectsInTree<ObjectType>( &SceneRoot::get(), SelectivityType );
55 std::shared_ptr<ResultType> newDataPtr = std::make_shared<ResultType>( std::move( newData ) );
56 cachedData[typeIndex] = std::dynamic_pointer_cast<BasicVectorHolder>( newDataPtr );
57 }
58 return std::dynamic_pointer_cast< ResultType >( cachedData[typeIndex] )->value;
59}
60
61}
Definition MRSceneCache.h:13
std::vector< std::shared_ptr< ObjectType > > ObjectList
Definition MRSceneCache.h:20
static MRVIEWER_API void invalidateAll()
static const ObjectList< ObjectType > & getAllObjects()
Definition MRSceneCache.h:46
static MRMESH_API SceneRootObject & get()
std::vector< std::shared_ptr< ObjectT > > getAllObjectsInTree(Object *root, const ObjectSelectivityType &type=ObjectSelectivityType::Selectable)
Definition MRCameraOrientationPlugin.h:8