Skip to content

Commit

Permalink
fix: prevent State addition and subtraction based on coordinates brok…
Browse files Browse the repository at this point in the history
…er 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 <pau.hebrero@gmail.com>
  • Loading branch information
phc1990 and Pau Hebrero authored Jun 11, 2024
1 parent 7406328 commit 8986e16
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions src/OpenSpaceToolkit/Astrodynamics/Trajectory/State.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down
40 changes: 40 additions & 0 deletions test/OpenSpaceToolkit/Astrodynamics/Trajectory/State.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const CoordinateBroker> brokerSPtr1 = std::make_shared<CoordinateBroker>(
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<const CoordinateBroker> brokerSPtr2 = std::make_shared<CoordinateBroker>(
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)
Expand Down Expand Up @@ -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<const CoordinateBroker> brokerSPtr1 = std::make_shared<CoordinateBroker>(
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<const CoordinateBroker> brokerSPtr2 = std::make_shared<CoordinateBroker>(
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)
Expand Down

0 comments on commit 8986e16

Please sign in to comment.