OWL
Loading...
Searching...
No Matches
box.h
1// ======================================================================== //
2// Copyright 2018-2019 Ingo Wald //
3// //
4// Licensed under the Apache License, Version 2.0 (the "License"); //
5// you may not use this file except in compliance with the License. //
6// You may obtain a copy of the License at //
7// //
8// http://www.apache.org/licenses/LICENSE-2.0 //
9// //
10// Unless required by applicable law or agreed to in writing, software //
11// distributed under the License is distributed on an "AS IS" BASIS, //
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
13// See the License for the specific language governing permissions and //
14// limitations under the License. //
15// ======================================================================== //
16
17#pragma once
18
19#include "vec.h"
20
21namespace owl {
22 namespace common {
23
24 template<typename T>
25 struct interval {
26 typedef T scalar_t;
27 inline __both__ interval()
28 : lower(owl::common::empty_bounds_lower<T>()),
29 upper(owl::common::empty_bounds_upper<T>())
30 {}
31 inline __both__ interval(T begin, T end) : begin(begin), end(end) {}
32
33 union {
34 T begin;
35 T lower;
36 T lo;
37 };
38 union {
39 T end;
40 T upper;
41 T hi;
42 };
43
44 inline __both__ bool contains(const T &t) const { return t >= lower && t <= upper; }
45 inline __both__ bool is_empty() const { return begin > end; }
46 inline __both__ bool empty() const { return begin > end; }
47 inline __both__ T center() const { return (begin+end)/2; }
48 inline __both__ T span() const { return end - begin; }
49 inline __both__ T diagonal() const { return end - begin; }
50 inline __both__ interval<T> &extend(const T &t)
51 { lower = min(lower,t); upper = max(upper,t); return *this; }
52 inline __both__ interval<T> &extend(const interval<T> &t)
53 { lower = min(lower,t.lower); upper = max(upper,t.upper); return *this; }
54 inline __both__ interval<T> including(const T &t)
55 { return interval<T>(min(lower,t),max(upper,t)); }
56
57 static inline __both__ interval<T> positive()
58 {
59 return interval<T>(0.f,owl::common::open_range_upper<T>());
60 }
61 };
62
63 template<typename T>
64 inline __both__ std::ostream &operator<<(std::ostream &o, const interval<T> &b)
65 {
66#ifndef __CUDA_ARCH__
67 o << "[" << b.lower << ":" << b.upper << "]";
68#endif
69 return o;
70 }
71
72 template<typename T>
73 inline __both__ interval<T> build_interval(const T &a, const T &b)
74 { return interval<T>(min(a,b),max(a,b)); }
75
76 template<typename T>
77 inline __both__ interval<T> intersect(const interval<T> &a, const interval<T> &b)
78 { return interval<T>(max(a.lower,b.lower),min(a.upper,b.upper)); }
79
80 template<typename T>
81 inline __both__ interval<T> operator-(const interval<T> &a, const T &b)
82 { return interval<T>(a.lower-b,a.upper-b); }
83
84 template<typename T>
85 inline __both__ interval<T> operator*(const interval<T> &a, const T &b)
86 { return build_interval<T>(a.lower*b,a.upper*b); }
87
88 template<typename T>
89 inline __both__ bool operator==(const interval<T> &a, const interval<T> &b)
90 { return a.lower == b.lower && a.upper == b.upper; }
91
92 template<typename T>
93 inline __both__ bool operator!=(const interval<T> &a, const interval<T> &b)
94 { return !(a == b); }
95
96
97
98 template<typename T>
99 struct box_t {
100 typedef T vec_t;
101 typedef typename T::scalar_t scalar_t;
102 enum { dims = T::dims };
103
104 inline __both__ box_t()
105 : lower(owl::common::empty_bounds_lower<typename T::scalar_t>()),
106 upper(owl::common::empty_bounds_upper<typename T::scalar_t>())
107 {}
108
109 // /*! construct a new, origin-oriented box of given size */
110 // explicit inline __both__ box_t(const vec_t &box_size)
111 // : lower(vec_t(0)),
112 // upper(box_size)
113 // {}
115 explicit inline __both__ box_t(const vec_t &v)
116 : lower(v),
117 upper(v)
118 {}
119
121 inline __both__ box_t(const vec_t &lo, const vec_t &hi)
122 : lower(lo),
123 upper(hi)
124 {}
125
127 inline __both__ box_t including(const vec_t &other) const
128 { return box_t(min(lower,other),max(upper,other)); }
130 inline __both__ box_t including(const box_t &other) const
131 { return box_t(min(lower,other.lower),max(upper,other.upper)); }
132
133
135 inline __both__ box_t &extend(const vec_t &other)
136 { lower = min(lower,other); upper = max(upper,other); return *this; }
138 inline __both__ box_t &extend(const box_t &other)
139 { lower = min(lower,other.lower); upper = max(upper,other.upper); return *this; }
140
141
143 inline __both__ interval<scalar_t> get_slab(const uint32_t dim)
144 {
145 return interval<scalar_t>(lower[dim],upper[dim]);
146 }
147
148 inline __both__ bool contains(const vec_t &point) const
149 { return !(any_less_than(point,lower) || any_greater_than(point,upper)); }
150
151 inline __both__ bool overlaps(const box_t &other) const
152 { return !(any_less_than(other.upper,lower) || any_greater_than(other.lower,upper)); }
153
154 inline __both__ vec_t center() const { return (lower+upper)/(typename vec_t::scalar_t)2; }
155 inline __both__ vec_t span() const { return upper-lower; }
156 inline __both__ vec_t size() const { return upper-lower; }
157
158 inline __both__ typename long_type_of<typename T::scalar_t>::type volume() const
159 { return owl::common::volume(size()); }
160
161 inline __both__ bool empty() const { return any_less_than(upper,lower); }
162
163 vec_t lower, upper;
164 };
165
166 // =======================================================
167 // default functions
168 // =======================================================
169
170 template<typename T>
171 inline __both__ typename long_type_of<T>::type area(const box_t<vec_t<T,2>> &b)
172 { return area(b.upper - b.lower); }
173
174 template<typename T>
175 inline __both__ typename long_type_of<T>::type area(const box_t<vec_t<T,3>> &b)
176 {
177 const vec_t<T,3> diag = b.upper - b.lower;
178 return T(2)*(area(vec_t<T,2>(diag.x,diag.y))+
179 area(vec_t<T,2>(diag.y,diag.z))+
180 area(vec_t<T,2>(diag.z,diag.x)));
181 }
182
183 template<typename T>
184 inline __both__ typename long_type_of<T>::type volume(const box_t<vec_t<T,3>> &b)
185 {
186 const vec_t<T,3> diag = b.upper - b.lower;
187 return diag.x*diag.y*diag.z;
188 }
189
190 template<typename T>
191 inline __both__ std::ostream &operator<<(std::ostream &o, const box_t<T> &b)
192 {
193#ifndef __CUDA_ARCH__
194 o << "[" << b.lower << ":" << b.upper << "]";
195#endif
196 return o;
197 }
198
199 template<typename T>
200 inline __both__ box_t<T> intersection(const box_t<T> &a, const box_t<T> &b)
201 { return box_t<T>(max(a.lower,b.lower),min(a.upper,b.upper)); }
202
203 template<typename T>
204 inline __both__ bool operator==(const box_t<T> &a, const box_t<T> &b)
205 { return a.lower == b.lower && a.upper == b.upper; }
206
207 template<typename T>
208 inline __both__ bool operator!=(const box_t<T> &a, const box_t<T> &b)
209 { return !(a == b); }
210
211
212
213
214 // =======================================================
215 // default instantiations
216 // =======================================================
217
218#define _define_box_types(T,t) \
219 typedef box_t<vec_t<T,2>> box2##t; \
220 typedef box_t<vec_t<T,3>> box3##t; \
221 typedef box_t<vec_t<T,4>> box4##t; \
222 typedef box_t<vec3a_t<T>> box3##t##a; \
223
224 _define_box_types(bool ,b);
225 _define_box_types(int8_t ,c);
226 _define_box_types(int16_t ,s);
227 _define_box_types(int32_t ,i);
228 _define_box_types(int64_t ,l);
229 _define_box_types(uint8_t ,uc);
230 _define_box_types(uint16_t,us);
231 _define_box_types(uint32_t,ui);
232 _define_box_types(uint64_t,ul);
233 _define_box_types(float,f);
234 _define_box_types(double,d);
235
236#undef _define_box_types
237
238 } // ::owl::common
239} // ::owl
Definition: box.h:99
__both__ box_t & extend(const vec_t &other)
Definition: box.h:135
__both__ box_t(const vec_t &lo, const vec_t &hi)
Definition: box.h:121
__both__ box_t(const vec_t &v)
Definition: box.h:115
__both__ interval< scalar_t > get_slab(const uint32_t dim)
Definition: box.h:143
__both__ box_t including(const vec_t &other) const
Definition: box.h:127
__both__ box_t including(const box_t &other) const
Definition: box.h:130
__both__ box_t & extend(const box_t &other)
Definition: box.h:138
Definition: box.h:25
Definition: vec.h:33