environment_cache.cpp
Go to the documentation of this file.
1 
31 
32 #include <mutex>
33 
34 namespace tesseract_environment
35 {
36 DefaultEnvironmentCache::DefaultEnvironmentCache(std::shared_ptr<const Environment> env, std::size_t cache_size)
37  : env_(std::move(env)), cache_size_(cache_size)
38 {
39 }
40 
42 {
43  std::unique_lock<std::shared_mutex> lock(cache_mutex_);
44  cache_size_ = static_cast<std::size_t>(size);
45 }
46 
47 long DefaultEnvironmentCache::getCacheSize() const { return static_cast<long>(cache_size_); }
48 
50 {
51  std::unique_lock<std::shared_mutex> lock(cache_mutex_);
53 }
54 
55 std::unique_ptr<Environment> DefaultEnvironmentCache::getCachedEnvironment() const
56 {
57  tesseract_scene_graph::SceneState current_state = env_->getState();
58 
59  std::unique_lock<std::shared_mutex> lock(cache_mutex_);
60  refreshCacheHelper(); // This is to make sure the cached items are updated if needed
61  assert(!cache_.empty());
62  std::unique_ptr<Environment> t = std::move(cache_.back());
63  // Update to the current joint values
64  t->setState(current_state.joints);
65 
66  cache_.pop_back();
67 
68  return t;
69 }
70 
72 {
73  std::unique_ptr<Environment> env;
74  auto lock_read = env_->lockRead();
75  int rev = env_->getRevision();
76  if (rev != cache_env_revision_ || cache_.empty())
77  {
78  env = env_->clone();
79  cache_env_revision_ = rev;
80  }
81 
82  if (env != nullptr)
83  {
84  cache_.clear();
85  for (std::size_t i = 0; i < cache_size_; ++i)
86  cache_.push_back(env->clone());
87  }
88  else if (cache_.size() <= 2)
89  {
90  for (std::size_t i = (cache_.size() - 1); i < cache_size_; ++i)
91  cache_.push_back(cache_.front()->clone());
92  }
93 }
94 
95 } // namespace tesseract_environment
tesseract_environment
Definition: command.h:45
tesseract_environment::DefaultEnvironmentCache::env_
std::shared_ptr< const Environment > env_
The tesseract_object used to create the cache.
Definition: environment_cache.h:107
tesseract_environment::DefaultEnvironmentCache::getCacheSize
long getCacheSize() const override final
Get the cache size used to hold tesseract objects for motion planning.
Definition: environment_cache.cpp:47
environment_cache.h
A environment cache.
tesseract_environment::DefaultEnvironmentCache::DefaultEnvironmentCache
DefaultEnvironmentCache(std::shared_ptr< const Environment > env, std::size_t cache_size=5)
Definition: environment_cache.cpp:36
tesseract_scene_graph::SceneState
tesseract_environment::DefaultEnvironmentCache::refreshCache
void refreshCache() const override final
If the environment has changed it will rebuild the cache of tesseract objects.
Definition: environment_cache.cpp:49
tesseract_scene_graph::SceneState::joints
std::unordered_map< std::string, double > joints
tesseract_environment::DefaultEnvironmentCache::refreshCacheHelper
void refreshCacheHelper() const
This does not take a lock.
Definition: environment_cache.cpp:71
kinematic_group.h
tesseract_environment::DefaultEnvironmentCache::cache_size_
std::size_t cache_size_
The assigned cache size.
Definition: environment_cache.h:110
tesseract_environment::DefaultEnvironmentCache::cache_
std::deque< std::unique_ptr< Environment > > cache_
A vector of cached Tesseract objects.
Definition: environment_cache.h:116
tesseract_environment::DefaultEnvironmentCache::cache_env_revision_
int cache_env_revision_
The environment revision number at the time the cache was populated.
Definition: environment_cache.h:113
environment.h
tesseract_environment::DefaultEnvironmentCache::getCachedEnvironment
std::unique_ptr< Environment > getCachedEnvironment() const override final
This will pop an Environment object from the queue.
Definition: environment_cache.cpp:55
joint_group.h
tesseract_environment::DefaultEnvironmentCache::setCacheSize
void setCacheSize(long size) override final
Set the cache size used to hold tesseract objects for motion planning.
Definition: environment_cache.cpp:41
tesseract_environment::DefaultEnvironmentCache::cache_mutex_
std::shared_mutex cache_mutex_
The mutex used when reading and writing to cache_.
Definition: environment_cache.h:119


tesseract_environment
Author(s): Levi Armstrong
autogenerated on Sun May 18 2025 03:02:21