|
| 1 | +#ifndef CLIP_H |
| 2 | +#define CLIP_H |
| 3 | + |
| 4 | +#include <algorithm> |
| 5 | +#include <optional> |
| 6 | + |
| 7 | +#include "rtbot/Message.h" |
| 8 | +#include "rtbot/Operator.h" |
| 9 | +#include "rtbot/PortType.h" |
| 10 | + |
| 11 | +namespace rtbot { |
| 12 | + |
| 13 | +class Clip : public Operator { |
| 14 | + public: |
| 15 | + Clip(std::string id, std::optional<double> lower = std::nullopt, std::optional<double> upper = std::nullopt) |
| 16 | + : Operator(std::move(id)), lower_(lower), upper_(upper) { |
| 17 | + add_data_port<NumberData>(); |
| 18 | + add_output_port<NumberData>(); |
| 19 | + } |
| 20 | + |
| 21 | + void reset() override { Operator::reset(); } |
| 22 | + |
| 23 | + std::string type_name() const override { return "Clip"; } |
| 24 | + |
| 25 | + std::optional<double> lower() const { return lower_; } |
| 26 | + std::optional<double> upper() const { return upper_; } |
| 27 | + |
| 28 | + Bytes collect() override { |
| 29 | + Bytes bytes = Operator::collect(); |
| 30 | + |
| 31 | + bool has_lower = lower_.has_value(); |
| 32 | + bool has_upper = upper_.has_value(); |
| 33 | + bytes.push_back(static_cast<uint8_t>(has_lower)); |
| 34 | + if (has_lower) { |
| 35 | + bytes.insert(bytes.end(), reinterpret_cast<const uint8_t*>(&(*lower_)), |
| 36 | + reinterpret_cast<const uint8_t*>(&(*lower_)) + sizeof(double)); |
| 37 | + } |
| 38 | + |
| 39 | + bytes.push_back(static_cast<uint8_t>(has_upper)); |
| 40 | + if (has_upper) { |
| 41 | + bytes.insert(bytes.end(), reinterpret_cast<const uint8_t*>(&(*upper_)), |
| 42 | + reinterpret_cast<const uint8_t*>(&(*upper_)) + sizeof(double)); |
| 43 | + } |
| 44 | + |
| 45 | + return bytes; |
| 46 | + } |
| 47 | + |
| 48 | + void restore(Bytes::const_iterator& it) override { |
| 49 | + Operator::restore(it); |
| 50 | + |
| 51 | + bool has_lower = static_cast<bool>(*it++); |
| 52 | + if (has_lower) { |
| 53 | + lower_ = *reinterpret_cast<const double*>(&(*it)); |
| 54 | + it += sizeof(double); |
| 55 | + } else { |
| 56 | + lower_.reset(); |
| 57 | + } |
| 58 | + |
| 59 | + bool has_upper = static_cast<bool>(*it++); |
| 60 | + if (has_upper) { |
| 61 | + upper_ = *reinterpret_cast<const double*>(&(*it)); |
| 62 | + it += sizeof(double); |
| 63 | + } else { |
| 64 | + upper_.reset(); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + protected: |
| 69 | + void process_data() override { |
| 70 | + auto& input_queue = get_data_queue(0); |
| 71 | + auto& output_queue = get_output_queue(0); |
| 72 | + |
| 73 | + while (!input_queue.empty()) { |
| 74 | + const auto* msg = dynamic_cast<const Message<NumberData>*>(input_queue.front().get()); |
| 75 | + if (!msg) { |
| 76 | + throw std::runtime_error("Invalid message type in Clip operator."); |
| 77 | + } |
| 78 | + |
| 79 | + double value = msg->data.value; |
| 80 | + if (lower_) { |
| 81 | + value = std::max(value, *lower_); |
| 82 | + } |
| 83 | + if (upper_) { |
| 84 | + value = std::min(value, *upper_); |
| 85 | + } |
| 86 | + |
| 87 | + output_queue.push_back(create_message<NumberData>(msg->time, NumberData{value})); |
| 88 | + input_queue.pop_front(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private: |
| 93 | + std::optional<double> lower_; |
| 94 | + std::optional<double> upper_; |
| 95 | +}; |
| 96 | + |
| 97 | +inline std::shared_ptr<Operator> make_clip(const std::string& id, std::optional<double> lower = std::nullopt, |
| 98 | + std::optional<double> upper = std::nullopt) { |
| 99 | + return std::make_shared<Clip>(id, lower, upper); |
| 100 | +} |
| 101 | + |
| 102 | +} // namespace rtbot |
| 103 | + |
| 104 | +#endif // CLIP_H |
0 commit comments