19#include "owl/common/owl-common.h"
20#include "owl/common/math/constants.h"
30 template<>
struct long_type_of<uint32_t> {
typedef uint64_t type; };
32 template<
typename T,
int N>
33 struct OWL_INTERFACE
vec_t { T t[N]; };
39 template<
typename ScalarType>
57 struct OWL_INTERFACE
vec_t<T,1> {
61 inline __both__
vec_t() {}
62 inline __both__
vec_t(
const T &v) : v(v) {}
74 inline __both__ T &operator[](
size_t dim) {
79 inline __both__
const T &operator[](
size_t dim)
const
96 struct OWL_INTERFACE
vec_t<T,2> {
100 inline __both__
vec_t() {}
101 inline __both__
vec_t(
const T &t) : x(t), y(t) {}
102 inline __both__
vec_t(
const T &x,
const T &y) : x(x), y(y) {}
104 inline __both__
vec_t(
const float2 v) : x(v.x), y(v.y) {}
105 inline __both__
vec_t(
const int2 v) : x(v.x), y(v.y) {}
106 inline __both__
vec_t(
const uint2 v) : x(v.x), y(v.y) {}
108 inline __both__
operator float2()
const {
return make_float2(x,y); }
109 inline __both__
operator int2()
const {
return make_int2(x,y); }
110 inline __both__
operator uint2()
const {
return make_uint2(x,y); }
114 inline __both__
operator dim3()
const { dim3 d; d.x = x; d.y = y; d.z = 1;
return d; }
115 inline explicit __both__
vec_t(
const dim3 v) : x(v.x), y(v.y) {}
126 template<
typename OT>
129 inline __both__ T &operator[](
size_t dim) {
return (&x)[dim]; }
130 inline __both__
const T &operator[](
size_t dim)
const {
return (&x)[dim]; }
149 inline __both__
vec_t() {}
150 inline __both__
vec_t(
const T &t) : x(t), y(t), z(t) {}
151 inline __both__
vec_t(
const T &_x,
const T &_y,
const T &_z) : x(_x), y(_y), z(_z) {}
153 inline __both__
vec_t(
const int3 &v) : x(v.x), y(v.y), z(v.z) {}
154 inline __both__
vec_t(
const uint3 &v) : x(v.x), y(v.y), z(v.z) {}
155 inline __both__
vec_t(
const float3 &v) : x(v.x), y(v.y), z(v.z) {}
159 inline __both__
vec_t(
const float4 v) : x(v.x), y(v.y), z(v.z) {}
163 inline __both__
vec_t(
const int4 v) : x(v.x), y(v.y), z(v.z) {}
167 inline __both__
vec_t(
const uint4 v) : x(v.x), y(v.y), z(v.z) {}
168 inline __both__
operator float3()
const {
return make_float3(x,y,z); }
169 inline __both__
operator int3()
const {
return make_int3(x,y,z); }
170 inline __both__
operator uint3()
const {
return make_uint3(x,y,z); }
174 template<
typename OT>
188 inline __both__ T &operator[](
size_t dim) {
return (&x)[dim]; }
189 inline __both__
const T &operator[](
size_t dim)
const {
return (&x)[dim]; }
191 template<
typename OT,
typename Lambda>
192 static inline __both__ vec_t<T,3> make_from(
const vec_t<OT,3> &v,
const Lambda &lambda)
193 {
return vec_t<T,3>(lambda(v.x),lambda(v.y),lambda(v.z)); }
196 struct { T x, y, z; };
197 struct { T r, s, t; };
198 struct { T u, v, w; };
206 struct OWL_INTERFACE OWL_ALIGN(16) vec3a_t :
public vec_t<T,3> {
208 inline vec3a_t(
const T &t) : vec_t<T,3>(t) {}
209 inline vec3a_t(
const T &x,
const T &y,
const T &z) : vec_t<T,3>(x,y,z) {}
211 inline __both__ vec3a_t(
const int3 &v) : vec_t<T,3>(v) {};
212 inline __both__ vec3a_t(
const uint3 &v) : vec_t<T,3>(v) {};
213 inline __both__ vec3a_t(
const float3 &v) : vec_t<T,3>(v) {};
214 inline __both__ vec3a_t(
const int4 v) : vec_t<T,3>(v) {};
215 inline __both__ vec3a_t(
const uint4 v) : vec_t<T,3>(v) {};
216 inline __both__ vec3a_t(
const float4 v) : vec_t<T,3>(v) {};
219 template<
typename OT>
220 inline vec3a_t(
const vec_t<OT,3> &v) : vec_t<T,3>(v.x,v.y,v.z) {}
234 inline __both__
vec_t() {}
236 inline __both__
vec_t(
const T &t)
237 : x(t), y(t), z(t), w(t)
240 : x(xyz.x), y(xyz.y), z(xyz.z), w(_w)
242 inline __both__
vec_t(
const T &_x,
const T &_y,
const T &_z,
const T &_w)
243 : x(_x), y(_y), z(_z), w(_w)
247 inline __both__
vec_t(
const float4 &v)
248 : x(v.x), y(v.y), z(v.z), w(v.w)
250 inline __both__
vec_t(
const int4 &v)
251 : x(v.x), y(v.y), z(v.z), w(v.w)
253 inline __both__
vec_t(
const uint4 &v)
254 : x(v.x), y(v.y), z(v.z), w(v.w)
256 inline __both__
operator float4()
const {
return make_float4(x,y,z,w); }
257 inline __both__
operator uint4()
const {
return make_uint4(x,y,z,w); }
258 inline __both__
operator int4()
const {
return make_int4(x,y,z,w); }
261 template<
typename OT>
263 : x((T)o.x), y((T)o.y), z((T)o.z), w((T)o.w)
265 inline __both__
vec_t(
const vec_t<T,4> &o) : x(o.x), y(o.y), z(o.z), w(o.w) {}
276 inline __both__ T &operator[](
size_t dim) {
return (&x)[dim]; }
277 inline __both__
const T &operator[](
size_t dim)
const {
return (&x)[dim]; }
279 template<
typename OT,
typename Lambda>
280 static inline __both__ vec_t<T,4> make_from(
const vec_t<OT,4> &v,
281 const Lambda &lambda)
282 {
return vec_t<T,4>(lambda(v.x),lambda(v.y),lambda(v.z),lambda(v.w)); }
288 inline __both__ vec_t<T,3>::vec_t(
const vec_t<T,4> &v)
289 : x(v.x), y(v.y), z(v.z)
297 inline __both__
typename long_type_of<T>::type area(
const vec_t<T,2> &v)
298 {
return (
typename long_type_of<T>::type)(v.x)*(
typename long_type_of<T>::type)(v.y); }
302 inline __both__
typename long_type_of<T>::type volume(
const vec_t<T,3> &v)
304 (
typename long_type_of<T>::type)(v.x)*
305 (
typename long_type_of<T>::type)(v.y)*
306 (
typename long_type_of<T>::type)(v.z);
310 inline __both__
typename long_type_of<T>::type volume(
const vec_t<T,4> &v)
312 (
typename long_type_of<T>::type)(v.x)*
313 (
typename long_type_of<T>::type)(v.y)*
314 (
typename long_type_of<T>::type)(v.z)*
315 (
typename long_type_of<T>::type)(v.w);
319 inline __both__
typename long_type_of<T>::type area(
const vec_t<T,3> &v)
321 T(2)*((
typename long_type_of<T>::type)(v.x)*v.y+
322 (
typename long_type_of<T>::type)(v.y)*v.z+
323 (
typename long_type_of<T>::type)(v.z)*v.x);
330 inline __both__ vec_t<T,3> cross(
const vec_t<T,3> &a,
const vec_t<T,3> &b)
332 return vec_t<T,3>(a.y*b.z-b.y*a.z,
339 inline __both__ T dot(
const vec_t<T,2> &a,
const vec_t<T,2> &b)
341 return a.x*b.x + a.y*b.y;
346 inline __both__ T dot(
const vec_t<T,3> &a,
const vec_t<T,3> &b)
348 return a.x*b.x + a.y*b.y + a.z*b.z;
353 inline __both__ vec_t<T,3> normalize(
const vec_t<T,3> &v)
355 return v * owl::common::polymorphic::rsqrt(dot(v,v));
360 inline __both__ T length(
const vec_t<T,3> &v)
362 return owl::common::polymorphic::sqrt(dot(v,v));
366 inline __owl_host std::ostream &operator<<(std::ostream &o,
const vec_t<T,1> &v)
368 o <<
"(" << v.x <<
")";
373 inline __owl_host std::ostream &operator<<(std::ostream &o,
const vec_t<T,2> &v)
375 o <<
"(" << v.x <<
"," << v.y <<
")";
380 inline __owl_host std::ostream &operator<<(std::ostream &o,
const vec_t<T,3> &v)
382 o <<
"(" << v.x <<
"," << v.y <<
"," << v.z <<
")";
387 inline __owl_host std::ostream &operator<<(std::ostream &o,
const vec_t<T,4> &v)
389 o <<
"(" << v.x <<
"," << v.y <<
"," << v.z <<
"," << v.w <<
")";
397#define _define_vec_types(T,t) \
398 using vec2##t = vec_t<T,2>; \
399 using vec3##t = vec_t<T,3>; \
400 using vec4##t = vec_t<T,4>; \
401 using vec3##t##a = vec3a_t<T>; \
403 _define_vec_types(
bool ,b);
404 _define_vec_types(int8_t ,c);
405 _define_vec_types(int16_t ,s);
406 _define_vec_types(int32_t ,i);
407 _define_vec_types(int64_t ,l);
408 _define_vec_types(uint8_t ,uc);
409 _define_vec_types(uint16_t,us);
410 _define_vec_types(uint32_t,ui);
411 _define_vec_types(uint64_t,ul);
412 _define_vec_types(
float,f);
413 _define_vec_types(
double,d);
415#undef _define_vec_types
417 inline __both__ vec_t<bool,3> ge(
const vec3f &a,
const vec3f &b)
418 {
return { a.x >= b.x, a.y >= b.y, a.z >= b.z }; }
420 inline __both__ vec_t<bool,3> lt(
const vec3f &a,
const vec3f &b)
421 {
return { a.x < b.x, a.y < b.y, a.z < b.z }; }
423 inline __both__
bool any(vec_t<bool,3> v)
424 {
return v.x | v.y | v.z; }
429 using namespace owl::common;
433#include "vec/compare.h"
434#include "vec/functors.h"
435#include "vec/rotate.h"
__both__ vec_t< T, 1 > & operator=(const vec_t< T, 1 > &other)
Definition: vec.h:65
__both__ vec_t(const vec_t< OT, 1 > &o)
Definition: vec.h:72
T x
just to allow all vec types to use x,y,z,w,...
Definition: vec.h:88
__both__ vec_t< T, 2 > & operator=(const vec_t< T, 2 > &other)
Definition: vec.h:119
__both__ vec_t(const vec_t< OT, 2 > &o)
Definition: vec.h:127
__both__ vec_t(const vec_t< OT, 3 > &o)
Definition: vec.h:175
__both__ vec_t< T, 3 > & operator=(const vec_t< T, 3 > &other)
Definition: vec.h:181
__both__ vec_t< T, 3 > yzx() const
Definition: vec.h:178
__both__ vec_t< T, 4 > & operator=(const vec_t< T, 4 > &other)
Definition: vec.h:268
__both__ vec_t(const vec_t< OT, 4 > &o)
Definition: vec.h:262