gwnavruntime/math/box3f.h Source File

box3f.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 Box3f
17 {
18  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
19 public:
20  // ------------------------------ Functions -----------------------------
21 
22  Box3f() { Clear(); }
23  Box3f(const Vec3f& min_, const Vec3f& max_) : m_min(min_), m_max(max_) {}
24  Box3f(const Vec3f& center, KyFloat32 extent) : m_min(center), m_max(center) { Enlarge(extent); }
25 
26  bool operator==(const Box3f& other) const { return m_min == other.m_min && m_max == other.m_max; }
27  bool operator!=(const Box3f& other) const { return !operator==(other); }
28 
30  void Clear()
31  {
34  }
35 
36  bool IsValid() { return m_min <= m_max; }
37 
38  void Set(const Vec3f& min_, const Vec3f& max_)
39  {
40  m_min = min_;
41  m_max = max_;
42  }
43 
44  void Set(const Vec3f& center, KyFloat32 halfSize)
45  {
46  m_min = center;
47  m_max = center;
48  Enlarge(halfSize);
49  }
50 
51  KyFloat32 SizeX() const { return m_max.x - m_min.x; }
52  KyFloat32 SizeY() const { return m_max.y - m_min.y; }
53  KyFloat32 SizeZ() const { return m_max.z - m_min.z; }
54 
55  Vec3f Center() const { return (m_min + m_max) * 0.5f; }
56 
57  KyFloat32 BoundingSphereRadius() const { return Vec3f(SizeX(), SizeY(), SizeZ()).Length() * 0.5f; }
58 
59  void Translate(const Vec3f& v) { m_min += v; m_max += v; }
60 
61  Box3f GetTranslated(const Vec3f& v) const { return Box3f(m_min + v, m_max + v); }
62 
63  void ExpandByTriangle(const Triangle3f& triangle)
64  {
65  ExpandByPos(triangle.A);
66  ExpandByPos(triangle.B);
67  ExpandByPos(triangle.C);
68  }
69 
70  void ExpandByPos(const Vec3f& pos)
71  {
72  m_min.x = Kaim::Min(m_min.x, pos.x);
73  m_min.y = Kaim::Min(m_min.y, pos.y);
74  m_min.z = Kaim::Min(m_min.z, pos.z);
75  m_max.x = Kaim::Max(m_max.x, pos.x);
76  m_max.y = Kaim::Max(m_max.y, pos.y);
77  m_max.z = Kaim::Max(m_max.z, pos.z);
78  }
79 
80  void ExpandByBox(const Box3f& box)
81  {
82  m_min.x = Kaim::Min(m_min.x, box.m_min.x);
83  m_min.y = Kaim::Min(m_min.y, box.m_min.y);
84  m_min.z = Kaim::Min(m_min.z, box.m_min.z);
85  m_max.x = Kaim::Max(m_max.x, box.m_max.x);
86  m_max.y = Kaim::Max(m_max.y, box.m_max.y);
87  m_max.z = Kaim::Max(m_max.z, box.m_max.z);
88  }
89 
90  void Enlarge(KyFloat32 enlargement)
91  {
92  Vec3f e(enlargement, enlargement, enlargement);
93  m_min -= e;
94  m_max += e;
95  }
96 
97  bool DoesContain(const Vec2f& P) const { return LessEq(m_min.x, P.x, m_max.x) && LessEq(m_min.y, P.y, m_max.y); }
98  bool DoesContainStrictly(const Vec2f& P) const { return Less(m_min.x, P.x, m_max.x) && Less(m_min.y, P.y, m_max.y); }
99 
100  bool DoesContain(const Vec3f& P) const { return LessEq(m_min.x, P.x, m_max.x) && LessEq(m_min.y, P.y, m_max.y) && LessEq(m_min.z, P.z, m_max.z); }
101  bool DoesContainStrictly(const Vec3f& P) const { return Less(m_min.x, P.x, m_max.x) && Less(m_min.y, P.y, m_max.y) && Less(m_min.z, P.z, m_max.z); }
102 
103  // ------------------------------ Data -----------------------------
104 
105  Vec3f m_min;
106  Vec3f m_max;
107 };
108 
109 inline void SwapEndianness(Endianness::Target e, Box3f& self)
110 {
111  SwapEndianness(e, self.m_min);
112  SwapEndianness(e, self.m_max);
113 }
114 
115 template <class OSTREAM>
116 inline OSTREAM& operator<<(OSTREAM& os, const Box3f& self)
117 {
118  os << "{" << self.m_min << "," << self.m_max << "}";
119  return os;
120 }
121 
122 }
123 
124 
125 
#define KyFloat32MAXVAL
KyFloat32 max value
Definition: types.h:71
void Set(KyFloat32 _x, KyFloat32 _y, KyFloat32 _z)
Sets {_x, _y, _z}.
Definition: vec3f.h:29
void Clear()
Sets { {+infinite, +infinite, +infinite}, {-infinite, -infinite, -infinite} }.
Definition: box3f.h:30
KyFloat32 SizeZ() const
Returns m_max.z - m_min.z.
Definition: box3f.h:53
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:132
Box3f()
Set { {+infinite, +infinite, +infinite}, {-infinite, -infinite, -infinite} }.
Definition: box3f.h:22
Target
Enumerates the possible endianness types relative to the current platform.
Definition: endianness.h:27
3d axis aligned box of 32bits floating points
Definition: box3f.h:16
KyFloat32 SizeY() const
Returns m_max.y - m_min.y.
Definition: box3f.h:52
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17
KyFloat32 SizeX() const
Returns m_max.x - m_min.x.
Definition: box3f.h:51
float KyFloat32
float
Definition: types.h:32
3d vector using 32bits floating points.
Definition: vec3f.h:16