Skip to content

Commit

Permalink
feat: add profile access_model, repr and missing bindings (#458)
Browse files Browse the repository at this point in the history
* feat: add profile access_model, repr and missing bindings

* feat: add test file for Transform

* Apply suggestions from code review

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* Update bindings/python/test/flight/test_profile.py

* Update include/OpenSpaceToolkit/Astrodynamics/Flight/Profile.hpp

* fix: tests

* Apply suggestions from code review

Co-authored-by: Antoine Paletta <98616558+apaletta3@users.noreply.github.com>

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Co-authored-by: Antoine Paletta <98616558+apaletta3@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 5, 2024
1 parent 32769f6 commit 7f4d988
Show file tree
Hide file tree
Showing 10 changed files with 337 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,18 @@ inline void OpenSpaceToolkitAstrodynamicsPy_Flight_Profile(pybind11::module& aMo
)doc"
)

.def(
"access_model",
&Profile::accessModel,
return_value_policy::reference_internal,
R"doc(
Access the profile model.
Returns:
Model: The profile model.
)doc"
)

.def(
"get_state_at",
&Profile::getStateAt,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,60 @@ inline void OpenSpaceToolkitAstrodynamicsPy_Flight_Profile_Model(pybind11::modul
)doc"
)

.def(
"is_tabulated",
+[](const Model &aModel) -> bool
{
return aModel.is<ostk::astrodynamics::flight::profile::model::Tabulated>();
},
R"doc(
Check if the model is a tabulated model.
Returns:
bool: True if the model is a tabulated model, False otherwise.
)doc"
)
.def(
"is_transform",
+[](const Model &aModel) -> bool
{
return aModel.is<ostk::astrodynamics::flight::profile::model::Transform>();
},
R"doc(
Check if the model is a transform model.
Returns:
bool: True if the model is a transform model, False otherwise.
)doc"
)

.def(
"as_tabulated",
+[](const Model &aModel) -> const ostk::astrodynamics::flight::profile::model::Tabulated &
{
return aModel.as<ostk::astrodynamics::flight::profile::model::Tabulated>();
},
R"doc(
Cast the model to a tabulated model.
Returns:
Tabulated: The tabulated model.
)doc"
)
.def(
"as_transform",
+[](const Model &aModel) -> const ostk::astrodynamics::flight::profile::model::Transform &
{
return aModel.as<ostk::astrodynamics::flight::profile::model::Transform>();
},
R"doc(
Cast the model to a transform model.
Returns:
Transform: The transform model.
)doc"
)

.def(
"calculate_state_at",
&Model::calculateStateAt,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ inline void OpenSpaceToolkitAstrodynamicsPy_Flight_Profile_Model_Tabulated(pybin

.def(
"__str__",
&(shiftToString<State>),
&(shiftToString<Tabulated>),
R"doc(
Convert the model to a string.
Expand All @@ -45,7 +45,7 @@ inline void OpenSpaceToolkitAstrodynamicsPy_Flight_Profile_Model_Tabulated(pybin

.def(
"__repr__",
&(shiftToString<State>),
&(shiftToString<Tabulated>),
R"doc(
Convert the model to a string.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ inline void OpenSpaceToolkitAstrodynamicsPy_Flight_Profile_Model_Transform(pybin
arg("frame")
)

.def("__str__", &(shiftToString<State>))
.def("__repr__", &(shiftToString<State>))
.def("__str__", &(shiftToString<Transform>))
.def("__repr__", &(shiftToString<Transform>))

.def(
"is_defined",
Expand Down
11 changes: 11 additions & 0 deletions bindings/python/test/flight/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,17 @@ def test_profile_target(self, alignment_target: Profile.Target):
assert alignment_target is not None
assert isinstance(alignment_target, Profile.Target)

def test_access_model(self, profile: Profile):
model = profile.access_model()

assert model is not None

if model.is_transform():
assert model.as_transform() is not None

if model.is_tabulated():
assert model.as_tabulated() is not None

def test_get_state_at(self, profile: Profile, instant: Instant):
state: State = profile.get_state_at(instant)

Expand Down
10 changes: 10 additions & 0 deletions include/OpenSpaceToolkit/Astrodynamics/Flight/Profile.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,16 @@ class Profile
/// @return True if profile is defined
bool isDefined() const;

/// @brief Access profile model
///
/// @code{.cpp}
/// Profile profile = { ... };
/// const Model& model = profile.accessModel();
/// @endcode
///
/// @return Const reference to the profile model
const Model& accessModel() const;

/// @brief Get state at a given instant
///
/// @code{.cpp}
Expand Down
10 changes: 10 additions & 0 deletions src/OpenSpaceToolkit/Astrodynamics/Flight/Profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ bool Profile::isDefined() const
return (this->modelUPtr_ != nullptr) && this->modelUPtr_->isDefined();
}

const Model& Profile::accessModel() const
{
if (!this->isDefined())
{
throw ostk::core::error::runtime::Undefined("Profile");
}

return *modelUPtr_;
}

State Profile::getStateAt(const Instant& anInstant) const
{
if (!this->isDefined())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ Transform* Transform::clone() const
return new Transform(*this);
}

std::ostream& operator<<(std::ostream& anOutputStream, const Transform& aTransformModel)
{
aTransformModel.print(anOutputStream, true);

return anOutputStream;
}

bool Transform::isDefined() const
{
return this->transformProvider_.isDefined() && (this->frameSPtr_ != nullptr) && this->frameSPtr_->isDefined();
Expand Down
12 changes: 12 additions & 0 deletions test/OpenSpaceToolkit/Astrodynamics/Flight/Profile.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,17 @@ TEST_F(OpenSpaceToolkit_Astrodynamics_Flight_Profile, IsDefined)
}
}

TEST_F(OpenSpaceToolkit_Astrodynamics_Flight_Profile, AccessModel)
{
{
EXPECT_TRUE(profile_.accessModel().isDefined());
}

{
EXPECT_THROW(Profile::Undefined().accessModel(), ostk::core::error::runtime::Undefined);
}
}

TEST_F(OpenSpaceToolkit_Astrodynamics_Flight_Profile, GetStateAt)
{
{
Expand Down Expand Up @@ -370,6 +381,7 @@ TEST_F(OpenSpaceToolkit_Astrodynamics_Flight_Profile, InertialPointing)
}
}

// TBI: Should move these tests to the Transform test suite and only test interface here
TEST_F(OpenSpaceToolkit_Astrodynamics_Flight_Profile, NadirPointing_VVLH)
{
// VVLH #1
Expand Down
Loading

0 comments on commit 7f4d988

Please sign in to comment.