OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimAnnotationPolyObject.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 // Copyright (C) 2000 ImageLinks Inc.
3 //
4 // License: See top level LICENSE.txt file.
5 //
6 // Author: Garrett Potts
7 //
8 //*************************************************************************
9 // $Id: ossimAnnotationPolyObject.cpp 13964 2009-01-14 16:30:07Z gpotts $
14 #include <ossim/base/ossimIrect.h>
15 
17  "ossimAnnotationPolyObject",
19 
21  ossim_uint8 r,
22  ossim_uint8 g,
23  ossim_uint8 b,
24  ossim_uint8 thickness)
25  :ossimAnnotationObject(r, g, b, thickness),
26  thePolygon(),
27  theBoundingRect(),
28  theFillEnabled(enableFill)
29 {
30 }
31 
33  const vector<ossimDpt>& imagePts,
34  bool enableFill,
35  ossim_uint8 r,
36  ossim_uint8 g,
37  ossim_uint8 b,
38  ossim_uint8 thickness)
39  :ossimAnnotationObject(r, g, b, thickness),
40  theFillEnabled(enableFill)
41 {
42  thePolygon = imagePts;
44 }
45 
47  const ossimAnnotationPolyObject& rhs)
48  : ossimAnnotationObject(rhs),
49  thePolygon(rhs.thePolygon),
50  theBoundingRect(rhs.theBoundingRect),
51  theFillEnabled(rhs.theFillEnabled)
52 {
53 }
54 
56 {
57  return new ossimAnnotationPolyObject(*this);
58 }
59 
61 {
62 }
63 
65 {
66  thePolygon *= ossimDpt(x, y);
68 }
69 
71 {
72  // do the quick checks first
73  //
74  if(rect.hasNans()) return false;
75  if(!rect.intersects(theBoundingRect)) return false;
76 
77  if(!theFillEnabled)
78  {
79  int vertexCount = thePolygon.getVertexCount();
80  ossimDpt start, end;
81  int j = 0;
82  while(j<vertexCount)
83  {
84  start = thePolygon[j];
85  end = thePolygon[(j+1)%vertexCount];
86  if(rect.clip(start, end))
87  {
88  return true;
89  }
90  ++j;
91  }
92 // start = thePolygon[vertexCount-1];
93 // end = thePolygon[0];
94 // int i = 0;
95 // do
96 // {
97 // if(rect.clip(start, end))
98 // {
99 // return true;
100 // }
101 // ++i;
102 // start = thePolygon[i];
103 // end = thePolygon[i-1];
104 // }while(i < vertexCount);
105  }
106  else
107  {
108  vector<ossimPolygon> result;
109  return thePolygon.clipToRect(result, rect);
110  }
111 
112  return false;
113 }
114 
116 {
118 
119  if(theBoundingRect.intersects(rect))
120  {
121  if(theFillEnabled)
122  {
123  vector<ossimPolygon> resultPoly;
124 
125  if(thePolygon.clipToRect(resultPoly, rect))
126  {
127  if(resultPoly.size() == 1)
128  {
129  result = new ossimAnnotationPolyObject(resultPoly[0].getVertexList(),
131  theRed,
132  theGreen,
133  theBlue,
134  theThickness);
135  }
136  else
137  {
138  result = new ossimAnnotationMultiPolyObject(resultPoly,
140  theRed,
141  theGreen,
142  theBlue,
143  theThickness);
144  }
145  }
146  }
147  else
148  {
149  vector<ossimPolyLine> lineListResult;
150  ossimPolyLine polyLine = thePolygon;
151 
152  if(polyLine.clipToRect(lineListResult,
153  rect))
154  {
155  result = new ossimAnnotationMultiLineObject(lineListResult,
156  theRed,
157  theGreen,
158  theBlue,
159  theThickness);
160  }
161  }
162  }
163 
164  return result;
165 }
166 
168 {
169  if(thePolygon.getVertexCount() < 2) return;
170  if(theBoundingRect.hasNans()) return;
171  int vertexCount = thePolygon.getVertexCount();
172 
173  anImage.setDrawColor(theRed, theGreen, theBlue);
174  anImage.setThickness(theThickness);
175  ossimDrect imageRect = anImage.getImageData()->getImageRectangle();
176  if(theBoundingRect.intersects(imageRect))
177  {
178  // we need to extend it by a couple of pixels since
179  // if a pixel lies on the edge and then another pixel is just off
180  // the edge we will get a stair step and so for several pixels
181  // the line might be inside the image rectangle but the clip
182  // algorithm will only draw 1 pixel since it thinks the first
183  // point is inside and the second point is outside and will
184  // execute the clip algorithm keeping only the first
185  // point.
186  ossimDrect clipRect(imageRect.ul().x - 10,
187  imageRect.ul().y - 10,
188  imageRect.lr().x + 10,
189  imageRect.lr().y + 10);
190 
191  if(!theFillEnabled)
192  {
193  ossimDpt start, end;
195  {
196  start = thePolygon[0];
197  end = thePolygon[0];
198  if(clipRect.clip(start, end))
199  {
200  anImage.drawLine(ossimIpt(start),
201  ossimIpt(end));
202  }
203  }
204  else if(thePolygon.getNumberOfVertices() == 2)
205  {
206  start = thePolygon[0];
207  end = thePolygon[1];
208  if(clipRect.clip(start, end))
209  {
210  anImage.drawLine(ossimIpt(start),
211  ossimIpt(end));
212  }
213  }
214  else
215  {
216  int j = 0;
217  while(j<vertexCount)
218  {
219  start = thePolygon[j];
220  end = thePolygon[(j+1)%vertexCount];
221  if(clipRect.clip(start, end))
222  {
223  anImage.drawLine(ossimIpt(start),
224  ossimIpt(end));
225  }
226  ++j;
227  }
228  }
229 #if 0
230  ossimDpt start, end;
231  start = thePolygon[vertexCount-1];
232  end = thePolygon[0];
233  int i = 0;
234  do
235  {
236  if(clipRect.clip(start, end))
237  {
238  anImage.drawLine(ossimIpt((int)start.x, (int)start.y),
239  ossimIpt((int)end.x, (int)end.y));
240  }
241  ++i;
242  start = thePolygon[i];
243  end = thePolygon[i-1];
244  }while(i < vertexCount);
245 #endif
246  }
247  else
248  {
249  vector<ossimPolygon> result;
250  if(thePolygon.clipToRect(result, imageRect))
251  {
252  for(int i = 0; i < (int)result.size();++i)
253  {
254  anImage.drawFilledPolygon(result[i].getVertexList());
255  }
256  }
257  }
258  }
259 }
260 
262 {
263  out << "number_of_points: " << thePolygon.getVertexCount();
264  if(thePolygon.getVertexCount() > 0)
265  {
266  for(long index =0; index < (long)(thePolygon.getVertexCount()-1); ++index)
267  {
268  out << thePolygon[index] << endl;
269 
270  }
271  out << thePolygon[thePolygon.getVertexCount()-1] << endl;
272  }
273  out << "fill_enabled: " << theFillEnabled << endl
274  << "bounding_rect: " << theBoundingRect << endl;
275  return out;
276 }
277 
279 {
280  rect = theBoundingRect;
281 }
282 
284 {
285  thePolygon.addPoint(pt);
286 }
287 
288 void ossimAnnotationPolyObject::setPolygon(const vector<ossimDpt>& imagePoints)
289 {
290  thePolygon = imagePoints;
291 }
292 
294 {
295  thePolygon = polygon;
296 }
297 
299 {
300  thePolygon = rect;
301 }
302 
304 {
305  thePolygon = rect;
306 }
307 
309 {
311 }
312 
314 {
315  return theBoundingRect.pointWithin(imagePoint);
316 }
317 
319 {
320  return thePolygon;
321 }
322 
324 {
325  return thePolygon;
326 }
327 
329 {
330  theFillEnabled = flag;
331 }
ossim_uint32 x
bool pointWithin(const ossimDpt &pt, double epsilon=0.0) const
Definition: ossimDrect.h:781
virtual void draw(ossimRgbImage &anImage) const
ossim_uint32 y
const ossimDpt & ul() const
Definition: ossimDrect.h:339
void addPoint(const ossimDpt &pt)
double y
Definition: ossimDpt.h:165
virtual ossimAnnotationObject * getNewClippedObject(const ossimDrect &rect) const
const ossimPolygon & getPolygon() const
void setThickness(ossim_int32 thickness)
All the drawing algorithms have thickness.
RTTI_DEF1(ossimAnnotationPolyObject, "ossimAnnotationPolyObject", ossimAnnotationObject) ossimAnnotationPolyObject
virtual std::ostream & print(std::ostream &out) const
Generic print method.
virtual void applyScale(double x, double y)
virtual ossimDrect getBoundingRect() const
ossimRefPtr< ossimImageData > getImageData()
Will return the image data.
ossimAnnotationPolyObject(bool enableFill=false, ossim_uint8 r=255, ossim_uint8 g=255, ossim_uint8 b=255, ossim_uint8 thickness=1)
ossim_uint32 getVertexCount() const
virtual ossimIrect getImageRectangle() const
bool hasNans() const
Definition: ossimDrect.h:396
bool intersects(const ossimDrect &rect) const
Definition: ossimDrect.cpp:289
void drawLine(double x1, double y1, double x2, double y2)
About all the draw routines will call draw line.
bool clip(ossimDpt &p1, ossimDpt &p2) const
Definition: ossimDrect.cpp:653
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
virtual bool intersects(const ossimDrect &rect) const
void drawFilledPolygon(const std::vector< ossimDpt > &p)
We will cut and paste the code from drawFilledPolygon(ossimIpt *p, int n).
virtual bool isPointWithin(const ossimDpt &imagePoint) const
double x
Definition: ossimDpt.h:164
virtual void addPoint(const ossimDpt &pt)
const ossimDpt & lr() const
Definition: ossimDrect.h:341
unsigned char ossim_uint8
virtual void setPolygon(const vector< ossimDpt > &imagePoints)
std::basic_ostream< char > ostream
Base class for char output streams.
Definition: ossimIosFwd.h:23