Skip to content

Commit

Permalink
feat: Add operators to trajectory state class (#78)
Browse files Browse the repository at this point in the history
* feat: Add operators and getCoordinates to State object

* chore: Added tests

* fix: add suggestions

* feat: Add operators and getCoordinates to State object

* chore: Added tests

* chore: Fix tests

* chore: Duplicates

Co-authored-by: Vishwa Shah <vishwa@loftorbital.com>
Co-authored-by: Lucas Brémond <lucas@loftorbital.com>
  • Loading branch information
3 people authored Nov 11, 2022
1 parent 3045486 commit dc2399f
Show file tree
Hide file tree
Showing 5 changed files with 754 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ inline void OpenSpaceToolkitAstrodynamicsPy_Trajectory_State

.def(self == self)
.def(self != self)
.def(self + self)
.def(self - self)

.def("__str__", &(shiftToString<State>))
.def("__repr__", &(shiftToString<State>))
Expand All @@ -43,6 +45,7 @@ inline void OpenSpaceToolkitAstrodynamicsPy_Trajectory_State
.def("get_instant", &State::getInstant)
.def("get_position", &State::getPosition)
.def("get_velocity", &State::getVelocity)
.def("get_coordinates", &State::getCoordinates)
.def("in_frame", &State::inFrame, arg("frame"))

.def_static("undefined", &State::Undefined)
Expand Down
31 changes: 16 additions & 15 deletions bindings/python/test/trajectory/test_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,16 @@

import pytest

import ostk.physics as physics
import numpy as np

import ostk.astrodynamics as astrodynamics
from ostk.physics.time import Instant
from ostk.physics.time import DateTime
from ostk.physics.time import Scale
from ostk.physics.coordinate import Position
from ostk.physics.coordinate import Velocity
from ostk.physics.coordinate import Frame

################################################################################################################################################################

Instant = physics.time.Instant
DateTime = physics.time.DateTime
Time = physics.units.Time
Scale = physics.time.Scale
Position = physics.coordinate.Position
Velocity = physics.coordinate.Velocity
Frame = physics.coordinate.Frame

State = astrodynamics.trajectory.State
from ostk.astrodynamics.trajectory import State

################################################################################################################################################################

Expand All @@ -47,7 +42,7 @@ def state (state_default_inputs) -> State:

class TestState:

def test_constructors(self, state: State):
def test_constructor (self, state: State):

assert state is not None
assert isinstance(state, State)
Expand All @@ -58,17 +53,23 @@ def test_comparators (self, state: State):
assert (state == state) is True
assert (state != state) is False

def test_operators (self, state: State):

assert isinstance(state + state, State)
assert isinstance(state - state, State)

def test_getters (self, state: State, state_default_inputs):

(instant, position, velocity, frame) = state_default_inputs

assert state.get_instant() == instant
assert state.get_position() == position
assert state.get_velocity() == velocity
assert state.get_coordinates() == np.append(position.get_coordinates(), velocity.get_coordinates())

def test_in_frame (self, state: State, state_default_inputs):

(instant, position, velocity, frame) = state_default_inputs
(_, _, _, frame) = state_default_inputs

assert state.in_frame(frame) == state
assert state.in_frame(Frame.ITRF()) != state
Expand Down
10 changes: 10 additions & 0 deletions include/OpenSpaceToolkit/Astrodynamics/Trajectory/State.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include <OpenSpaceToolkit/Physics/Coordinate/Position.hpp>
#include <OpenSpaceToolkit/Physics/Time/Instant.hpp>

#include <OpenSpaceToolkit/Mathematics/Objects/Vector.hpp>

#include <OpenSpaceToolkit/Core/Types/Shared.hpp>

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -30,6 +32,8 @@ namespace trajectory

using ostk::core::types::Shared ;

using ostk::math::obj::VectorXd ;

using ostk::physics::time::Instant ;
using ostk::physics::coord::Position ;
using ostk::physics::coord::Velocity ;
Expand All @@ -52,6 +56,10 @@ class State

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

State operator + ( const State& aState ) const ;

State operator - ( const State& aState ) const ;

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

Expand All @@ -69,6 +77,8 @@ class State

Velocity getVelocity ( ) const ;

VectorXd getCoordinates ( ) const ;

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

void print ( std::ostream& anOutputStream,
Expand Down
100 changes: 100 additions & 0 deletions src/OpenSpaceToolkit/Astrodynamics/Trajectory/State.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,90 @@ bool State::operator != (
return !((*this) == aState) ;
}

State State::operator + ( const State& aState ) const
{

if ((!this->isDefined()) || (!aState.isDefined()))
{
throw ostk::core::error::runtime::Undefined("State") ;
}

if (this->instant_ != aState.instant_)
{
throw ostk::core::error::runtime::Wrong("Instant") ;
}

if (this->accessPosition().accessFrame() != aState.accessPosition().accessFrame())
{
throw ostk::core::error::runtime::Wrong("Position Frame") ;
}

if (this->accessVelocity().accessFrame() != aState.accessVelocity().accessFrame())
{
throw ostk::core::error::runtime::Wrong("Velocity Frame") ;
}

if (this->accessPosition().getUnit() != aState.accessPosition().getUnit())
{
throw ostk::core::error::runtime::Wrong("Position Unit") ;
}

if (this->accessVelocity().getUnit() != aState.accessVelocity().getUnit())
{
throw ostk::core::error::runtime::Wrong("Velocity Unit") ;
}

return
{
this->instant_,
Position(this->getPosition().getCoordinates() + aState.getPosition().getCoordinates(), this->accessPosition().getUnit(), this->accessPosition().accessFrame()),
Velocity(this->getVelocity().getCoordinates() + aState.getVelocity().getCoordinates(), this->accessVelocity().getUnit(), this->accessVelocity().accessFrame())
} ;

}

State State::operator - ( const State& aState ) const
{

if ((!this->isDefined()) || (!aState.isDefined()))
{
throw ostk::core::error::runtime::Undefined("State") ;
}

if (this->instant_ != aState.instant_)
{
throw ostk::core::error::runtime::Wrong("Instant") ;
}

if (this->accessPosition().accessFrame() != aState.accessPosition().accessFrame())
{
throw ostk::core::error::runtime::Wrong("Position Frame") ;
}

if (this->accessVelocity().accessFrame() != aState.accessVelocity().accessFrame())
{
throw ostk::core::error::runtime::Wrong("Velocity Frame") ;
}

if (this->accessPosition().getUnit() != aState.accessPosition().getUnit())
{
throw ostk::core::error::runtime::Wrong("Position Unit") ;
}

if (this->accessVelocity().getUnit() != aState.accessVelocity().getUnit())
{
throw ostk::core::error::runtime::Wrong("Velocity Unit") ;
}

return
{
this->instant_,
Position(this->getPosition().getCoordinates() - aState.getPosition().getCoordinates(), this->accessPosition().getUnit(), this->accessPosition().accessFrame()),
Velocity(this->getVelocity().getCoordinates() - aState.getVelocity().getCoordinates(), this->accessVelocity().getUnit(), this->accessVelocity().accessFrame())
} ;

}

std::ostream& operator << ( std::ostream& anOutputStream,
const State& aState )
{
Expand Down Expand Up @@ -116,6 +200,22 @@ Velocity State::getVelocity ( )
return this->accessVelocity() ;
}

VectorXd State::getCoordinates ( ) const
{

if (!this->isDefined())
{
throw ostk::core::error::runtime::Undefined("State") ;
}

VectorXd coordinates(6);
coordinates.segment(0, 3) = this->accessPosition().accessCoordinates() ;
coordinates.segment(3, 3) = this->accessVelocity().accessCoordinates() ;

return coordinates ;

}

State State::inFrame ( const Shared<const Frame>& aFrameSPtr ) const
{

Expand Down
Loading

0 comments on commit dc2399f

Please sign in to comment.