28 template<
typename T,
int N>
inline __both__
29 bool any_less_than(
const vec_t<T,N> &a,
const vec_t<T,N> &b);
31 template<
typename T,
int N>
inline __both__
32 bool all_less_than(
const vec_t<T,N> &a,
const vec_t<T,N> &b);
34 template<
typename T,
int N>
inline __both__
35 bool any_greater_or_equal(
const vec_t<T,N> &a,
const vec_t<T,N> &b);
37 template<
typename T,
int N>
inline __both__
38 bool any_less_than(
const vec_t<T,N> &a,
const vec_t<T,N> &b);
41 template<
typename T>
inline __both__
42 bool any_less_than(
const vec_t<T,2> &a,
const vec_t<T,2> &b)
43 {
return (a.x < b.x) | (a.y < b.y); }
45 template<
typename T>
inline __both__
46 bool all_less_than(
const vec_t<T,2> &a,
const vec_t<T,2> &b)
47 {
return (a.x < b.x) & (a.y < b.y); }
49 template<
typename T>
inline __both__
50 bool any_greater_than(
const vec_t<T,2> &a,
const vec_t<T,2> &b)
51 {
return (a.x > b.x) | (a.y > b.y); }
53 template<
typename T>
inline __both__
54 bool any_greater_or_equal(
const vec_t<T,2> &a,
const vec_t<T,2> &b)
55 {
return (a.x >= b.x) | (a.y >= b.y); }
58 template<
typename T>
inline __both__
59 bool any_less_than(
const vec_t<T,3> &a,
const vec_t<T,3> &b)
60 {
return (a.x < b.x) | (a.y < b.y) | (a.z < b.z); }
62 template<
typename T>
inline __both__
63 bool all_less_than(
const vec_t<T,3> &a,
const vec_t<T,3> &b)
64 {
return (a.x < b.x) & (a.y < b.y) & (a.z < b.z); }
66 template<
typename T>
inline __both__
67 bool any_greater_than(
const vec_t<T,3> &a,
const vec_t<T,3> &b)
68 {
return (a.x > b.x) | (a.y > b.y) | (a.z > b.z); }
70 template<
typename T>
inline __both__
71 bool any_greater_or_equal(
const vec_t<T,3> &a,
const vec_t<T,3> &b)
72 {
return (a.x >= b.x) | (a.y >= b.y) | (a.z >= b.z); }
75 template<
typename T>
inline __both__
76 bool any_less_than(
const vec_t<T,4> &a,
const vec_t<T,4> &b)
77 {
return (a.x < b.x) | (a.y < b.y) | (a.z < b.z) | (a.w < b.w); }
79 template<
typename T>
inline __both__
80 bool all_less_than(
const vec_t<T,4> &a,
const vec_t<T,4> &b)
81 {
return (a.x < b.x) & (a.y < b.y) & (a.z < b.z) & (a.w < b.w); }
83 template<
typename T>
inline __both__
84 bool any_greater_than(
const vec_t<T,4> &a,
const vec_t<T,4> &b)
85 {
return (a.x > b.x) | (a.y > b.y) | (a.z > b.z) | (a.w > b.w); }
87 template<
typename T>
inline __both__
88 bool any_greater_or_equal(
const vec_t<T,4> &a,
const vec_t<T,4> &b)
89 {
return (a.x >= b.x) | (a.y >= b.y) | (a.z >= b.z) | (a.w >= b.w); }
96 inline __both__ T clamp(
const T &val,
const T &lo,
const T &hi)
97 {
return min(hi,max(lo,val)); }
100 inline __both__ T clamp(
const T &val,
const T &hi)
101 {
return clamp(val,(T)0,hi); }
103#define _define_float_functor(func) \
104 template<typename T> inline __both__ vec_t<T,2> func(const vec_t<T,2> &v) \
105 { return vec_t<T,2>(owl::common::func(v.x),owl::common::func(v.y)); } \
107 template<typename T> inline __both__ vec_t<T,3> func(const vec_t<T,3> &v) \
108 { return vec_t<T,3>(owl::common::func(v.x),owl::common::func(v.y),owl::common::func(v.z)); } \
110 template<typename T> inline __both__ vec_t<T,4> func(const vec_t<T,4> &v) \
111 { return vec_t<T,4>(owl::common::func(v.x),owl::common::func(v.y),owl::common::func(v.z),owl::common::func(v.w)); } \
113 _define_float_functor(rcp)
114 _define_float_functor(sin)
115 _define_float_functor(cos)
116 _define_float_functor(abs)
117 _define_float_functor(saturate)
119#undef _define_float_functor
125#define _define_binary_functor(fct) \
126 template<typename T> \
127 inline __both__ vec_t<T,1> fct(const vec_t<T,1> &a, const vec_t<T,1> &b) \
129 return vec_t<T,1>(fct(a.x,b.x)); \
132 template<typename T> \
133 inline __both__ vec_t<T,2> fct(const vec_t<T,2> &a, const vec_t<T,2> &b) \
135 return vec_t<T,2>(fct(a.x,b.x), \
139 template<typename T> \
140 inline __both__ vec_t<T,3> fct(const vec_t<T,3> &a, const vec_t<T,3> &b) \
142 return vec_t<T,3>(fct(a.x,b.x), \
147 template<typename T1, typename T2> \
148 inline __both__ vec_t<typename BinaryOpResultType<T1,T2>::type,3> \
149 fct(const vec_t<T1,3> &a, const vec_t<T2,3> &b) \
151 return vec_t<typename BinaryOpResultType<T1,T2>::type,3> \
157 template<typename T> \
158 inline __both__ vec_t<T,4> fct(const vec_t<T,4> &a, const vec_t<T,4> &b) \
160 return vec_t<T,4>(fct(a.x,b.x), \
167 _define_binary_functor(divRoundUp)
168 _define_binary_functor(min)
169 _define_binary_functor(max)
170#undef _define_binary_functor
180#define _define_operator(op) \
182 template<typename T> \
183 inline __both__ vec_t<T,2> operator op(const vec_t<T,2> &a, \
184 const vec_t<T,2> &b) \
185 { return vec_t<T,2>(a.x op b.x, a.y op b.y); } \
187 template<typename T> \
188 inline __both__ vec_t<T,3> operator op(const vec_t<T,3> &a, \
189 const vec_t<T,3> &b) \
190 { return vec_t<T,3>(a.x op b.x, a.y op b.y, a.z op b.z); } \
192 template<typename T> \
193 inline __both__ vec_t<T,4> operator op(const vec_t<T,4> &a, \
194 const vec_t<T,4> &b) \
195 { return vec_t<T,4>(a.x op b.x,a.y op b.y,a.z op b.z,a.w op b.w); } \
198 template<typename T> \
199 inline __both__ vec_t<T,2> operator op(const vec_t<T,2> &a, \
201 { return vec_t<T,2>(a.x op b, a.y op b); } \
203 template<typename T1, typename T2> \
204 inline __both__ vec_t<typename BinaryOpResultType<T1,T2>::type,3> \
205 operator op(const vec_t<T1,3> &a, const T2 &b) \
206 { return vec_t<typename BinaryOpResultType<T1,T2>::type,3> \
207 (a.x op b, a.y op b, a.z op b); \
210 template<typename T> \
211 inline __both__ vec_t<T,4> operator op(const vec_t<T,4> &a, \
213 { return vec_t<T,4>(a.x op b, a.y op b, a.z op b, a.w op b); } \
216 template<typename T> \
217 inline __both__ vec_t<T,2> operator op(const T &a, \
218 const vec_t<T,2> &b) \
219 { return vec_t<T,2>(a op b.x, a op b.y); } \
221 template<typename T> \
222 inline __both__ vec_t<T,3> operator op(const T &a, \
223 const vec_t<T,3> &b) \
224 { return vec_t<T,3>(a op b.x, a op b.y, a op b.z); } \
226 template<typename T> \
227 inline __both__ vec_t<T,4> operator op(const T &a, \
228 const vec_t<T,4> &b) \
229 { return vec_t<T,4>(a op b.x, a op b.y, a op b.z, a op b.w); } \
239#undef _define_operator
249 inline __both__ vec_t<T,2> operator-(
const vec_t<T,2> &v)
250 {
return vec_t<T,2>(-v.x, -v.y); }
253 inline __both__ vec_t<T,2> operator+(
const vec_t<T,2> &v)
254 {
return vec_t<T,2>(v.x, v.y); }
257 inline __both__ vec_t<T,3> operator-(
const vec_t<T,3> &v)
258 {
return vec_t<T,3>(-v.x, -v.y, -v.z); }
261 inline __both__ vec_t<T,3> operator+(
const vec_t<T,3> &v)
262 {
return vec_t<T,3>(v.x, v.y, v.z); }
269#define _define_op_assign_operator(operator_op,op) \
271 template<typename T, typename OT> \
272 inline __both__ vec_t<T,2> &operator_op(vec_t<T,2> &a, \
273 const vec_t<OT,2> &b) \
280 template<typename T, typename OT> \
281 inline __both__ vec_t<T,3> &operator_op(vec_t<T,3> &a, \
282 const vec_t<OT,3> &b) \
290 template<typename T, typename OT> \
291 inline __both__ vec_t<T,4> &operator_op(vec_t<T,4> &a, \
292 const vec_t<OT,4> &b) \
302 template<typename T, typename OT> \
303 inline __both__ vec_t<T,2> &operator_op(vec_t<T,2> &a, \
305 { a.x op (T)b; a.y op (T)b; return a; } \
307 template<typename T, typename OT> \
308 inline __both__ vec_t<T,3> &operator_op(vec_t<T,3> &a, \
310 { a.x op (T)b; a.y op (T)b; a.z op (T)b; return a; } \
312 template<typename T, typename OT> \
313 inline __both__ vec_t<T,4> &operator_op(vec_t<T,4> &a, \
315 { a.x op (T)b; a.y op (T)b; a.z op (T)b; a.w op (T)b; return a; } \
317 _define_op_assign_operator(
operator*=,*=);
318 _define_op_assign_operator(
operator/=,/=);
319 _define_op_assign_operator(
operator+=,+=);
320 _define_op_assign_operator(
operator-=,-=);
322#undef _define_op_assign_operator
326 __both__ T reduce_min(
const vec_t<T,1> &v) {
return v.x; }
328 __both__ T reduce_min(
const vec_t<T,2> &v) {
return min(v.x,v.y); }
330 __both__ T reduce_min(
const vec_t<T,3> &v) {
return min(min(v.x,v.y),v.z); }
332 __both__ T reduce_min(
const vec_t<T,4> &v) {
return min(min(v.x,v.y),min(v.z,v.w)); }
334 __both__ T reduce_max(
const vec_t<T,2> &v) {
return max(v.x,v.y); }
336 __both__ T reduce_max(
const vec_t<T,3> &v) {
return max(max(v.x,v.y),v.z); }
338 __both__ T reduce_max(
const vec_t<T,4> &v) {
return max(max(v.x,v.y),max(v.z,v.w)); }
341 template<
typename T,
int N>
342 __both__ vec_t<T,3> madd(
const vec_t<T,N> &a,
const vec_t<T,N> &b,
const vec_t<T,N> &c)
348 template<
typename T,
int N>
349 __both__
int arg_max(
const vec_t<T,N> &v)
352 for (
int i=1;i<N;i++)
353 if ((v[i]) > (v[biggestDim])) biggestDim = i;
357 template<
typename T,
int N>
358 __both__
int arg_min(
const vec_t<T,N> &v)
361 for (
int i=1;i<N;i++)
362 if ((v[i]) < (v[biggestDim])) biggestDim = i;
369 __both__
bool operator<(
const vec_t<T,2> &a,
const vec_t<T,2> &b)
371 if (a.x < b.x)
return true;
372 if (a.x == b.x && a.y < b.y)
return true;
376 __both__
bool operator<(
const vec_t<T,3> &a,
const vec_t<T,3> &b)
378 if (a.x < b.x)
return true;
379 if (a.x == b.x && a.y < b.y)
return true;
380 if (a.x == b.x && a.y == b.y && a.z < b.z)
return true;
384 __both__
bool operator<(
const vec_t<T,4> &a,
const vec_t<T,4> &b)
386 if (a.x < b.x)
return true;
387 if (a.x == b.x && a.y < b.y)
return true;
388 if (a.x == b.x && a.y == b.y && a.z < b.z)
return true;
389 if (a.x == b.x && a.y == b.y && a.z == b.z && a.w < b.w)
return true;
394 inline __both__ vec3f randomColor(
int i)
396 int r = unsigned(i)*13*17 + 0x234235;
397 int g = unsigned(i)*7*3*5 + 0x773477;
398 int b = unsigned(i)*11*19 + 0x223766;
399 return vec3f((r&255)/255.f,
405 inline __both__ vec3f randomColor(
size_t idx)
407 unsigned int r = (
unsigned int)(idx*13*17 + 0x234235);
408 unsigned int g = (
unsigned int)(idx*7*3*5 + 0x773477);
409 unsigned int b = (
unsigned int)(idx*11*19 + 0x223766);
410 return vec3f((r&255)/255.f,
417 inline __both__ vec3f randomColor(
const T *ptr)
419 return randomColor((
size_t)ptr);
422 inline __both__
float sqrt(
const float v) {
return sqrtf(v); }
423 inline __both__ vec2f sqrt(
const vec2f v) {
return vec2f(sqrtf(v.x),sqrtf(v.y)); }
424 inline __both__ vec3f sqrt(
const vec3f v) {
return vec3f(sqrtf(v.x),sqrtf(v.y),sqrtf(v.z)); }
425 inline __both__ vec4f sqrt(
const vec4f v) {
return vec4f(sqrtf(v.x),sqrtf(v.y),sqrtf(v.z),sqrtf(v.w)); }