OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimLagrangeInterpolator.cpp
Go to the documentation of this file.
1 //**************************************************************************************************
2 // OSSIM -- Open Source Software Image Map
3 //
4 // LICENSE: See top level LICENSE.txt file.
5 //
6 // AUTHOR: Oscar Kramer, GeoEye Inc.
7 //
8 // DESCRIPTION: Contains Lagrange Interpolator class
9 //
10 //**************************************************************************************************
11 // $Id$
12 #include <iomanip>
13 #include <string>
14 using namespace std;
15 
17 
18 // Define Trace flags for use within this file:
19 #include "ossim/base/ossimTrace.h"
20 static ossimTrace traceDebug ("ossimLagrangeInterpolator:debug");
21 
22 static const char HEADER_LABEL[] = "LAGRANGE_INTERP";
23 
24 
25 //**************************************************************************************************
26 // CONSTRUCTOR: ossimLagrangeInterpolator()
27 //**************************************************************************************************
29 {
30  static const char MODULE[] = "ossimLagrangeInterpolator(istream&) CONSTRUCTOR";
31  if (traceDebug()) CLOG << "entering..." << endl;
32 
33  stream >> *this;
34 
35  if (traceDebug()) CLOG << "returning..." << endl;
36 }
37 
38 
39 //**************************************************************************************************
40 // CONSTRUCTOR: ossimLagrangeInterpolator()
41 //**************************************************************************************************
42 ossimLagrangeInterpolator::ossimLagrangeInterpolator(const std::vector<double>& t_array,
43  const std::vector<NEWMAT::ColumnVector>& data_array)
44 : theTeeArray (t_array),
45  theDataArray(data_array),
46  theNumElements(0)
47 {
48  static const char MODULE[]="ossimLagrangeInterpolator(int, double, Vector) CONSTRUCTOR";
49  if (traceDebug()) CLOG << "entering..." << endl;
50 
51  // Assign data members:
52  ossim_uint32 numPoints = (ossim_uint32) theTeeArray.size();
53  if (theDataArray.size() > 0)
54  theNumElements = theDataArray[0].Nrows();
55 
56  ossim_uint32 i;
57  double n;
58  for (i=0; i<numPoints; i++)
59  {
60  // Compute the normalizer value at this data point:
61  n = 1.0;
62  for (ossim_uint32 j=0; j<numPoints; j++)
63  {
64  if (i != j)
65  n *= (theTeeArray[i] - theTeeArray[j]);
66  }
67  theNormalizer.push_back(n);
68  }
69 
70  if (traceDebug()) CLOG << "returning..." << endl;
71 }
72 
73 
74 //**************************************************************************************************
75 // DESTRUCTOR: ossimLagrangeInterpolator
76 //**************************************************************************************************
78 {
79 }
80 
81 //**************************************************************************************************
82 // Adds one data sample to the collection.
83 //**************************************************************************************************
84 void ossimLagrangeInterpolator::addData(const double& t, const NEWMAT::ColumnVector& data)
85 {
86  theTeeArray.push_back(t);
87  theDataArray.push_back(data);
88 }
89 
90 //**************************************************************************************************
91 // PUBLIC METHOD: ossimLagrangeInterpolator::interpolate(double t)
92 //**************************************************************************************************
93 bool ossimLagrangeInterpolator::interpolate(const double& t, NEWMAT::ColumnVector& result) const
94 {
95  static const char MODULE[] = "ossimLagrangeInterpolator::interpolate()";
96  if (traceDebug()) CLOG << "entering..." << endl;
97 
98  // Prepare to sum:
99  double weight;
100 
101  // Perform interpolation:
102  ossim_uint32 numPoints = (ossim_uint32) theTeeArray.size();
103  for (ossim_uint32 i=0; i<numPoints; i++)
104  {
105  weight = 1.0/theNormalizer[i];
106  for (ossim_uint32 j=0; j<numPoints; j++)
107  {
108  if (i != j)
109  weight *= (t - theTeeArray[j]);
110  }
111 
112  result += theDataArray[i] * weight;
113  }
114 
115  if (traceDebug()) CLOG << "leaving." << endl;
116  return true;
117 }
118 
119 
120 //*****************************************************************************
121 // Outputs contents to output stream. Compatible with input operator >> for
122 // restoring state of interpolator from file:
123 //*****************************************************************************
125 {
126  stream << "\n" << HEADER_LABEL
127  << " " << interpolator.theTeeArray.size()
128  << " " << interpolator.theNumElements << endl;
129 
130  for (ossim_uint32 i=0; i<interpolator.theTeeArray.size(); ++i)
131  {
132  stream << setprecision(20);
133  stream << interpolator.theTeeArray[i] << " "
134  << interpolator.theNormalizer[i] << endl
135  << "( "<<interpolator.theDataArray[i](0)
136  << interpolator.theDataArray[i](1)
137  << interpolator.theDataArray[2](1) << " )" << endl;
138  }
139 
140  return stream;
141 }
142 
143 
144 //*****************************************************************************
145 // Inputs object from input stream. Compatible with output operator << for
146 // restoring state of interpolator from file. Assumes 3D data vector.
147 //*****************************************************************************
149 {
150  // Start with ensuring we're at the right spot. Let it go a max of ten
151  // reads. If we go beyond that there's a problem...
152  string s;
153  int count = 0;
154  stream >> s;
155  while (s != "LAGRANGE_INTERP")
156  {
157  stream >> s;
158  ++count;
159  if (count == 10) break; // Avoid infinite loop...
160  }
161 
162  if (count == 10)
163  {
164  cerr << "ossimLagrangeInterpolator operator>> ERROR:"
165  << "\nStream seems to not contain LAGRANGE_INTERP header!"
166  << "\nReturning..." << endl;
167  return stream;
168  }
169 
170  ossim_uint32 numPoints;
171  stream >> numPoints >> interpolator.theNumElements;
172 
173  interpolator.theTeeArray.clear();
174  interpolator.theDataArray.clear();
175  interpolator.theNormalizer.clear();
176 
177  double tee, normalizer;
178  NEWMAT::ColumnVector v(3);
179  for (ossim_uint32 i=0; i<numPoints; i++)
180  {
181  stream >> tee >> v(0) >> v(1) >> v(2) >> normalizer;
182  if (!stream.fail())
183  {
184  interpolator.theTeeArray.push_back(tee);
185  interpolator.theDataArray.push_back(v);
186  interpolator.theNormalizer.push_back(normalizer);
187  }
188  }
189 
190  return stream;
191 }
bool interpolate(const double &t, NEWMAT::ColumnVector &result) const
#define CLOG
Definition: ossimTrace.h:23
void addData(const double &t, const NEWMAT::ColumnVector &data)
os2<< "> n<< " > nendobj n
unsigned int ossim_uint32
std::basic_istream< char > istream
Base class for char input streams.
Definition: ossimIosFwd.h:20
std::vector< double > theNormalizer
istream & operator>>(istream &stream, ossimLagrangeInterpolator &interpolator)
ostream & operator<<(ostream &stream, const ossimLagrangeInterpolator &interpolator)
std::vector< NEWMAT::ColumnVector > theDataArray
std::basic_ostream< char > ostream
Base class for char output streams.
Definition: ossimIosFwd.h:23