ai_bbox.h
Go to the documentation of this file.
1// Copyright 2023 Autodesk, Inc. All rights reserved.
2//
3// Use of this software is subject to the terms of the Autodesk license
4// agreement provided at the time of installation or download, or which
5// otherwise accompanies this software in either electronic or hard copy form.
6
12#pragma once
13#include "ai_vector.h"
14#include "ai_api.h"
15
16#include <limits>
17
32struct AtBBox
33{
34 AtBBox() = default;
35
36 AI_DEVICE AtBBox(const AtVector& mn, const AtVector& mx)
37 {
38 min = mn;
39 max = mx;
40 }
41
45 AI_DEVICE AtBBox(const AtVector& p0, const AtVector& p1, const AtVector& p2)
46 {
47 min = max = p0;
48 min = AiV3Min(min, p1);
49 max = AiV3Max(max, p1);
50 min = AiV3Min(min, p2);
51 max = AiV3Max(max, p2);
52 }
53
57 static AI_DEVICE AtBBox empty()
58 {
59 constexpr float bound = std::numeric_limits<float>::infinity();
60 return {{bound, bound, bound}, {-bound, -bound, -bound}};
61 }
62
66 static AI_DEVICE AtBBox infinity()
67 {
68 constexpr float bound = std::numeric_limits<float>::infinity();
69 return {{-bound, -bound, -bound}, {bound, bound, bound}};
70 }
71
75 AI_DEVICE void addSlack(float slack)
76 {
77 min -= slack;
78 max += slack;
79 }
80
84 void init()
85 {
86 const float bound = std::numeric_limits<float>::infinity();
87 min = AtVector( bound, bound, bound);
88 max = AtVector(-bound, -bound, -bound);
89 }
90
94 void expand(const AtVector& v)
95 {
96 min = AiV3Min(min, v);
97 max = AiV3Max(max, v);
98 }
99
103 void expand(const AtBBox& b2)
104 {
105 min = AiV3Min(min, b2.min);
106 max = AiV3Max(max, b2.max);
107 }
108
112 AI_DEVICE bool inside(const AtVector& p) const
113 {
114 return AiAll(p >= min) && AiAll(p <= max);
115 }
116
120 float volume() const
121 {
122 return (max.x - min.x) * (max.y - min.y) * (max.z - min.z);
123 }
124
128 bool isEmpty() const
129 {
130 return AiAny(min > max);
131 }
132
136 float halfArea() const
137 {
138 const AtVector diag = max - min;
139 return diag.x * (diag.y + diag.z) + diag.y * diag.z;
140 }
141
145 float area() const
146 {
147 return halfArea() * 2;
148 }
149
153 AI_DEVICE AtVector center() const
154 {
155 return (max + min) * 0.5f;
156 }
157
158 AtVector min, max;
159};
160
167inline AtBBox AiBBoxUnion(const AtBBox& b1, const AtBBox& b2)
168{
169 return AtBBox(AiV3Min(b1.min, b2.min),
170 AiV3Max(b1.max, b2.max));
171}
172
176inline AtBBox AiBBoxIntersection(const AtBBox& b1, const AtBBox& b2)
177{
178 return AtBBox(AiV3Max(b1.min, b2.min),
179 AiV3Min(b1.max, b2.max));
180}
181
186inline AtBBox AiBBoxLerp(float k, const AtBBox& lo, const AtBBox& hi)
187{
188 return AtBBox(AiV3Lerp(k, lo.min, hi.min),
189 AiV3Lerp(k, lo.max, hi.max));
190}
191
196{
197 AtBBox2() = default;
198
199 constexpr AtBBox2(int min_x, int min_y, int max_x, int max_y) :
200 minx(min_x), miny(min_y), maxx(max_x), maxy(max_y) {}
201
205 constexpr int AiBBox2Area() const
206 {
207 return (maxx - minx + 1) * (maxy - miny + 1);
208 }
209
210 AI_DEVICE constexpr bool operator==(const AtBBox2& bb) const
211 {
212 return (minx == bb.minx && miny == bb.miny &&
213 maxx == bb.maxx && maxy == bb.maxy);
214 }
215
216 AI_DEVICE constexpr bool operator!=(const AtBBox2& bb) const
217 {
218 return !(*this == bb);
219 }
220
221 int minx, miny, maxx, maxy;
222};
223
224
228static const AtBBox AI_BBOX_ZERO(AI_V3_ZERO, AI_V3_ZERO);
229/*\}*/
230
231/*\}*/
DLL export prefix for API functions (necessary for multi-platform development)
Vector math types, operators and utilities.
AtBBox AiBBoxIntersection(const AtBBox &b1, const AtBBox &b2)
Compute the intersection of two bboxes.
Definition: ai_bbox.h:176
AI_DEVICE bool inside(const AtVector &p) const
Check to see if the specified point is inside the bbox.
Definition: ai_bbox.h:112
static AI_DEVICE AtBBox infinity()
Convenience method returning infinitely large box.
Definition: ai_bbox.h:66
AtBBox AiBBoxLerp(float k, const AtBBox &lo, const AtBBox &hi)
Linear interpolation between two bboxes (k=0 -> bbox=lo, k=1 -> bbox=hi)
Definition: ai_bbox.h:186
AI_DEVICE void addSlack(float slack)
Expand a bounding box with some safety slack volume.
Definition: ai_bbox.h:75
float volume() const
Compute the volume of a bbox.
Definition: ai_bbox.h:120
AI_DEVICE AtVector center() const
Compute the center of a bbox.
Definition: ai_bbox.h:153
constexpr int AiBBox2Area() const
Compute the area (# of pixels) of an integer bbox.
Definition: ai_bbox.h:205
AtBBox AiBBoxUnion(const AtBBox &b1, const AtBBox &b2)
Compute the "union" of two bboxes.
Definition: ai_bbox.h:167
void expand(const AtBBox &b2)
Expand a bounding box with another bounding box.
Definition: ai_bbox.h:103
static AI_DEVICE AtBBox empty()
Convenience method returning empty box.
Definition: ai_bbox.h:57
bool isEmpty() const
Returns whether or not the specified box is empty.
Definition: ai_bbox.h:128
float area() const
Compute the surface area of a bbox.
Definition: ai_bbox.h:145
void expand(const AtVector &v)
Expand a bounding box with a point.
Definition: ai_bbox.h:94
float halfArea() const
Compute half the surface area of a bbox.
Definition: ai_bbox.h:136
void init()
Initialize a bounding box to be empty.
Definition: ai_bbox.h:84
AI_DEVICE constexpr AtVector AiV3Min(const AtVector &a, const AtVector &b)
Minimum of two vectors, component-wise.
Definition: ai_vector.h:696
AI_DEVICE constexpr AtVector AiV3Max(const AtVector &a, const AtVector &b)
Maximum of two vectors, component-wise.
Definition: ai_vector.h:706
AI_DEVICE constexpr AtVector AiV3Lerp(float t, const AtVector &lo, const AtVector &hi)
3D vector linear interpolation (t=0 -> result=lo, t=1 -> result=hi)
Definition: ai_vector.h:678
2D axis-aligned bounding box (uses integers)
Definition: ai_bbox.h:196
3D axis-aligned bounding box (uses single-precision)
Definition: ai_bbox.h:33
3D point (single precision)
Definition: ai_vector.h:30

© 2023 Autodesk, Inc. · All rights reserved · www.arnoldrenderer.com