-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBBox.h
More file actions
87 lines (72 loc) · 1.83 KB
/
BBox.h
File metadata and controls
87 lines (72 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
//
// Created by 孙万捷 on 16/5/16.
//
#ifndef SUNPATHTRACER_BBOX_H
#define SUNPATHTRACER_BBOX_H
#include <glm/glm.hpp>
class BBox
{
public:
BBox()
{
bmin = glm::vec3(FLT_MAX);
bmax = glm::vec3(-FLT_MAX);
bcenter = glm::vec3(0.f);
}
BBox(const glm::vec3& _min, const glm::vec3& _max)
{
bmin = _min;
bmax = _max;
bcenter = (bmin + bmax) * 0.5f;
}
// initialize BBox with triangle vertices (bounding box of a triangle)
BBox(const glm::vec3& v1, const glm::vec3& v2, const glm::vec3& v3)
{
bmin = glm::vec3(FLT_MAX);
bmax = glm::vec3(-FLT_MAX);
bcenter = glm::vec3(0.f);
bmin = glm::min(bmin, v1);
bmin = glm::min(bmin, v2);
bmin = glm::min(bmin, v3);
bmin -= 1e-6f;
bmax = glm::max(bmax, v1);
bmax = glm::max(bmax, v2);
bmax = glm::max(bmax, v3);
bmax += 1e-6f;
bcenter = (bmin + bmax) * 0.5f;
}
BBox(const BBox& box)
{
this->bmin = box.bmin;
this->bmax = box.bmax;
this->bcenter = box.bcenter;
}
int MaxExtent()
{
glm::vec3 diag = bmax - bmin;
if((diag.x > diag.y) && (diag.x > diag.z))
return 0;
else if(diag.y > diag.z)
return 1;
else
return 2;
}
float SurfaceArea()
{
glm::vec3 extent = bmax - bmin;
return (extent.x * extent.y + extent.x * extent.z + extent.y * extent.z) * 2.f;
}
public:
glm::vec3 bmax;
glm::vec3 bmin;
glm::vec3 bcenter;
};
inline BBox Union(const BBox& b, const glm::vec3& v)
{
return BBox(glm::min(b.bmin, v), glm::max(b.bmax, v));
}
inline BBox Union(const BBox& b1, const BBox& b2)
{
return BBox(glm::min(b1.bmin, b2.bmin), glm::max(b1.bmax, b2.bmax));
}
#endif //SUNPATHTRACER_BBOX_H