-
Notifications
You must be signed in to change notification settings - Fork 70
/
discrete_trajectory_body.hpp
338 lines (285 loc) · 10.7 KB
/
discrete_trajectory_body.hpp
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
#pragma once
#include "physics/discrete_trajectory.hpp"
#include <algorithm>
#include <list>
#include <map>
#include <vector>
#include "astronomy/epoch.hpp"
#include "geometry/named_quantities.hpp"
#include "glog/logging.h"
namespace principia {
namespace physics {
namespace internal_forkable {
using geometry::Instant;
template<typename Frame>
Instant const& ForkableTraits<DiscreteTrajectory<Frame>>::time(
TimelineConstIterator const it) {
return it->first;
}
template<typename Frame>
Instant const& DiscreteTrajectoryIterator<Frame>::time() const {
return this->current()->first;
}
template<typename Frame>
DegreesOfFreedom<Frame> const&
DiscreteTrajectoryIterator<Frame>::degrees_of_freedom() const {
return this->current()->second;
}
template<typename Frame>
not_null<DiscreteTrajectoryIterator<Frame>*>
DiscreteTrajectoryIterator<Frame>::that() {
return this;
}
template<typename Frame>
not_null<DiscreteTrajectoryIterator<Frame> const*>
DiscreteTrajectoryIterator<Frame>::that() const {
return this;
}
} // namespace internal_forkable
namespace internal_discrete_trajectory {
using astronomy::InfiniteFuture;
using astronomy::InfinitePast;
using base::make_not_null_unique;
template<typename Frame>
typename DiscreteTrajectory<Frame>::Iterator
DiscreteTrajectory<Frame>::last() const {
return --this->End();
}
template<typename Frame>
not_null<DiscreteTrajectory<Frame>*>
DiscreteTrajectory<Frame>::NewForkWithCopy(Instant const& time) {
// May be at |timeline_end()| if |time| is the fork time of this object.
auto timeline_it = timeline_.find(time);
CHECK(timeline_it != timeline_end() ||
(!this->is_root() && time == this->Fork().time()))
<< "NewForkWithCopy at nonexistent time " << time;
auto const fork = this->NewFork(timeline_it);
// Copy the tail of the trajectory in the child object.
if (timeline_it != timeline_.end()) {
fork->timeline_.insert(++timeline_it, timeline_.end());
}
return fork;
}
template<typename Frame>
not_null<DiscreteTrajectory<Frame>*>
DiscreteTrajectory<Frame>::NewForkWithoutCopy(Instant const& time) {
// May be at |timeline_end()| if |time| is the fork time of this object.
auto timeline_it = timeline_.find(time);
CHECK(timeline_it != timeline_end() ||
(!this->is_root() && time == this->Fork().time()))
<< "NewForkWithoutCopy at nonexistent time " << time;
return this->NewFork(timeline_it);
}
template<typename Frame>
not_null<DiscreteTrajectory<Frame>*>
DiscreteTrajectory<Frame>::NewForkAtLast() {
auto end = timeline_.end();
if (timeline_.empty()) {
return this->NewFork(end);
} else {
return this->NewFork(--end);
}
}
template<typename Frame>
void DiscreteTrajectory<Frame>::AttachFork(
not_null<std::unique_ptr<DiscreteTrajectory<Frame>>> fork) {
CHECK(fork->is_root());
CHECK(!fork->timeline_.empty());
// Append to this trajectory a copy of the first point of |fork|.
auto& fork_timeline = fork->timeline_;
auto fork_begin = fork_timeline.begin();
Append(fork_begin->first, fork_begin->second);
// Attach |fork| to this trajectory.
this->AttachForkToCopiedBegin(std::move(fork));
// Remove the first point of |fork| now that it properly attached to its
// parent.
fork_timeline.erase(fork_begin);
}
template<typename Frame>
not_null<std::unique_ptr<DiscreteTrajectory<Frame>>>
DiscreteTrajectory<Frame>::DetachFork() {
CHECK(!this->is_root());
// Insert a new point in the timeline for the fork time. It should go at the
// beginning of the timeline.
auto const fork_it = this->Fork();
auto const begin_it = timeline_.emplace_hint(
timeline_.begin(), fork_it.time(), fork_it.degrees_of_freedom());
CHECK(begin_it == timeline_.begin());
// Detach this trajectory and tell the caller that it owns the pieces.
return this->DetachForkWithCopiedBegin();
}
template<typename Frame>
void DiscreteTrajectory<Frame>::Append(
Instant const& time,
DegreesOfFreedom<Frame> const& degrees_of_freedom) {
CHECK(this->is_root() || time > this->Fork().time())
<< "Append at " << time << " which is before fork time "
<< this->Fork().time();
if (!timeline_.empty() && timeline_.cbegin()->first == time) {
LOG(WARNING) << "Append at existing time " << time
<< ", time range = [" << this->Begin().time() << ", "
<< last().time() << "]";
return;
}
auto it = timeline_.emplace_hint(timeline_.end(),
time,
degrees_of_freedom);
// Decrementing |end()| is much faster than incrementing |it|. Don't ask.
CHECK(--timeline_.end() == it)
<< "Append out of order at " << time << ", last time is "
<< (--timeline_.end())->first;
}
template<typename Frame>
void DiscreteTrajectory<Frame>::ForgetAfter(Instant const& time) {
this->DeleteAllForksAfter(time);
// Get an iterator denoting the first entry with time > |time|. Remove that
// entry and all the entries that follow it. This preserves any entry with
// time == |time|.
auto const it = timeline_.upper_bound(time);
timeline_.erase(it, timeline_.end());
}
template<typename Frame>
void DiscreteTrajectory<Frame>::ForgetBefore(Instant const& time) {
this->CheckNoForksBefore(time);
// Get an iterator denoting the first entry with time >= |time|. Remove all
// the entries that precede it. This preserves any entry with time == |time|.
auto it = timeline_.lower_bound(time);
timeline_.erase(timeline_.begin(), it);
}
template<typename Frame>
Instant DiscreteTrajectory<Frame>::t_min() const {
return this->Empty() ? InfiniteFuture : this->Begin().time();
}
template<typename Frame>
Instant DiscreteTrajectory<Frame>::t_max() const {
return this->Empty() ? InfinitePast : last().time();
}
template<typename Frame>
Position<Frame> DiscreteTrajectory<Frame>::EvaluatePosition(
Instant const& time) const {
return GetInterpolation(time).Evaluate(time);
}
template<typename Frame>
Velocity<Frame> DiscreteTrajectory<Frame>::EvaluateVelocity(
Instant const& time) const {;
return GetInterpolation(time).EvaluateDerivative(time);
}
template<typename Frame>
DegreesOfFreedom<Frame> DiscreteTrajectory<Frame>::EvaluateDegreesOfFreedom(
Instant const& time) const {
auto const interpolation = GetInterpolation(time);
return {interpolation.Evaluate(time), interpolation.EvaluateDerivative(time)};
}
template<typename Frame>
void DiscreteTrajectory<Frame>::WriteToMessage(
not_null<serialization::DiscreteTrajectory*> const message,
std::vector<DiscreteTrajectory<Frame>*> const& forks)
const {
CHECK(this->is_root());
std::vector<DiscreteTrajectory<Frame>*> mutable_forks = forks;
WriteSubTreeToMessage(message, mutable_forks);
CHECK(std::all_of(mutable_forks.begin(),
mutable_forks.end(),
[](DiscreteTrajectory<Frame>* const fork) {
return fork == nullptr;
}));
}
template<typename Frame>
not_null<std::unique_ptr<DiscreteTrajectory<Frame>>>
DiscreteTrajectory<Frame>::ReadFromMessage(
serialization::DiscreteTrajectory const& message,
std::vector<DiscreteTrajectory<Frame>**> const& forks) {
auto trajectory = make_not_null_unique<DiscreteTrajectory>();
CHECK(std::all_of(forks.begin(),
forks.end(),
[](DiscreteTrajectory<Frame>** const fork) {
return fork != nullptr && *fork == nullptr;
}));
trajectory->FillSubTreeFromMessage(message, forks);
return trajectory;
}
template<typename Frame>
not_null<DiscreteTrajectory<Frame>*> DiscreteTrajectory<Frame>::that() {
return this;
}
template<typename Frame>
not_null<DiscreteTrajectory<Frame> const*>
DiscreteTrajectory<Frame>::that() const {
return this;
}
template<typename Frame>
typename DiscreteTrajectory<Frame>::TimelineConstIterator
DiscreteTrajectory<Frame>::timeline_begin() const {
return timeline_.begin();
}
template<typename Frame>
typename DiscreteTrajectory<Frame>::TimelineConstIterator
DiscreteTrajectory<Frame>::timeline_end() const {
return timeline_.end();
}
template<typename Frame>
typename DiscreteTrajectory<Frame>::TimelineConstIterator
DiscreteTrajectory<Frame>::timeline_find(Instant const& time) const {
return timeline_.find(time);
}
template<typename Frame>
typename DiscreteTrajectory<Frame>::TimelineConstIterator
DiscreteTrajectory<Frame>::timeline_lower_bound(Instant const& time) const {
return timeline_.lower_bound(time);
}
template<typename Frame>
bool DiscreteTrajectory<Frame>::timeline_empty() const {
return timeline_.empty();
}
template<typename Frame>
std::int64_t DiscreteTrajectory<Frame>::timeline_size() const {
return timeline_.size();
}
template<typename Frame>
void DiscreteTrajectory<Frame>::WriteSubTreeToMessage(
not_null<serialization::DiscreteTrajectory*> const message,
std::vector<DiscreteTrajectory<Frame>*>& forks) const {
Forkable<DiscreteTrajectory, Iterator>::WriteSubTreeToMessage(message, forks);
for (auto const& pair : timeline_) {
Instant const& instant = pair.first;
DegreesOfFreedom<Frame> const& degrees_of_freedom = pair.second;
auto const instantaneous_degrees_of_freedom = message->add_timeline();
instant.WriteToMessage(instantaneous_degrees_of_freedom->mutable_instant());
degrees_of_freedom.WriteToMessage(
instantaneous_degrees_of_freedom->mutable_degrees_of_freedom());
}
}
template<typename Frame>
void DiscreteTrajectory<Frame>::FillSubTreeFromMessage(
serialization::DiscreteTrajectory const& message,
std::vector<DiscreteTrajectory<Frame>**> const& forks) {
for (auto timeline_it = message.timeline().begin();
timeline_it != message.timeline().end();
++timeline_it) {
Append(Instant::ReadFromMessage(timeline_it->instant()),
DegreesOfFreedom<Frame>::ReadFromMessage(
timeline_it->degrees_of_freedom()));
}
Forkable<DiscreteTrajectory, Iterator>::FillSubTreeFromMessage(message,
forks);
}
template<typename Frame>
Hermite3<Instant, Position<Frame>> DiscreteTrajectory<Frame>::GetInterpolation(
Instant const& time) const {
CHECK_LE(t_min(), time);
CHECK_GE(t_max(), time);
// This is the upper bound of the interval upon which we will do the
// interpolation.
auto const upper = this->LowerBound(time);
auto const lower = upper == this->Begin() ? upper : --Iterator{upper};
return Hermite3<Instant, Position<Frame>>{
{lower.time(), upper.time()},
{lower.degrees_of_freedom().position(),
upper.degrees_of_freedom().position()},
{lower.degrees_of_freedom().velocity(),
upper.degrees_of_freedom().velocity()}};
}
} // namespace internal_discrete_trajectory
} // namespace physics
} // namespace principia