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