OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimAnnotationMultiPolyObject.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)
6 //
7 //*************************************************************************
8 // $Id: ossimAnnotationMultiPolyObject.cpp 10867 2007-05-09 19:58:25Z gpotts $
12 #include <ossim/base/ossimIrect.h>
13 #include <ossim/base/ossimDrect.h>
14 #include <ossim/base/ossimLine.h>
15 
16 RTTI_DEF1(ossimAnnotationMultiPolyObject, "ossimAnnotationMultiPolyObject", ossimAnnotationObject)
17 
20  theFillEnabled(false)
21 {
22  computeBoundingRect();
23 }
24 
26  bool enableFill,
27  unsigned char r,
28  unsigned char g,
29  unsigned char b,
30  long thickness)
31  :ossimAnnotationObject(r, g, b, thickness),
32  theFillEnabled(enableFill)
33 {
34  theMultiPolygon = multiPoly;
36 }
37 
39 {
40 }
41 
43  double y)
44 {
45  for(ossim_uint32 i =0; i<theMultiPolygon.size(); ++i)
46  {
47  theMultiPolygon[i] *= ossimDpt(x, y);
48  }
49 
51 }
52 
54 {
55  // do the quick checks first
56  //
57  if(rect.hasNans()) return false;
58  if(!rect.intersects(theBoundingRect)) return false;
59  if(theMultiPolygon.size()<1) return false;
60 
61  for(ossim_uint32 i =0; i < theMultiPolygon.size(); ++i)
62  {
63  vector<ossimPolygon> result;
64 
65  if(theMultiPolygon[i].clipToRect(result, rect))
66  {
67  return true;
68  }
69  }
70 
71  return false;
72 }
73 
75 {
77 
78  if(theBoundingRect.intersects(rect))
79  {
80  ossimDrect clipRect(rect.ul().x - 10,
81  rect.ul().y - 10,
82  rect.lr().x + 10,
83  rect.lr().y + 10);
84 
85  if(theFillEnabled)
86  {
87  vector<ossimPolygon> polyListResult;
88 
89  for(ossim_uint32 i = 0; i < theMultiPolygon.size(); ++i)
90  {
91  vector<ossimPolygon> tempList;
92 
93  if(theMultiPolygon[i].clipToRect(tempList, rect))
94  {
95  polyListResult.insert(polyListResult.end(),
96  tempList.begin(),
97  tempList.end());
98  }
99  }
100 
101  if(polyListResult.size())
102  {
103  result = new ossimAnnotationMultiPolyObject(polyListResult,
105  theRed,
106  theGreen,
107  theBlue,
108  theThickness);
109  }
110  }
111  else
112  {
113  vector<ossimPolyLine> lineListResult;
114  vector<ossimPolyLine> tempResult;
115 
116  for(ossim_uint32 i = 0; i< theMultiPolygon.size();++i)
117  {
118  ossimPolyLine polyLine = theMultiPolygon[i];
119 
120  if(polyLine.clipToRect(tempResult,
121  clipRect))
122  {
123  lineListResult.insert(lineListResult.end(),
124  tempResult.begin(),
125  tempResult.end());
126  }
127  }
128 
129  if(lineListResult.size())
130  {
131  result = new ossimAnnotationMultiLineObject(lineListResult,
132  theRed,
133  theGreen,
134  theBlue,
135  theThickness);
136  }
137  }
138  }
139 
140  return result;
141 }
142 
144 {
145  if(theMultiPolygon.size()<1) return;
146  if(theBoundingRect.hasNans()) return;
147 
148 
149  anImage.setDrawColor(theRed, theGreen, theBlue);
150  anImage.setThickness(theThickness);
151  ossimDrect imageRect = anImage.getImageData()->getImageRectangle();
152 
153  if(theBoundingRect.intersects(imageRect))
154  {
155  // we need to extend it by a couple of pixels since
156  // if a pixel lies on the edge and then another pixel is just off
157  // the edge we will get a stair step and so for several pixels
158  // the line might be inside the image rectangle but the clip
159  // algorithm will only draw 1 pixel since it thinks the first
160  // point is inside and the second point is outside and will
161  // execute the clip algorithm keeping only the first
162  // point.
163  ossimDrect clipRect(imageRect.ul().x - 10,
164  imageRect.ul().y - 10,
165  imageRect.lr().x + 10,
166  imageRect.lr().y + 10);
167 
168  int j = 0;
169  for(ossim_uint32 i = 0; i < theMultiPolygon.size(); ++i)
170  {
171  const ossimPolygon& poly = theMultiPolygon[i];
172  int vertexCount = poly.getVertexCount();
173  if(!theFillEnabled)
174  {
175  if(poly.getNumberOfVertices() == 1)
176  {
177  ossimDpt start, end;
178  start = poly[0];
179  end = poly[0];
180  if(clipRect.clip(start, end))
181  {
182  anImage.drawLine(ossimIpt(start),
183  ossimIpt(end));
184  }
185  }
186  else if(poly.getNumberOfVertices() == 2)
187  {
188  ossimDpt start, end;
189  start = poly[0];
190  end = poly[1];
191  if(clipRect.clip(start, end))
192  {
193  anImage.drawLine(ossimIpt(start),
194  ossimIpt(end));
195  }
196  }
197  else if(vertexCount > 2)
198  {
199  ossimDpt start, end;
200  j = 0;
201  while(j<vertexCount)
202  {
203  start = poly[j];
204  end = poly[(j+1)%vertexCount];
205  if(clipRect.clip(start, end))
206  {
207  anImage.drawLine(ossimIpt(start),
208  ossimIpt(end));
209  }
210  ++j;
211  }
212 #if 0
213  ossimDpt start, end;
214  start = poly[vertexCount-1];
215  end = poly[0];
216  j = 0;
217  do
218  {
219  if(clipRect.clip(start, end))
220  {
221  anImage.drawLine(ossimIpt(start),
222  ossimIpt(end));
223  }
224  ++j;
225  start = poly[j-1];
226  end = poly[j];
227  }while(j < vertexCount);
228 #endif
229  }
230  }
231  else
232  {
233  vector<ossimPolygon> result;
234  if(poly.clipToRect(result, imageRect))
235  {
236  for(j = 0; j < (int)result.size();++j)
237  {
238  anImage.drawFilledPolygon(result[j].getVertexList());
239  }
240  }
241  }
242  }
243  }
244 }
245 
247 {
249  out << endl;
250  out << setw(15)<<setiosflags(ios::left)<<"type:"<<getClassName() << endl
251  << setw(15)<<setiosflags(ios::left)<<"polygons:"<<theMultiPolygon.size()<<endl
252  << setw(15)<<setiosflags(ios::left)<<"fill enable: " << theFillEnabled << endl;
253 
254  for(ossim_uint32 i = 0; i < theMultiPolygon.size(); ++i)
255  {
256  out << "____________________________________________________"<<endl
257  << theMultiPolygon[i] << endl;
258  }
259  out << "____________________________________________________"<<endl;
260  return out;
261 }
262 
264 {
265  rect = theBoundingRect;
266 }
267 
269 {
270  theMultiPolygon.clear();
271 }
272 
274 {
275  if(theMultiPolygon.size())
276  {
277  ossimDrect rect(theMultiPolygon[0]);
278 
279  for(ossim_uint32 i = 1; i < theMultiPolygon.size(); ++i)
280  {
281  ossimDrect rect2(theMultiPolygon[i]);
282 
283  if(rect.hasNans())
284  {
285  rect = rect2;
286  }
287  else
288  {
289  if(!rect2.hasNans())
290  {
291  rect = rect.combine(rect2);
292  }
293  }
294  }
295 
296  theBoundingRect = rect;
297  }
298  else
299  {
301  }
302  if(!theBoundingRect.hasNans())
303  {
304  ossimIpt origin = theBoundingRect.ul();
306  origin.y - theThickness/2,
307  origin.x + (theBoundingRect.width()-1) + theThickness/2,
308  origin.y + (theBoundingRect.height()-1) + theThickness/2);
309  }
310 }
311 
313 {
314  for(ossim_uint32 i = 0; i < theMultiPolygon.size(); ++i)
315  {
316  if(theMultiPolygon[i].isPointWithin(imagePoint))
317  {
318  return true;
319  }
320  }
321 
322  return false;
323 }
void makeNan()
Definition: ossimDrect.h:388
ossim_uint32 x
ossim_float64 width() const
Definition: ossimDrect.h:522
virtual void draw(ossimRgbImage &anImage) const
ossim_uint32 y
const ossimDpt & ul() const
Definition: ossimDrect.h:339
double y
Definition: ossimDpt.h:165
virtual ossimAnnotationObject * getNewClippedObject(const ossimDrect &rect) const
virtual ossimString getClassName() const
Definition: ossimObject.cpp:64
ossimDrect combine(const ossimDrect &rect) const
Definition: ossimDrect.h:826
void setThickness(ossim_int32 thickness)
All the drawing algorithms have thickness.
virtual std::ostream & print(std::ostream &out) const
Generic print method.
virtual ossimDrect getBoundingRect() const
ossimRefPtr< ossimImageData > getImageData()
Will return the image data.
virtual bool isPointWithin(const ossimDpt &imagePoint) const
ossim_uint32 getVertexCount() const
virtual bool intersects(const ossimDrect &rect) const
unsigned int ossim_uint32
virtual ossimIrect getImageRectangle() const
virtual std::ostream & print(std::ostream &out) const
Generic print method.
bool hasNans() const
Definition: ossimDrect.h:396
bool intersects(const ossimDrect &rect) const
Definition: ossimDrect.cpp:289
ossim_float64 height() const
Definition: ossimDrect.h:517
void drawLine(double x1, double y1, double x2, double y2)
About all the draw routines will call draw line.
bool clipToRect(vector< ossimPolygon > &result, const ossimDrect &rect) const
Uses the ossimPolyArea2d class for the intersection.
ossim_uint32 getNumberOfVertices() const
void setDrawColor(ossim_uint8 r=255, ossim_uint8 g=255, ossim_uint8 b=255)
bool clipToRect(vector< ossimPolyLine > &result, const ossimDrect &rect) const
void drawFilledPolygon(const std::vector< ossimDpt > &p)
We will cut and paste the code from drawFilledPolygon(ossimIpt *p, int n).
ossim_int32 y
Definition: ossimIpt.h:142
double x
Definition: ossimDpt.h:164
ossim_int32 x
Definition: ossimIpt.h:141
virtual void applyScale(double x, double y)
#define RTTI_DEF1(cls, name, b1)
Definition: ossimRtti.h:485
const ossimDpt & lr() const
Definition: ossimDrect.h:341
std::basic_ostream< char > ostream
Base class for char output streams.
Definition: ossimIosFwd.h:23