OWL
Loading...
Searching...
No Matches
vec.h
1// ======================================================================== //
2// Copyright 2018-2020 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 "owl/common/owl-common.h"
20#include "owl/common/math/constants.h"
21#include <iostream>
22#include <math.h>
23#include <algorithm>
24
25namespace owl {
26 namespace common {
27
28 template<typename T> struct long_type_of { typedef T type; };
29 template<> struct long_type_of<int32_t> { typedef int64_t type; };
30 template<> struct long_type_of<uint32_t> { typedef uint64_t type; };
31
32 template<typename T, int N>
33 struct OWL_INTERFACE vec_t { T t[N]; };
34
35
36 template<typename ScalarTypeA, typename ScalarTypeB> struct BinaryOpResultType;
37
38 // Binary Result type: scalar type with itself always returns same type
39 template<typename ScalarType>
40 struct BinaryOpResultType<ScalarType,ScalarType> { typedef ScalarType type; };
41
42 template<> struct BinaryOpResultType<int,float> { typedef float type; };
43 template<> struct BinaryOpResultType<float,int> { typedef float type; };
44 template<> struct BinaryOpResultType<unsigned int,float> { typedef float type; };
45 template<> struct BinaryOpResultType<float,unsigned int> { typedef float type; };
46
47 template<> struct BinaryOpResultType<int,double> { typedef double type; };
48 template<> struct BinaryOpResultType<double,int> { typedef double type; };
49 template<> struct BinaryOpResultType<unsigned int,double> { typedef double type; };
50 template<> struct BinaryOpResultType<double,unsigned int> { typedef double type; };
51
52 // ------------------------------------------------------------------
53 // vec1 - not really a vector, but makes a scalar look like a
54 // vector, so we can use it in, say, box1f
55 // ------------------------------------------------------------------
56 template<typename T>
57 struct OWL_INTERFACE vec_t<T,1> {
58 enum { dims = 1 };
59 typedef T scalar_t;
60
61 inline __both__ vec_t() {}
62 inline __both__ vec_t(const T &v) : v(v) {}
63
65 inline __both__ vec_t<T,1> &operator=(const vec_t<T,1> &other) {
66 this->v = other.v;
67 return *this;
68 }
69
71 template<typename OT>
72 inline __both__ explicit vec_t(const vec_t<OT,1> &o) : v(o.v) {}
73
74 inline __both__ T &operator[](size_t dim) {
75 assert(dim == 0);
76 return x;
77 // return (&x)[dim];
78 }
79 inline __both__ const T &operator[](size_t dim) const
80 {
81 assert(dim == 0);
82 return x;
83 // return (&x)[dim];
84 }
85
86 union {
87 T v;
88 T x;
89 };
90 };
91
92 // ------------------------------------------------------------------
93 // vec2
94 // ------------------------------------------------------------------
95 template<typename T>
96 struct OWL_INTERFACE vec_t<T,2> {
97 enum { dims = 2 };
98 typedef T scalar_t;
99
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) {}
103#ifdef __CUDACC__
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) {}
107
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); }
111
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) {}
116#endif
117
119 inline __both__ vec_t<T,2> &operator=(const vec_t<T,2> &other) {
120 this->x = other.x;
121 this->y = other.y;
122 return *this;
123 }
124
126 template<typename OT>
127 inline __both__ explicit vec_t(const vec_t<OT,2> &o) : x((T)o.x), y((T)o.y) {}
128
129 inline __both__ T &operator[](size_t dim) { return (&x)[dim]; }
130 inline __both__ const T &operator[](size_t dim) const { return (&x)[dim]; }
131
132 union {
133 struct { T x, y; };
134 struct { T s, t; };
135 struct { T u, v; };
136 };
137 };
138
139 // ------------------------------------------------------------------
140 // vec3
141 // ------------------------------------------------------------------
142 template<typename T>
143 struct OWL_INTERFACE vec_t<T,3> {
144 enum { dims = 3 };
145 typedef T scalar_t;
146
147 inline // __both__
148 vec_t(const vec_t &) = default;
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) {}
152#ifdef __CUDACC__
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); }
171#endif
172 inline __both__ explicit vec_t(const vec_t<T,4> &v);
174 template<typename OT>
175 inline __both__ explicit vec_t(const vec_t<OT,3> &o) : x((T)o.x), y((T)o.y), z((T)o.z) {}
176
178 inline __both__ vec_t<T,3> yzx() const { return vec_t<T,3>(y,z,x); }
179
181 inline __both__ vec_t<T,3> &operator=(const vec_t<T,3> &other) {
182 this->x = other.x;
183 this->y = other.y;
184 this->z = other.z;
185 return *this;
186 }
187
188 inline __both__ T &operator[](size_t dim) { return (&x)[dim]; }
189 inline __both__ const T &operator[](size_t dim) const { return (&x)[dim]; }
190
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)); }
194
195 union {
196 struct { T x, y, z; };
197 struct { T r, s, t; };
198 struct { T u, v, w; };
199 };
200 };
201
202 // ------------------------------------------------------------------
203 // vec3a
204 // ------------------------------------------------------------------
205 template<typename T>
206 struct OWL_INTERFACE OWL_ALIGN(16) vec3a_t : public vec_t<T,3> {
207 inline vec3a_t() {}
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) {}
210#ifdef __CUDACC__
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) {};
217#endif
218
219 template<typename OT>
220 inline vec3a_t(const vec_t<OT,3> &v) : vec_t<T,3>(v.x,v.y,v.z) {}
221
222 T a;
223 // add one elemnet for 'forced' alignment
224 };
225
226 // ------------------------------------------------------------------
227 // vec4
228 // ------------------------------------------------------------------
229 template<typename T>
230 struct OWL_INTERFACE vec_t<T,4> {
231 enum { dims = 4 };
232 typedef T scalar_t;
233
234 inline __both__ vec_t() {}
235
236 inline __both__ vec_t(const T &t)
237 : x(t), y(t), z(t), w(t)
238 {}
239 inline __both__ vec_t(const vec_t<T,3> &xyz, const T &_w)
240 : x(xyz.x), y(xyz.y), z(xyz.z), w(_w)
241 {}
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)
244 {}
245
246#ifdef __CUDACC__
247 inline __both__ vec_t(const float4 &v)
248 : x(v.x), y(v.y), z(v.z), w(v.w)
249 {}
250 inline __both__ vec_t(const int4 &v)
251 : x(v.x), y(v.y), z(v.z), w(v.w)
252 {}
253 inline __both__ vec_t(const uint4 &v)
254 : x(v.x), y(v.y), z(v.z), w(v.w)
255 {}
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); }
259#endif
261 template<typename OT>
262 inline __both__ explicit vec_t(const vec_t<OT,4> &o)
263 : x((T)o.x), y((T)o.y), z((T)o.z), w((T)o.w)
264 {}
265 inline __both__ vec_t(const vec_t<T,4> &o) : x(o.x), y(o.y), z(o.z), w(o.w) {}
266
268 inline __both__ vec_t<T,4> &operator=(const vec_t<T,4> &other) {
269 this->x = other.x;
270 this->y = other.y;
271 this->z = other.z;
272 this->w = other.w;
273 return *this;
274 }
275
276 inline __both__ T &operator[](size_t dim) { return (&x)[dim]; }
277 inline __both__ const T &operator[](size_t dim) const { return (&x)[dim]; }
278
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)); }
283
284 T x, y, z, w;
285 };
286
287 template<typename T>
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)
290 {}
291
292 // =======================================================
293 // default functions
294 // =======================================================
295
296 template<typename T>
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); }
299
300
301 template<typename T>
302 inline __both__ typename long_type_of<T>::type volume(const vec_t<T,3> &v)
303 { return
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);
307 }
308
309 template<typename T>
310 inline __both__ typename long_type_of<T>::type volume(const vec_t<T,4> &v)
311 { return
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);
316 }
317
318 template<typename T>
319 inline __both__ typename long_type_of<T>::type area(const vec_t<T,3> &v)
320 { return
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);
324 }
325
326
327
329 template<typename T>
330 inline __both__ vec_t<T,3> cross(const vec_t<T,3> &a, const vec_t<T,3> &b)
331 {
332 return vec_t<T,3>(a.y*b.z-b.y*a.z,
333 a.z*b.x-b.z*a.x,
334 a.x*b.y-b.x*a.y);
335 }
336
338 template<typename T>
339 inline __both__ T dot(const vec_t<T,2> &a, const vec_t<T,2> &b)
340 {
341 return a.x*b.x + a.y*b.y;
342 }
343
345 template<typename T>
346 inline __both__ T dot(const vec_t<T,3> &a, const vec_t<T,3> &b)
347 {
348 return a.x*b.x + a.y*b.y + a.z*b.z;
349 }
350
352 template<typename T>
353 inline __both__ vec_t<T,3> normalize(const vec_t<T,3> &v)
354 {
355 return v * owl::common::polymorphic::rsqrt(dot(v,v));
356 }
357
359 template<typename T>
360 inline __both__ T length(const vec_t<T,3> &v)
361 {
362 return owl::common::polymorphic::sqrt(dot(v,v));
363 }
364
365 template<typename T>
366 inline __owl_host std::ostream &operator<<(std::ostream &o, const vec_t<T,1> &v)
367 {
368 o << "(" << v.x << ")";
369 return o;
370 }
371
372 template<typename T>
373 inline __owl_host std::ostream &operator<<(std::ostream &o, const vec_t<T,2> &v)
374 {
375 o << "(" << v.x << "," << v.y << ")";
376 return o;
377 }
378
379 template<typename T>
380 inline __owl_host std::ostream &operator<<(std::ostream &o, const vec_t<T,3> &v)
381 {
382 o << "(" << v.x << "," << v.y << "," << v.z << ")";
383 return o;
384 }
385
386 template<typename T>
387 inline __owl_host std::ostream &operator<<(std::ostream &o, const vec_t<T,4> &v)
388 {
389 o << "(" << v.x << "," << v.y << "," << v.z << "," << v.w << ")";
390 return o;
391 }
392
393 // =======================================================
394 // default instantiations
395 // =======================================================
396
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>; \
402
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);
414
415#undef _define_vec_types
416
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 }; }
419
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 }; }
422
423 inline __both__ bool any(vec_t<bool,3> v)
424 { return v.x | v.y | v.z; }
425
426
427 } // ::owl::common
428
429 using namespace owl::common;
430} // ::owl
431
432// comparison operators
433#include "vec/compare.h"
434#include "vec/functors.h"
435#include "vec/rotate.h"
436
Definition: vec.h:28
__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
Definition: vec.h:33