OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimGridRemapSource.cpp
Go to the documentation of this file.
1 //*****************************************************************************
2 // FILE: ossimGridRemapper.cc
3 //
4 // License: See top level LICENSE.txt file.
5 //
6 // AUTHOR: Oscar Kramer
7 //
8 // DESCRIPTION: Contains implementation of class ossimGridRemapper. This is
9 // a spacially variant remapper that utilizes a grid for interpolating the
10 // remap value given an image x, y.
11 //
12 // LIMITATIONS: None.
13 //
14 //*****************************************************************************
15 // $Id: ossimGridRemapSource.cpp 19682 2011-05-31 14:21:20Z dburken $
16 
18 
20  "ossimGridRemapSource",
22 
24 #include <ossim/base/ossimDrect.h>
25 #include <ossim/base/ossimDpt.h>
29 #include <cstdio>
30 #include <fstream>
31 
32 
33 //***
34 // Define Trace flags for use within this file:
35 //***
36 #include <ossim/base/ossimTrace.h>
37 static ossimTrace traceExec ("ossimGridRemapSource:exec");
38 static ossimTrace traceDebug ("ossimGridRemapSource:debug");
39 
40 static const char* GRID_FILENAME_KW = "grid_remap_file";
41 static const char* REMAP_ENGINE_KW = "remap_engine";
42 
43 //*****************************************************************************
44 // DEFAULT CONSTRUCTOR #1: ossimGridRemapper()
45 //
46 //*****************************************************************************
48  :
50  theGridFilename (0),
51  theRemapEngine (0),
52  theRemapIsLockedFlag(true),
53  theGridIsFilled(false)
54 {
55  disableSource();
56 }
57 
58 //*****************************************************************************
59 // CONSTRUCTOR #2: ossimGridRemapper(inputSource)
60 //
61 //*****************************************************************************
63  ossimGridRemapEngine* engine)
64  :
65  ossimImageSourceFilter(inputSource),
66  theGridFilename (0),
67  theRemapEngine ((ossimGridRemapEngine*)engine->dup()),
68  theRemapIsLockedFlag(true),
69  theGridIsFilled(false)
70 {
71  disableSource();
72 }
73 
74 //*****************************************************************************
75 // DESTRUCTOR: ~ossimGridRemapSource
76 //
77 //*****************************************************************************
79 {
81  theRemapEngine = 0;
82 }
83 
84 //*****************************************************************************
85 // METHOD: ossimGridRemapSource::initialize
86 //
87 //*****************************************************************************
89  const ossimDpt& grid_spacing)
90 {
92  int num_params;
93 
94  if (theRemapEngine)
95  {
96  num_params = theRemapEngine->getNumberOfParams();
97 
98  for (int p=0; p<num_params; p++)
99  {
100  ossimDblGrid* grid = new ossimDblGrid(uv_rect, grid_spacing);
101  theGrids.push_back(grid);
102  }
103 
104  theGridFilename = "ogrs";
105  theGridFilename += ossimString::toString(reinterpret_cast<ossim_int64>(this));
106  theGridFilename += ".org";
107 
108  theGridIsFilled = false;
109  }
110 }
111 
112 //*****************************************************************************
113 // METHOD: ossimGridRemapSource::getTile()
114 //
115 // Implementation of virtual method to return remapped tile.
116 //
117 //*****************************************************************************
119  const ossimIrect& rect, ossim_uint32 resLevel)
120 {
121  if(!theInputConnection)
122  return NULL;
123 
124  //***
125  // Fetch tile from input source:
126  //***
128  resLevel);
129 
130  //---
131  // Bypass this filter if it is not initialized, or if input tile is bogus:
132  //---
133  if ((!isSourceEnabled()) || (!tile.valid()) || (!theRemapEngine) ||
134  (tile->getDataObjectStatus()==OSSIM_NULL) ||
135  (tile->getDataObjectStatus()==OSSIM_EMPTY))
136  {
137  return tile;
138  }
139 
140  //***
141  // Insure that the grid has been filled in case nodes were being randomly set
142  //***
143  if (!theGridIsFilled)
144  {
145  vector<ossimDblGrid*>::iterator grid = theGrids.begin();
146  while (grid != theGrids.end())
147  {
148 // (*grid)->setInterpolationType(ossimDblGrid::BILINEAR);
149  (*grid)->interpolateNullValuedNodes();
150  grid++;
151  }
152  theGridIsFilled = true;
153  }
154 
155  //***
156  // hand off the actual remap to the ATB engine. This object knows how to
157  // interpret the grids for the particular remap algorithm selected:
158  //***
159  theRemapEngine->remapTile(rect.ul(), this, tile);
160 
161  tile->validate();
162  return tile;
163 }
164 
165 //*****************************************************************************
166 // METHOD: ossimGridRemapSource::loadState()
167 //
168 //*****************************************************************************
170  const char* prefix)
171 {
172  static const char MODULE[] = "ossimGridRemapSource::loadState()";
173  if (traceExec()) CLOG << "entering..." << endl;
174 
175  bool successful = false;
176  const char* value;
177 
178  //***
179  // Reset object in preparation for reassignment:
180  //***
181  deallocateGrids();
182  theRemapEngine = 0;
183 
184  //***
185  // Read the remap engine type:
186  //***
187  value = kwl.find(prefix, REMAP_ENGINE_KW);
189  if (!theRemapEngine)
190  {
191 // CLOG << "ERROR: could not instantiate remap engine. Aborting..." << endl;
192 // if (traceExec()) CLOG << "returning..." << endl;
193 // return false;
194  }
195 
196  //***
197  // Read the grid filename and open input stream:
198  //***
199  theGridIsFilled = false;
200  value = kwl.find(prefix, GRID_FILENAME_KW);
201  if (value)
202  {
203  theGridFilename = value;
205 
206  //***
207  // Create an input stream from the grid file to pass to the
208  // corresponding grid:
209  //***
210  int num_grids = theRemapEngine->getNumberOfParams();
211  for (int i=0; (i<num_grids) && successful; i++)
212  {
213  ossimDblGrid* grid = new ossimDblGrid;
214  successful = grid->load(is);
215  if (successful)
216  theGrids.push_back(grid);
217  }
218  if (!successful)
219  {
220  CLOG << "ERROR: Encountered errorloading remap grids at file: "
221  << theGridFilename << ". Remapper disabled." << endl;
222  if (traceExec()) CLOG << "returning..." << endl;
223  return false;
224  }
225  theGridIsFilled = true;
226  }
227 
228  //***
229  // Allow base class to parse list:
230  //***
231  bool rtn_stat = ossimImageSourceFilter::loadState(kwl, prefix);
232 
233  //***
234  // Enable source only if KWL read was successful:
235  //***
236  if (theRemapEngine && successful && rtn_stat)
237  enableSource();
238 
239  if (traceExec()) CLOG << "returning..." << endl;
240  return rtn_stat;
241 }
242 
243 //*****************************************************************************
244 // METHOD: ossimGridRemapSource::saveState()
245 //
246 //*****************************************************************************
248  const char* prefix) const
249 {
250  static const char MODULE[] = "ossimGridRemapSource::saveState()";
251 
252  //***
253  // No filename indicates that this remapper has not been initialized:
254  //***
255  if (!theGridFilename)
256  return false;
257 
258  //***
259  // Write the remap engine type:
260  //***
261  if (theRemapEngine)
262  kwl.add(prefix, REMAP_ENGINE_KW, theRemapEngine->getClassName());
263 
264  //***
265  // Write the filename to the KWL:
266  //***
267  if (!theGridFilename.empty())
268  {
269  kwl.add(prefix, GRID_FILENAME_KW, theGridFilename);
270 
271  //***
272  // Loop over each remap component to write out the grid:
273  //***
275  bool successful = true;
276  int num_components = theRemapEngine->getNumberOfParams();
277  for (int p=0; (p<num_components)&&successful; p++)
278  successful = theGrids[p]->save(os, "Remap-Grid");
279 
280  if (!successful)
281  {
282  CLOG << "ERROR: Encountered saving remap grids to file: "
283  << theGridFilename << ". State not properly saved."
284  << endl;
285  return false;
286  }
287  }
288 
289  return ossimImageSourceFilter::saveState(kwl, prefix);
290 }
291 
292 //*****************************************************************************
293 // METHOD: ossimGridRemapSource::setGridNode()
294 //
295 // Sets a node of the member grid to the value specified.
296 //
297 //*****************************************************************************
299  const double* value)
300 {
301  int numGrids = (int)theGrids.size();
302  for (int i=0; i<numGrids; i++)
303  theGrids[i]->setNearestNode(view_pt, value[i]);
304 
305  theGridIsFilled = false;
306 
307  return;
308 }
309 
310 //*****************************************************************************
311 // METHOD: ossimGridRemapSource::getGrid()
312 //
313 //*****************************************************************************
315 {
316  if (index >= theGrids.size())
317  return 0;
318 
319  return theGrids[index];
320 }
321 
322 //*****************************************************************************
323 // METHOD: ossimGridRemapSource::deallocateGrids()
324 //
325 //*****************************************************************************
327 {
328  static const char MODULE[] = "ossimGridRemapSource::deallocateMemory()";
329  if (traceExec()) CLOG << "entering..." << endl;
330 
331  vector<ossimDblGrid*>::iterator grid = theGrids.begin();
332  while (grid != theGrids.end())
333  {
334  delete *grid;
335  grid++;
336  }
337  theGrids.clear();
338 
339  if (traceExec()) CLOG << "returning..." << endl;
340  return;
341 }
342 
343 
344 //*****************************************************************************
345 // METHOD: ossimGridRemapSource::setRemapEngine()
346 //
347 //*****************************************************************************
349 {
350 
351  theRemapEngine = engine;
352  theGridIsFilled = false;
353 }
354 
355 //*****************************************************************************
356 // METHOD: ossimGridRemapSource::setGridFilename()
357 //
358 //*****************************************************************************
360 {
361  theGridFilename = grid_filename;
362  //***
363  // NOT COMPLETE###
364  //***
365 }
static ossimGridRemapEngine * create(const char *spec)
void setRemapEngine(ossimGridRemapEngine *engine)
virtual bool isSourceEnabled() const
Definition: ossimSource.cpp:79
#define CLOG
Definition: ossimTrace.h:23
virtual ossimRefPtr< ossimImageData > getTile(const ossimIrect &origin, ossim_uint32 resLevel=0)
Represents serializable keyword/value map.
std::basic_ifstream< char > ifstream
Class for char input file streams.
Definition: ossimIosFwd.h:44
bool valid() const
Definition: ossimRefPtr.h:75
const char * find(const char *key) const
static ossimString toString(bool aValue)
Numeric to string methods.
virtual void disableSource()
Definition: ossimSource.cpp:89
const ossimIpt & ul() const
Definition: ossimIrect.h:274
virtual ossimDataObjectStatus getDataObjectStatus() const
virtual ossimString getClassName() const
Definition: ossimObject.cpp:64
void add(const char *prefix, const ossimKeywordlist &kwl, bool overwrite=true)
virtual void remapTile(const ossimDpt &origin_point, ossimGridRemapSource *remapper, ossimRefPtr< ossimImageData > &tile)=0
void setGridFilename(const ossimFilename &grid_filename)
bool load(std::istream &is)
virtual ossimDataObjectStatus validate() const
ossimImageSource * theInputConnection
unsigned int ossim_uint32
const char * chars() const
For backward compatibility.
Definition: ossimString.h:77
void setGridNode(const ossimDpt &view_pt, const double *value)
vector< ossimDblGrid * > theGrids
virtual bool loadState(const ossimKeywordlist &kwl, const char *prefix=0)
Method to the load (recreate) the state of an object from a keyword list.
ossimDblGrid * getGrid(unsigned int index)
virtual void enableSource()
Definition: ossimSource.cpp:84
ossimGridRemapEngine * theRemapEngine
virtual bool saveState(ossimKeywordlist &kwl, const char *prefix=0) const
virtual bool saveState(ossimKeywordlist &kwl, const char *prefix=0) const
Method to save the state of an object to a keyword list.
virtual bool loadState(const ossimKeywordlist &kwl, const char *prefix=0)
RTTI_DEF1(ossimGridRemapSource, "ossimGridRemapSource", ossimImageSourceFilter)
const char * c_str() const
Returns a pointer to a null-terminated array of characters representing the string&#39;s contents...
Definition: ossimString.h:396
bool empty() const
Definition: ossimString.h:411
std::basic_ofstream< char > ofstream
Class for char output file streams.
Definition: ossimIosFwd.h:47
virtual ossimRefPtr< ossimImageData > getTile(const ossimIpt &origin, ossim_uint32 resLevel=0)