$darkmode
object_sensor_functional.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Robert Bosch GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  */
27 #pragma once
28 #ifndef CLOE_COMPONENT_OBJECT_SENSOR_FUNCTIONAL_HPP_
29 #define CLOE_COMPONENT_OBJECT_SENSOR_FUNCTIONAL_HPP_
30 
31 #include <functional> // for function
32 #include <memory> // for shared_ptr<>
33 #include <string> // for string
34 
35 #include <cloe/component/object.hpp> // for Object
36 #include <cloe/component/object_sensor.hpp> // for ObjectSensor
37 
38 namespace cloe {
39 
44 using ObjectFilter = std::function<bool(const Object&)>;
45 
54 using ObjectFilterMap = std::function<std::shared_ptr<Object>(const std::shared_ptr<Object>&)>;
55 
92  public:
93  ObjectSensorFilter(std::shared_ptr<ObjectSensor> obs, ObjectFilter f)
94  : ObjectSensor("object_sensor_filter"), sensor_(obs), filter_func_(f) {}
95 
96  virtual ~ObjectSensorFilter() noexcept = default;
97 
101  const Objects& sensed_objects() const override {
102  if (!cached_) {
103  for (auto o : sensor_->sensed_objects()) {
104  if (filter_func_(*o)) {
105  objects_.push_back(o);
106  }
107  }
108  cached_ = true;
109  }
110  return objects_;
111  }
112 
113  const Frustum& frustum() const override { return sensor_->frustum(); }
114 
115  const Eigen::Isometry3d& mount_pose() const override { return sensor_->mount_pose(); }
116 
125  Duration process(const Sync& sync) override {
126  // This currently shouldn't do anything, but this class acts as a prototype
127  // for How It Should Be Done.
129  if (t < sync.time()) {
130  return t;
131  }
132 
133  // Process the underlying sensor and clear the cache.
134  t = sensor_->process(sync);
135  clear_cache();
136  return t;
137  }
138 
139  void reset() override {
141  sensor_->reset();
142  clear_cache();
143  }
144 
145  void abort() override {
147  sensor_->abort();
148  }
149 
150  protected:
151  void clear_cache() {
152  objects_.clear();
153  cached_ = false;
154  }
155 
156  private:
157  mutable bool cached_;
158  mutable Objects objects_;
159  std::shared_ptr<ObjectSensor> sensor_;
160  ObjectFilter filter_func_;
161 };
162 
175  public:
176  ObjectSensorFilterMap(const std::string& name, std::shared_ptr<ObjectSensor> obs,
177  ObjectFilterMap f)
178  : ObjectSensor(name), sensor_(obs), map_func_(f) {}
179 
180  virtual ~ObjectSensorFilterMap() noexcept = default;
181 
182  const Objects& sensed_objects() const override {
183  if (!cached_) {
184  for (auto o : sensor_->sensed_objects()) {
185  auto obj = map_func_(o);
186  if (obj) {
187  objects_.push_back(obj);
188  }
189  }
190  cached_ = true;
191  }
192  return objects_;
193  }
194 
195  const Frustum& frustum() const override { return sensor_->frustum(); }
196 
197  const Eigen::Isometry3d& mount_pose() const override { return sensor_->mount_pose(); }
198 
207  Duration process(const Sync& sync) override {
208  // This currently shouldn't do anything, but this class acts as a prototype
209  // for How It Should Be Done.
211  if (t < sync.time()) {
212  return t;
213  }
214 
215  // Process the underlying sensor and clear the cache.
216  t = sensor_->process(sync);
217  clear_cache();
218  return t;
219  }
220 
221  void reset() override {
223  sensor_->reset();
224  clear_cache();
225  }
226 
227  void abort() override {
229  sensor_->abort();
230  }
231 
232  protected:
233  virtual void clear_cache() {
234  objects_.clear();
235  cached_ = false;
236  }
237 
238  protected:
239  mutable bool cached_;
240  mutable Objects objects_;
241  std::shared_ptr<ObjectSensor> sensor_;
242  ObjectFilterMap map_func_;
243 };
244 
245 } // namespace cloe
246 
247 #endif // CLOE_COMPONENT_OBJECT_SENSOR_FUNCTIONAL_HPP_
const Frustum & frustum() const override
Definition: object_sensor_functional.hpp:113
Definition: object_sensor_functional.hpp:174
void abort() override
Definition: object_sensor_functional.hpp:227
virtual Duration time() const =0
Duration process(const Sync &sync) override
Definition: object_sensor_functional.hpp:125
void reset() override
Definition: object_sensor_functional.hpp:139
Definition: sync.hpp:35
const Objects & sensed_objects() const override
Definition: object_sensor_functional.hpp:182
std::vector< std::shared_ptr< Object > > Objects
Definition: object.hpp:111
std::function< bool(const Object &)> ObjectFilter
Definition: object_sensor_functional.hpp:44
Definition: frustum.hpp:37
Definition: coordinator.hpp:36
Duration process(const Sync &sync) override
Definition: component.hpp:182
std::chrono::nanoseconds Duration
Definition: duration.hpp:45
Definition: object_sensor.hpp:33
std::function< std::shared_ptr< Object >(const std::shared_ptr< Object > &)> ObjectFilterMap
Definition: object_sensor_functional.hpp:54
void abort() override
Definition: object_sensor_functional.hpp:145
Duration process(const Sync &sync) override
Definition: object_sensor_functional.hpp:207
const Eigen::Isometry3d & mount_pose() const override
Definition: object_sensor_functional.hpp:197
const Objects & sensed_objects() const override
Definition: object_sensor_functional.hpp:101
const Frustum & frustum() const override
Definition: object_sensor_functional.hpp:195
void abort() override
Definition: component.hpp:192
std::string name() const
Definition: entity.hpp:68
const Eigen::Isometry3d & mount_pose() const override
Definition: object_sensor_functional.hpp:115
void reset() override
Definition: component.hpp:187
void reset() override
Definition: object_sensor_functional.hpp:221
Definition: object_sensor_functional.hpp:91