1 | /* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ |
---|
2 | /* |
---|
3 | * Copyright (c) 2004-2013 HUBzero Foundation, LLC |
---|
4 | * |
---|
5 | * Author: Insoo Woo <iwoo@purdue.edu> |
---|
6 | */ |
---|
7 | |
---|
8 | #include <float.h> |
---|
9 | |
---|
10 | #include <vrmath/BBox.h> |
---|
11 | #include <vrmath/Matrix4x4d.h> |
---|
12 | #include <vrmath/Vector3f.h> |
---|
13 | #include <vrmath/Vector4f.h> |
---|
14 | |
---|
15 | using namespace vrmath; |
---|
16 | |
---|
17 | BBox::BBox() |
---|
18 | { |
---|
19 | makeEmpty(); |
---|
20 | } |
---|
21 | |
---|
22 | BBox::BBox(const BBox& bbox) |
---|
23 | { |
---|
24 | min = bbox.min; |
---|
25 | max = bbox.max; |
---|
26 | } |
---|
27 | |
---|
28 | BBox::BBox(const Vector3f& minv, const Vector3f& maxv) |
---|
29 | { |
---|
30 | min = minv; |
---|
31 | max = maxv; |
---|
32 | } |
---|
33 | |
---|
34 | void BBox::makeEmpty() |
---|
35 | { |
---|
36 | min.set(FLT_MAX, FLT_MAX, FLT_MAX); |
---|
37 | max.set(-FLT_MAX, -FLT_MAX, -FLT_MAX); |
---|
38 | } |
---|
39 | |
---|
40 | bool BBox::isEmpty() |
---|
41 | { |
---|
42 | if ((min.x > max.x) || (min.y > max.y) || (min.z > max.z)) { |
---|
43 | return true; |
---|
44 | } |
---|
45 | |
---|
46 | return false; |
---|
47 | } |
---|
48 | |
---|
49 | void BBox::make(const Vector3f& center, const Vector3f& size) |
---|
50 | { |
---|
51 | float halfX = size.x * 0.5f, halfY = size.y * 0.5f, halfZ = size.z * 0.5f; |
---|
52 | |
---|
53 | min.x = center.x - halfX; |
---|
54 | max.x = center.x + halfX; |
---|
55 | |
---|
56 | min.y = center.y - halfY; |
---|
57 | max.y = center.y + halfY; |
---|
58 | |
---|
59 | min.z = center.z - halfZ; |
---|
60 | max.z = center.z + halfZ; |
---|
61 | } |
---|
62 | |
---|
63 | void BBox::extend(const Vector3f& point) |
---|
64 | { |
---|
65 | if (min.x > point.x) { |
---|
66 | min.x = point.x; |
---|
67 | } else if (max.x < point.x) { |
---|
68 | max.x = point.x; |
---|
69 | } |
---|
70 | |
---|
71 | if (min.y > point.y) { |
---|
72 | min.y = point.y; |
---|
73 | } else if (max.y < point.y) { |
---|
74 | max.y = point.y; |
---|
75 | } |
---|
76 | |
---|
77 | if (min.z > point.z) { |
---|
78 | min.z = point.z; |
---|
79 | } else if (max.z < point.z) { |
---|
80 | max.z = point.z; |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | void BBox::extend(const BBox& box) |
---|
85 | { |
---|
86 | if (min.x > box.min.x) min.x = box.min.x; |
---|
87 | if (min.y > box.min.y) min.y = box.min.y; |
---|
88 | if (min.z > box.min.z) min.z = box.min.z; |
---|
89 | |
---|
90 | if (max.x < box.max.x) max.x = box.max.x; |
---|
91 | if (max.y < box.max.y) max.y = box.max.y; |
---|
92 | if (max.z < box.max.z) max.z = box.max.z; |
---|
93 | } |
---|
94 | |
---|
95 | void BBox::transform(const BBox& box, const Matrix4x4d& mat) |
---|
96 | { |
---|
97 | float halfSizeX = (box.max.x - box.min.x) * 0.5f; |
---|
98 | float halfSizeY = (box.max.y - box.min.y) * 0.5f; |
---|
99 | float halfSizeZ = (box.max.z - box.min.z) * 0.5f; |
---|
100 | |
---|
101 | float centerX = (box.max.x + box.min.x) * 0.5f; |
---|
102 | float centerY = (box.max.y + box.min.y) * 0.5f; |
---|
103 | float centerZ = (box.max.z + box.min.z) * 0.5f; |
---|
104 | |
---|
105 | Vector4f points[8]; |
---|
106 | |
---|
107 | points[0].set(centerX + halfSizeX, centerY + halfSizeY, centerZ + halfSizeZ, 1); |
---|
108 | points[1].set(centerX + halfSizeX, centerY + halfSizeY, centerZ - halfSizeZ, 1); |
---|
109 | points[2].set(centerX - halfSizeX, centerY + halfSizeY, centerZ - halfSizeZ, 1); |
---|
110 | points[3].set(centerX - halfSizeX, centerY + halfSizeY, centerZ + halfSizeZ, 1); |
---|
111 | points[4].set(centerX - halfSizeX, centerY - halfSizeY, centerZ + halfSizeZ, 1); |
---|
112 | points[5].set(centerX - halfSizeX, centerY - halfSizeY, centerZ - halfSizeZ, 1); |
---|
113 | points[6].set(centerX + halfSizeX, centerY - halfSizeY, centerZ - halfSizeZ, 1); |
---|
114 | points[7].set(centerX + halfSizeX, centerY - halfSizeY, centerZ + halfSizeZ, 1); |
---|
115 | |
---|
116 | float minX, minY, minZ; |
---|
117 | float maxX, maxY, maxZ; |
---|
118 | |
---|
119 | points[0] = mat.transform(points[0]); |
---|
120 | |
---|
121 | minX = maxX = points[0].x; |
---|
122 | minY = maxY = points[0].y; |
---|
123 | minZ = maxZ = points[0].z; |
---|
124 | |
---|
125 | for (int i = 1; i < 8; i++) { |
---|
126 | points[i] = mat.transform(points[i]); |
---|
127 | |
---|
128 | if (points[i].x > maxX) maxX = points[i].x; |
---|
129 | else if (points[i].x < minX) minX = points[i].x; |
---|
130 | |
---|
131 | if (points[i].y > maxY) maxY = points[i].y; |
---|
132 | else if (points[i].y < minY) minY = points[i].y; |
---|
133 | |
---|
134 | if (points[i].z > maxZ) maxZ = points[i].z; |
---|
135 | else if (points[i].z < minZ) minZ = points[i].z; |
---|
136 | } |
---|
137 | |
---|
138 | min.set(minX, minY, minZ); |
---|
139 | max.set(maxX, maxY, maxZ); |
---|
140 | } |
---|
141 | |
---|
142 | bool BBox::intersect(const BBox& box) |
---|
143 | { |
---|
144 | if (min.x > box.max.x || max.x < box.min.x) return false; |
---|
145 | if (min.y > box.max.y || max.y < box.min.y) return false; |
---|
146 | if (min.z > box.max.z || max.z < box.min.z) return false; |
---|
147 | |
---|
148 | return true; |
---|
149 | } |
---|
150 | |
---|
151 | bool BBox::intersect(const Vector3f& point) |
---|
152 | { |
---|
153 | if ((point.x < min.x) || (point.x > max.x)) return false; |
---|
154 | if ((point.y < min.y) || (point.y > max.y)) return false; |
---|
155 | if ((point.z < min.z) || (point.z > max.z)) return false; |
---|
156 | |
---|
157 | return true; |
---|
158 | } |
---|