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

feat: add State constructor with Array of Subsets #238

Merged
merged 2 commits into from
Oct 11, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -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 Frame>&,
const Array<Shared<const CoordinatesSubset>>&>(),
arg("instant"),
arg("coordinates"),
arg("frame"),
arg("coordinates_subsets")
)

.def(self == self)
.def(self != self)
Expand Down
18 changes: 18 additions & 0 deletions bindings/python/test/trajectory/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 20 additions & 4 deletions include/OpenSpaceToolkit/Astrodynamics/Trajectory/State.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -59,7 +58,24 @@ class State
const Shared<const CoordinatesBroker>& 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<const Frame>& aFrameSPtr,
const Array<Shared<const CoordinatesSubset>>& 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
Expand Down
13 changes: 13 additions & 0 deletions src/OpenSpaceToolkit/Astrodynamics/Trajectory/State.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ State::State(
{
}

State::State(
const Instant& anInstant,
const VectorXd& aCoordinates,
const Shared<const Frame>& aFrameSPtr,
const Array<Shared<const CoordinatesSubset>>& aCoordinatesSubsetsArray
)
: instant_(anInstant),
coordinates_(aCoordinates),
frameSPtr_(aFrameSPtr),
coordinatesBrokerSPtr_(std::make_shared<CoordinatesBroker>(CoordinatesBroker(aCoordinatesSubsetsArray)))
{
}

State::State(const Instant& anInstant, const Position& aPosition, const Velocity& aVelocity)
: instant_(anInstant)
{
Expand Down
16 changes: 16 additions & 0 deletions test/OpenSpaceToolkit/Astrodynamics/Trajectory/State.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Shared<const CoordinatesSubset>> subsets = {
CartesianPosition::Default(), CartesianVelocity::Default()
};
EXPECT_NO_THROW(State state(instant, coordinates, Frame::GCRF(), subsets));
kyle-cochran marked this conversation as resolved.
Show resolved Hide resolved

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());
Expand Down
Loading