-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[py trajectories] Adjust Clone and MakeDerivative for unique_ptr
Because we allow implementations of Trajectory as Python subclasses, we cannot assume that the deleter associated with a call to Clone is `delete MyClass`, so we now adjust our PyTrajectory override logic to wrap its return value in a WrappedTrajectory. The only reason the unique_ptr used to work is Drake's custom fork or pybind11 with evil hacks, which will be going away soon. We also now warn Python subclasses to implement the canonical spelling of the __deepcopy__ method, instead of overriding the public Clone method. (Overriding Clone was already documented as deprecated in a prior commit; this just adds the warning.)
- Loading branch information
1 parent
d9512ca
commit fe6a6f5
Showing
7 changed files
with
327 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include "drake/common/trajectories/wrapped_trajectory.h" | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include "drake/common/test_utilities/expect_throws_message.h" | ||
#include "drake/common/trajectories/function_handle_trajectory.h" | ||
|
||
namespace drake { | ||
namespace trajectories { | ||
namespace internal { | ||
namespace { | ||
|
||
Eigen::Vector2d Circle(const double& t) { | ||
return Eigen::Vector2d(std::sin(t), std::cos(t)); | ||
} | ||
|
||
Eigen::Vector2d CircleDerivative(const double& t, int order) { | ||
DRAKE_DEMAND(order == 2); | ||
return -Circle(t); | ||
} | ||
|
||
std::shared_ptr<const Trajectory<double>> MakeFunctionHandleTrajectory() { | ||
const double start_time = 0; | ||
const double end_time = 1; | ||
auto result = std::make_shared<FunctionHandleTrajectory<double>>( | ||
&Circle, 2, 1, start_time, end_time); | ||
result->set_derivative(&CircleDerivative); | ||
return result; | ||
} | ||
|
||
GTEST_TEST(WrappedTrajectoryTest, BasicTest) { | ||
const WrappedTrajectory<double> dut(MakeFunctionHandleTrajectory()); | ||
EXPECT_EQ(dut.rows(), 2); | ||
EXPECT_EQ(dut.cols(), 1); | ||
EXPECT_EQ(dut.start_time(), 0); | ||
EXPECT_EQ(dut.end_time(), 1); | ||
EXPECT_TRUE(dut.has_derivative()); | ||
const double t = 0.25; | ||
EXPECT_EQ(dut.value(t), Circle(t)); | ||
EXPECT_EQ(dut.EvalDerivative(t, 2), -Circle(t)); | ||
EXPECT_EQ(dut.MakeDerivative(2)->value(t), -Circle(t)); | ||
|
||
auto clone = dut.Clone(); | ||
EXPECT_EQ(clone->rows(), 2); | ||
EXPECT_EQ(clone->cols(), 1); | ||
EXPECT_EQ(clone->start_time(), 0); | ||
EXPECT_EQ(clone->end_time(), 1); | ||
EXPECT_TRUE(clone->has_derivative()); | ||
EXPECT_EQ(clone->value(t), Circle(t)); | ||
EXPECT_EQ(clone->EvalDerivative(t, 2), -Circle(t)); | ||
EXPECT_EQ(clone->MakeDerivative(2)->value(t), -Circle(t)); | ||
} | ||
|
||
} // namespace | ||
} // namespace internal | ||
} // namespace trajectories | ||
} // namespace drake |
Oops, something went wrong.