MrSID Decode SDK for Raster Reference Manual  9.0.0.3864
lti_geometry.h
Go to the documentation of this file.
1 /* $Id$ */
2 /* //////////////////////////////////////////////////////////////////////////
3 // //
4 // This code is Copyright (c) 2004 LizardTech, Inc, 1008 Western Avenue, //
5 // Suite 200, Seattle, WA 98104. Unauthorized use or distribution //
6 // prohibited. Access to and use of this code is permitted only under //
7 // license from LizardTech, Inc. Portions of the code are protected by //
8 // US and foreign patents and other filings. All Rights Reserved. //
9 // //
11 
12 #ifndef LTI_GEOMETRY_H
13 #define LTI_GEOMETRY_H
14 
15 // lt_lib_mrsid_core
16 #include "lti_types.h"
17 
18 LT_BEGIN_NAMESPACE(LizardTech)
19 
20 
21 template<class T>
22 class LTIGeomPoint
23 {
24 public:
25  LTIGeomPoint(T inX = 0, T inY = 0) :
26  x(inX),
27  y(inY)
28  {
29  }
30 
31  // ------------------------------
32  // * copy constructor
33  // ------------------------------
34  LTIGeomPoint(const LTIGeomPoint<T>& copy) :
35  x(copy.x),
36  y(copy.y)
37 
38  {
39  }
40 
41  // ------------------------------
42  // * assignment operator
43  // ------------------------------
44  LTIGeomPoint<T>& operator=(const LTIGeomPoint<T>& copy)
45  {
46  x = copy.x;
47  y = copy.y;
48  return *this;
49  }
50 
51 
52  // ----------------------------
53  // * addition
54  // ----------------------------
55  template<class T2>
56  LTIGeomPoint<T>& operator +=(const LTIGeomPoint<T2>& offset)
57  {
58  x += offset.x;
59  y += offset.y;
60  return *this;
61  }
62 
63  // ----------------------------
64  // * subtraction
65  // ----------------------------
66  template<class T2>
67  LTIGeomPoint<T>& operator -=(const LTIGeomPoint<T2>& offset)
68  {
69  x -= offset.x;
70  y -= offset.y;
71  return *this;
72  }
73 
74  // --------------------------------------------------------
75  // * equality operator
76  // --------------------------------------------------------
77  inline bool operator ==(const LTIGeomPoint<T>& other) const
78  {
79  return x == other.x && y == other.y;
80  }
81 
82  // --------------------------------------------------------
83  // * not equals operator
84  // --------------------------------------------------------
85  inline bool operator !=(const LTIGeomPoint<T>& other) const
86  {
87  return !operator==(other);
88  }
89 
90  inline bool equal(const LTIGeomPoint<T>& other, T eps) const
91  {
92  return (x < other.x ? other.x - x : x - other.x) <= eps &&
93  (y < other.y ? other.y - y : y - other.y) <= eps;
94  }
95 
96 public:
97  // --------------------------------------------------------
98  // * Data
99  // --------------------------------------------------------
100  T x;
101  T y;
102 };
103 
104 
105 
106 // --------------------------------------------------------------
107 // * global point operators
108 // --------------------------------------------------------------
109 template<class T>
110 LTIGeomPoint<T> operator+(const LTIGeomPoint<T>& p1, const LTIGeomPoint<T>& p2)
111 {
112  LTIGeomPoint<T> p3 = p1;
113  p3 += p2;
114  return p3;
115 }
116 
117 template<class T>
118 LTIGeomPoint<T> operator-(const LTIGeomPoint<T>& p1, const LTIGeomPoint<T>& p2)
119 {
120  LTIGeomPoint<T> p3 = p1;
121  p3 -= p2;
122  return p3;
123 }
124 
125 
126 
127 
128 // ----------------------------------------
129 //
130 // Dimensions
131 //
132 // ----------------------------------------
133 
134 template<class T>
135 class LTIGeomDim
136 {
137 public:
138  LTIGeomDim(T w = 0, T h = 0) :
139  width(w),
140  height(h)
141  {}
142 
143  LTIGeomDim(const LTIGeomDim<T>& other) :
144  width(other.width),
145  height(other.height)
146  {}
147 
148  LTIGeomDim<T>& operator=(const LTIGeomDim<T>& other)
149  {
150  width = other.width;
151  height = other.height;
152  return *this;
153  }
154 
155  bool operator==(const LTIGeomDim<T>& other) const
156  {
157  return width == other.width && height == other.height;
158  }
159 
160  bool operator!=(const LTIGeomDim<T>& other) const
161  {
162  return !operator==(other);
163  }
164 
165 public:
166  T width;
167  T height;
168 };
169 
170 
171 
172 // ------------------------------------------------------------
173 //
174 // Rectangles
175 //
176 // The LTIGeomRect class represents rectangles in 2 dimensional space.
177 // A rectangle is defined as having an upper left and a lower right corner.
178 // +X goes to the right and +Y goes down. (It is a left handed world.)
179 //
180 // ------------------------------------------------------------
181 
182 template<class T, bool inclusive = true>
183 class LTIGeomRect
184 {
185 public:
186  // ------------------------------------------------------------
187  // * Construct from 2 points
188  // ------------------------------------------------------------
189  LTIGeomRect(const LTIGeomPoint<T>& inUL, const LTIGeomPoint<T>& inLR) :
190  uLeft(inUL),
191  lRight(inLR)
192  {}
193 
194  // ------------------------------------------------------------
195  // * Construct from 4 values - the first pair is upper left,
196  // second pair is lower right
197  // ------------------------------------------------------------
198  LTIGeomRect(T ulx=0, T uly=1, T lrx=-1, T lry=0) :
199  uLeft(ulx, uly),
200  lRight(lrx, lry)
201  {}
202 
203 
204  // ------------------------------------------------------------
205  // * Construct from a dimension
206  // Resulting rect has uLeft = (0,0) and dimensions d
207  // ------------------------------------------------------------
208  LTIGeomRect(const LTIGeomDim<T> d) :
209  uLeft(0,0),
210  lRight(d.width - (inclusive ? 1 : 0), d.height - (inclusive ? 1 : 0))
211  {}
212 
213  // ------------------------------------------------------------
214  // * Copy constructor
215  // ------------------------------------------------------------
216  LTIGeomRect(const LTIGeomRect<T, inclusive>& copy) :
217  uLeft(copy.uLeft),
218  lRight(copy.lRight)
219  {}
220 
221  // ------------------------------------------------------------
222  // * Assignment operator
223  // ------------------------------------------------------------
224  LTIGeomRect<T, inclusive>& operator=(const LTIGeomRect<T, inclusive>& copy)
225  {
226  uLeft = copy.uLeft;
227  lRight = copy.lRight;
228  return *this;
229  }
230 
231  // ------------------------------------------------------------
232  // * width
233  // ------------------------------------------------------------
234  T getWidth() const
235  {
236  return (lRight.x - uLeft.x) + (T)(inclusive ? 1 : 0);
237  }
238  T width() const { return getWidth(); } // BUG: api change
239 
240  // ------------------------------------------------------------
241  // * height
242  // ------------------------------------------------------------
243  T getHeight() const
244  {
245  return (lRight.y - uLeft.y) + (T)(inclusive ? 1 : 0);
246  }
247  T height() const { return getHeight(); } // BUG: api change
248 
249  // ------------------------------------------------------------
250  // * isEmpty
251  // ------------------------------------------------------------
252  bool isEmpty() const
253  {
254  return getWidth() <= 0 || getHeight() <= 0;
255  }
256 
257  // ------------------------------------------------------------
258  // * dimensions
259  // ------------------------------------------------------------
260  LTIGeomDim<T> getDimensions() const
261  {
262  return LTIGeomDim<T>(getWidth(), getHeight());
263  }
264 
265  // ------------------------------------------------------------
266  // * returns center point of the rectangle
267  // ------------------------------------------------------------
268  LTIGeomPoint<T> getCenter() const
269  {
270  T cx = uLeft.x + getWidth()/2;
271  T cy = uLeft.y + getHeight()/2;
272  return LTIGeomPoint<T>(cx, cy);
273  }
274 
275 
276  // ------------------------------------------------------------
277  // * location
278  // returns the location of the specified reference point
279  // ------------------------------------------------------------
280  LTIGeomPoint<T> location(LTIPosition referencePoint)
281  {
282  LTIGeomPoint<T> ref = uLeft;
283  switch (referencePoint)
284  {
285  case LTI_POSITION_UPPER_LEFT:
286  break;
287  case LTI_POSITION_UPPER_CENTER:
288  ref.x += static_cast<T>(getWidth()/2.0);
289  break;
290  case LTI_POSITION_UPPER_RIGHT:
291  ref.x = static_cast<T>(lRight.x);
292  break;
293 
294  case LTI_POSITION_CENTER_LEFT:
295  ref.y -= static_cast<T>(-getHeight()/2.0);
296  break;
297  case LTI_POSITION_CENTER:
298  ref.x += static_cast<T>(getWidth()/2.0);
299  ref.y -= static_cast<T>(-getHeight()/2.0);
300  break;
301  case LTI_POSITION_CENTER_RIGHT:
302  ref.x = static_cast<T>(lRight.x);
303  ref.y -= static_cast<T>(-getHeight()/2.0);
304  break;
305 
306  case LTI_POSITION_LOWER_LEFT:
307  ref.y = static_cast<T>(lRight.y);
308  break;
309  case LTI_POSITION_LOWER_CENTER:
310  ref.y = static_cast<T>(lRight.y);
311  ref.x += static_cast<T>(getWidth()/2.0);
312  break;
313  case LTI_POSITION_LOWER_RIGHT:
314  ref = lRight;
315  break;
316  }
317  return ref;
318  }
319 
320 
321  // ------------------------------------------------------------
322  // * addition
323  // moves the restangle by an offset
324  // ------------------------------------------------------------
325  template<class T2>
326  LTIGeomRect<T, inclusive>& operator+=(const LTIGeomPoint<T2>& offset)
327  {
328  uLeft += offset;
329  lRight += offset;
330  return *this;
331  }
332 
333  // ------------------------------------------------------------
334  // * subtraction
335  // moves the restangle by an offset
336  // ------------------------------------------------------------
337  template<class T2>
338  LTIGeomRect<T, inclusive>& operator-=(const LTIGeomPoint<T2>& offset)
339  {
340  uLeft -= offset;
341  lRight -= offset;
342  return *this;
343  }
344 
345  // ------------------------------------------------------------
346  // * intersection operator
347  // returns the intersection between two rectangles
348  // ------------------------------------------------------------
349  LTIGeomRect<T, inclusive>& operator &=(const LTIGeomRect<T, inclusive>& other)
350  {
351  if(!intersect(other))
352  *this = LTIGeomRect(); // invalid rectangle
353  return *this;
354  }
355 
356  // ------------------------------------------------------------
357  // * union operator
358  // returns the union of two rectangles
359  // ------------------------------------------------------------
360  LTIGeomRect<T, inclusive>& operator |=(const LTIGeomRect<T, inclusive>& other)
361  {
362  uLeft.x = LT_MIN(uLeft.x, other.uLeft.x);
363  uLeft.y = LT_MIN(uLeft.y, other.uLeft.y);
364  lRight.x = LT_MAX(lRight.x, other.lRight.x);
365  lRight.y = LT_MAX(lRight.y, other.lRight.y);
366  return *this;
367  }
368 
369  bool operator==(const LTIGeomRect<T, inclusive>& other) const
370  {
371  return uLeft == other.uLeft && lRight == other.lRight;
372  }
373 
374  // --------------------------------------------------------
375  // * not equals operator
376  // --------------------------------------------------------
377  inline bool operator !=(const LTIGeomRect<T, inclusive>& other) const
378  {
379  return !operator==(other);
380  };
381 
382  // clip our rectangle to the given size, i.e. do an intersection
383  // return true iff we remain a valid rectangle
384  bool intersect(const LTIGeomRect<T, inclusive>& other)
385  {
386  uLeft.x = LT_MAX(uLeft.x, other.uLeft.x);
387  uLeft.y = LT_MAX(uLeft.y, other.uLeft.y);
388  lRight.x = LT_MIN(lRight.x, other.lRight.x);
389  lRight.y = LT_MIN(lRight.y, other.lRight.y);
390 
391  return uLeft.x <= lRight.x && uLeft.y <= lRight.y;
392  }
393 
394 public:
395  // ------------------------------------------------
396  // * Data
397  // ------------------------------------------------
398  LTIGeomPoint<T> uLeft; // upper left corner
399  LTIGeomPoint<T> lRight; // lower right corner
400 };
401 
402 
403 // --------------------------------------------------------------
404 // * global rect operators
405 // --------------------------------------------------------------
406 template<class T, bool inclusive>
407 LTIGeomRect<T, inclusive> operator+(const LTIGeomRect<T, inclusive>& r1,
408  const LTIGeomPoint<T>& offset)
409 {
410  LTIGeomRect<T, inclusive> r3 = r1;
411  r3 += offset;
412  return r3;
413 }
414 
415 template<class T, bool inclusive>
416 LTIGeomRect<T, inclusive> operator-(const LTIGeomRect<T, inclusive>& r1,
417  const LTIGeomPoint<T>& offset)
418 {
419  LTIGeomRect<T, inclusive> r3 = r1;
420  r3 -= offset;
421  return r3;
422 }
423 
424 template<class T, bool inclusive>
425 LTIGeomRect<T, inclusive> operator&(const LTIGeomRect<T, inclusive>& r1,
426  const LTIGeomRect<T, inclusive>& r2)
427 {
428  LTIGeomRect<T, inclusive> r3 = r1;
429  r3 &= r2;
430  return r3;
431 }
432 
433 template<class T, bool inclusive>
434 LTIGeomRect<T, inclusive> operator|(const LTIGeomRect<T, inclusive>& r1,
435  const LTIGeomRect<T, inclusive>& r2)
436 {
437  LTIGeomRect<T, inclusive> r3 = r1;
438  r3 |= r2;
439  return r3;
440 }
441 
442 
443 // ------------------------------------------------------------
444 //
445 // Bound Box
446 //
447 // The LTIGeomBBox class represents rectangles in 2 dimensional space.
448 // A rectangle is defined as having a min point and a max point.
449 // +X goes to the right and +Y goes up. (It is a right handed world.)
450 //
451 // ------------------------------------------------------------
452 
453 template<class T>
454 class LTIGeomBBox
455 {
456 public:
457  LTIGeomBBox(T minX = 0, T minY = 0, T maxX = 0, T maxY = 0) :
458  xMin(minX),
459  yMin(minY),
460  xMax(maxX),
461  yMax(maxY)
462  {}
463 
464  LTIGeomBBox(const LTIGeomBBox<T>& copy) :
465  xMin(copy.xMin),
466  yMin(copy.yMin),
467  xMax(copy.xMax),
468  yMax(copy.yMax)
469  {}
470 
471  LTIGeomBBox<T>& operator=(const LTIGeomBBox<T>& copy)
472  {
473  xMin = copy.xMin;
474  yMin = copy.yMin;
475  xMax = copy.xMax;
476  yMax = copy.yMax;
477  return *this;
478  }
479 
480  T getWidth() const { return xMax - xMin; }
481  T getHeight() const { return yMax - yMin; }
482  bool isEmpty() const { return xMax <= xMin || yMax <= yMin; }
483  LTIGeomPoint<T> getCenter() const
484  {
485  return LTIGeomPoint<T>((xMin + xMax) / 2, (yMin + yMax) / 2);
486  }
487 
488  // intersection operator
489  LTIGeomBBox<T>& operator &=(const LTIGeomBBox<T>& other)
490  {
491  xMin = LT_MAX(xMin, other.xMin);
492  yMin = LT_MAX(yMin, other.yMin);
493  xMax = LT_MIN(xMax, other.xMax);
494  yMax = LT_MIN(yMax, other.yMax);
495  return *this;
496  }
497 
498  // union operator
499  LTIGeomBBox<T>& operator |=(const LTIGeomBBox<T>& other)
500  {
501  xMin = LT_MIN(xMin, other.xMin);
502  yMin = LT_MIN(yMin, other.yMin);
503  xMax = LT_MAX(xMax, other.xMax);
504  yMax = LT_MAX(yMax, other.yMax);
505  return *this;
506  }
507 
508  bool overlap(const LTIGeomBBox<T>& other) const
509  {
510  if(xMin > other.xMax || other.xMin > xMax)
511  return false;
512  if(yMin > other.yMax || other.yMin > yMax)
513  return false;
514  return true;
515  }
516 
517  bool containsPoint(const LTIGeomPoint<T> &pt) const
518  {
519  return (xMin <= pt.x && pt.x <= xMax) &&
520  (yMin <= pt.y && pt.y <= yMax);
521  }
522 
523  // equalty operators
524  bool operator ==(const LTIGeomBBox<T>& other) const
525  {
526  return xMin == other.xMin &&
527  yMin == other.yMin &&
528  xMax == other.xMax &&
529  yMax == other.yMax;
530  }
531  bool operator !=(const LTIGeomBBox<T>& other) const
532  {
533  return !operator==(other);
534  }
535 
536 public:
537  T xMin;
538  T yMin;
539  T xMax;
540  T yMax;
541 };
542 
543 typedef LTIGeomRect<lt_int32, true> LTIGeomIntRect;
544 typedef LTIGeomDim<lt_int32> LTIGeomIntDim;
545 typedef LTIGeomPoint<lt_int32> LTIGeomIntPoint;
546 
547 typedef LTIGeomRect<double, false> LTIGeomDblRect;
548 typedef LTIGeomDim<double> LTIGeomDblDim;
549 typedef LTIGeomPoint<double> LTIGeomDblPoint;
550 
551 // BUG: these just for backwards compatability (mg3)
552 typedef LTIGeomRect<lt_int32, true> MG3Rect;
553 typedef LTIGeomDim<lt_int32> MG3Dim;
554 typedef LTIGeomPoint<lt_int32> MG3Point;
555 
556 // BUG: these just for backwards compatability (mg2)
557 typedef LTIGeomRect<lt_int32, true> IntRect;
558 typedef LTIGeomDim<lt_int32> IntDimension;
559 typedef LTIGeomPoint<lt_int32> IntPoint;
560 
561 LT_END_NAMESPACE(LizardTech)
562 
563 
564 #endif // LTI_GEOMETRY_H

LizardTech