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