ai_bbox.h
1 /*
2  * Arnold API header file
3  * Copyright (c) 1998-2009 Marcos Fajardo, (c) 2009-2013 Solid Angle SL
4  */
5 
11 #pragma once
12 #include "ai_vector.h"
13 #include "ai_api.h"
14 
22 struct AtBBox
23 {
24  AtPoint min, max;
25 };
26 
30 struct AtBBox2
31 {
32  int minx, miny, maxx, maxy;
33 };
34 
35 
39 inline void AiBBoxAddSlack(AtBBox& bbox, float slack)
40 {
41  bbox.min -= slack;
42  bbox.max += slack;
43 }
44 
48 inline void AiBBoxInit(AtBBox& bbox, float bound)
49 {
50  AiV3Create(bbox.min, bound, bound, bound);
51  AiV3Create(bbox.max, -bound, -bound, -bound);
52 }
53 
57 inline void AiBBoxExpand(AtBBox& bbox, const AtVector& v)
58 {
59  bbox.min = AiV3Min(bbox.min, v);
60  bbox.max = AiV3Max(bbox.max, v);
61 }
62 
66 inline void AiBBoxTriangle(AtBBox& bbox, const AtPoint& p0, const AtPoint& p1, const AtPoint& p2)
67 {
68  bbox.min = bbox.max = p0;
69  bbox.min = AiV3Min(bbox.min, p1);
70  bbox.max = AiV3Max(bbox.max, p1);
71  bbox.min = AiV3Min(bbox.min, p2);
72  bbox.max = AiV3Max(bbox.max, p2);
73 }
74 
81 inline void AiBBoxUnion(AtBBox& bbox, const AtBBox& b1, const AtBBox& b2)
82 {
83  bbox.min = AiV3Min(b1.min, b2.min);
84  bbox.max = AiV3Max(b1.max, b2.max);
85 }
86 
90 inline void AiBBoxIntersection(AtBBox& bbox, const AtBBox& b1, const AtBBox& b2)
91 {
92  bbox.min = AiV3Max(b1.min, b2.min);
93  bbox.max = AiV3Min(b1.max, b2.max);
94 }
95 
99 inline bool AiBBoxInside(const AtBBox& bbox, const AtPoint& p)
100 {
101  return ((p.x >= bbox.min.x) && (p.y >= bbox.min.y) && (p.z >= bbox.min.z) &&
102  (p.x <= bbox.max.x) && (p.y <= bbox.max.y) && (p.z <= bbox.max.z));
103 }
104 
108 inline float AiBBoxVolume(const AtBBox& bbox)
109 {
110  return (bbox.max.x - bbox.min.x) *
111  (bbox.max.y - bbox.min.y) *
112  (bbox.max.z - bbox.min.z);
113 }
114 
118 inline bool AiBBoxIsEmpty(const AtBBox& bbox)
119 {
120  return (bbox.min.x > bbox.max.x) ||
121  (bbox.min.y > bbox.max.y) ||
122  (bbox.min.z > bbox.max.z);
123 }
124 
128 inline float AiBBoxArea(const AtBBox& bbox)
129 {
130  const AtPoint diag = bbox.max-bbox.min;
131  return (diag.x * (diag.y+diag.z) + diag.y*diag.z) * 2;
132 }
133 
137 inline float AiBBoxHalfArea(const AtBBox& bbox)
138 {
139  const AtPoint diag = bbox.max-bbox.min;
140  return diag.x * (diag.y+diag.z) + diag.y*diag.z;
141 }
142 
146 inline void AiBBoxCenter(const AtBBox& bbox, AtPoint& c)
147 {
148  c = (bbox.max + bbox.min) * 0.5f;
149 }
150 
155 inline void AiBBoxLerp(AtBBox& bbox, float k, const AtBBox& lo, const AtBBox& hi)
156 {
157  AiV3Lerp(bbox.min, k, lo.min, hi.min);
158  AiV3Lerp(bbox.max, k, lo.max, hi.max);
159 }
160 
164 inline int AiBBox2Area(const AtBBox2& bbox)
165 {
166  return (bbox.maxx - bbox.minx + 1) *
167  (bbox.maxy - bbox.miny + 1);
168 }
169 
170 
176 /*\}*/
177 
178 /*\}*/

© 2009-2013 Solid Angle SL · all rights reserved · www.solidangle.com