ai_bbox.h
Go to the documentation of this file.
1 // Copyright 2020 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 
32 struct 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 
67  AI_DEPRECATED void init(float bound)
68  {
69  min = AtVector( bound, bound, bound);
70  max = AtVector(-bound, -bound, -bound);
71  }
75  void init()
76  {
77  const float bound = std::numeric_limits<float>::infinity();
78  min = AtVector( bound, bound, bound);
79  max = AtVector(-bound, -bound, -bound);
80  }
81 
85  void expand(const AtVector& v)
86  {
87  min = AiV3Min(min, v);
88  max = AiV3Max(max, v);
89  }
90 
94  AI_DEVICE bool inside(const AtVector& p) const
95  {
96  return AiAll(p >= min) && AiAll(p <= max);
97  }
98 
102  float volume() const
103  {
104  return (max.x - min.x) * (max.y - min.y) * (max.z - min.z);
105  }
106 
110  bool isEmpty() const
111  {
112  return AiAny(min > max);
113  }
114 
118  float halfArea() const
119  {
120  const AtVector diag = max - min;
121  return diag.x * (diag.y + diag.z) + diag.y * diag.z;
122  }
123 
127  float area() const
128  {
129  return halfArea() * 2;
130  }
131 
135  AI_DEVICE AtVector center() const
136  {
137  return (max + min) * 0.5f;
138  }
139 
140  AtVector min, max;
141 };
142 
149 inline AtBBox AiBBoxUnion(const AtBBox& b1, const AtBBox& b2)
150 {
151  return AtBBox(AiV3Min(b1.min, b2.min),
152  AiV3Max(b1.max, b2.max));
153 }
154 
158 inline AtBBox AiBBoxIntersection(const AtBBox& b1, const AtBBox& b2)
159 {
160  return AtBBox(AiV3Max(b1.min, b2.min),
161  AiV3Min(b1.max, b2.max));
162 }
163 
168 inline AtBBox AiBBoxLerp(float k, const AtBBox& lo, const AtBBox& hi)
169 {
170  return AtBBox(AiV3Lerp(k, lo.min, hi.min),
171  AiV3Lerp(k, lo.max, hi.max));
172 }
173 
177 struct AtBBox2
178 {
179  AtBBox2() = default;
180 
181  constexpr AtBBox2(int min_x, int min_y, int max_x, int max_y) :
182  minx(min_x), miny(min_y), maxx(max_x), maxy(max_y) {}
183 
187  constexpr int AiBBox2Area() const
188  {
189  return (maxx - minx + 1) * (maxy - miny + 1);
190  }
191 
192  int minx, miny, maxx, maxy;
193 };
194 
195 
199 static const AtBBox AI_BBOX_ZERO(AI_V3_ZERO, AI_V3_ZERO);
200 /*\}*/
201 
202 /*\}*/
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
AI_DEPRECATED void init(float bound)
Initialize a bounding box to be empty.
Definition: ai_bbox.h:67
bool isEmpty() const
Returns whether or not the specified box is empty.
Definition: ai_bbox.h:110
AI_DEVICE AtBBox(const AtVector &p0, const AtVector &p1, const AtVector &p2)
Compute the bbox of a triangle.
Definition: ai_bbox.h:45
Vector math types, operators and utilities.
void init()
Initialize a bounding box to be empty.
Definition: ai_bbox.h:75
3D point (single precision)
Definition: ai_vector.h:29
constexpr int AiBBox2Area() const
Compute the area (# of pixels) of an integer bbox.
Definition: ai_bbox.h:187
AI_DEVICE constexpr AtVector AiV3Min(const AtVector &a, const AtVector &b)
Minimum of two vectors, component-wise.
Definition: ai_vector.h:696
DLL export prefix for API functions (necessary for multi-platform development)
float area() const
Compute the surface area of a bbox.
Definition: ai_bbox.h:127
AI_DEVICE AtVector center() const
Compute the center of a bbox.
Definition: ai_bbox.h:135
float volume() const
Compute the volume of a bbox.
Definition: ai_bbox.h:102
void expand(const AtVector &v)
Expand a bounding box with a point.
Definition: ai_bbox.h:85
2D axis-aligned bounding box (uses integers)
Definition: ai_bbox.h:177
AI_DEVICE void addSlack(float slack)
Expand a bounding box with some safety slack volume.
Definition: ai_bbox.h:57
AI_DEVICE constexpr AtVector AiV3Max(const AtVector &a, const AtVector &b)
Maximum of two vectors, component-wise.
Definition: ai_vector.h:706
AI_DEVICE bool inside(const AtVector &p) const
Check to see if the specified point is inside the bbox.
Definition: ai_bbox.h:94
float halfArea() const
Compute half the surface area of a bbox.
Definition: ai_bbox.h:118
3D axis-aligned bounding box (uses single-precision)
Definition: ai_bbox.h:32
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:168
AtBBox AiBBoxUnion(const AtBBox &b1, const AtBBox &b2)
Compute the "union" of two bboxes.
Definition: ai_bbox.h:149
AtBBox AiBBoxIntersection(const AtBBox &b1, const AtBBox &b2)
Compute the intersection of two bboxes.
Definition: ai_bbox.h:158

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