|
| 1 | +// Copyright 2024 DeepMind Technologies Limited |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "mjpc/spline/spline.h" |
| 16 | + |
| 17 | +#include <algorithm> |
| 18 | +#include <cstddef> |
| 19 | +#include <utility> |
| 20 | +#include <vector> |
| 21 | + |
| 22 | +#include <absl/log/check.h> |
| 23 | +#include <absl/log/log.h> |
| 24 | +#include <absl/types/span.h> |
| 25 | + |
| 26 | +namespace mjpc::spline { |
| 27 | + |
| 28 | +TimeSpline::TimeSpline(int dim, int initial_capacity) : dim_(dim) { |
| 29 | + values_.resize(initial_capacity * dim); // Reserve space for node values |
| 30 | +} |
| 31 | + |
| 32 | +std::size_t TimeSpline::Size() const { return times_.size(); } |
| 33 | + |
| 34 | +TimeSpline::Node TimeSpline::NodeAt(int index) { |
| 35 | + int values_index_ = values_begin_ + index * dim_; |
| 36 | + if (values_index_ >= values_.size()) { |
| 37 | + values_index_ -= values_.size(); |
| 38 | + CHECK_LE(values_index_, values_.size()); |
| 39 | + } |
| 40 | + return Node(times_[index], values_.data() + values_index_, dim_); |
| 41 | +} |
| 42 | + |
| 43 | +TimeSpline::ConstNode TimeSpline::NodeAt(int index) const { |
| 44 | + int values_index_ = values_begin_ + index * dim_; |
| 45 | + if (values_index_ >= values_.size()) { |
| 46 | + values_index_ -= values_.size(); |
| 47 | + CHECK_LE(values_index_, values_.size()); |
| 48 | + } |
| 49 | + return ConstNode(times_[index], values_.data() + values_index_, dim_); |
| 50 | +} |
| 51 | + |
| 52 | +int TimeSpline::Dim() const { return dim_; } |
| 53 | + |
| 54 | +// Reserves memory for at least num_nodes. If the spline already contains |
| 55 | +// more nodes, does nothing. |
| 56 | +void TimeSpline::Reserve(int num_nodes) { |
| 57 | + if (num_nodes * dim_ <= values_.size()) { |
| 58 | + return; |
| 59 | + } |
| 60 | + if (values_begin_ < values_end_ || times_.empty()) { |
| 61 | + // Easy case: just resize the values_ vector and remap the spans if needed, |
| 62 | + // without any further data copies. |
| 63 | + values_.resize(num_nodes * dim_); |
| 64 | + } else { |
| 65 | + std::vector<double> new_values(num_nodes * dim_); |
| 66 | + // Copy all existing values to the start of the new vector |
| 67 | + std::copy(values_.begin() + values_begin_, values_.end(), |
| 68 | + new_values.begin()); |
| 69 | + std::copy(values_.begin(), values_.begin() + values_end_, |
| 70 | + new_values.begin() + values_.size() - values_begin_); |
| 71 | + values_ = std::move(new_values); |
| 72 | + values_begin_ = 0; |
| 73 | + values_end_ = times_.size() * dim_; |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +void TimeSpline::Sample(double time, absl::Span<double> values) const { |
| 78 | + CHECK_EQ(values.size(), dim_) |
| 79 | + << "Tried to sample " << values.size() |
| 80 | + << " values, but the dimensionality of the spline is " << dim_; |
| 81 | + |
| 82 | + if (times_.empty()) { |
| 83 | + std::fill(values.begin(), values.end(), 0.0); |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + auto upper = std::upper_bound(times_.begin(), times_.end(), time); |
| 88 | + if (upper == times_.end()) { |
| 89 | + ConstNode n = NodeAt(upper - times_.begin() - 1); |
| 90 | + std::copy(n.values().begin(), n.values().end(), values.begin()); |
| 91 | + return; |
| 92 | + } |
| 93 | + if (upper == times_.begin()) { |
| 94 | + ConstNode n = NodeAt(upper - times_.begin()); |
| 95 | + std::copy(n.values().begin(), n.values().end(), values.begin()); |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + auto lower = upper - 1; |
| 100 | + ConstNode n = NodeAt(lower - times_.begin()); |
| 101 | + std::copy(n.values().begin(), n.values().end(), values.begin()); |
| 102 | +} |
| 103 | + |
| 104 | +std::vector<double> TimeSpline::Sample(double time) const { |
| 105 | + std::vector<double> values(dim_); |
| 106 | + Sample(time, absl::MakeSpan(values)); |
| 107 | + return values; |
| 108 | +} |
| 109 | + |
| 110 | +int TimeSpline::DiscardBefore(double time) { |
| 111 | + // Find the first node that has n.time > time. |
| 112 | + auto last_node = std::upper_bound(times_.begin(), times_.end(), time); |
| 113 | + if (last_node == times_.begin()) { |
| 114 | + return 0; |
| 115 | + } |
| 116 | + last_node--; |
| 117 | + |
| 118 | + int nodes_to_remove = last_node - times_.begin(); |
| 119 | + |
| 120 | + times_.erase(times_.begin(), last_node); |
| 121 | + values_begin_ += dim_ * nodes_to_remove; |
| 122 | + if (values_begin_ >= values_.size()) { |
| 123 | + values_begin_ -= values_.size(); |
| 124 | + CHECK_LE(values_begin_, values_.size()); |
| 125 | + } |
| 126 | + return nodes_to_remove; |
| 127 | +} |
| 128 | + |
| 129 | +void TimeSpline::Clear() { |
| 130 | + times_.clear(); |
| 131 | + values_begin_ = 0; |
| 132 | + values_end_ = 0; |
| 133 | + // Don't change capacity_ or reset values_. |
| 134 | +} |
| 135 | + |
| 136 | +// Adds a new set of values at the given time. Implementation is only |
| 137 | +// efficient if time is later than any previously added nodes. |
| 138 | +TimeSpline::Node TimeSpline::AddNode(double time) { |
| 139 | + return AddNode(time, absl::Span<const double>()); // Default empty values |
| 140 | +} |
| 141 | + |
| 142 | +TimeSpline::Node TimeSpline::AddNode(double time, |
| 143 | + absl::Span<const double> new_values) { |
| 144 | + CHECK(new_values.size() == dim_ || new_values.empty()); |
| 145 | + // TODO(nimrod): Implement node insertion in the middle of the spline |
| 146 | + CHECK(times_.empty() || time > times_.back() || time < times_.front()) |
| 147 | + << "Adding nodes to the middle of the spline isn't supported."; |
| 148 | + if (times_.size() * dim_ >= values_.size()) { |
| 149 | + Reserve(times_.size() * 2); |
| 150 | + } |
| 151 | + Node new_node; |
| 152 | + if (times_.empty() || time > times_.back()) { |
| 153 | + CHECK_LE(values_end_ + dim_, values_.size()); |
| 154 | + times_.push_back(time); |
| 155 | + values_end_ += dim_; |
| 156 | + if (values_end_ >= values_.size()) { |
| 157 | + CHECK_EQ(values_end_, values_.size()); |
| 158 | + values_end_ -= values_.size(); |
| 159 | + } |
| 160 | + new_node = NodeAt(times_.size() - 1); |
| 161 | + } else { |
| 162 | + CHECK_LT(time, times_.front()); |
| 163 | + values_begin_ -= dim_; |
| 164 | + if (values_begin_ < 0) { |
| 165 | + values_begin_ += values_.size(); |
| 166 | + } |
| 167 | + CHECK_LE(values_begin_ + dim_, values_.size()); |
| 168 | + times_.push_front(time); |
| 169 | + new_node = NodeAt(0); |
| 170 | + } |
| 171 | + if (!new_values.empty()) { |
| 172 | + std::copy(new_values.begin(), new_values.end(), new_node.values().begin()); |
| 173 | + } else { |
| 174 | + std::fill(new_node.values().begin(), new_node.values().end(), 0.0); |
| 175 | + } |
| 176 | + return new_node; |
| 177 | +} |
| 178 | +} // namespace mjpc::spline |
0 commit comments