Euphoria
aabb.cc
Go to the documentation of this file.
1 #include "base/aabb.h"
2 
3 #include "assert/assert.h"
4 #include "base/numeric.h"
5 #include "base/range.h"
6 #include "base/random.h"
7 
8 namespace eu
9 {
10  Aabb::Aabb(const vec3f& amin, const vec3f& amax) : min(amin), max(amax)
11  {
12  ASSERT(is_valid());
13  }
14 
15 
16  vec3f
17  Aabb::wrap(const vec3f& vec) const
18  {
19  ASSERT(is_valid());
20 #define COMP(C) \
21  const auto C = ::eu::wrap(make_range(min.C, max.C), vec.C)
22  COMP(x);
23  COMP(y);
24  COMP(z);
25 #undef COMP
26  return vec3f {x, y, z};
27  }
28 
29 
30  void
31  Aabb::extend(const vec3f& vec)
32  {
33  ASSERT(is_valid());
34  min = eu::min(min, vec);
35  max = eu::max(max, vec);
36  }
37 
38 
39  void
40  Aabb::extend(const Aabb& aabb)
41  {
42  ASSERT(is_valid());
43  min = eu::min(min, aabb.min);
44  max = eu::max(max, aabb.max);
45  }
46 
47 
48  Aabb
50  {
51  return Aabb {zero3f, zero3f};
52  }
53 
54 
55  vec3f
57  {
58  ASSERT(is_valid());
59  return vec3f::from_to(min, max);
60  }
61 
62 
63  bool
65  {
66 #define M(var) min.var <= max.var
67  return M(x) && M(y) && M(z);
68 #undef M
69  }
70 
71 
72  vec3f
74  {
75  ASSERT(is_valid());
76  return min;
77  }
78 
79 
80  void
81  Aabb::offset(const vec3f& vec)
82  {
83  min += vec;
84  max += vec;
85  }
86 
87 
88  Aabb
89  Aabb::offset_copy(const vec3f& vec) const
90  {
91  auto self = *this;
92  self.offset(vec);
93  return self;
94  }
95 
96 
97  [[nodiscard]] std::vector<vec3f>
99  {
100  return
101  {
102  {min.x, min.y, min.z}, {max.x, min.y, min.z},
103  {min.x, min.y, max.z}, {max.x, min.y, max.z},
104  {min.x, max.y, min.z}, {max.x, max.y, min.z},
105  {min.x, max.y, max.z}, {max.x, max.y, max.z},
106  };
107  }
108 
109  std::string
110  to_string(const Aabb& a)
111  {
112  return fmt::format("{{ {}, {} }}", a.min, a.max);
113  }
114 
115  vec3f
116  get_random_point(Random* rand, const Aabb& a)
117  {
118  const auto x = get_random_in_range(rand, make_range(a.min.x, a.max.x));
119  const auto y = get_random_in_range(rand, make_range(a.min.y, a.max.y));
120  const auto z = get_random_in_range(rand, make_range(a.min.z, a.max.z));
121 
122  return vec3f {x, y, z};
123  }
124 }
#define M(var)
#define COMP(C)
#define ASSERT(x)
Definition: assert.h:29
Definition: assert.h:90
Range< T > make_range(T min, T max)
Definition: range.h:39
T get_random_in_range(Random *rand, const Range< T > &range)
Definition: random.h:50
std::string to_string(const Aabb &a)
Definition: aabb.cc:110
size2f min(const size2f lhs, const size2f rhs)
Definition: size2.cc:140
size2f max(const size2f lhs, const size2f rhs)
Definition: size2.cc:149
constexpr vec3f zero3f
Definition: vec3.h:95
vec3f get_random_point(Random *rand, const Aabb &a)
Definition: aabb.cc:116
Definition: aabb.h:15
Aabb(const vec3f &amin, const vec3f &amax)
Definition: aabb.cc:10
Aabb offset_copy(const vec3f &vec) const
Definition: aabb.cc:89
vec3f get_size() const
Definition: aabb.cc:56
std::vector< vec3f > calc_all_corners() const
Definition: aabb.cc:98
bool is_valid() const
Definition: aabb.cc:64
vec3f min
Definition: aabb.h:16
vec3f get_offset() const
Definition: aabb.cc:73
static Aabb create_empty()
Definition: aabb.cc:49
vec3f wrap(const vec3f &vec) const
Definition: aabb.cc:17
void extend(const vec3f &vec)
Definition: aabb.cc:31
void offset(const vec3f &vec)
Definition: aabb.cc:81
vec3f max
Definition: aabb.h:17
WEL512 Random Number Generator.
Definition: random.h:21
Definition: vec3.h:48
float x
Definition: vec3.h:49
float y
Definition: vec3.h:50
static vec3f from_to(const vec3f &from, const vec3f &to)
Definition: vec3.cc:127
float z
Definition: vec3.h:51