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

Make sure that the number of dense points is correctly maintained by serialization and ForgetAfter #3222

Merged
merged 4 commits into from
Dec 1, 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
14 changes: 8 additions & 6 deletions physics/discrete_trajectory_segment_body.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,11 +353,12 @@ void DiscreteTrajectorySegment<Frame>::ForgetBefore(Instant const& t) {
template<typename Frame>
void DiscreteTrajectorySegment<Frame>::ForgetBefore(
typename Timeline::const_iterator const end) {
std::int64_t number_of_points_to_remove =
std::int64_t const number_of_points_to_remove =
std::distance(timeline_.cbegin(), end);
number_of_dense_points_ =
std::max<std::int64_t>(
0, number_of_dense_points_ - number_of_points_to_remove);
std::int64_t const number_of_dense_points_to_remove = std::max<std::int64_t>(
0,
number_of_points_to_remove + number_of_dense_points_ - timeline_.size());
number_of_dense_points_ -= number_of_dense_points_to_remove;

timeline_.erase(timeline_.cbegin(), end);
}
Expand Down Expand Up @@ -542,9 +543,10 @@ void DiscreteTrajectorySegment<Frame>::WriteToMessage(
downsampling_parameters_->tolerance.WriteToMessage(
serialized_downsampling_parameters->mutable_tolerance());
}
message->set_number_of_dense_points(
message->set_number_of_dense_points(std::min(
timeline_size,
std::max<std::int64_t>(
0, number_of_dense_points_ - number_of_points_to_skip_at_end));
0, number_of_dense_points_ - number_of_points_to_skip_at_end)));

// Convert the |exact| vector into a set, and add the extremities. This
// ensures that we don't have redundancies. The set is sorted by time to
Expand Down
10 changes: 6 additions & 4 deletions physics/discrete_trajectory_segment_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,15 +523,17 @@ TEST_F(DiscreteTrajectorySegmentTest, SerializationRange) {

serialization::DiscreteTrajectorySegment message1;
circle1.WriteToMessage(&message1,
/*begin=*/circle1.upper_bound(t0_ + 1 * Second),
/*end=*/circle1.upper_bound(t0_ + 3 * Second),
/*begin=*/circle1.upper_bound(t0_ + 4.9 * Second),
/*end=*/circle1.upper_bound(t0_ + 4.98 * Second),
/*exact=*/{});
EXPECT_LE(message1.number_of_dense_points(), message1.zfp().timeline_size());

serialization::DiscreteTrajectorySegment message2;
ForgetBefore(circle2.upper_bound(t0_ + 1 * Second)->time, circle2);
ForgetAfter(circle2.upper_bound(t0_ + 3 * Second)->time, circle2);
ForgetBefore(circle2.upper_bound(t0_ + 4.9 * Second)->time, circle2);
ForgetAfter(circle2.upper_bound(t0_ + 4.98 * Second)->time, circle2);
circle2.WriteToMessage(&message2,
/*exact=*/{});
EXPECT_LE(message2.number_of_dense_points(), message2.zfp().timeline_size());

// Writing a range of the segment is equivalent to forgetting and writing the
// result.
Expand Down