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