OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimGeoPolyCutter.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 //
3 // License: See top level LICENSE.txt file.
4 //
5 // Author: Garrett Potts (gpotts@imagelinks.com)
6 //
7 //*************************************************************************
8 // $Id: ossimGeoPolyCutter.cpp 22303 2013-07-04 18:15:52Z dburken $
9 
11 #include <ossim/base/ossimCommon.h>
12 #include <ossim/base/ossimNotify.h>
16 #include <algorithm>
17 
18 static const char* NUMBER_POLYGONS_KW = "number_polygons";
19 
21 
23  : ossimPolyCutter(),
25  m_viewGeometry(0),
26  m_geoPolygonList(0)
27 {
29  m_geoPolygonList.push_back(ossimGeoPolygon());
30 }
31 
33 {
34 }
35 
37  const char* prefix)const
38 {
39  ossimString newPrefix = prefix;
40 
41  for(int i = 0; i < (int)thePolygonList.size();++i)
42  {
43  newPrefix = ossimString(prefix) + "geo_polygon" + ossimString::toString(i)+".";
44 
45  m_geoPolygonList[i].saveState(kwl, newPrefix.c_str());
46  }
47  kwl.add(prefix,
48  NUMBER_POLYGONS_KW,
49  static_cast<ossim_uint32>(m_geoPolygonList.size()),
50  true);
51 
52  ossimString fillType = "null_inside";
54  {
55  fillType = "null_outside";
56  }
57  kwl.add(prefix,
58  "cut_type",
59  fillType.c_str(),
60  true);
61 
62  if(m_viewGeometry.valid())
63  {
64  ossimString viewPrefix = prefix;
65  viewPrefix += "view.";
66 
67  m_viewGeometry->saveState(kwl, viewPrefix.c_str());
68  }
69 
70  return ossimImageSourceFilter::saveState(kwl, prefix);;
71 }
72 
74  const char* prefix)
75 {
76  ossimString copyPrefix(prefix);
77  ossimString polygons = ossimString("^(") + copyPrefix + "geo_polygon[0-9]+.)";
78  vector<ossimString> keys =
79  kwl.getSubstringKeyList( polygons );
80  int offset = (int)(copyPrefix+"geo_polygon").size();
81 
82  std::vector<int> numberList(keys.size());
83  for(int idx = 0; idx < (int)numberList.size();++idx)
84  {
85  ossimString numberStr(keys[idx].begin() + offset,
86  keys[idx].end());
87  numberList[idx] = numberStr.toInt();
88  }
89  std::sort(numberList.begin(), numberList.end());
90 
91  ossimString newPrefix;
92  thePolygonList.clear();
93  for(int i = 0; i < (int)numberList.size();++i)
94  {
95  m_geoPolygonList.push_back(ossimGeoPolygon());
96  newPrefix = copyPrefix+"geo_polygon"+ossimString::toString(numberList[i])+".";
97  m_geoPolygonList[i].loadState(kwl, newPrefix.c_str());
98  }
99 
100  const char* lookup = kwl.find(prefix,
101  "cut_type");
102  if(lookup)
103  {
105  ossimString test = lookup;
106  if(test == "null_outside")
107  {
109  }
110  }
111  else
112  {
114  }
115 
116  ossimString viewPrefix = prefix;
117  viewPrefix += "view.";
119  if(m_viewGeometry->loadState(kwl,
120  viewPrefix))
121  {
123  }
124  return ossimImageSourceFilter::loadState(kwl, prefix);
125 }
126 
127 
128 void ossimGeoPolyCutter::setPolygon(const vector<ossimDpt>& polygon,
129  ossim_uint32 index)
130 {
131  if(m_viewGeometry.valid())
132  {
134  invertPolygon(index);
135  }
136 }
137 
138 void ossimGeoPolyCutter::setPolygon(const vector<ossimIpt>& polygon,
139  ossim_uint32 index)
140 {
141  if(m_viewGeometry.valid())
142  {
144  invertPolygon(index);
145  }
146 }
147 
148 void ossimGeoPolyCutter::setPolygon(const vector<ossimGpt>& polygon,
149  ossim_uint32 i)
150 {
151  if(i < m_geoPolygonList.size())
152  {
153  m_geoPolygonList[i] = polygon;
154 
156  }
157 }
158 
160  ossim_uint32 i)
161 {
162  if(i < m_geoPolygonList.size())
163  {
164  m_geoPolygonList[i] = polygon.getVertexList();
165 
167  }
168 }
169 
170 
171 void ossimGeoPolyCutter::addPolygon(const vector<ossimGpt>& polygon)
172 {
173  m_geoPolygonList.push_back(polygon);
174  thePolygonList.push_back(ossimPolygon());
175 
176  if(m_viewGeometry.valid())
177  {
178  transformVertices(((int)m_geoPolygonList.size())-1);
179  }
180 }
181 
182 void ossimGeoPolyCutter::addPolygon(const vector<ossimIpt>& polygon)
183 {
184  if(m_viewGeometry.valid())
185  {
187  m_geoPolygonList.push_back(ossimGeoPolygon());
188  invertPolygon((int)thePolygonList.size()-1);
189  }
190 }
191 
192 void ossimGeoPolyCutter::addPolygon(const vector<ossimDpt>& polygon)
193 {
194  if(m_viewGeometry.valid())
195  {
197  m_geoPolygonList.push_back(ossimGeoPolygon());
198  invertPolygon((int)thePolygonList.size()-1);
199  }
200 }
201 
203 {
204  if(m_viewGeometry.valid())
205  {
207  m_geoPolygonList.push_back(ossimGeoPolygon());
208  invertPolygon((int)thePolygonList.size()-1);
209  }
210 }
211 
213 {
215  m_geoPolygonList.push_back(polygon);
216 
217  if(m_viewGeometry.valid())
218  {
219  transformVertices(((int)m_geoPolygonList.size())-1);
220  }
221 }
222 
224 {
225  if(!count)
226  {
227  m_geoPolygonList.clear();
228  }
229  else
230  {
231  m_geoPolygonList.resize(count);
232  }
234 }
235 
236 std::vector<ossimGeoPolygon>& ossimGeoPolyCutter::getGeoPolygonList()
237 {
238  return m_geoPolygonList;
239 }
240 
241 const std::vector<ossimGeoPolygon>& ossimGeoPolyCutter::getGeoPolygonList() const
242 {
243  return m_geoPolygonList;
244 }
245 
246 void ossimGeoPolyCutter::invertPolygon(int polygonNumber)
247 {
248  if(!m_viewGeometry) return;
249 
250 
251  ossimPolygon& poly = thePolygonList[polygonNumber];
252  ossimGeoPolygon& gpoly = m_geoPolygonList[polygonNumber];
253 
254  gpoly.clear();
255  gpoly.resize(poly.getVertexCount());
256  int j = 0;
257  for(j = 0; j < (int)poly.getVertexCount(); ++j)
258  {
259  m_viewGeometry->localToWorld(poly[j], gpoly[j]);
260  }
261 }
262 
264 {
265  if ( baseObject )
266  {
268 
269  ossimRefPtr<ossimImageGeometry> geom = dynamic_cast<ossimImageGeometry*>(baseObject);
270  if ( geom.valid() )
271  {
272  m_viewGeometry = geom;
273  }
274  else
275  {
276  ossimRefPtr<ossimProjection> proj = dynamic_cast<ossimProjection*>(baseObject);
277  if( proj.valid() )
278  {
279  m_viewGeometry = new ossimImageGeometry( 0, proj.get() );
280  }
281  else
282  {
284  << "ossimGeoPolyCutter::setView WARNING: Invalid class type!"
285  << std::endl;
286  }
287  }
288 
289  if ( m_viewGeometry.valid() )
290  {
291  // If view changed transform points...
292  if ( !origGeom ||
293  ( m_viewGeometry->isEqualTo( *( (ossimObject*)(origGeom.get()) ) ) == false )
294  )
295  {
297  }
298  }
299  }
300 
301  return m_viewGeometry.valid();
302 
303 } // End: ossimGeoPolyCutter::setView(ossimObject* baseObject)
304 
306 {
307  return m_viewGeometry.get();
308 }
309 
311 {
312  return m_viewGeometry.get();
313 }
314 
316 {
317  if( m_viewGeometry.valid() )
318  {
319  if( m_geoPolygonList.size() )
320  {
321  if(m_geoPolygonList.size() != thePolygonList.size())
322  {
323  thePolygonList.resize(m_geoPolygonList.size());
324  }
325  int i = 0;
326  int j = 0;
327  for(i = 0; i < (int)m_geoPolygonList.size(); ++i)
328  {
329  ossimGeoPolygon& gpoly = m_geoPolygonList[i];
330  ossimPolygon& poly = thePolygonList[i];
331  int nvert = (int)gpoly.size();
332  if((int)poly.getVertexCount() != nvert)
333  {
334  poly.resize(nvert);
335  }
336  for(j = 0; j < nvert; ++j)
337  {
338 // ossimDpt error;
339  m_viewGeometry->worldToLocal(gpoly[j], poly[j]);
340 // m_viewGeometry->getRoundTripError(ossimIpt(poly[j]), error);
341 // poly[j] = poly[j] + error;
342  poly[j] = ossimDpt(ossim::round<int>(poly[j].x),
343  ossim::round<int>(poly[j].y));
344  }
345  }
347  }
348  }
349 }
350 
352 {
353  if( m_viewGeometry.valid() )
354  {
355  ossimGeoPolygon& gpoly = m_geoPolygonList[i];
356  ossimPolygon& poly = thePolygonList[i];
357  int nvert = (int)gpoly.size();
358  if((int)poly.getVertexCount() != nvert)
359  {
360  poly.resize(nvert);
361  }
362  int j = 0;
363  for(j = 0; j < nvert; ++j)
364  {
365 // ossimDpt error;
366  m_viewGeometry->worldToLocal(gpoly[j], poly[j]);
367 // m_viewGeometry->getRoundTripError(poly[j], error);
368 // poly[j] = poly[j] + error;
369  poly[j] = ossimDpt(ossim::round<int>(poly[j].x),
370  ossim::round<int>(poly[j].y));
371  }
372 
374  }
375 }
ossim_uint32 x
ossimObject * theObject
virtual bool loadState(const ossimKeywordlist &kwl, const char *prefix=0)
virtual void setPolygon(const vector< ossimDpt > &polygon, ossim_uint32 i=0)
Represents serializable keyword/value map.
ossim_uint32 y
bool valid() const
Definition: ossimRefPtr.h:75
virtual bool loadState(const ossimKeywordlist &kwl, const char *prefix=0)
Attempts to initialize a transform and a projection given the KWL.
const char * find(const char *key) const
static ossimString toString(bool aValue)
Numeric to string methods.
std::vector< ossimGeoPolygon > m_geoPolygonList
virtual bool isEqualTo(const ossimObject &obj, ossimCompareType compareType=OSSIM_COMPARE_FULL) const
virtual void addPolygon(const vector< ossimIpt > &polygon)
ossimRefPtr< ossimImageGeometry > m_viewGeometry
virtual bool saveState(ossimKeywordlist &kwl, const char *prefix=0) const
Method to save the state of an object to a keyword list.
virtual std::vector< ossimGeoPolygon > & getGeoPolygonList()
void add(const char *prefix, const ossimKeywordlist &kwl, bool overwrite=true)
virtual void setNumberOfPolygons(ossim_uint32 count)
#define RTTI_DEF2(cls, name, b1, b2)
Definition: ossimRtti.h:493
void resize(ossim_uint32 newSize)
virtual bool setView(ossimObject *baseObject)
bool localToWorld(const ossimDpt &local_pt, ossimGpt &world_pt) const
Exposes the 3D projection from image to world coordinates.
ossim_uint32 getVertexCount() const
virtual void setPolygon(const vector< ossimDpt > &polygon, ossim_uint32 i=0)
unsigned int ossim_uint32
const std::vector< ossimGpt > & getVertexList() const
virtual bool loadState(const ossimKeywordlist &kwl, const char *prefix=0)
Method to the load (recreate) the state of an object from a keyword list.
virtual ossimObject * getView()
virtual void addPolygon(const vector< ossimIpt > &polygon)
ossim_uint32 size() const
Container class that holds both 2D transform and 3D projection information for an image Only one inst...
std::vector< ossimString > getSubstringKeyList(const ossimString &regularExpression) const
virtual bool saveState(ossimKeywordlist &kwl, const char *prefix=0) const
Method to save the state of an object to a keyword list.
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
virtual void setNumberOfPolygons(ossim_uint32 count)
bool worldToLocal(const ossimGpt &world_pt, ossimDpt &local_pt) const
Exposes the 3D world-to-local image coordinate reverse projection.
virtual bool saveState(ossimKeywordlist &kwl, const char *prefix=0) const
Saves the transform (if any) and projection (if any) states to the KWL.
void resize(ossim_uint32 newSize)
std::vector< ossimPolygon > thePolygonList
ossimPolyCutterCutType theCutType
void invertPolygon(int polygonNumber)
int toInt() const
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)