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

DiscreteTrajectorySegment, part 2: forgetting #3146

Merged
merged 4 commits into from
Oct 10, 2021
Merged
Show file tree
Hide file tree
Changes from all 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_trajectory_segment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,13 @@ class DiscreteTrajectorySegment {
absl::Status Append(Instant const& t,
DegreesOfFreedom<Frame> const& degrees_of_freedom);

// Removes all points with a time greater than or equal to |t| (1st overload)
// or starting at |begin| (2nd overload).
void ForgetAfter(Instant const& t);
void ForgetAfter(typename Timeline::const_iterator begin);

// Removes all points with a time strictly less than |t| (1st overload) or
// ending at |end| (2nd overload).
void ForgetBefore(Instant const& t);
void ForgetBefore(typename Timeline::const_iterator end);

Expand Down
20 changes: 16 additions & 4 deletions physics/discrete_trajectory_segment_body.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,30 @@ absl::Status DiscreteTrajectorySegment<Frame>::Append(
}

template<typename Frame>
void DiscreteTrajectorySegment<Frame>::ForgetAfter(Instant const& t) {}
void DiscreteTrajectorySegment<Frame>::ForgetAfter(Instant const& t) {
ForgetAfter(timeline_.lower_bound(t));
// TODO(phl): Downsampling.
}

template<typename Frame>
void DiscreteTrajectorySegment<Frame>::ForgetAfter(
typename Timeline::const_iterator const begin) {}
typename Timeline::const_iterator const begin) {
timeline_.erase(begin, timeline_.end());
// TODO(phl): Downsampling.
}

template<typename Frame>
void DiscreteTrajectorySegment<Frame>::ForgetBefore(Instant const& t) {}
void DiscreteTrajectorySegment<Frame>::ForgetBefore(Instant const& t) {
ForgetBefore(timeline_.lower_bound(t));
// TODO(phl): Downsampling.
}

template<typename Frame>
void DiscreteTrajectorySegment<Frame>::ForgetBefore(
typename Timeline::const_iterator const end) {}
typename Timeline::const_iterator const end) {
timeline_.erase(timeline_.begin(), end);
// TODO(phl): Downsampling.
}

template<typename Frame>
typename DiscreteTrajectorySegment<Frame>::Timeline::const_iterator
Expand Down
38 changes: 38 additions & 0 deletions physics/discrete_trajectory_segment_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ class DiscreteTrajectorySegmentTest : public ::testing::Test {
segment_->Append(t0_ + 11 * Second, unmoving_origin_);
}

void ForgetAfter(Instant const& t) {
segment_->ForgetAfter(t);
}

void ForgetBefore(Instant const& t) {
segment_->ForgetBefore(t);
}

DiscreteTrajectorySegment<World>* segment_;
internal_discrete_trajectory_types::Segments<World> segments_;
Instant const t0_;
Expand Down Expand Up @@ -106,5 +114,35 @@ TEST_F(DiscreteTrajectorySegmentTest, EmptySize) {
EXPECT_EQ(5, segment_->size());
}

TEST_F(DiscreteTrajectorySegmentTest, ForgetAfterExisting) {
ForgetAfter(t0_ + 5 * Second);
EXPECT_EQ(t0_ + 3 * Second, segment_->rbegin()->first);
}

TEST_F(DiscreteTrajectorySegmentTest, ForgetAfterNonexisting) {
ForgetAfter(t0_ + 6 * Second);
EXPECT_EQ(t0_ + 5 * Second, segment_->rbegin()->first);
}

TEST_F(DiscreteTrajectorySegmentTest, ForgetAfterPastTheEnd) {
ForgetAfter(t0_ + 29 * Second);
EXPECT_EQ(t0_ + 11 * Second, segment_->rbegin()->first);
}

TEST_F(DiscreteTrajectorySegmentTest, ForgetBeforeExisting) {
ForgetBefore(t0_ + 7 * Second);
EXPECT_EQ(t0_ + 7 * Second, segment_->begin()->first);
}

TEST_F(DiscreteTrajectorySegmentTest, ForgetBeforeNonexisting) {
ForgetBefore(t0_ + 6 * Second);
EXPECT_EQ(t0_ + 7 * Second, segment_->begin()->first);
}

TEST_F(DiscreteTrajectorySegmentTest, ForgetBeforeTheBeginning) {
ForgetBefore(t0_ + 1 * Second);
EXPECT_EQ(t0_ + 2 * Second, segment_->begin()->first);
}

} // namespace physics
} // namespace principia