opennurbs_ipoint.h
1 //
2 // Copyright (c) 1993-2017 Robert McNeel & Associates. All rights reserved.
3 // OpenNURBS, Rhinoceros, and Rhino3D are registered trademarks of Robert
4 // McNeel & Associates.
5 //
6 // THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
7 // ALL IMPLIED WARRANTIES OF FITNESS FOR ANY PARTICULAR PURPOSE AND OF
8 // MERCHANTABILITY ARE HEREBY DISCLAIMED.
9 //
10 // For complete openNURBS copyright information see <http://www.opennurbs.org>.
11 //
12 ////////////////////////////////////////////////////////////////
13 
14 
15 #if !defined(OPENNURBS_IPOINT_INC_)
16 #define OPENNURBS_IPOINT_INC_
17 
18 /*
19 A 2 dimensional point with integer coordinates.
20 Clear code will distinguish between situation where (x,y) is a
21 location (ON_2iPoint) or a direction (ON_2iVector) and use
22 the appropriate class.
23 */
24 class ON_CLASS ON_2iPoint
25 {
26 public:
27  // Default construction intentionally leaves x and y uninitialized.
28  // Use something like
29  // ON_2iPoint pt(1,2);
30  // or
31  // ON_2iPoint pt = ON_2iPoint::Origin;
32  // when you need an initialized ON_2iPoint.
33  ON_2iPoint() = default;
34 
35  ~ON_2iPoint() = default;
36  ON_2iPoint(const ON_2iPoint& ) = default;
37  ON_2iPoint& operator=(const ON_2iPoint& ) = default;
38 
39  ON_2iPoint(
40  int x,
41  int y
42  );
43 
44 public:
45  static const ON_2iPoint Origin; // (0,0)
46  static const ON_2iPoint Unset; // (ON_UNSET_INT_INDEX,ON_UNSET_INT_INDEX)
47 
48  /*
49  Dictionary order compare.
50  */
51  static int Compare(
52  const ON_2iPoint& lhs,
53  const ON_2iPoint& rhs
54  );
55 
56 public:
57  ON_2iPoint& operator+=(const class ON_2iVector&);
58  ON_2iPoint& operator-=(const class ON_2iVector&);
59 
60  // It is intentional that points are not added to points to encourage
61  // code that is clear about what is a location and what is diplacement.
62 
63 public:
64  /*
65  For those times when a location was incorrectly represented by a vector.
66  It is intentional that ther is not an ON_2iPoint constructor from an ON_2iVector.
67  */
68  static const ON_2iPoint FromVector(const class ON_2iVector& v);
69 
70  static const ON_2iPoint From2dex(const class ON_2dex& src);
71 
72 public:
73  /*
74  Returns:
75  (0 == x && 0 == y)
76  */
77  bool IsOrigin() const;
78 
79  /*
80  Returns:
81  (ON_UNSET_INT_INDEX == x || ON_UNSET_INT_INDEX ==y)
82  */
83  bool IsSet() const;
84 
85 public:
86  ON__INT32 x;
87  ON__INT32 y;
88 };
89 
90 ON_DECL
91 bool operator==(const ON_2iPoint&, const ON_2iPoint&);
92 
93 ON_DECL
94 bool operator!=(const ON_2iPoint&, const ON_2iPoint&);
95 
96 
97 /*
98 A 2 dimensional vector with integer coordinates.
99 Clear code will distinguish between situation where (x,y) is a
100 location (ON_2iPoint) or a direction (ON_2iVector) and use
101 the appropriate class.
102 */
103 class ON_CLASS ON_2iVector
104 {
105 public:
106  // Default construction intentionally leaves x and y uninitialized.
107  // Use something like
108  // ON_2iVector pt(1,2);
109  // or
110  // ON_2iVector pt = ON_2iVector::Zero;
111  // when you need an initialized ON_2iVector.
112  ON_2iVector() = default;
113 
114  ~ON_2iVector() = default;
115  ON_2iVector(const ON_2iVector& ) = default;
116  ON_2iVector& operator=(const ON_2iVector& ) = default;
117 
118  ON_2iVector(
119  int x,
120  int y
121  );
122 
123  /*
124  For those times when a direction was incorrectly represented by a point.
125  It is intentional that ther is not an ON_2iVector constructor from an ON_2iPoint.
126  */
127  static const ON_2iVector FromPoint(const class ON_2iPoint& p);
129  static const ON_2iVector From2dex(const class ON_2dex& src);
131 public:
132  static const ON_2iVector Zero; // (0,0)
133  static const ON_2iVector UnitX; // (1,0)
134  static const ON_2iVector UnitY; // (0,1)
135  static const ON_2iVector Unset; // (ON_UNSET_INT_INDEX,ON_UNSET_INT_INDEX)
136 
137  /*
138  Dictionary order compare.
139  */
140  static int Compare(
141  const ON_2iVector& lhs,
142  const ON_2iVector& rhs
143  );
144 
145 public:
146  ON_2iVector& operator+=(const class ON_2iVector&);
147  ON_2iVector& operator-=(const class ON_2iVector&);
148  ON_2iVector& operator*=(int);
149 
150  ON_2iVector operator-() const;
151 
152 public:
153  /*
154  Returns:
155  (0 == x && 0 == y)
156  */
157  bool IsZero() const;
158 
159  /*
160  Returns:
161  IsSet() && (0 != x || 0 != y)
162  */
163  bool IsNotZero() const;
165  /*
166  Returns:
167  (ON_UNSET_INT_INDEX == x || ON_UNSET_INT_INDEX ==y)
168  */
169  bool IsSet() const;
170 
171 public:
172  ON__INT32 x;
173  ON__INT32 y;
174 };
175 
176 ON_DECL
177 bool operator==(const ON_2iVector&, const ON_2iVector&);
178 
179 ON_DECL
180 bool operator!=(const ON_2iVector&, const ON_2iVector&);
181 
182 ON_DECL
183 ON_2iPoint operator+(const ON_2iPoint&, const ON_2iVector&);
184 
185 ON_DECL
186 ON_2iPoint operator-(const ON_2iPoint&, const ON_2iVector&);
187 
188 ON_DECL
189 ON_2iVector operator+(const ON_2iVector&, const ON_2iVector&);
190 
191 ON_DECL
192 ON_2iVector operator-(const ON_2iVector&, const ON_2iVector&);
193 
194 ON_DECL
195 ON_2iVector operator*(int, const ON_2iVector&);
196 
197 class ON_CLASS ON_2iBoundingBox
198 {
199 public:
200  // Default construction intentionally leaves m_min and m_max uninitialized.
201  // Use something like
202  // ON_2iBoundingBox bbox(min_pt,max_pt);
203  // or
204  // ON_2iBoundingBox bbox = ON_2iBoundingBox::Unset;
205  ON_2iBoundingBox() = default;
206 
207  ~ON_2iBoundingBox() = default;
208  ON_2iBoundingBox(const ON_2iBoundingBox& ) = default;
209  ON_2iBoundingBox& operator=(const ON_2iBoundingBox& ) = default;
210 
212  const class ON_2iPoint bbox_min,
213  const class ON_2iPoint bbox_max
214  );
215 
216 public:
217  static const ON_2iBoundingBox Zero; // (ON_2iPoint::Origin,ON_2iPoint::Origin);
218  static const ON_2iBoundingBox Unset; // (ON_2iPoint::Unset,ON_2iPoint::Unset)
219 
220 public:
221  /*
222  Returns:
223  m_min.IsSet() && m_max.IsSet() && m_min.x <= m_max.x && m_min.y <= m_max.y.
224  */
225  bool IsSet() const;
226 
227  const ON_2iPoint Min() const;
228  const ON_2iPoint Max() const;
229 
230 public:
231  ON_2iPoint m_min;
232  ON_2iPoint m_max;
233 };
234 
235 ON_DECL
236 bool operator==(const ON_2iBoundingBox&, const ON_2iBoundingBox&);
237 
238 ON_DECL
239 bool operator!=(const ON_2iBoundingBox&, const ON_2iBoundingBox&);
240 
241 /*
242 Class ON_2iSize
243  For those situations where a Windows SDK SIZE or MFC CSize
244  value needs to be used in code that does not link with MFC.
245 */
246 class ON_CLASS ON_2iSize
247 {
248 public:
249  // Default construction intentionally leaves x and y uninitialized.
250  // Use something like
251  // ON_2iSize pt(1,2);
252  // or
253  // ON_2iSize pt = ON_2iSize::Zero;
254  // when you need an initialized ON_2iSize.
255  ON_2iSize() = default;
256 
257  ~ON_2iSize() = default;
258  ON_2iSize(const ON_2iSize& ) = default;
259  ON_2iSize& operator=(const ON_2iSize& ) = default;
260 
261  ON_2iSize(
262  int cx,
263  int cy
264  );
265 
266  /*
267  Dictionary compare.
268  Returns:
269  -1: lhs < rhs
270  0: lsh == rsh
271  +1: lhs > rhs
272  */
273  static int Compare(
274  const ON_2iSize& lhs,
275  const ON_2iSize& rhs
276  );
277 
278  /*
279  Dictionary compare.
280  Returns:
281  -1: lhs < rhs
282  0: lsh == rsh
283  +1: lhs > rhs
284  */
285  static int ComparePointer(
286  const ON_2iSize* lhs,
287  const ON_2iSize* rhs
288  );
289 
290 public:
291  static const ON_2iSize Zero; // (0,0)
292  static const ON_2iSize Unset; // (ON_UNSET_INT_INDEX,ON_UNSET_INT_INDEX)
293 
294 public:
295  /*
296  Returns:
297  true if both cx and cy are 0.
298  */
299  bool IsZero() const;
300 
301  /*
302  Returns:
303  true if neither cx nor cy are ON_UNSET_INT_INDEX.
304  */
305  bool IsSet() const;
306 
307 public:
308  ON__INT32 cx;
309  ON__INT32 cy;
310 };
311 
312 ON_DECL
313 bool operator==(
314  const ON_2iSize& lhs,
315  const ON_2iSize& rhs
316  );
317 
318 ON_DECL
319 bool operator!=(
320  const ON_2iSize& lhs,
321  const ON_2iSize& rhs
322  );
323 
324 #if defined(ON_DLL_TEMPLATE)
325 
326 ON_DLL_TEMPLATE template class ON_CLASS ON_SimpleArray<ON_2iSize>;
327 
328 #endif
329 
330 /*
331 Class ON_4iRect
332  For those situations where a Windows SDK RECT or MFC CRect
333  value needs to be used in code that does not link with MFC.
334  If you want a traditional bounding box, use ON_2dBoundingBox.
335 */
336 class ON_CLASS ON_4iRect
337 {
338 public:
339  // Default construction intentionally leaves x and y uninitialized.
340  // Use something like
341  // ON_4iRect pt(1,2,3,4);
342  // or
343  // ON_4iRect pt = ON_4iRect::Zero;
344  // when you need an initialized ON_4iRect.
345  ON_4iRect() = default;
346 
347  ~ON_4iRect() = default;
348  ON_4iRect(const ON_4iRect& ) = default;
349  ON_4iRect& operator=(const ON_4iRect& ) = default;
352  int left,
353  int top,
354  int right,
355  int bottom
356  );
357 
358  ON_4iRect(const ON_2iPoint topLeft, const ON_2iPoint& bottomRight);
359  ON_4iRect(const ON_2iPoint& point, const ON_2iSize& size);
360 
361 public:
362  static const ON_4iRect Zero; // (0,0,0,0)
363  static const ON_4iRect Unset; // (ON_UNSET_INT_INDEX,ON_UNSET_INT_INDEX,ON_UNSET_INT_INDEX,ON_UNSET_INT_INDEX)
364 
365 public:
366  /*
367  Returns:
368  true if all of left, top, right, and bottom are set to 0.
369  */
370  bool IsZero() const;
371 
372  void SetZero();
373 
374  /*
375  Returns:
376  true if none of left, top, right, or bottom is set to ON_UNSET_INT_INDEX
377  */
378  bool IsSet() const;
379 
380  int Width(void) const;
381  int Height(void) const;
382 
383  const ON_2iSize Size(void) const;
384 
385  const ON_2iPoint CenterPoint(void) const;
386  const ON_2iPoint TopLeft(void) const;
387  const ON_2iPoint BottomRight(void) const;
388 
389  bool IntersectRect(const ON_4iRect* r1, const ON_4iRect* r2);
390  bool IntersectRect(const ON_4iRect& r1, const ON_4iRect& r2);
391 
392  bool IsRectEmpty(void) const;
393  bool IsRectNull(void) const;
394  void SetRectEmpty(void) { *this = Zero; }
395  void SetRect(int l, int t, int r, int b);
396 
397  bool PtInRect(const ON_2iPoint& pt) const;
398 
399  void OffsetRect(int, int);
400  void OffsetRect(const ON_2iVector&);
401  void InflateRect(int, int);
402  void InflateRect(int, int, int, int);
403  void DeflateRect(int, int);
404  bool SubtractRect(const ON_4iRect* rect1, const ON_4iRect* rect2);
406  void NormalizeRect();
407 
408 public:
409  // NOTE WELL:
410  // Windows 2d integer device coordinates have a
411  // strong y-down bias and it is common for top < bottom.
412  // General 2d bounding boxes have a strong lower < upper / min < max bias.
413  // Take care when converting between ON_2iBoundingBox and ON_4iRect.
414  // It is intentional that no automatic conversion between bounding box
415  // and ON_4iRect is supplied because each case must be carefully considered.
416  ON__INT32 left;
417  ON__INT32 top;
418  ON__INT32 right;
419  ON__INT32 bottom;
420 };
421 
422 #if defined(ON_DLL_TEMPLATE)
423 ON_DLL_TEMPLATE template class ON_CLASS ON_SimpleArray<ON_4iRect>;
424 #endif
425 
426 
427 ON_DECL
428 bool operator==(const ON_4iRect&, const ON_4iRect&);
429 
430 ON_DECL
431 bool operator!=(const ON_4iRect&, const ON_4iRect&);
432 
433 #endif
Definition: opennurbs_ipoint.h:24
static const ON_2iPoint Origin
Definition: opennurbs_ipoint.h:45
Definition: opennurbs_ipoint.h:236
static const ON_2iPoint Unset
Definition: opennurbs_ipoint.h:46
Definition: opennurbs_array.h:36
Definition: opennurbs_ipoint.h:324
Definition: opennurbs_ipoint.h:99
Definition: opennurbs_ipoint.h:188