diff --git a/bindings/python/src/OpenSpaceToolkitAstrodynamicsPy/Trajectory/State.cpp b/bindings/python/src/OpenSpaceToolkitAstrodynamicsPy/Trajectory/State.cpp index 4cb07dd22..26a240824 100644 --- a/bindings/python/src/OpenSpaceToolkitAstrodynamicsPy/Trajectory/State.cpp +++ b/bindings/python/src/OpenSpaceToolkitAstrodynamicsPy/Trajectory/State.cpp @@ -30,6 +30,17 @@ inline void OpenSpaceToolkitAstrodynamicsPy_Trajectory_State(pybind11::module& a arg("frame"), arg("coordinates_broker") ) + .def( + init< + const Instant&, + const VectorXd&, + const Shared&, + const Array>&>(), + arg("instant"), + arg("coordinates"), + arg("frame"), + arg("coordinates_subsets") + ) .def(self == self) .def(self != self) diff --git a/bindings/python/test/trajectory/test_state.py b/bindings/python/test/trajectory/test_state.py index fef8a8160..753e5c18d 100644 --- a/bindings/python/test/trajectory/test_state.py +++ b/bindings/python/test/trajectory/test_state.py @@ -87,6 +87,24 @@ def test_explicit_constructor( assert isinstance(state, State) assert state.is_defined() + def test_subsets_constructor( + self, + instant: Instant, + position: Position, + velocity: Velocity, + frame: Frame, + ): + state = State( + instant, + np.append(position.get_coordinates(), velocity.get_coordinates()), + frame, + [CartesianPosition.default(), CartesianVelocity.default()], + ) + + assert state is not None + assert isinstance(state, State) + assert state.is_defined() + def test_comparators(self, state: State): assert (state == state) is True assert (state != state) is False diff --git a/include/OpenSpaceToolkit/Astrodynamics/Trajectory/State.hpp b/include/OpenSpaceToolkit/Astrodynamics/Trajectory/State.hpp index df5f47686..327ad22ca 100644 --- a/include/OpenSpaceToolkit/Astrodynamics/Trajectory/State.hpp +++ b/include/OpenSpaceToolkit/Astrodynamics/Trajectory/State.hpp @@ -43,11 +43,10 @@ using ostk::astro::trajectory::state::CoordinatesSubset; class State { public: - /// @brief Constructor. + /// @brief Constructor with a pre-defined Coordinates Broker. /// /// @param [in] anInstant An instant - /// @param [in] aCoordinates The {cartesian-position, cartesian-velocity} coordinates at the - /// instant in International System of Units + /// @param [in] aCoordinates The coordinates at the instant in International System of Units /// @param [in] aFrameSPtr The reference frame in which the coordinates are referenced to and /// resolved in /// @param [in] aCoordinatesBrokerSPtr The coordinates broker associated to the coordinates @@ -59,7 +58,24 @@ class State const Shared& aCoordinatesBrokerSPtr ); - /// @brief Constructor. + /// @brief Constructor. This constructor makes a new Coordinates Broker under the hood for every + /// State. When possible, users should prefer passing in an existing Coordinates Broker or using a StateBuilder to + /// reduce memory footprint when constructing many states. + /// + /// @param [in] anInstant An instant + /// @param [in] aCoordinates The coordinates at the instant in International System of Units + /// @param [in] aFrameSPtr The reference frame in which the coordinates are referenced to and + /// resolved in + /// @param [in] aCoordinatesSubsetsArray The coordinates subsets associated to the coordinates + + State( + const Instant& anInstant, + const VectorXd& aCoordinates, + const Shared& aFrameSPtr, + const Array>& aCoordinatesSubsetsArray + ); + + /// @brief Utility constructor for Position/Velocity only. /// /// @param [in] anInstant An instant /// @param [in] aPosition The cartesian position at the instant in International System of Units diff --git a/src/OpenSpaceToolkit/Astrodynamics/Trajectory/State.cpp b/src/OpenSpaceToolkit/Astrodynamics/Trajectory/State.cpp index effd586ea..f10656c4a 100644 --- a/src/OpenSpaceToolkit/Astrodynamics/Trajectory/State.cpp +++ b/src/OpenSpaceToolkit/Astrodynamics/Trajectory/State.cpp @@ -36,6 +36,19 @@ State::State( { } +State::State( + const Instant& anInstant, + const VectorXd& aCoordinates, + const Shared& aFrameSPtr, + const Array>& aCoordinatesSubsetsArray +) + : instant_(anInstant), + coordinates_(aCoordinates), + frameSPtr_(aFrameSPtr), + coordinatesBrokerSPtr_(std::make_shared(CoordinatesBroker(aCoordinatesSubsetsArray))) +{ +} + State::State(const Instant& anInstant, const Position& aPosition, const Velocity& aVelocity) : instant_(anInstant) { diff --git a/test/OpenSpaceToolkit/Astrodynamics/Trajectory/State.test.cpp b/test/OpenSpaceToolkit/Astrodynamics/Trajectory/State.test.cpp index 72818224e..c7721120c 100644 --- a/test/OpenSpaceToolkit/Astrodynamics/Trajectory/State.test.cpp +++ b/test/OpenSpaceToolkit/Astrodynamics/Trajectory/State.test.cpp @@ -38,6 +38,22 @@ TEST(OpenSpaceToolkit_Astrodynamics_Trajectory_State, Constructor) EXPECT_NO_THROW(State state(instant, coordinates, Frame::GCRF(), brokerSPtr)); } + { + const Instant instant = Instant::DateTime(DateTime(2018, 1, 1, 0, 0, 0), Scale::UTC); + VectorXd coordinates(6); + coordinates << 1.0, 2.0, 3.0, 4.0, 5.0, 6.0; + + const Array> subsets = { + CartesianPosition::Default(), CartesianVelocity::Default() + }; + EXPECT_NO_THROW(State state(instant, coordinates, Frame::GCRF(), subsets)); + + const State state(instant, coordinates, Frame::GCRF(), subsets); + + EXPECT_EQ(state.extractCoordinates(CartesianPosition::Default()), coordinates.segment(0, 3)); + EXPECT_EQ(state.extractCoordinates(CartesianVelocity::Default()), coordinates.segment(3, 3)); + } + { const Instant instant = Instant::DateTime(DateTime(2018, 1, 1, 0, 0, 0), Scale::UTC); const Position position = Position::Meters({1.2, 3.4, 5.6}, Frame::GCRF());