From d6409f465cb3f3bf948ef977b182fe3e9ff71c93 Mon Sep 17 00:00:00 2001
From: Kristin <kberry@usgs.gov>
Date: Fri, 19 Jun 2020 14:47:35 -0700
Subject: [PATCH] Adds a "convenience constructor" to States (#362)

* Update format of range conversion coefficients isd output to include a separate list for times. Also update driver, test data, and remove unneeded semicolons

* Fix documentaion error

* fix other documentation error

* Added convenience constructor for states

* Remove comments
---
 include/ale/States.h         |  3 +++
 src/States.cpp               | 14 +++++++++++++-
 tests/ctests/StatesTests.cpp | 17 +++++++++++++++++
 3 files changed, 33 insertions(+), 1 deletion(-)

diff --git a/include/ale/States.h b/include/ale/States.h
index ffcf5b5..bbe7575 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 f5b6e5a..9a498cc 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 59a6161..56e8087 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 = {
-- 
GitLab