Skip to content

Commit

Permalink
feat: add functions to extract multiple subsets at once. (#234)
Browse files Browse the repository at this point in the history
* feat: add functions to State and CoordinatesBroker to extract multiple subsets at once.

* style: missed a couple of doc-string newlines

* feat: add bindings (and fix typo bug

* Apply suggestions from code review

Co-authored-by: Vishwa Shah <vishwa2710@gmail.com>

---------

Co-authored-by: Vishwa Shah <vishwa2710@gmail.com>
  • Loading branch information
kyle-cochran and vishwa2710 authored Oct 9, 2023
1 parent 92cbe01 commit 973343a
Show file tree
Hide file tree
Showing 10 changed files with 307 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,16 @@ inline void OpenSpaceToolkitAstrodynamicsPy_Trajectory_State(pybind11::module& a
.def("get_coordinates_subsets", &State::getCoordinatesSubsets)
.def("get_frame", &State::getFrame)

.def("extract_coordinates", &State::extractCoordinates, arg("coordinates_subset"))
.def(
"extract_coordinates",
overload_cast<const Shared<const CoordinatesSubset>&>(&State::extractCoordinates, const_),
arg("coordinates_subset")
)
.def(
"extract_coordinates",
overload_cast<const Array<Shared<const CoordinatesSubset>>&>(&State::extractCoordinates, const_),
arg("coordinates_subsets")
)

.def("in_frame", &State::inFrame, arg("frame"))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,14 @@ inline void OpenSpaceToolkitAstrodynamicsPy_Trajectory_State_CoordinatesBroker(p
arg("coordinates"),
arg("coordinates_subset")
)
.def(
"extract_coordinates",
overload_cast<const VectorXd&, const Array<Shared<const CoordinatesSubset>>&>(
&CoordinatesBroker::extractCoordinates, const_
),
arg("coordinates"),
arg("coordinates_subsets")
)

;
}
21 changes: 14 additions & 7 deletions bindings/python/test/trajectory/state/test_coordinates_broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,22 @@ def test_has_subset(
):
assert coordinates_broker.has_subset(coordinates_subsets[0])

def extract_coordinates(
def test_extract_coordinates(
self,
coordinates_broker: CoordinatesBroker,
coordinates: list[float],
coordinates_subsets: list[CoordinatesSubset],
):
assert coordinates_broker.extract_coordinates(
coordinates, coordinates_subsets[0]
) == [1.0, 2.0]
assert coordinates_broker.extract_coordinates(
coordinates, coordinates_subsets[1]
) == [3.0, 4.0, 5.0]
assert (
coordinates_broker.extract_coordinates(coordinates, coordinates_subsets[0])
== [1.0, 2.0]
).all()
assert (
coordinates_broker.extract_coordinates(coordinates, coordinates_subsets[1])
== [3.0, 4.0, 5.0]
).all()

assert (
coordinates_broker.extract_coordinates(coordinates, coordinates_subsets)
== [1.0, 2.0, 3.0, 4.0, 5.0]
).all()
11 changes: 9 additions & 2 deletions bindings/python/test/trajectory/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,14 @@ def test_extract_coordinates(
):
position_coordinates = state.extract_coordinates(CartesianPosition.default())
velocity_coordinates = state.extract_coordinates(CartesianVelocity.default())
len(position_coordinates) == 3
len(velocity_coordinates) == 3

assert len(position_coordinates) == 3
assert len(velocity_coordinates) == 3
assert (position_coordinates == state.get_position().get_coordinates()).all()
assert (velocity_coordinates == state.get_velocity().get_coordinates()).all()

pv_coordinates = state.extract_coordinates(
[CartesianPosition.default(), CartesianVelocity.default()]
)
assert len(pv_coordinates) == 6
assert (pv_coordinates == state.get_coordinates()).all()
124 changes: 115 additions & 9 deletions include/OpenSpaceToolkit/Astrodynamics/Trajectory/State.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ using ostk::physics::time::Instant;
using ostk::astro::trajectory::state::CoordinatesBroker;
using ostk::astro::trajectory::state::CoordinatesSubset;

/// @brief Trajectory state
/// @brief Trajectory State

class State
{
public:
/// @brief Constructor.
/// @brief Constructor.
///
/// @param [in] anInstant An instant
/// @param [in] aCoordinates The {cartesian-position, cartesian-velocity} 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
/// @param [in] anInstant An instant
/// @param [in] aCoordinates The {cartesian-position, cartesian-velocity} 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

State(
const Instant& anInstant,
Expand All @@ -59,48 +59,154 @@ class State
const Shared<const CoordinatesBroker>& aCoordinatesBrokerSPtr
);

/// @brief Constructor.
///
/// @param [in] anInstant An instant
/// @param [in] aPosition The cartesian position at the instant in International System of Units
/// @param [in] aVelocity The cartesian velocity at the instant in International System of Units

State(const Instant& anInstant, const Position& aPosition, const Velocity& aVelocity);

/// @brief Equality operator.
///
/// @param [in] aState The State to compare to
/// @return True if the States are equal, false otherwise

bool operator==(const State& aState) const;

/// @brief Inequality operator.
///
/// @param [in] aState The State to compare to
/// @return True if the States are not equal, false otherwise

bool operator!=(const State& aState) const;

/// @brief Addition operator.
///
/// @param [in] aState The State to add to this State
/// @return The sum of the two States

State operator+(const State& aState) const;

/// @brief Subtraction operator.
///
/// @param [in] aState The State to subtract from this State
/// @return The difference between the two States

State operator-(const State& aState) const;

/// @brief Stream insertion operator.
///
/// @param [in] anOutputStream The output stream to insert into
/// @param [in] aState The State to insert
/// @return The output stream with the State inserted

friend std::ostream& operator<<(std::ostream& anOutputStream, const State& aState);

/// @brief Check if the State is defined.
///
/// @return True if the State is defined, false otherwise

bool isDefined() const;

/// @brief Accessor for the instant.
///
/// @return The instant

const Instant& accessInstant() const;

/// @brief Accessor for the reference frame.
///
/// @return The reference frame

const Shared<const Frame> accessFrame() const;

/// @brief Accessor for the coordinates.
///
/// @return The coordinates

const VectorXd& accessCoordinates() const;

/// @brief Access the coordinates broker associated with the State.
///
/// @return The coordinates broker associated to the State

const Shared<const CoordinatesBroker>& accessCoordinatesBroker() const;

/// @brief Get the size of the State.
///
/// @return The size of the State

Size getSize() const;

/// @brief Get the instant associated with the State.
///
/// @return The instant

Instant getInstant() const;

/// @brief Get the reference frame associated with the State.
///
/// @return The reference frame

Shared<const Frame> getFrame() const;

/// @brief Get the cartesian position associated with the State (if present).
///
/// @return The cartesian position

Position getPosition() const;

/// @brief Get the cartesian velocity associated with the State (if present).
///
/// @return The cartesian velocity

Velocity getVelocity() const;

/// @brief Get the coordinates of the State.
///
/// @return The coordinates

VectorXd getCoordinates() const;

/// @brief Get the coordinates subsets of the State.
///
/// @return The coordinates subsets

const Array<Shared<const CoordinatesSubset>> getCoordinatesSubsets() const;

VectorXd extractCoordinates(const Shared<const CoordinatesSubset>& aSubetSPtr) const;
/// @brief Extract the coordinates for a single subset.
///
/// @param [in] aSubsetSPtr The subset to extract the coordinates for
/// @return The coordinates for the subset

VectorXd extractCoordinates(const Shared<const CoordinatesSubset>& aSubsetSPtr) const;

/// @brief Extract the coordinates for multiple subsets.
///
/// @param [in] aCoordinatesSubsetsArray The array of subsets to extract the coordinates for
/// @return The coordinates for the subsets

VectorXd extractCoordinates(const Array<Shared<const CoordinatesSubset>>& aCoordinatesSubsetsArray) const;

/// @brief Transform the State to a different reference frame.
///
/// @param [in] aFrameSPtr The reference frame to transform to
/// @return The transformed State

State inFrame(const Shared<const Frame>& aFrameSPtr) const;

/// @brief Print the State to an output stream.
///
/// @param [in] anOutputStream The output stream to print to
/// @param [in] displayDecorator Whether or not to display the decorator

void print(std::ostream& anOutputStream, bool displayDecorator = true) const;

/// @brief Get an undefined State.
///
/// @return An undefined State

static State Undefined();

private:
Expand Down
Loading

0 comments on commit 973343a

Please sign in to comment.