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 AI_DEVICE void addSlack(float slack)
58 {
59 min -= slack;
60 max += slack;
61 }
62
66 void init()
67 {
68 const float bound = std::numeric_limits<float>::infinity();
69 min = AtVector( bound, bound, bound);
70 max = AtVector(-bound, -bound, -bound);
71 }
72
76 void expand(const AtVector& v)
77 {
78 min = AiV3Min(min, v);
79 max = AiV3Max(max, v);
80 }
81
85 AI_DEVICE bool inside(const AtVector& p) const
86 {
87 return AiAll(p >= min) && AiAll(p <= max);
88 }
89
93 float volume() const
94 {
95 return (max.x - min.x) * (max.y - min.y) * (max.z - min.z);
96 }
97
101 bool isEmpty() const
102 {
103 return AiAny(min > max);
104 }
105
109 float halfArea() const
110 {
111 const AtVector diag = max - min;
112 return diag.x * (diag.y + diag.z) + diag.y * diag.z;
113 }
114
118 float area() const
119 {
120 return halfArea() * 2;
121 }
122
126 AI_DEVICE AtVector center() const
127 {
128 return (max + min) * 0.5f;
129 }
130
131 AtVector min, max;
132};
133
140inline AtBBox AiBBoxUnion(const AtBBox& b1, const AtBBox& b2)
141{
142 return AtBBox(AiV3Min(b1.min, b2.min),
143 AiV3Max(b1.max, b2.max));
144}
145
149inline AtBBox AiBBoxIntersection(const AtBBox& b1, const AtBBox& b2)
150{
151 return AtBBox(AiV3Max(b1.min, b2.min),
152 AiV3Min(b1.max, b2.max));
153}
154
159inline AtBBox AiBBoxLerp(float k, const AtBBox& lo, const AtBBox& hi)
160{
161 return AtBBox(AiV3Lerp(k, lo.min, hi.min),
162 AiV3Lerp(k, lo.max, hi.max));
163}
164
169{
170 AtBBox2() = default;
171
172 constexpr AtBBox2(int min_x, int min_y, int max_x, int max_y) :
173 minx(min_x), miny(min_y), maxx(max_x), maxy(max_y) {}
174
178 constexpr int AiBBox2Area() const
179 {
180 return (maxx - minx + 1) * (maxy - miny + 1);
181 }
182
183 int minx, miny, maxx, maxy;
184};
185
186
190static const AtBBox AI_BBOX_ZERO(AI_V3_ZERO, AI_V3_ZERO);
191/*\}*/
192
193/*\}*/
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:149
AI_DEVICE bool inside(const AtVector &p) const
Check to see if the specified point is inside the bbox.
Definition: ai_bbox.h:85
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:159
AI_DEVICE void addSlack(float slack)
Expand a bounding box with some safety slack volume.
Definition: ai_bbox.h:57
float volume() const
Compute the volume of a bbox.
Definition: ai_bbox.h:93
AI_DEVICE AtVector center() const
Compute the center of a bbox.
Definition: ai_bbox.h:126
constexpr int AiBBox2Area() const
Compute the area (# of pixels) of an integer bbox.
Definition: ai_bbox.h:178
AtBBox AiBBoxUnion(const AtBBox &b1, const AtBBox &b2)
Compute the "union" of two bboxes.
Definition: ai_bbox.h:140
bool isEmpty() const
Returns whether or not the specified box is empty.
Definition: ai_bbox.h:101
float area() const
Compute the surface area of a bbox.
Definition: ai_bbox.h:118
void expand(const AtVector &v)
Expand a bounding box with a point.
Definition: ai_bbox.h:76
float halfArea() const
Compute half the surface area of a bbox.
Definition: ai_bbox.h:109
void init()
Initialize a bounding box to be empty.
Definition: ai_bbox.h:66
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:169
3D axis-aligned bounding box (uses single-precision)
Definition: ai_bbox.h:33
3D point (single precision)
Definition: ai_vector.h:30

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