$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 
29 #include <functional> // for function
30 #include <memory> // for shared_ptr<>
31 #include <string> // for string
32 
33 #include <cloe/component/object.hpp> // for Object
34 #include <cloe/component/object_sensor.hpp> // for ObjectSensor
35 
36 namespace cloe {
37 
42 using ObjectFilter = std::function<bool(const Object&)>;
43 
52 using ObjectFilterMap = std::function<std::shared_ptr<Object>(const std::shared_ptr<Object>&)>;
53 
90  public:
91  ObjectSensorFilter(std::shared_ptr<ObjectSensor> obs, ObjectFilter f)
92  : ObjectSensor("object_sensor_filter"), sensor_(obs), filter_func_(f) {}
93 
94  virtual ~ObjectSensorFilter() noexcept = default;
95 
99  const Objects& sensed_objects() const override {
100  if (!cached_) {
101  for (auto o : sensor_->sensed_objects()) {
102  if (filter_func_(*o)) {
103  objects_.push_back(o);
104  }
105  }
106  cached_ = true;
107  }
108  return objects_;
109  }
110 
111  const Frustum& frustum() const override { return sensor_->frustum(); }
112 
113  const Eigen::Isometry3d& mount_pose() const override { return sensor_->mount_pose(); }
114 
123  Duration process(const Sync& sync) override {
124  // This currently shouldn't do anything, but this class acts as a prototype
125  // for How It Should Be Done.
127  if (t < sync.time()) {
128  return t;
129  }
130 
131  // Process the underlying sensor and clear the cache.
132  t = sensor_->process(sync);
133  clear_cache();
134  return t;
135  }
136 
137  void reset() override {
139  sensor_->reset();
140  clear_cache();
141  }
142 
143  void abort() override {
145  sensor_->abort();
146  }
147 
148  protected:
149  void clear_cache() {
150  objects_.clear();
151  cached_ = false;
152  }
153 
154  private:
155  mutable bool cached_;
156  mutable Objects objects_;
157  std::shared_ptr<ObjectSensor> sensor_;
158  ObjectFilter filter_func_;
159 };
160 
173  public:
174  ObjectSensorFilterMap(const std::string& name, std::shared_ptr<ObjectSensor> obs,
175  ObjectFilterMap f)
176  : ObjectSensor(name), sensor_(obs), map_func_(f) {}
177 
178  virtual ~ObjectSensorFilterMap() noexcept = default;
179 
180  const Objects& sensed_objects() const override {
181  if (!cached_) {
182  for (auto o : sensor_->sensed_objects()) {
183  auto obj = map_func_(o);
184  if (obj) {
185  objects_.push_back(obj);
186  }
187  }
188  cached_ = true;
189  }
190  return objects_;
191  }
192 
193  const Frustum& frustum() const override { return sensor_->frustum(); }
194 
195  const Eigen::Isometry3d& mount_pose() const override { return sensor_->mount_pose(); }
196 
205  Duration process(const Sync& sync) override {
206  // This currently shouldn't do anything, but this class acts as a prototype
207  // for How It Should Be Done.
209  if (t < sync.time()) {
210  return t;
211  }
212 
213  // Process the underlying sensor and clear the cache.
214  t = sensor_->process(sync);
215  clear_cache();
216  return t;
217  }
218 
219  void reset() override {
221  sensor_->reset();
222  clear_cache();
223  }
224 
225  void abort() override {
227  sensor_->abort();
228  }
229 
230  protected:
231  virtual void clear_cache() {
232  objects_.clear();
233  cached_ = false;
234  }
235 
236  protected:
237  mutable bool cached_;
238  mutable Objects objects_;
239  std::shared_ptr<ObjectSensor> sensor_;
240  ObjectFilterMap map_func_;
241 };
242 
243 } // namespace cloe
Duration process(const Sync &sync) override
Definition: component.hpp:182
void abort() override
Definition: component.hpp:192
void reset() override
Definition: component.hpp:187
const std::string & name() const
Definition: entity.hpp:67
Definition: object_sensor_functional.hpp:172
Duration process(const Sync &sync) override
Definition: object_sensor_functional.hpp:205
void reset() override
Definition: object_sensor_functional.hpp:219
const Eigen::Isometry3d & mount_pose() const override
Definition: object_sensor_functional.hpp:195
const Frustum & frustum() const override
Definition: object_sensor_functional.hpp:193
void abort() override
Definition: object_sensor_functional.hpp:225
const Objects & sensed_objects() const override
Definition: object_sensor_functional.hpp:180
Definition: object_sensor_functional.hpp:89
const Objects & sensed_objects() const override
Definition: object_sensor_functional.hpp:99
Duration process(const Sync &sync) override
Definition: object_sensor_functional.hpp:123
void abort() override
Definition: object_sensor_functional.hpp:143
void reset() override
Definition: object_sensor_functional.hpp:137
const Frustum & frustum() const override
Definition: object_sensor_functional.hpp:111
const Eigen::Isometry3d & mount_pose() const override
Definition: object_sensor_functional.hpp:113
Definition: object_sensor.hpp:33
Definition: sync.hpp:34
virtual Duration time() const =0
std::chrono::nanoseconds Duration
Definition: cloe_fwd.hpp:36
std::vector< std::shared_ptr< Object > > Objects
Definition: object.hpp:128
std::function< bool(const Object &)> ObjectFilter
Definition: object_sensor_functional.hpp:42
std::function< std::shared_ptr< Object >(const std::shared_ptr< Object > &)> ObjectFilterMap
Definition: object_sensor_functional.hpp:52
Definition: frustum.hpp:37
Definition: object.hpp:51