forked from RobotLocomotion/drake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frame_kinematics_vector.cc
118 lines (102 loc) · 3.35 KB
/
frame_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
#include "drake/geometry/frame_kinematics_vector.h"
#include <stdexcept>
#include <fmt/format.h>
#include <fmt/ostream.h>
#include "drake/common/autodiff.h"
#include "drake/common/symbolic.h"
#include "drake/math/rigid_transform.h"
namespace drake {
namespace geometry {
namespace {
template <typename T>
void InitializeKinematicsValue(math::RigidTransform<T>* value) {
value->SetIdentity();
}
} // namespace
template <typename KinematicsValue>
FrameKinematicsVector<KinematicsValue>::FrameKinematicsVector() {
DRAKE_ASSERT_VOID(CheckInvariants());
}
template <typename KinematicsValue>
FrameKinematicsVector<KinematicsValue>::FrameKinematicsVector(
std::initializer_list<std::pair<const FrameId, KinematicsValue>> init) {
values_.insert(init.begin(), init.end());
size_ = init.size();
DRAKE_ASSERT_VOID(CheckInvariants());
}
template <typename KinematicsValue>
FrameKinematicsVector<KinematicsValue>&
FrameKinematicsVector<KinematicsValue>::operator=(
std::initializer_list<std::pair<const FrameId, 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 KinematicsValue>
void FrameKinematicsVector<KinematicsValue>::clear() {
for (auto& item : values_) {
item.second = std::nullopt;
}
size_ = 0;
}
template <typename KinematicsValue>
void FrameKinematicsVector<KinematicsValue>::set_value(
FrameId id, const KinematicsValue& value) {
auto& map_value = values_[id];
if (!map_value.has_value()) { ++size_; }
map_value = value;
}
template <typename KinematicsValue>
const KinematicsValue& FrameKinematicsVector<KinematicsValue>::value(
FrameId 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("No such FrameId " + to_string(id) + ".");
}
template <typename KinematicsValue>
bool FrameKinematicsVector<KinematicsValue>::has_id(FrameId id) const {
auto iter = values_.find(id);
return (iter != values_.end()) && iter->second.has_value();
}
template <typename KinematicsValue>
std::vector<FrameId>
FrameKinematicsVector<KinematicsValue>::frame_ids() const {
std::vector<FrameId> 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 KinematicsValue>
void FrameKinematicsVector<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.
template class FrameKinematicsVector<math::RigidTransform<double>>;
template class FrameKinematicsVector<math::RigidTransform<AutoDiffXd>>;
template class FrameKinematicsVector<
math::RigidTransform<symbolic::Expression>>;
} // namespace geometry
} // namespace drake