SBoundingBox3.h
Go to the documentation of this file.
1 
2 #pragma once
3 
4 #include "SVector3.h"
5 #include "SLine3.h"
6 
7 
10 template <typename T, typename Vector = vec3<T> >
12 {
13 
15 
16 public:
17 
19 
21  {}
22 
23  SBoundingBox3(Vector const & min, Vector const & max)
24  : MinCorner(min), MaxCorner(max)
25  {}
26 
27  explicit SBoundingBox3(Vector const & v)
28  : MinCorner(v), MaxCorner(v)
29  {}
30 
31  Vector GetExtent() const
32  {
33  return MaxCorner - MinCorner;
34  }
35 
36  Vector GetHalfExtent() const
37  {
38  return (MaxCorner - MinCorner) / 2;
39  }
40 
41  Vector GetCenter() const
42  {
43  return (MaxCorner + MinCorner) / 2;
44  }
45 
46  bool IsPointInside(Vector const & p) const
47  {
48  return
49  p.X >= MinCorner.X && p.X <= MaxCorner.X &&
50  p.Y >= MinCorner.Y && p.Y <= MaxCorner.Y &&
51  p.Z >= MinCorner.Z && p.Z <= MaxCorner.Z;
52  }
53 
54  bool Intersects(Type const & r) const
55  {
56  return (MaxCorner.Y >= r.MinCorner.Y &&
57  MinCorner.Y <= r.MaxCorner.Y &&
58  MaxCorner.X >= r.MinCorner.X &&
59  MinCorner.X <= r.MaxCorner.X &&
60  MaxCorner.Z >= r.MinCorner.Z &&
61  MinCorner.Z <= r.MaxCorner.Z);
62  }
63 
64  void AddInternalPoint(Vector const & v)
65  {
66  if (v.X > MaxCorner.X)
67  MaxCorner.X = v.X;
68  if (v.Y > MaxCorner.Y)
69  MaxCorner.Y = v.Y;
70  if (v.Z > MaxCorner.Z)
71  MaxCorner.Z = v.Z;
72 
73  if (v.X < MinCorner.X)
74  MinCorner.X = v.X;
75  if (v.Y < MinCorner.Y)
76  MinCorner.Y = v.Y;
77  if (v.Z < MinCorner.Z)
78  MinCorner.Z = v.Z;
79  }
80 
81  void AddInternalBox(Type const & bb)
82  {
85  }
86 
87  bool IntersectsWithRay(vec3f const & Origin, vec3f const & Direction, vec3f & coord) const
88  {
89  T tmin = -std::numeric_limits<T>::max();
90  T tmax = std::numeric_limits<T>::max();
91 
92  for (int i = 0; i < 3; ++ i)
93  {
94  if (Direction[i] == 0)
95  {
96  if (Origin[i] <= MinCorner[i] || Origin[i] >= MaxCorner[i])
97  {
98  return false;
99  }
100  }
101  else
102  {
103  T t1 = (MinCorner[i] - Origin[i]) / Direction[i];
104  T t2 = (MaxCorner[i] - Origin[i]) / Direction[i];
105 
106  if (t1 > t2)
107  {
108  std::swap(t1, t2);
109  }
110 
111  tmin = Max(t1, tmin);
112  tmax = Min(t2, tmax);
113 
114  if (tmin > tmax)
115  {
116  return false;
117  }
118  if (tmax < 0)
119  {
120  return false;
121  }
122  }
123  }
124 
125  if ((tmin > std::numeric_limits<T>::max()) || (tmax < 0))
126  {
127  return false;
128  }
129 
130  if (tmin > 0)
131  {
132  coord = Origin + Direction * tmin;
133  }
134  else
135  {
136  coord = Origin + Direction * tmax;
137  }
138  return true;
139  }
140 
141  T const GetMaximumRadius(Vector const Scale) const
142  {
143  Vector const Extents = GetExtent() / 2;
144  return max((Extents*Scale).xy().length(), (Extents*Scale).xz().length(), (Extents*Scale).yz().length());
145  }
146 
147  Vector const GetCorner(int const i) const
148  {
149  switch (i)
150  {
151  default:
152  case 0:
153  return Vector(MinCorner.X, MinCorner.Y, MinCorner.Z);
154  case 1:
155  return Vector(MinCorner.X, MinCorner.Y, MaxCorner.Z);
156  case 2:
157  return Vector(MinCorner.X, MaxCorner.Y, MinCorner.Z);
158  case 3:
159  return Vector(MinCorner.X, MaxCorner.Y, MaxCorner.Z);
160  case 4:
161  return Vector(MaxCorner.X, MinCorner.Y, MinCorner.Z);
162  case 5:
163  return Vector(MaxCorner.X, MinCorner.Y, MaxCorner.Z);
164  case 6:
165  return Vector(MaxCorner.X, MaxCorner.Y, MinCorner.Z);
166  case 7:
167  return Vector(MaxCorner.X, MaxCorner.Y, MaxCorner.Z);
168  }
169  }
170 
171  void MoveTo(Vector const & Center)
172  {
173  Move(Center - GetCenter());
174  }
175 
176  void Move(Vector const & Offset)
177  {
178  MinCorner += Offset;
179  MaxCorner += Offset;
180  }
181 
182  void Resize(Vector const & Size)
183  {
184  Vector const Center = GetCenter();
185  MinCorner = Center - Size / 2;
186  MaxCorner = Center + Size / 2;
187  }
188 
189  void Reset(Vector const & Point)
190  {
191  MinCorner = MaxCorner = Point;
192  }
193 
194  void Transform(glm::mat4 const & Transformation)
195  {
196  std::vector<Vector> Vertices;
197  for (int i = 0; i < 8; ++ i)
198  Vertices.push_back(GetCorner(i));
199 
200  Reset(Vertices[0].GetTransformed(Transformation));
201  for (int i = 0; i < 8; ++ i)
202  AddInternalPoint(Vertices[i].GetTransformed(Transformation));
203  }
204 
205 };
206 
207 template <typename T>
209 
210 template <typename T>
212 
213 template <typename T>
215 
219 
bool Intersects(Type const &r) const
Definition: SBoundingBox3.h:54
void Transform(glm::mat4 const &Transformation)
Definition: SBoundingBox3.h:194
Vector GetHalfExtent() const
Definition: SBoundingBox3.h:36
SBoundingBox3(Vector const &v)
Definition: SBoundingBox3.h:27
void AddInternalBox(Type const &bb)
Definition: SBoundingBox3.h:81
T const GetMaximumRadius(Vector const Scale) const
Definition: SBoundingBox3.h:141
bool IsPointInside(Vector const &p) const
Definition: SBoundingBox3.h:46
void Reset(Vector const &Point)
Definition: SBoundingBox3.h:189
SBoundingBox3()
Definition: SBoundingBox3.h:20
bool IntersectsWithRay(vec3f const &Origin, vec3f const &Direction, vec3f &coord) const
Definition: SBoundingBox3.h:87
void AddInternalPoint(Vector const &v)
Definition: SBoundingBox3.h:64
Vector GetExtent() const
Definition: SBoundingBox3.h:31
Vector MaxCorner
Definition: SBoundingBox3.h:18
Vector const GetCorner(int const i) const
Definition: SBoundingBox3.h:147
3D axis-aligned bounding box
Definition: SBoundingBox3.h:11
SBoundingBox3< s32 > SBoundingBox3i
Definition: SBoundingBox3.h:218
SBoundingBox3< f32 > SBoundingBox3f
Definition: SBoundingBox3.h:216
SBoundingBox3< f64 > box3d
Definition: SBoundingBox3.h:221
SBoundingBox3(Vector const &min, Vector const &max)
Definition: SBoundingBox3.h:23
void Move(Vector const &Offset)
Definition: SBoundingBox3.h:176
SBoundingBox3< s32 > box3i
Definition: SBoundingBox3.h:222
Vector MinCorner
Definition: SBoundingBox3.h:18
SBoundingBox3< f64 > SBoundingBox3d
Definition: SBoundingBox3.h:217
SBoundingBox3< f32 > box3f
Definition: SBoundingBox3.h:220
Vector GetCenter() const
Definition: SBoundingBox3.h:41
void MoveTo(Vector const &Center)
Definition: SBoundingBox3.h:171
void Resize(Vector const &Size)
Definition: SBoundingBox3.h:182