From 8986e16b7cbe7be1fa8e3050dda04fd14a2f8227 Mon Sep 17 00:00:00 2001 From: Pau Hebrero <65550121+phc1990@users.noreply.github.com> Date: Tue, 11 Jun 2024 20:27:04 +0200 Subject: [PATCH] fix: prevent State addition and subtraction based on coordinates broker not being equal (#407) * fix: prevent State addition and subtraction based on coordinates broker not being equal * test: fix State addition and subtraction operation tests --------- Co-authored-by: Pau Hebrero --- .../Trajectory/Orbit.cpp | 2 +- .../Astrodynamics/Trajectory/Orbit.hpp | 2 +- .../Astrodynamics/Trajectory/State.cpp | 8 ++-- .../Astrodynamics/Trajectory/State.test.cpp | 40 +++++++++++++++++++ 4 files changed, 46 insertions(+), 6 deletions(-) diff --git a/bindings/python/src/OpenSpaceToolkitAstrodynamicsPy/Trajectory/Orbit.cpp b/bindings/python/src/OpenSpaceToolkitAstrodynamicsPy/Trajectory/Orbit.cpp index 3746766e6..5e4c9dbe4 100644 --- a/bindings/python/src/OpenSpaceToolkitAstrodynamicsPy/Trajectory/Orbit.cpp +++ b/bindings/python/src/OpenSpaceToolkitAstrodynamicsPy/Trajectory/Orbit.cpp @@ -19,8 +19,8 @@ inline void OpenSpaceToolkitAstrodynamicsPy_Trajectory_Orbit(pybind11::module& a using ostk::core::type::Shared; using ostk::physics::environment::object::Celestial; - using ostk::physics::unit::Angle; using ostk::physics::time::Duration; + using ostk::physics::unit::Angle; using ostk::astrodynamics::trajectory::Orbit; using ostk::astrodynamics::trajectory::orbit::model::Kepler; diff --git a/include/OpenSpaceToolkit/Astrodynamics/Trajectory/Orbit.hpp b/include/OpenSpaceToolkit/Astrodynamics/Trajectory/Orbit.hpp index c6970d102..7b2c8a4d5 100644 --- a/include/OpenSpaceToolkit/Astrodynamics/Trajectory/Orbit.hpp +++ b/include/OpenSpaceToolkit/Astrodynamics/Trajectory/Orbit.hpp @@ -43,8 +43,8 @@ using ostk::core::type::Unique; using ostk::physics::coordinate::Frame; using ostk::physics::environment::object::Celestial; -using ostk::physics::time::Instant; using ostk::physics::time::Duration; +using ostk::physics::time::Instant; using ostk::physics::time::Time; using ostk::physics::unit::Angle; using ostk::physics::unit::Length; diff --git a/src/OpenSpaceToolkit/Astrodynamics/Trajectory/State.cpp b/src/OpenSpaceToolkit/Astrodynamics/Trajectory/State.cpp index 105dae88f..15af119da 100644 --- a/src/OpenSpaceToolkit/Astrodynamics/Trajectory/State.cpp +++ b/src/OpenSpaceToolkit/Astrodynamics/Trajectory/State.cpp @@ -230,9 +230,9 @@ State State::operator+(const State& aState) const throw ostk::core::error::runtime::Wrong("Frame"); } - if (this->getSize() != aState.getSize()) + if (this->coordinatesBrokerSPtr_ != aState.coordinatesBrokerSPtr_) { - throw ostk::core::error::runtime::Wrong("Size"); + throw ostk::core::error::runtime::Wrong("Coordinate Subsets"); } VectorXd addedCoordinates = VectorXd(this->coordinatesBrokerSPtr_->getNumberOfCoordinates()); @@ -275,9 +275,9 @@ State State::operator-(const State& aState) const throw ostk::core::error::runtime::Wrong("Frame"); } - if (this->getSize() != aState.getSize()) + if (this->coordinatesBrokerSPtr_ != aState.coordinatesBrokerSPtr_) { - throw ostk::core::error::runtime::Wrong("Size"); + throw ostk::core::error::runtime::Wrong("Coordinate Subsets"); } VectorXd subtractedCoordinates = VectorXd(this->coordinatesBrokerSPtr_->getNumberOfCoordinates()); diff --git a/test/OpenSpaceToolkit/Astrodynamics/Trajectory/State.test.cpp b/test/OpenSpaceToolkit/Astrodynamics/Trajectory/State.test.cpp index 6cea62e75..a1c09a42c 100644 --- a/test/OpenSpaceToolkit/Astrodynamics/Trajectory/State.test.cpp +++ b/test/OpenSpaceToolkit/Astrodynamics/Trajectory/State.test.cpp @@ -833,6 +833,26 @@ TEST(OpenSpaceToolkit_Astrodynamics_Trajectory_State, AdditionOperator) EXPECT_ANY_THROW(aState + anotherState); } + + { + const Instant instant = Instant::DateTime(DateTime(2018, 1, 1, 0, 0, 0), Scale::UTC); + + VectorXd aCoordinates(6); + aCoordinates << 0.0, 0.0, 0.0, 0.0, 0.0, 0.0; + const Shared brokerSPtr1 = std::make_shared( + CoordinateBroker({CartesianPosition::Default(), CartesianVelocity::Default()}) + ); + const State aState = State(instant, aCoordinates, Frame::GCRF(), brokerSPtr1); + + VectorXd anotherCoordinates(6); + anotherCoordinates << 0.0, 0.0, 0.0, 0.0, 0.0, 0.0; + const Shared brokerSPtr2 = std::make_shared( + CoordinateBroker({CartesianVelocity::Default(), CartesianPosition::Default()}) + ); + const State anotherState = State(instant, anotherCoordinates, Frame::GCRF(), brokerSPtr2); + + EXPECT_ANY_THROW(aState + anotherState); + } } TEST(OpenSpaceToolkit_Astrodynamics_Trajectory_State, SubtractionOperator) @@ -934,6 +954,26 @@ TEST(OpenSpaceToolkit_Astrodynamics_Trajectory_State, SubtractionOperator) EXPECT_ANY_THROW(aState - anotherState); } + + { + const Instant instant = Instant::DateTime(DateTime(2018, 1, 1, 0, 0, 0), Scale::UTC); + + VectorXd aCoordinates(6); + aCoordinates << 0.0, 0.0, 0.0, 0.0, 0.0, 0.0; + const Shared brokerSPtr1 = std::make_shared( + CoordinateBroker({CartesianPosition::Default(), CartesianVelocity::Default()}) + ); + const State aState = State(instant, aCoordinates, Frame::GCRF(), brokerSPtr1); + + VectorXd anotherCoordinates(6); + anotherCoordinates << 0.0, 0.0, 0.0, 0.0, 0.0, 0.0; + const Shared brokerSPtr2 = std::make_shared( + CoordinateBroker({CartesianVelocity::Default(), CartesianPosition::Default()}) + ); + const State anotherState = State(instant, anotherCoordinates, Frame::GCRF(), brokerSPtr2); + + EXPECT_ANY_THROW(aState - anotherState); + } } TEST(OpenSpaceToolkit_Astrodynamics_Trajectory_State, StreamOperator)