OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimGrect.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 // Copyright (C) 2000 ImageLinks Inc.
3 //
4 // License: LGPL
5 //
6 // See LICENSE.txt file in the top level directory for more details.
7 //
8 // Author: Garrett Potts
9 // Description:
10 //
11 //*************************************************************************
12 // $Id: ossimGrect.cpp 23461 2015-08-05 20:20:20Z okramer $
13 
14 #include <ossim/base/ossimGrect.h>
15 using namespace std;
16 
18 {
19  return os << rect.theUlCorner << ", " << rect.theLrCorner << endl;
20 }
21 
22 ossimGrect::ossimGrect(vector<ossimGpt>& points)
23  :
24  theUlCorner(0.0, 0.0, 0.0),
25  theLrCorner(0.0, 0.0, 0.0)
26 {
27  // initialize everyone to the first point
28  if(points.size() > 0)
29  {
30  double minHgt = 0.0;
31  double maxHgt = 0.0;
32  theUlCorner = points[0];
33  theLrCorner = theUlCorner;
34  minHgt = theUlCorner.hgt;
35  maxHgt = minHgt;
36 
37  // find the bounds
38  for(ossim_uint32 index = 1; index < points.size(); index++)
39  {
40  // find left most
41  if(theUlCorner.lond() > points[index].lond())
42  {
43  theUlCorner.lond(points[index].lond());
44  } // find right most
45  else if(theLrCorner.lond() < points[index].lond())
46  {
47  theLrCorner.lond(points[index].lond());
48  }
49  //find top most
50  if(points[index].latd() > theUlCorner.latd())
51  {
52  theUlCorner.latd(points[index].latd());
53  }// find bottom most
54  else if(points[index].latd() < theLrCorner.latd())
55  {
56  theLrCorner.latd(points[index].latd());
57  }
58 
59  if (points[index].hgt > maxHgt)
60  {
61  maxHgt = points[index].hgt;
62  }
63  else if (points[index].hgt < minHgt)
64  {
65  minHgt = points[index].hgt;
66  }
67  }
68 
69  theUlCorner.hgt = maxHgt;
70  theLrCorner.hgt = minHgt;
71  }
72 }
73 
75 {
76  if(p1.lon < p2.lon)
77  {
78  theUlCorner.lon = p1.lon;
79  theLrCorner.lon = p2.lon;
80  }
81  else
82  {
83  theUlCorner.lon = p2.lon;
84  theLrCorner.lon = p1.lon;
85  }
86 
87  if(p1.lat > p2.lat)
88  {
89  theUlCorner.lat = p1.lat;
90  theLrCorner.lat = p2.lat;
91  }
92  else
93  {
94  theUlCorner.lat = p2.lat;
95  theLrCorner.lat = p1.lat;
96  }
97 
98  if(p1.hgt > p2.hgt)
99  {
100  theUlCorner.hgt = p1.hgt;
101  theLrCorner.hgt = p2.hgt;
102  }
103  else
104  {
105  theUlCorner.hgt = p2.hgt;
106  theLrCorner.hgt = p1.hgt;
107  }
108 }
109 
111  const ossimGpt& p2,
112  const ossimGpt& p3,
113  const ossimGpt& p4)
114 {
115  unsigned long index;
116  double minHgt, maxHgt;
117 
118  std::vector<ossimGpt> points(4);
119  points[0] = p1;
120  points[1] = p2;
121  points[2] = p3;
122  points[3] = p4;
123 
124  // initialize everyone to the first point
125  theUlCorner = points[0];
127  minHgt = theUlCorner.hgt;
128  maxHgt = minHgt;
129 
130  // find the bounds
131  for(index = 1; index < points.size(); index++)
132  {
133  // find left most
134  if(theUlCorner.lond() > points[index].lond())
135  {
136  theUlCorner.lond(points[index].lond());
137  } // find right most
138  else if(theLrCorner.lond() < points[index].lond())
139  {
140  theLrCorner.lond(points[index].lond());
141  }
142  //find top most
143  if(points[index].latd() > theUlCorner.latd())
144  {
145  theUlCorner.latd(points[index].latd());
146  }// find bottom most
147  else if(points[index].latd() < theLrCorner.latd())
148  {
149  theLrCorner.latd(points[index].latd());
150  }
151 
152  if (points[index].hgt > maxHgt)
153  maxHgt = points[index].hgt;
154  else if (points[index].hgt < minHgt)
155  minHgt = points[index].hgt;
156  }
157  theUlCorner.hgt = maxHgt;
158  theLrCorner.hgt = minHgt;
159 }
160 
162  double lonSpacingInDegrees)const
163 {
164  double ulLat = ((long)ceil(theUlCorner.latd()/latSpacingInDegrees))*
165  latSpacingInDegrees;
166  double ulLon = ((long)floor(theUlCorner.lond()/lonSpacingInDegrees))*
167  lonSpacingInDegrees;
168  double lrLat = ((long)floor(theLrCorner.latd()/latSpacingInDegrees))*
169  latSpacingInDegrees;
170  double lrLon = ((long)ceil(theLrCorner.lond()/lonSpacingInDegrees))*
171  lonSpacingInDegrees;
172 
173  ossimGpt ul (ulLat, ulLon, theUlCorner.hgt, theUlCorner.datum());
174  ossimGpt lr (lrLat, lrLon, theLrCorner.hgt, theLrCorner.datum());
175 
176  return ossimGrect(ul, lr);
177 }
178 
179 void ossimGrect::computeEvenTiles(std::vector<ossimGrect>& result,
180  double latSpacingInDegrees,
181  double lonSpacingInDegrees,
182  bool clipToGeographicBounds)const
183 {
184  ossimGrect clipRect = ossimGrect(90, -180, -90, 180);
185  result.clear();
186  ossimGrect temp = stretchToEvenBoundary(latSpacingInDegrees,
187  lonSpacingInDegrees);
188 
189  ossimGpt point = temp.ul();
190 
191  while(temp.pointWithin(point))
192  {
193  while(temp.pointWithin(point))
194  {
195  ossimGrect rect(point.latd(),
196  point.lond(),
197  point.latd()-latSpacingInDegrees,
198  point.lond()+lonSpacingInDegrees);
199 
201  rect.theLrCorner.datum( theUlCorner.datum());
202  if(clipToGeographicBounds)
203  {
204  rect = rect.clipToRect(clipRect);
205  }
206  result.push_back(rect);
207 
208  point.lond(point.lond()+lonSpacingInDegrees);
209  }
210  point.lond(temp.ul().lond());
211  point.latd(point.latd()-latSpacingInDegrees);
212  }
213 }
214 
215 //*******************************************************************
216 // Public Method: ossimGrect::completely_within
217 //*******************************************************************
219 {
220  if(rect.isLonLatNan() || isLonLatNan())
221  {
222  return false;
223  }
224 
225  /* --------------
226  | 1 |
227  | ---------- |
228  | | | |
229  | | | |
230  | | 2 | |
231  | | | |
232  | | | |
233  | ---------- |
234  | |
235  -------------- */
236 
237  bool rtn = true;
238 
239  if ((theUlCorner.lon < rect.ul().lon)||
240  (theUlCorner.lon > rect.ur().lon))
241  {
242  rtn = false;
243  }
244  else if ((theLrCorner.lon > rect.lr().lon)||
245  (theLrCorner.lon < rect.ul().lon))
246  {
247  rtn = false;
248  }
249  else if ((theUlCorner.lat > rect.ul().lat)||
250  (theUlCorner.lat < rect.lr().lat))
251  {
252  rtn = false;
253  }
254  else if ((theLrCorner.lat < rect.lr().lat)||
255  (theLrCorner.lat > rect.ul().lat))
256  {
257  rtn = false;
258  }
259 
260  return rtn;
261 }
262 
263 //*******************************************************************
264 // Public Method: ossimGrect::intersects
265 //*******************************************************************
266 bool ossimGrect::intersects(const ossimGrect& rect) const
267 {
268 
269  if(rect.isLonLatNan() || isLonLatNan())
270  {
271  return false;
272  }
273 
274  ossim_float64 ulx = ossim::max(rect.ul().lon, ul().lon);
275  ossim_float64 lrx = ossim::min(rect.lr().lon, lr().lon);
276  ossim_float64 uly, lry;
277  bool rtn;
278 
279  uly = ossim::min(rect.ul().lat, ul().lat);
280  lry = ossim::max(rect.lr().lat, lr().lat);
281  rtn = ((ulx <= lrx) && (uly >= lry));
282 
283  return (rtn);
284 }
285 
287 {
288  ossimDpt scale (midPoint().metersPerDegree());
289  return height()*scale.y;
290 }
291 
293 {
294  ossimDpt scale (midPoint().metersPerDegree());
295  return width()*scale.x;
296 }
297 
298 
300 {
301  if (isLonLatNan())
302  {
303  theUlCorner = gpt;
304  theLrCorner = gpt;
305  }
306  else if (!pointWithin(gpt, false))
307  {
308  if (gpt.lat > theUlCorner.lat)
309  theUlCorner.lat = gpt.lat;
310  else if (gpt.lat < theLrCorner.lat)
311  theLrCorner.lat = gpt.lat;
312  if (gpt.lon < theUlCorner.lon)
313  theUlCorner.lon = gpt.lon;
314  else if (gpt.lon > theLrCorner.lon)
315  theLrCorner.lon = gpt.lon;
316  }
317 }
318 
320 {
321  expandToInclude(rect.ul());
322  expandToInclude(rect.lr());
323 }
T max(T a, T b)
Definition: ossimCommon.h:236
double lond() const
Will convert the radian measure to degrees.
Definition: ossimGpt.h:97
ossim_float64 width() const
Returns the width of a rectangle in deg.
Definition: ossimGrect.h:247
double y
Definition: ossimDpt.h:165
ossimGrect stretchToEvenBoundary(double latSpacingInDegrees, double lonSpacingInDegrees) const
Definition: ossimGrect.cpp:161
ossimGrect()
Will default to 0,0,0,0.
Definition: ossimGrect.h:31
ossim_float64 hgt
Height in meters above the ellipsiod.
Definition: ossimGpt.h:274
ostream & operator<<(ostream &os, const ossimGrect &rect)
Definition: ossimGrect.cpp:17
double latd() const
Will convert the radian measure to degrees.
Definition: ossimGpt.h:87
bool completely_within(const ossimGrect &rect) const
Definition: ossimGrect.cpp:218
const ossimDatum * datum() const
datum().
Definition: ossimGpt.h:196
void expandToInclude(const ossimGpt &gpt)
Expands existing rect to accommodate argument point.
Definition: ossimGrect.cpp:299
ossim_float64 widthMeters() const
Returns the width of a rectangle in meters using the center lat for scaling EW direction.
Definition: ossimGrect.cpp:292
double ossim_float64
bool pointWithin(const ossimGpt &gpt, bool considerHgt=false) const
METHOD: pointWithin(ossimGpt)
Definition: ossimGrect.h:232
void computeEvenTiles(std::vector< ossimGrect > &result, double latSpacingInDegrees, double lonSpacingInDegrees, bool clipToGeographicBounds=true) const
Definition: ossimGrect.cpp:179
ossim_float64 lon
Definition: ossimGpt.h:266
unsigned int ossim_uint32
const ossimGpt & ul() const
Definition: ossimGrect.h:252
ossimGpt ur() const
Definition: ossimGrect.h:257
T min(T a, T b)
Definition: ossimCommon.h:203
ossimGpt midPoint() const
Definition: ossimGrect.h:178
ossimGpt theLrCorner
Definition: ossimGrect.h:169
double x
Definition: ossimDpt.h:164
ossim_float64 heightMeters() const
Returns the height of a rectangle in meters.
Definition: ossimGrect.cpp:286
ossim_float64 lat
Definition: ossimGpt.h:265
ossimGpt theUlCorner
Definition: ossimGrect.h:168
bool isLonLatNan() const
Definition: ossimGrect.h:290
ossim_float64 height() const
Returns the height of a rectangle in deg.
Definition: ossimGrect.h:242
bool intersects(const ossimGrect &rect) const
Definition: ossimGrect.cpp:266
const ossimGpt & lr() const
Definition: ossimGrect.h:269
std::basic_ostream< char > ostream
Base class for char output streams.
Definition: ossimIosFwd.h:23