MCPcopy Create free account
hub / github.com/PacktPublishing/3D-Graphics-Rendering-Cookbook / BoundingBox

Class BoundingBox

shared/UtilsMath.h:19–67  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

17}
18
19struct BoundingBox
20{
21 vec3 min_;
22 vec3 max_;
23 BoundingBox() = default;
24 BoundingBox(const vec3& min, const vec3& max) : min_(glm::min(min, max)), max_(glm::max(min, max)) {}
25 BoundingBox(const vec3* points, size_t numPoints)
26 {
27 vec3 vmin(std::numeric_limits<float>::max());
28 vec3 vmax(std::numeric_limits<float>::lowest());
29
30 for (size_t i = 0; i != numPoints; i++)
31 {
32 vmin = glm::min(vmin, points[i]);
33 vmax = glm::max(vmax, points[i]);
34 }
35 min_ = vmin;
36 max_ = vmax;
37 }
38 vec3 getSize() const { return vec3(max_[0] - min_[0], max_[1] - min_[1], max_[2] - min_[2]); }
39 vec3 getCenter() const { return 0.5f * vec3(max_[0] + min_[0], max_[1] + min_[1], max_[2] + min_[2]); }
40 void transform(const glm::mat4& t)
41 {
42 vec3 corners[] = {
43 vec3(min_.x, min_.y, min_.z),
44 vec3(min_.x, max_.y, min_.z),
45 vec3(min_.x, min_.y, max_.z),
46 vec3(min_.x, max_.y, max_.z),
47 vec3(max_.x, min_.y, min_.z),
48 vec3(max_.x, max_.y, min_.z),
49 vec3(max_.x, min_.y, max_.z),
50 vec3(max_.x, max_.y, max_.z),
51 };
52 for (auto& v : corners)
53 v = vec3(t * vec4(v, 1.0f));
54 *this = BoundingBox(corners, 8);
55 }
56 BoundingBox getTransformed(const glm::mat4& t) const
57 {
58 BoundingBox b = *this;
59 b.transform(t);
60 return b;
61 }
62 void combinePoint(const vec3& p)
63 {
64 min_ = glm::min(min_, p);
65 max_ = glm::max(max_, p);
66 }
67};
68
69template <typename T>
70T clamp(T v, T a, T b)

Callers 2

transformMethod · 0.85
combineBoxesFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected