Blender V5.0
boundbox.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#pragma once
6
7#include <cfloat>
8#include <cmath>
9
10#include "util/math.h"
11#include "util/transform.h"
12#include "util/types.h"
13
15
16/* 3D BoundBox */
17
18class BoundBox {
19 public:
21
23
24 __forceinline BoundBox(const float3 &pt) : min(pt), max(pt) {}
25
26 __forceinline BoundBox(const float3 &min_, const float3 &max_) : min(min_), max(max_) {}
27
28 enum empty_t { empty = 0 };
29
34
35 __forceinline void grow(const float3 &pt)
36 {
37 /* the order of arguments to min is such that if pt is nan, it will not
38 * influence the resulting bounding box */
39 min = ccl::min(pt, min);
40 max = ccl::max(pt, max);
41 }
42
43 __forceinline void grow(const float3 &pt, const float border)
44 {
45 const float3 shift = make_float3(border, border, border);
46 min = ccl::min(pt - shift, min);
47 max = ccl::max(pt + shift, max);
48 }
49
50 __forceinline void grow(const BoundBox &bbox)
51 {
52 min = ccl::min(bbox.min, min);
53 max = ccl::max(bbox.max, max);
54 }
55
57 {
58 /* the order of arguments to min is such that if pt is nan, it will not
59 * influence the resulting bounding box */
60 if (isfinite(pt.x) && isfinite(pt.y) && isfinite(pt.z)) {
61 min = ccl::min(pt, min);
62 max = ccl::max(pt, max);
63 }
64 }
65
66 __forceinline void grow_safe(const float3 &pt, const float border)
67 {
68 if (isfinite(pt.x) && isfinite(pt.y) && isfinite(pt.z) && isfinite(border)) {
69 const float3 shift = make_float3(border, border, border);
70 min = ccl::min(pt - shift, min);
71 max = ccl::max(pt + shift, max);
72 }
73 }
74
76 {
77 if (isfinite_safe(bbox.min)) {
78 min = ccl::min(bbox.min, min);
79 }
80 if (isfinite_safe(bbox.max)) {
81 max = ccl::max(bbox.max, max);
82 }
83 }
84
86 {
87 min = ccl::max(min, bbox.min);
88 max = ccl::min(max, bbox.max);
89 }
90
91 /* todo: avoid using this */
93 {
94 if (!((min.x <= max.x) && (min.y <= max.y) && (min.z <= max.z))) {
95 return 0.0f;
96 }
97
98 return area();
99 }
100
101 __forceinline float area() const
102 {
103 return half_area() * 2.0f;
104 }
105
107 {
108 const float3 d = max - min;
109 return (d.x * d.z + d.y * d.z + d.x * d.y);
110 }
111
113 {
114 return 0.5f * (min + max);
115 }
116
118 {
119 return min + max;
120 }
121
123 {
124 return max - min;
125 }
126
127 __forceinline bool valid() const
128 {
129 return (min.x <= max.x) && (min.y <= max.y) && (min.z <= max.z) &&
130 (isfinite(min.x) && isfinite(min.y) && isfinite(min.z)) &&
131 (isfinite(max.x) && isfinite(max.y) && isfinite(max.z));
132 }
133
135 {
137
138 /* Avoid transforming empty bounding box since the code below performs per-corner transform
139 * which will convert non-valid bounds to valid. */
140 if (!valid()) {
141 return result;
142 }
143
144 for (int i = 0; i < 8; i++) {
145 float3 p;
146
147 p.x = (i & 1) ? min.x : max.x;
148 p.y = (i & 2) ? min.y : max.y;
149 p.z = (i & 4) ? min.z : max.z;
150
151 result.grow(transform_point(tfm, p));
152 }
153
154 return result;
155 }
156
158 {
159 const float3 center_diff = center() - other.center();
160 const float3 total_size = (size() + other.size()) * 0.5f;
161 return fabsf(center_diff.x) <= total_size.x && fabsf(center_diff.y) <= total_size.y &&
162 fabsf(center_diff.z) <= total_size.z;
163 }
164};
165
167{
168 return BoundBox(min(bbox.min, pt), max(bbox.max, pt));
169}
170
172{
173 return BoundBox(min(a.min, b.min), max(a.max, b.max));
174}
175
177 const BoundBox &b,
178 const BoundBox &c,
179 const BoundBox &d)
180{
181 return merge(merge(a, b), merge(c, d));
182}
183
185{
186 return BoundBox(max(a.min, b.min), min(a.max, b.max));
187}
188
190{
191 return intersect(a, intersect(b, c));
192}
193
194/* 2D BoundBox */
195
197 public:
198 float left = 0.0f;
199 float right = 1.0f;
200 float bottom = 0.0f;
201 float top = 1.0f;
202
203 BoundBox2D() = default;
204
205 BoundBox2D(const float2 radius)
206 : left(-radius.x), right(radius.x), bottom(-radius.y), top(radius.y)
207 {
208 }
209
210 bool operator==(const BoundBox2D &other) const
211 {
212 return (left == other.left && right == other.right && bottom == other.bottom &&
213 top == other.top);
214 }
215
216 float width()
217 {
218 return right - left;
219 }
220
221 float height()
222 {
223 return top - bottom;
224 }
225
226 BoundBox2D operator*(float f) const
227 {
229
230 result.left = left * f;
231 result.right = right * f;
232 result.bottom = bottom * f;
233 result.top = top * f;
234
235 return result;
236 }
237
238 BoundBox2D subset(const BoundBox2D &other) const
239 {
241
242 subset.left = left + other.left * (right - left);
243 subset.right = left + other.right * (right - left);
244 subset.bottom = bottom + other.bottom * (top - bottom);
245 subset.top = bottom + other.top * (top - bottom);
246
247 return subset;
248 }
249
251 {
253
254 result.left = ((left - other.left) / (other.right - other.left));
255 result.right = ((right - other.left) / (other.right - other.left));
256 result.bottom = ((bottom - other.bottom) / (other.top - other.bottom));
257 result.top = ((top - other.bottom) / (other.top - other.bottom));
258
259 return result;
260 }
261
262 BoundBox2D clamp(const float mn = 0.0f, const float mx = 1.0f)
263 {
265
266 result.left = ccl::clamp(left, mn, mx);
267 result.right = ccl::clamp(right, mn, mx);
268 result.bottom = ccl::clamp(bottom, mn, mx);
269 result.top = ccl::clamp(top, mn, mx);
270
271 return result;
272 }
273
275 {
277
278 result.left = left + offset.x;
279 result.right = right + offset.x;
280 result.bottom = bottom + offset.y;
281 result.top = top + offset.y;
282
283 return result;
284 }
285};
286
struct BoundBox BoundBox
__forceinline BoundBox merge(const BoundBox &bbox, const float3 &pt)
Definition boundbox.h:166
__forceinline BoundBox intersect(const BoundBox &a, const BoundBox &b)
Definition boundbox.h:184
BoundBox2D clamp(const float mn=0.0f, const float mx=1.0f)
Definition boundbox.h:262
BoundBox2D make_relative_to(const BoundBox2D &other) const
Definition boundbox.h:250
BoundBox2D(const float2 radius)
Definition boundbox.h:205
BoundBox2D offset(const float2 offset) const
Definition boundbox.h:274
float width()
Definition boundbox.h:216
BoundBox2D operator*(float f) const
Definition boundbox.h:226
float height()
Definition boundbox.h:221
float bottom
Definition boundbox.h:200
float top
Definition boundbox.h:201
float right
Definition boundbox.h:199
BoundBox2D subset(const BoundBox2D &other) const
Definition boundbox.h:238
bool operator==(const BoundBox2D &other) const
Definition boundbox.h:210
float left
Definition boundbox.h:198
BoundBox2D()=default
#define __forceinline
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
ccl_device_inline bool isfinite_safe(const float f)
Definition math_base.h:348
#define fabsf
#define min(a, b)
Definition sort.cc:36
#define FLT_MAX
Definition stdcycles.h:14
__forceinline void grow(const BoundBox &bbox)
Definition boundbox.h:50
__forceinline void grow_safe(const float3 &pt, const float border)
Definition boundbox.h:66
__forceinline void grow(const float3 &pt, const float border)
Definition boundbox.h:43
__forceinline float half_area() const
Definition boundbox.h:106
__forceinline BoundBox(empty_t)
Definition boundbox.h:30
__forceinline void grow_safe(const BoundBox &bbox)
Definition boundbox.h:75
__forceinline BoundBox(const float3 &pt)
Definition boundbox.h:24
BoundBox transformed(const Transform *tfm) const
Definition boundbox.h:134
__forceinline BoundBox()=default
__forceinline bool valid() const
Definition boundbox.h:127
__forceinline float3 center() const
Definition boundbox.h:112
__forceinline BoundBox(const float3 &min_, const float3 &max_)
Definition boundbox.h:26
float3 max
Definition boundbox.h:20
__forceinline float3 size() const
Definition boundbox.h:122
__forceinline void intersect(const BoundBox &bbox)
Definition boundbox.h:85
__forceinline void grow_safe(const float3 &pt)
Definition boundbox.h:56
__forceinline float safe_area() const
Definition boundbox.h:92
__forceinline bool intersects(const BoundBox &other)
Definition boundbox.h:157
__forceinline void grow(const float3 &pt)
Definition boundbox.h:35
__forceinline float area() const
Definition boundbox.h:101
float3 min
Definition boundbox.h:20
__forceinline float3 center2() const
Definition boundbox.h:117
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
i
Definition text_draw.cc:230
max
Definition text_draw.cc:251
ccl_device_inline float3 transform_point(const ccl_private Transform *t, const float3 a)
Definition transform.h:56