-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
kinematics_vector.cc
119 lines (103 loc) · 3.63 KB
/
kinematics_vector.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "drake/geometry/kinematics_vector.h"
#include <stdexcept>
#include <fmt/format.h>
#include "drake/common/default_scalars.h"
#include "drake/common/nice_type_name.h"
namespace drake {
namespace geometry {
template <typename Id, typename KinematicsValue>
KinematicsVector<Id, KinematicsValue>::KinematicsVector() {
DRAKE_ASSERT_VOID(CheckInvariants());
}
template <typename Id, typename KinematicsValue>
KinematicsVector<Id, KinematicsValue>::KinematicsVector(
std::initializer_list<std::pair<const Id, KinematicsValue>> init) {
values_.insert(init.begin(), init.end());
size_ = init.size();
DRAKE_ASSERT_VOID(CheckInvariants());
}
template <typename Id, typename KinematicsValue>
KinematicsVector<Id, KinematicsValue>&
KinematicsVector<Id, KinematicsValue>::operator=(
std::initializer_list<std::pair<const Id, KinematicsValue>> init) {
// N.B. We can't use unordered_map::insert in our operator= implementation
// because it does not overwrite pre-existing keys. (Our clear() doesn't
// remove the keys, it only nulls the values.)
clear();
for (const auto& item : init) {
set_value(item.first, item.second);
}
DRAKE_ASSERT_VOID(CheckInvariants());
return *this;
}
template <typename Id, typename KinematicsValue>
KinematicsVector<Id, KinematicsValue>::~KinematicsVector() = default;
template <typename Id, typename KinematicsValue>
void KinematicsVector<Id, KinematicsValue>::clear() {
for (auto& item : values_) {
item.second = std::nullopt;
}
size_ = 0;
}
template <typename Id, typename KinematicsValue>
void KinematicsVector<Id, KinematicsValue>::set_value(
Id id, const KinematicsValue& value) {
auto& map_value = values_[id];
if (!map_value.has_value()) {
++size_;
}
map_value = value;
}
template <typename Id, typename KinematicsValue>
const KinematicsValue& KinematicsVector<Id, KinematicsValue>::value(
Id id) const {
using std::to_string;
auto iter = values_.find(id);
if (iter != values_.end()) {
const std::optional<KinematicsValue>& map_value = iter->second;
if (map_value.has_value()) {
return *map_value;
}
}
throw std::runtime_error(fmt::format(
"No such {}: {}.",
NiceTypeName::RemoveNamespaces(NiceTypeName::Get<Id>()), to_string(id)));
}
template <typename Id, typename KinematicsValue>
bool KinematicsVector<Id, KinematicsValue>::has_id(Id id) const {
auto iter = values_.find(id);
return (iter != values_.end()) && iter->second.has_value();
}
template <typename Id, typename KinematicsValue>
std::vector<Id> KinematicsVector<Id, KinematicsValue>::ids() const {
std::vector<Id> result;
result.reserve(size_);
for (const auto& item : values_) {
if (item.second.has_value()) {
result.emplace_back(item.first);
}
}
DRAKE_ASSERT(static_cast<int>(result.size()) == size_);
return result;
}
template <typename Id, typename KinematicsValue>
void KinematicsVector<Id, KinematicsValue>::CheckInvariants() const {
int num_nonnull = 0;
for (const auto& item : values_) {
if (item.second.has_value()) {
++num_nonnull;
}
}
DRAKE_DEMAND(num_nonnull == size_);
}
// Explicitly instantiates on the most common scalar types.
using math::RigidTransform;
using symbolic::Expression;
template class KinematicsVector<FrameId, RigidTransform<double>>;
template class KinematicsVector<FrameId, RigidTransform<AutoDiffXd>>;
template class KinematicsVector<FrameId, RigidTransform<Expression>>;
template class KinematicsVector<GeometryId, VectorX<double>>;
template class KinematicsVector<GeometryId, VectorX<AutoDiffXd>>;
template class KinematicsVector<GeometryId, VectorX<Expression>>;
} // namespace geometry
} // namespace drake