|
| 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 | + */ |
| 18 | +/** |
| 19 | + * \file cloe/component/lane_sensor_functional.hpp |
| 20 | + * \see cloe/component/lane_sensor.hpp |
| 21 | + * \see cloe/component/lane_boundary.hpp |
| 22 | + * |
| 23 | + * This file provides definitions for common functional idioms with respect |
| 24 | + * to LaneBoundaries and LaneBoundarySensors. |
| 25 | + */ |
| 26 | + |
| 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 | + |
| 38 | +/** |
| 39 | + * LaneBoundaryFilter shall return true for any LaneBoundary that should be yielded, |
| 40 | + * and false for every LaneBoundary that should be skipped. |
| 41 | + */ |
| 42 | +using LaneBoundaryFilter = std::function<bool(const LaneBoundary&)>; |
| 43 | + |
| 44 | +/** |
| 45 | + * LaneBoundarySensorFilter filters lane boundaries from an LaneBoundarySensor, and can be used in |
| 46 | + * place of the original LaneBoundarySensor. |
| 47 | + * |
| 48 | + * This class can be used in a very functional way, and the use of C++11 |
| 49 | + * lambdas is highly highly recommended! |
| 50 | + * |
| 51 | + * Warning: Do not rely on volatile state that can change within a step for the |
| 52 | + * filter function. This LaneBoundarySensor filter class caches the resulting vector |
| 53 | + * of filtered lane boundaries till clear_cache is called. |
| 54 | + * |
| 55 | + * Example: |
| 56 | + * |
| 57 | + * ``` |
| 58 | + * using namespace cloe; |
| 59 | + * LaneBoundaryFilter my_super_filter; |
| 60 | + * LaneBoundarySensorInterface my_lbs; |
| 61 | + * my_lbs = LaneBoundarySensorFilter(my_lbs, my_super_filter); |
| 62 | + * ``` |
| 63 | + * |
| 64 | + * You can also use the filters in cloe::utility::filter to combine Filters: |
| 65 | + * |
| 66 | + * ``` |
| 67 | + * using filter = cloe::utility::filter; |
| 68 | + * my_lbs = LaneBoundarySensorFilter(my_lbs, filter::And(my_super_filter, [](const LaneBoundary& o) { |
| 69 | + * if (o.id < 64) { |
| 70 | + * if IsStatic(o) { |
| 71 | + * // ... |
| 72 | + * return true; |
| 73 | + * } |
| 74 | + * } |
| 75 | + * return false; |
| 76 | + * })); |
| 77 | + * ``` |
| 78 | + */ |
| 79 | +class LaneBoundarySensorFilter : public LaneBoundarySensor { |
| 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 | + |
| 86 | + /** |
| 87 | + * Return all sensed lane boundaries that the LaneBoundaryFilter returns true for. |
| 88 | + */ |
| 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 | + |
| 107 | + /** |
| 108 | + * Process the underlying sensor and clear the cache. |
| 109 | + * |
| 110 | + * We could process and create the filtered list of lane boundaries now, but we can |
| 111 | + * also delay it (lazy computation) and only do it when absolutely necessary. |
| 112 | + * This comes at the minor cost of checking whether cached_ is true every |
| 113 | + * time sensed_lane_boundaries() is called. |
| 114 | + */ |
| 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. |
| 118 | + Duration t = LaneBoundarySensor::process(sync); |
| 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 { |
| 130 | + LaneBoundarySensor::reset(); |
| 131 | + sensor_->reset(); |
| 132 | + clear_cache(); |
| 133 | + } |
| 134 | + |
| 135 | + void abort() override { |
| 136 | + LaneBoundarySensor::abort(); |
| 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 |
0 commit comments