Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for empty segments #3172

Merged
merged 4 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions physics/discrete_traject0ry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,12 @@ class DiscreteTraject0ry : public Trajectory<Frame> {
SegmentIterator AttachSegments(DiscreteTraject0ry&& trajectory);
void DeleteSegments(SegmentIterator begin);

// Deletes the point at times in [t, end[. Drops empty segment.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

points? segments?

void ForgetAfter(Instant const& t);
void ForgetAfter(iterator it);

// Deletes the points at times in [begin, t[. Preserves empty segments and
// doesn't invalidate any segment iterator.
void ForgetBefore(Instant const& t);
void ForgetBefore(iterator it);

Expand Down
4 changes: 3 additions & 1 deletion physics/discrete_traject0ry_body.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,9 @@ template<typename Frame>
void DiscreteTraject0ry<Frame>::ForgetBefore(Instant const& t) {
auto const sit = FindSegment(t);
sit->ForgetBefore(t);
segments_->erase(segments_->begin(), sit);
for (auto s = segments_->begin(); s != sit; ++s) {
s->ForgetBefore(t);
}
}

template<typename Frame>
Expand Down
33 changes: 31 additions & 2 deletions physics/discrete_traject0ry_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -481,14 +481,43 @@ TEST_F(DiscreteTraject0ryTest, ForgetBefore) {
EXPECT_EQ(t0_ + 14 * Second, trajectory.rbegin()->time);

trajectory.ForgetBefore(t0_ + 6.1 * Second);
EXPECT_EQ(2, trajectory.segments().size());
EXPECT_EQ(3, trajectory.segments().size());
EXPECT_EQ(t0_ + 7 * Second, trajectory.begin()->time);
EXPECT_EQ(t0_ + 14 * Second, trajectory.rbegin()->time);

trajectory.ForgetBefore(t0_ + 9 * Second);
EXPECT_EQ(1, trajectory.segments().size());
EXPECT_EQ(3, trajectory.segments().size());
EXPECT_EQ(t0_ + 9 * Second, trajectory.begin()->time);
EXPECT_EQ(t0_ + 14 * Second, trajectory.rbegin()->time);

// The trajectory now has empty segments, so let's check that we can properly
// iterate over those.
{
std::vector<Instant> times;
for (auto const& [t, _] : trajectory) {
times.push_back(t);
}
EXPECT_THAT(times,
ElementsAre(t0_ + 9 * Second,
t0_ + 10 * Second,
t0_ + 11 * Second,
t0_ + 12 * Second,
t0_ + 13 * Second,
t0_ + 14 * Second));
}
{
std::vector<Instant> times;
for (auto it = trajectory.rbegin(); it != trajectory.rend(); ++it) {
times.push_back(it->time);
}
EXPECT_THAT(times,
ElementsAre(t0_ + 14 * Second,
t0_ + 13 * Second,
t0_ + 12 * Second,
t0_ + 11 * Second,
t0_ + 10 * Second,
t0_ + 9 * Second));
}
}

TEST_F(DiscreteTraject0ryTest, TMinTMaxEvaluate) {
Expand Down
16 changes: 13 additions & 3 deletions physics/discrete_trajectory_iterator_body.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ DiscreteTrajectoryIterator<Frame>::operator++() {
Instant const previous_time = point->time;
do {
if (point == --segment_->timeline_end()) {
++segment_;
if (segment_.is_end() || segment_->timeline_empty()) {
do {
++segment_;
} while (!segment_.is_end() && segment_->timeline_empty());

if (segment_.is_end()) {
point_.reset();
break;
} else {
Expand Down Expand Up @@ -111,8 +114,15 @@ DiscreteTrajectoryIterator<Frame>::DiscreteTrajectoryIterator(
OptionalTimelineConstIterator const point)
: segment_(segment),
point_(point) {
if (segment_.is_end() || segment_->timeline_empty()) {
bool incremented_segment = false;
while (!segment_.is_end() && segment_->timeline_empty()) {
++segment_;
incremented_segment = true;
}
if (segment_.is_end()) {
point_.reset();
} else if (incremented_segment) {
point_ = segment_->timeline_begin();
}
}

Expand Down
3 changes: 1 addition & 2 deletions physics/discrete_trajectory_segment_body.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,8 @@ DiscreteTrajectorySegment<Frame>::begin() const {
template<typename Frame>
typename DiscreteTrajectorySegment<Frame>::iterator
DiscreteTrajectorySegment<Frame>::end() const {
// TODO(phl): We probably don't want empty segments.
if (timeline_.empty()) {
return iterator(self_, timeline_.begin());
return iterator(self_, timeline_.end());
} else {
// The decrement/increment ensures that we normalize the end iterator to the
// next segment or to the end of the trajectory.
Expand Down