GA::kit v0.3
G&A's in-house C++ application framework
Loading...
Searching...
No Matches
bounds_component.h
Go to the documentation of this file.
1#pragma once
3#include "ga/math.h"
4#include <vector>
5
6namespace ga {
7
8// 3D axis aligned bounding box
9class Bounds : public Component
10{
11public:
12 vec3 min = vec3( 0.f );
13 vec3 max = vec3( 0.f );
14
15 Bounds() {}
16 Bounds( vec2 min_, vec2 max_ )
17 : min( min_, 0. )
18 , max( max_, 0. )
19 {
20 }
21 Bounds( vec3 min_, vec3 max_ )
22 : min( min_ )
23 , max( max_ )
24 {
25 }
26 Bounds( const Rect& rect )
27 : min( rect.min(), 0 )
28 , max( rect.max(), 0 )
29 {
30 }
31 Bounds( const std::vector<vec3>& points )
32 {
33 min = vec3( std::numeric_limits<float>::max() );
34 max = vec3( std::numeric_limits<float>::min() );
35 for ( auto& pt : points ) {
36 include( pt );
37 }
38 }
39
40 inline Bounds& include( vec3 pt )
41 {
42 for ( int i = 0; i < 3; ++i ) {
43 min[i] = std::min( pt[i], min[i] );
44 max[i] = std::max( pt[i], max[i] );
45 }
46 return *this;
47 }
48
49 inline Bounds& translate( const vec3& delta )
50 {
51 min += delta;
52 max += delta;
53 }
54
55 inline vec3 center() const
56 {
57 return min + ( max - min ) * .5f;
58 }
59
60 inline vec3 size() const
61 {
62 return max - min;
63 }
64
65 bool contains( const vec3& pos ) const
66 {
67 return glm::all( glm::lessThanEqual( min, pos ) ) && glm::all( glm::greaterThanEqual( max, pos ) );
68 }
69};
70} // namespace ga
Definition: bounds_component.h:10
Bounds & include(vec3 pt)
Definition: bounds_component.h:40
vec3 max
Definition: bounds_component.h:13
Bounds & translate(const vec3 &delta)
Definition: bounds_component.h:49
Bounds(vec2 min_, vec2 max_)
Definition: bounds_component.h:16
Bounds(const std::vector< vec3 > &points)
Definition: bounds_component.h:31
vec3 size() const
Definition: bounds_component.h:60
bool contains(const vec3 &pos) const
Definition: bounds_component.h:65
Bounds(vec3 min_, vec3 max_)
Definition: bounds_component.h:21
vec3 center() const
Definition: bounds_component.h:55
Bounds()
Definition: bounds_component.h:15
Bounds(const Rect &rect)
Definition: bounds_component.h:26
vec3 min
Definition: bounds_component.h:12
Definition: component.h:11
Definition: color.h:9
A 2D axis-aligned rectangle, with x,y (float) position and w,h (float) size components.
Definition: math.h:64