$darkmode
lane_sensor_functional.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2022 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/lane_boundary.hpp> // for LaneBoundary
34 #include <cloe/component/lane_sensor.hpp> // for LaneBoundarySensor
35 
36 namespace cloe {
37 
42 using LaneBoundaryFilter = std::function<bool(const LaneBoundary&)>;
43 
80  public:
81  LaneBoundarySensorFilter(std::shared_ptr<LaneBoundarySensor> lbs, LaneBoundaryFilter f)
82  : LaneBoundarySensor("lane_sensor_filter"), sensor_(lbs), filter_func_(f) {}
83 
84  virtual ~LaneBoundarySensorFilter() noexcept = default;
85 
89  const LaneBoundaries& sensed_lane_boundaries() const override {
90  if (!cached_) {
91  for (const auto& kv : sensor_->sensed_lane_boundaries()) {
92  auto lb = kv.second;
93  auto id = kv.first;
94  if (filter_func_(lb)) {
95  lbs_.insert(std::pair<int, LaneBoundary>(id, lb));
96  }
97  }
98  cached_ = true;
99  }
100  return lbs_;
101  }
102 
103  const Frustum& frustum() const override { return sensor_->frustum(); }
104 
105  const Eigen::Isometry3d& mount_pose() const override { return sensor_->mount_pose(); }
106 
115  Duration process(const Sync& sync) override {
116  // This currently shouldn't do anything, but this class acts as a prototype
117  // for How It Should Be Done.
119  if (t < sync.time()) {
120  return t;
121  }
122 
123  // Process the underlying sensor and clear the cache.
124  t = sensor_->process(sync);
125  clear_cache();
126  return t;
127  }
128 
129  void reset() override {
131  sensor_->reset();
132  clear_cache();
133  }
134 
135  void abort() override {
137  sensor_->abort();
138  }
139 
140  protected:
141  void clear_cache() {
142  lbs_.clear();
143  cached_ = false;
144  }
145 
146  private:
147  mutable bool cached_;
148  mutable LaneBoundaries lbs_;
149  std::shared_ptr<LaneBoundarySensor> sensor_;
150  LaneBoundaryFilter filter_func_;
151 };
152 
153 } // namespace cloe
Duration process(const Sync &sync) override
Definition: component.hpp:181
void abort() override
Definition: component.hpp:191
void reset() override
Definition: component.hpp:186
Definition: lane_sensor_functional.hpp:79
const LaneBoundaries & sensed_lane_boundaries() const override
Definition: lane_sensor_functional.hpp:89
const Eigen::Isometry3d & mount_pose() const override
Definition: lane_sensor_functional.hpp:105
const Frustum & frustum() const override
Definition: lane_sensor_functional.hpp:103
Duration process(const Sync &sync) override
Definition: lane_sensor_functional.hpp:115
void abort() override
Definition: lane_sensor_functional.hpp:135
void reset() override
Definition: lane_sensor_functional.hpp:129
Definition: lane_sensor.hpp:33
Definition: lane_boundary.hpp:36
Definition: sync.hpp:34
virtual Duration time() const =0
std::chrono::nanoseconds Duration
Definition: cloe_fwd.hpp:36
std::function< bool(const LaneBoundary &)> LaneBoundaryFilter
Definition: lane_sensor_functional.hpp:42
Definition: frustum.hpp:37