diff --git a/include/ale/States.h b/include/ale/States.h index ffcf5b53ae93ff727ad6ea302cf294311637674f..bbe7575154bd4b9ff17107cacc8cf486309962fd 100644 --- a/include/ale/States.h +++ b/include/ale/States.h @@ -45,6 +45,9 @@ namespace ale { States(const std::vector<double>& ephemTimes, const std::vector<Vec3d>& positions, int refFrame=1); + States(const std::vector<double>& ephemTimes, const std::vector<std::vector<double>>& positions, + int refFrame=1); + States(const std::vector<double>& ephemTimes, const std::vector<Vec3d>& positions, const std::vector<Vec3d>& velocities, int refFrame=1); diff --git a/src/States.cpp b/src/States.cpp index f5b6e5ad79e30b4bca419a5c12ceda897b243b55..9a498cc4121cafb0105fc56369269145aa7da03c 100644 --- a/src/States.cpp +++ b/src/States.cpp @@ -18,7 +18,6 @@ namespace ale { int refFrame) : m_ephemTimes(ephemTimes), m_refFrame(refFrame) { // Construct State vector from position and velocity vectors - if (positions.size() != ephemTimes.size()) { throw std::invalid_argument("Length of times must match number of positions"); } @@ -28,6 +27,19 @@ namespace ale { } } + States::States(const std::vector<double>& ephemTimes, const std::vector<std::vector<double>>& positions, + int refFrame) : + m_ephemTimes(ephemTimes), m_refFrame(refFrame) { + + // Construct State vector from position and velocity vectors + if (positions.size() != ephemTimes.size()) { + throw std::invalid_argument("Length of times must match number of positions"); + } + + for (Vec3d position : positions) { + m_states.push_back(State(position)); + } + } States::States(const std::vector<double>& ephemTimes, const std::vector<Vec3d>& positions, const std::vector<Vec3d>& velocities, int refFrame) : diff --git a/tests/ctests/StatesTests.cpp b/tests/ctests/StatesTests.cpp index 59a616133a52d4c7fb65786c64877c34edd6c7a2..56e8087ff27557711a854f40b1275e107e7539f1 100644 --- a/tests/ctests/StatesTests.cpp +++ b/tests/ctests/StatesTests.cpp @@ -35,6 +35,23 @@ TEST(StatesTest, ConstructorPositionNoVelocity) { EXPECT_FALSE(noVelocityState.hasVelocity()); } +TEST(StatesTest, ConstructorPositionNoVelocityStdVector) { + std::vector<double> ephemTimes = {0.0, 1.0}; + std::vector<double> position = {4.0, 1.0, 4.0}; + std::vector<std::vector<double>> positions = {position, position}; + + States noVelocityState(ephemTimes, positions); + vector<State> states = noVelocityState.getStates(); + EXPECT_EQ(states.size(), 2); + EXPECT_NEAR(states[0].position.x, 4.0, 1e-10); + EXPECT_NEAR(states[0].position.y, 1.0, 1e-10); + EXPECT_NEAR(states[0].position.z, 4.0, 1e-10); + EXPECT_NEAR(states[1].position.x, 4.0, 1e-10); + EXPECT_NEAR(states[1].position.y, 1.0, 1e-10); + EXPECT_NEAR(states[1].position.z, 4.0, 1e-10); + EXPECT_FALSE(noVelocityState.hasVelocity()); +} + TEST(StatesTest, ConstructorPositionAndVelocity) { std::vector<double> ephemTimes = {0.0, 1.0, 2.0, 3.0}; std::vector<Vec3d> positions = {