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

Extensis