OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
PlatformPosition.cpp
Go to the documentation of this file.
1 //----------------------------------------------------------------------------
2 //
3 // "Copyright Centre National d'Etudes Spatiales"
4 //
5 // License: LGPL
6 //
7 // See LICENSE.txt file in the top level directory for more details.
8 //
9 //----------------------------------------------------------------------------
10 // $Id$
11 
12 #include <iostream>
13 #include <string>
14 #include <cmath>
15 #include <iomanip>
16 #include <vector>
17 
18 #include <otb/PlatformPosition.h>
19 #include <otb/Ephemeris.h>
22 
23 namespace ossimplugins
24 {
25 
26 static const char NUMBER_PLATFORM_POSITIONS_KW[] = "platform_positions_count";
27 
29  _nbrData(0),
30  _data(NULL),
31  _t(NULL),
32  _p(NULL),
33  _dp(NULL),
34  _interpolator(NULL)
35 {
36 }
37 
39 {
40  Clear();
41  }
42 
44 {
45  if(_data != NULL)
46  {
47  for (int i=0;i<_nbrData;i++)
48  {
49  delete _data[i];
50  }
51  delete [] _data;
52  }
53  _data = NULL;
54  _nbrData = 0;
55 
56  delete[] _t;
57  if ((_p != NULL) && (_dp != NULL)) {
58  for (int j=0; j<3; ++j) {
59  delete[] _p[j];
60  delete[] _dp[j];
61  delete _interpolator[j];
62  }
63  }
64  delete[] _p;
65  delete[] _dp;
66 
67  delete[] _interpolator;
68  }
69 
71 {
72  InitData(rhs._data, rhs._nbrData);
73 }
74 
76 {
77  Clear();
78  InitData(rhs._data, rhs._nbrData);
79  return *this;
80  }
81 
83 {
84  InitData(data, nbrData);
85  }
86 
87 void PlatformPosition::InitData(Ephemeris** data, int nbrData)
88 {
89  _nbrData = nbrData;
90  _data = new Ephemeris*[_nbrData];
91  for (int i=0; i<_nbrData; i++)
92  {
93  _data[i] = data[i]->Clone();
94  }
96  }
97 
99 {
100  const double JOURCIVIL_LENGTH = 86400.0;
101  _t = new double[_nbrData];
102  _p = new double*[3];
103  _dp = new double*[3];
105  for (int j=0; j<3; ++j) {
106  _p[j] = new double[_nbrData];
107  _dp[j] = new double[_nbrData];
108  }
109 
110  _t[0] = 0.0;
111  for (int i = 1; i < _nbrData; i++)
112  {
114  * JOURCIVIL_LENGTH
115  + _data[i]->get_date().get_second() - _data[0]->get_date().get_second()
116  + _data[i]->get_date().get_decimal() - _data[0]->get_date().get_decimal();
117  }
118 
119  for (int j = 0; j < 3; j++)
120  {
121  for (int i = 0; i < _nbrData; i++)
122  {
123  _p[j][i] = _data[i]->get_position()[j];
124  _dp[j][i] = _data[i]->get_speed()[j];
125  }
126  _interpolator[j] = new HermiteInterpolator(_nbrData, _t, _p[j], _dp[j]);
127  }
128 
129  }
130 
132 {
133  const double JOURCIVIL_LENGTH = 86400.0;
134  Ephemeris* ephem = NULL;
135  if (_nbrData <= 1)
136  {
137  return NULL;
138  }
139  /*
140  * The first element of the list is cloned to ensure that the
141  * output ephemeris is expressed in the same coordinate system as
142  * input ones
143  */
144  ephem = _data[0]->Clone();
145 
146  /* NORMAL CASE */
147  /*------------*/
148  double dt = 0.0;
149 
150  if (ephem != NULL)
151  {
152  ephem->set_date(date);
153 
154  dt = (date.get_day0hTU().get_julianDate()
156  * JOURCIVIL_LENGTH
157  + date.get_second() - _data[0]->get_date().get_second()
158  + date.get_decimal() - _data[0]->get_date().get_decimal();
159 
160  /* Computation by Everett */
161  /*---------------------*/
162  double pos[3];
163  double speed[3];
164  for (int j = 0; j < 3; j++)
165  {
166  _interpolator[j]->Interpolate(dt, pos[j], speed[j]);
167  }
168  ephem->set_position(pos);
169  ephem->set_speed(speed);
170  }
171  return ephem;
172  }
173 
174 bool PlatformPosition::getPlatformPositionAtTime(JSDDateTime time, std::vector<double>& position, std::vector<double>& speed)
175 {
176  Ephemeris* ephemeris = this->Interpolate(time);
177  double* position_ptr = ephemeris->get_position();
178  double* speed_ptr = ephemeris->get_speed();
179  if (position.size() != 3) position.resize(3);
180  if (speed.size() != 3) speed.resize(3);
181  position[0] = position_ptr[0];
182  position[1] = position_ptr[1];
183  position[2] = position_ptr[2];
184  speed[0] = speed_ptr[0];
185  speed[1] = speed_ptr[1];
186  speed[2] = speed_ptr[2];
187  return true;
188 }
189 
190 
191 void PlatformPosition::setData(Ephemeris** data, int nbrData)
192 {
193  Clear();
194  InitData(data, nbrData);
195 }
196 
198 {
199  if(noData >=0 && noData < _nbrData)
200  {
201  return _data[noData];
202  }
203  return NULL;
204  }
205 
207 {
208  return _nbrData;
209  }
210 
211 
213  const char* prefix) const
214 {
215  kwl.add(prefix, NUMBER_PLATFORM_POSITIONS_KW, _nbrData);
216 
217  std::string s1;
218  if (prefix)
219  {
220  s1 = prefix;
221  }
222 
223  for (int i = 0; i < _nbrData; ++i)
224  {
225  std::string s2 = s1;
226  s2 += "platform_position[";
227  s2 += ossimString::toString(i).chars();
228  s2+= "]";
229  _data[i]->saveState(kwl, s2.c_str());
230  }
231 
232  return true;
233  }
234 
235 
237  const char* prefix)
238 {
239  bool result = true;
240 
241  Clear();
242 
243  const char* lookup = 0;
244  lookup = kwl.find(prefix, NUMBER_PLATFORM_POSITIONS_KW);
245  if (!lookup)
246  {
247  return false;
248  }
249  ossimString s = lookup;
250  _nbrData = s.toInt();
251 
252  if (_nbrData)
253  {
254  std::string s1;
255  if (prefix)
256  {
257  s1 = prefix;
258  }
259 
260  _data = new Ephemeris*[_nbrData];
261  for (int i = 0; i < _nbrData; ++i)
262  {
263  std::string s2 = s1;
264  s2 += "platform_position[";
265  s2 += ossimString::toString(i).chars();
266  s2+= "]";
267 
268  _data[i] = new Ephemeris();
269  _data[i]->loadState(kwl, s2.c_str());
270  }
271  }
273  return result;
274  }
275 }
bool loadState(const ossimKeywordlist &kwl, const char *prefix=0)
Method to the load (recreate) the state of the object from a keyword list.
int _nbrData
Number of platform positions.
void set_position(double position[3])
Definition: Ephemeris.h:93
bool saveState(ossimKeywordlist &kwl, const char *prefix=0) const
Method to save object state to a keyword list.
Definition: Ephemeris.cpp:78
double get_julianDate() const
Definition: JulianDate.h:70
This class handles the platform position.
Represents serializable keyword/value map.
const char * find(const char *key) const
static ossimString toString(bool aValue)
Numeric to string methods.
void set_speed(double speed[3])
Definition: Ephemeris.h:100
void set_date(JSDDateTime date)
Definition: Ephemeris.h:88
double * get_speed()
Definition: Ephemeris.h:78
Ephemeris * Interpolate(JSDDateTime date) const
This function interpolates its ephemeris to create a new ephemeris at the given date and time...
Ephemeris * getData(int noData) const
void add(const char *prefix, const ossimKeywordlist &kwl, bool overwrite=true)
This class represents an ephemeris.
Definition: Ephemeris.h:28
Ephemeris ** _data
Platform positions.
bool loadState(const ossimKeywordlist &kwl, const char *prefix=0)
Method to the load (recreate) the state of the object from a keyword list.
Definition: Ephemeris.cpp:100
JulianDate get_day0hTU() const
Definition: JSDDateTime.h:71
const char * chars() const
For backward compatibility.
Definition: ossimString.h:77
double * get_position()
Definition: Ephemeris.h:68
double get_second() const
Definition: JSDDateTime.h:76
virtual Ephemeris * Clone()
This function creatse a copy of the current class.
Definition: Ephemeris.h:58
void Clear()
This function deletes all the contents of the class.
JSDDateTime get_date() const
Definition: Ephemeris.h:63
void setData(Ephemeris **data, int nbrData)
double get_decimal() const
Definition: JSDDateTime.h:81
bool saveState(ossimKeywordlist &kwl, const char *prefix=0) const
Method to save object state to a keyword list.
int Interpolate(double x, double &y, double &dy) const
This function performs the interpolation for the abscissa x.
HermiteInterpolator ** _interpolator
bool getPlatformPositionAtTime(JSDDateTime time, std::vector< double > &position, std::vector< double > &speed)
This function interpolates its ephemeris to create and extract platform&#39;s position and speed...
This class represents a date.
Definition: JSDDateTime.h:30
void InitData(Ephemeris **data, int nbrData)
Internal method to initialize data structures.
int toInt() const
PlatformPosition & operator=(const PlatformPosition &rhs)
Affectation operator.