gwnavruntime/math/box2ll.h Source File

box2ll.h
Go to the documentation of this file.
1 /*
2 * Copyright 2016 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 #pragma once
8 
11 
12 namespace Kaim
13 {
14 
16 class Box2LL
17 {
18  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
19 public:
20  // ------------------------------ Functions -----------------------------
21 
22  Box2LL() { Clear(); }
23  Box2LL(const Vec2LL& min_, const Vec2LL& max_) : m_min(min_), m_max(max_) {}
24  Box2LL(KyInt64 min_x, KyInt64 min_y, KyInt64 max_x, KyInt64 max_y) : m_min(min_x, min_y), m_max(max_x, max_y) {}
25 
26  bool operator==(const Box2LL& other) const { return m_min == other.m_min && m_max == other.m_max; }
27  bool operator!=(const Box2LL& other) const { return !operator==(other); }
28 
30  void Clear()
31  {
32  m_min.Set(KyInt64MAXVAL, KyInt64MAXVAL);
33  m_max.Set(KyInt64MINVAL, KyInt64MINVAL);
34  }
35 
36  bool IsValid() const { return CountX() >= 0LL && CountY() >= 0LL; }
37 
38  void MakeZero() { m_min.Set(0, 0); m_max.Set(0, 0); }
39 
40  void Set(const Vec2LL& min_, const Vec2LL& max_) { m_min = min_; m_max = max_; }
41  void SetMin(const Vec2LL& min_) { m_min = min_; }
42  void SetMax(const Vec2LL& max_) { m_max = max_; }
43 
44  const Vec2LL& Min() const { return m_min; }
45  const Vec2LL& Max() const { return m_max; }
46 
47  KyInt64 CountX() const { return m_max.x - m_min.x + 1; }
48  KyInt64 CountY() const { return m_max.y - m_min.y + 1; }
49 
50  bool DoesContain(const Vec2LL& pos) const { return pos.x >= m_min.x && pos.x <= m_max.x && pos.y >= m_min.y && pos.y <= m_max.y; }
51 
52  bool DoesStrictlyContain(const Vec2LL& pos) const { return pos.x > m_min.x && pos.x < m_max.x && pos.y > m_min.y && pos.y < m_max.y; }
53 
54  void ExpandByPos(const Vec2LL& pos)
55  {
56  m_min.x = Kaim::Min(m_min.x, pos.x);
57  m_max.x = Kaim::Max(m_max.x, pos.x);
58  m_min.y = Kaim::Min(m_min.y, pos.y);
59  m_max.y = Kaim::Max(m_max.y, pos.y);
60  }
61 
62  void ExpandByBox(const Box2LL& box)
63  {
64  m_min.x = Kaim::Min(m_min.x, box.m_min.x);
65  m_max.x = Kaim::Max(m_max.x, box.m_max.x);
66  m_min.y = Kaim::Min(m_min.y, box.m_min.y);
67  m_max.y = Kaim::Max(m_max.y, box.m_max.y);
68  }
69 
70  void Enlarge(KyInt64 enlargement)
71  {
72  Vec2LL e(enlargement, enlargement);
73  m_min -= e;
74  m_max += e;
75  }
76 
77  void GetEnlarged(KyInt64 enlargement, Box2LL& enlarged) const
78  {
79  enlarged = *this;
80  enlarged.Enlarge(enlargement);
81  }
82 
84  bool IntersectWith(const Box2LL& box)
85  {
86  m_min.x = Kaim::Max(m_min.x, box.m_min.x);
87  m_max.x = Kaim::Min(m_max.x, box.m_max.x);
88  m_min.y = Kaim::Max(m_min.y, box.m_min.y);
89  m_max.y = Kaim::Min(m_max.y, box.m_max.y);
90  return IsValid();
91  }
92 
94  bool SetAsIntersection(const Box2LL& box_1, const Box2LL& box_2)
95  {
96  m_min.x = Kaim::Max(box_1.m_min.x, box_2.m_min.x);
97  m_max.x = Kaim::Min(box_1.m_max.x, box_2.m_max.x);
98  m_min.y = Kaim::Max(box_1.m_min.y, box_2.m_min.y);
99  m_max.y = Kaim::Min(box_1.m_max.y, box_2.m_max.y);
100  return IsValid();
101  }
102 
103  // ------------------------------ Data -----------------------------
104 
105  Vec2LL m_min;
106  Vec2LL m_max;
107 };
108 
109 template <class OSTREAM>
110 inline OSTREAM& operator<<(OSTREAM& os, const Box2LL& self)
111 {
112  os << "{" << self.Min() << "," << self.Max() << "}";
113  return os;
114 }
115 
116 }
117 
118 
119 
120 
121 
bool IntersectWith(const Box2LL &box)
Set itself as its intersection with box, return true if itself and box do intersect.
Definition: box2ll.h:84
Box2LL()
Set { {+infinite, +infinite}, {-infinite, -infinite} }.
Definition: box2ll.h:22
2d vector using KyInt64
Definition: vec2ll.h:18
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:132
std::int64_t KyInt64
int64_t
Definition: types.h:25
void Clear()
Set { {+infinite, +infinite}, {-infinite, -infinite} }.
Definition: box2ll.h:30
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17
#define KyInt64MINVAL
KyInt64 min value
Definition: types.h:64
#define KyInt64MAXVAL
KyInt64 max value
Definition: types.h:63
bool SetAsIntersection(const Box2LL &box_1, const Box2LL &box_2)
Set itself as the intersection of box_1 and box_2, return true if box_1 and box_2 do intersect...
Definition: box2ll.h:94
2d axis aligned box of 64bits integers. Does not maintain m_countXY = m_max - m_min as a member...
Definition: box2ll.h:16