gwnavruntime/math/box2f.h Source File

box2f.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 
10 
11 namespace Kaim
12 {
13 
15 class Box2f
16 {
17  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
18 public:
19  // ------------------------------ Functions -----------------------------
20 
21  Box2f() { Clear(); }
22  Box2f(const Vec2f& _min, const Vec2f& _max) : m_min(_min), m_max(_max) {}
23  Box2f(KyFloat32 min_x, KyFloat32 min_y, KyFloat32 max_x, KyFloat32 max_y) : m_min(min_x, min_y), m_max(max_x, max_y) {}
24 
25  bool operator==(const Box2f& other) const { return m_min == other.m_min && m_max == other.m_max; }
26  bool operator!=(const Box2f& other) const { return !operator==(other); }
27 
29  void Clear()
30  {
31  m_min.Set(KyFloat32MAXVAL , KyFloat32MAXVAL);
32  m_max.Set(-KyFloat32MAXVAL, -KyFloat32MAXVAL);
33  }
34 
35  bool IsValid() const { return m_min <= m_max; }
36 
37  void Set(const Vec2f& min_, const Vec2f& max_)
38  {
39  m_min = min_;
40  m_max = max_;
41  }
42 
43  void Set(KyFloat32 min_x, KyFloat32 min_y, KyFloat32 max_x, KyFloat32 max_y)
44  {
45  m_min.x = min_x;
46  m_min.y = min_y;
47  m_max.x = max_x;
48  m_max.y = max_y;
49  }
50 
51  const Vec2f& Min() const { return m_min; }
52  const Vec2f& Max() const { return m_max; }
53 
54  KyFloat32 SizeX() { return m_max.x - m_min.x; }
55  KyFloat32 SizeY() { return m_max.y - m_min.y; }
56 
57  template<typename T>
58  void ExpandByPos(const T& pos)
59  {
60  m_min.x = Kaim::Min(m_min.x, pos.x);
61  m_min.y = Kaim::Min(m_min.y, pos.y);
62  m_max.x = Kaim::Max(m_max.x, pos.x);
63  m_max.y = Kaim::Max(m_max.y, pos.y);
64  }
65 
66  template<typename T>
67  void ExpandByBox(const T& box)
68  {
69  m_min.x = Kaim::Min(m_min.x, box.m_min.x);
70  m_max.x = Kaim::Max(m_max.x, box.m_max.x);
71  m_min.y = Kaim::Min(m_min.y, box.m_min.y);
72  m_max.y = Kaim::Max(m_max.y, box.m_max.y);
73  }
74 
75  void Enlarge(KyFloat32 enlargement)
76  {
77  Vec2f e(enlargement, enlargement);
78  m_min -= e;
79  m_max += e;
80  }
81 
82  template <typename T>
83  bool DoesContain(const T& P) const { return P.x >= m_min.x && P.x <= m_max.x && P.y >= m_min.y && P.y <= m_max.y; }
84 
85  template <typename T>
86  bool DoesContainStrictly(const T& P) const { return P.x > m_min.x && P.x < m_max.x && P.y > m_min.y && P.y < m_max.y; }
87 
88  // ------------------------------ Data -----------------------------
89 
90  Vec2f m_min;
91  Vec2f m_max;
92 };
93 
94 inline void SwapEndianness(Endianness::Target e, Box2f& self)
95 {
96  SwapEndianness(e, self.m_min);
97  SwapEndianness(e, self.m_max);
98 }
99 
100 template <class OSTREAM>
101 inline OSTREAM& operator<<(OSTREAM& os, const Box2f& self)
102 {
103  os << "{" << self.Min() << "," << self.Max() << "}";
104  return os;
105 }
106 
107 }
108 
109 
110 
2d axis aligned box of 32bits floating points
Definition: box2f.h:15
#define KyFloat32MAXVAL
KyFloat32 max value
Definition: types.h:71
Box2f()
Set { {+infinite, +infinite}, {-infinite, -infinite} }.
Definition: box2f.h:21
KyFloat32 SizeY()
Return m_max.y - m_min.y.
Definition: box2f.h:55
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:132
Target
Enumerates the possible endianness types relative to the current platform.
Definition: endianness.h:27
2d vector using KyFloat32.
Definition: vec2f.h:18
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17
void Clear()
Set { {+infinite, +infinite}, {-infinite, -infinite} }.
Definition: box2f.h:29
float KyFloat32
float
Definition: types.h:32
KyFloat32 SizeX()
Return m_max.x - m_min.x.
Definition: box2f.h:54