// Copyright 2009-2021 Intel Corporation // SPDX-License-Identifier: Apache-2.0 #pragma once #include "math.h" namespace embree { struct Vec3fa; //////////////////////////////////////////////////////////////////////////////// /// Generic 3D vector Class //////////////////////////////////////////////////////////////////////////////// template struct Vec3 { enum { N = 3 }; union { struct { T x, y, z; }; #if !(defined(__WIN32__) && _MSC_VER == 1800) // workaround for older VS 2013 compiler T components[N]; #endif }; typedef T Scalar; //////////////////////////////////////////////////////////////////////////////// /// Construction //////////////////////////////////////////////////////////////////////////////// __forceinline Vec3( ) {} __forceinline explicit Vec3( const T& a ) : x(a), y(a), z(a) {} __forceinline Vec3( const T& x, const T& y, const T& z ) : x(x), y(y), z(z) {} __forceinline Vec3( const Vec3& other ) { x = other.x; y = other.y; z = other.z; } __forceinline Vec3( const Vec3fa& other ); template __forceinline Vec3( const Vec3& a ) : x(T(a.x)), y(T(a.y)), z(T(a.z)) {} template __forceinline Vec3& operator =(const Vec3& other) { x = other.x; y = other.y; z = other.z; return *this; } __forceinline Vec3& operator =(const Vec3& other) { x = other.x; y = other.y; z = other.z; return *this; } //////////////////////////////////////////////////////////////////////////////// /// Constants //////////////////////////////////////////////////////////////////////////////// __forceinline Vec3( ZeroTy ) : x(zero), y(zero), z(zero) {} __forceinline Vec3( OneTy ) : x(one), y(one), z(one) {} __forceinline Vec3( PosInfTy ) : x(pos_inf), y(pos_inf), z(pos_inf) {} __forceinline Vec3( NegInfTy ) : x(neg_inf), y(neg_inf), z(neg_inf) {} #if defined(__WIN32__) && (_MSC_VER == 1800) // workaround for older VS 2013 compiler __forceinline const T& operator []( const size_t axis ) const { assert(axis < 3); return (&x)[axis]; } __forceinline T& operator []( const size_t axis ) { assert(axis < 3); return (&x)[axis]; } #else __forceinline const T& operator [](const size_t axis) const { assert(axis < 3); return components[axis]; } __forceinline T& operator [](const size_t axis) { assert(axis < 3); return components[axis]; } #endif }; //////////////////////////////////////////////////////////////////////////////// /// Unary Operators //////////////////////////////////////////////////////////////////////////////// template __forceinline Vec3 operator +( const Vec3& a ) { return Vec3(+a.x, +a.y, +a.z); } template __forceinline Vec3 operator -( const Vec3& a ) { return Vec3(-a.x, -a.y, -a.z); } template __forceinline Vec3 abs ( const Vec3& a ) { return Vec3(abs (a.x), abs (a.y), abs (a.z)); } template __forceinline Vec3 rcp ( const Vec3& a ) { return Vec3(rcp (a.x), rcp (a.y), rcp (a.z)); } template __forceinline Vec3 rsqrt ( const Vec3& a ) { return Vec3(rsqrt(a.x), rsqrt(a.y), rsqrt(a.z)); } template __forceinline Vec3 sqrt ( const Vec3& a ) { return Vec3(sqrt (a.x), sqrt (a.y), sqrt (a.z)); } template __forceinline Vec3 zero_fix( const Vec3& a ) { return Vec3(select(abs(a.x) __forceinline Vec3 rcp_safe(const Vec3& a) { return rcp(zero_fix(a)); } //////////////////////////////////////////////////////////////////////////////// /// Binary Operators //////////////////////////////////////////////////////////////////////////////// template __forceinline Vec3 operator +( const Vec3& a, const Vec3& b ) { return Vec3(a.x + b.x, a.y + b.y, a.z + b.z); } template __forceinline Vec3 operator -( const Vec3& a, const Vec3& b ) { return Vec3(a.x - b.x, a.y - b.y, a.z - b.z); } template __forceinline Vec3 operator *( const Vec3& a, const Vec3& b ) { return Vec3(a.x * b.x, a.y * b.y, a.z * b.z); } template __forceinline Vec3 operator *( const T& a, const Vec3& b ) { return Vec3(a * b.x, a * b.y, a * b.z); } template __forceinline Vec3 operator *( const Vec3& a, const T& b ) { return Vec3(a.x * b , a.y * b , a.z * b ); } template __forceinline Vec3 operator /( const Vec3& a, const T& b ) { return Vec3(a.x / b , a.y / b , a.z / b ); } template __forceinline Vec3 operator /( const T& a, const Vec3& b ) { return Vec3(a / b.x, a / b.y, a / b.z); } template __forceinline Vec3 operator /( const Vec3& a, const Vec3& b ) { return Vec3(a.x / b.x, a.y / b.y, a.z / b.z); } template __forceinline Vec3 min(const Vec3& a, const Vec3& b) { return Vec3(min(a.x, b.x), min(a.y, b.y), min(a.z, b.z)); } template __forceinline Vec3 max(const Vec3& a, const Vec3& b) { return Vec3(max(a.x, b.x), max(a.y, b.y), max(a.z, b.z)); } template __forceinline Vec3 operator >>( const Vec3& a, const int b ) { return Vec3(a.x >> b, a.y >> b, a.z >> b); } template __forceinline Vec3 operator <<( const Vec3& a, const int b ) { return Vec3(a.x << b, a.y << b, a.z << b); } //////////////////////////////////////////////////////////////////////////////// /// Ternary Operators //////////////////////////////////////////////////////////////////////////////// template __forceinline Vec3 madd ( const Vec3& a, const Vec3& b, const Vec3& c) { return Vec3( madd(a.x,b.x,c.x), madd(a.y,b.y,c.y), madd(a.z,b.z,c.z)); } template __forceinline Vec3 msub ( const Vec3& a, const Vec3& b, const Vec3& c) { return Vec3( msub(a.x,b.x,c.x), msub(a.y,b.y,c.y), msub(a.z,b.z,c.z)); } template __forceinline Vec3 nmadd ( const Vec3& a, const Vec3& b, const Vec3& c) { return Vec3(nmadd(a.x,b.x,c.x),nmadd(a.y,b.y,c.y),nmadd(a.z,b.z,c.z));} template __forceinline Vec3 nmsub ( const Vec3& a, const Vec3& b, const Vec3& c) { return Vec3(nmsub(a.x,b.x,c.x),nmsub(a.y,b.y,c.y),nmsub(a.z,b.z,c.z)); } template __forceinline Vec3 madd ( const T& a, const Vec3& b, const Vec3& c) { return Vec3( madd(a,b.x,c.x), madd(a,b.y,c.y), madd(a,b.z,c.z)); } template __forceinline Vec3 msub ( const T& a, const Vec3& b, const Vec3& c) { return Vec3( msub(a,b.x,c.x), msub(a,b.y,c.y), msub(a,b.z,c.z)); } template __forceinline Vec3 nmadd ( const T& a, const Vec3& b, const Vec3& c) { return Vec3(nmadd(a,b.x,c.x),nmadd(a,b.y,c.y),nmadd(a,b.z,c.z));} template __forceinline Vec3 nmsub ( const T& a, const Vec3& b, const Vec3& c) { return Vec3(nmsub(a,b.x,c.x),nmsub(a,b.y,c.y),nmsub(a,b.z,c.z)); } //////////////////////////////////////////////////////////////////////////////// /// Assignment Operators //////////////////////////////////////////////////////////////////////////////// template __forceinline Vec3& operator +=( Vec3& a, const T b ) { a.x += b; a.y += b; a.z += b; return a; } template __forceinline Vec3& operator +=( Vec3& a, const Vec3& b ) { a.x += b.x; a.y += b.y; a.z += b.z; return a; } template __forceinline Vec3& operator -=( Vec3& a, const Vec3& b ) { a.x -= b.x; a.y -= b.y; a.z -= b.z; return a; } template __forceinline Vec3& operator *=( Vec3& a, const T& b ) { a.x *= b ; a.y *= b ; a.z *= b ; return a; } template __forceinline Vec3& operator /=( Vec3& a, const T& b ) { a.x /= b ; a.y /= b ; a.z /= b ; return a; } //////////////////////////////////////////////////////////////////////////////// /// Reduction Operators //////////////////////////////////////////////////////////////////////////////// template __forceinline T reduce_add( const Vec3& a ) { return a.x + a.y + a.z; } template __forceinline T reduce_mul( const Vec3& a ) { return a.x * a.y * a.z; } template __forceinline T reduce_min( const Vec3& a ) { return min(a.x, a.y, a.z); } template __forceinline T reduce_max( const Vec3& a ) { return max(a.x, a.y, a.z); } //////////////////////////////////////////////////////////////////////////////// /// Comparison Operators //////////////////////////////////////////////////////////////////////////////// template __forceinline bool operator ==( const Vec3& a, const Vec3& b ) { return a.x == b.x && a.y == b.y && a.z == b.z; } template __forceinline bool operator !=( const Vec3& a, const Vec3& b ) { return a.x != b.x || a.y != b.y || a.z != b.z; } template __forceinline bool operator < ( const Vec3& a, const Vec3& b ) { if (a.x != b.x) return a.x < b.x; if (a.y != b.y) return a.y < b.y; if (a.z != b.z) return a.z < b.z; return false; } //////////////////////////////////////////////////////////////////////////////// /// Shift Operators //////////////////////////////////////////////////////////////////////////////// template __forceinline Vec3 shift_right_1( const Vec3& a ) { return Vec3(shift_right_1(a.x),shift_right_1(a.y),shift_right_1(a.z)); } //////////////////////////////////////////////////////////////////////////////// /// Select //////////////////////////////////////////////////////////////////////////////// template __forceinline Vec3 select ( bool s, const Vec3& t, const Vec3& f ) { return Vec3(select(s,t.x,f.x),select(s,t.y,f.y),select(s,t.z,f.z)); } template __forceinline Vec3 select ( const Vec3& s, const Vec3& t, const Vec3& f ) { return Vec3(select(s.x,t.x,f.x),select(s.y,t.y,f.y),select(s.z,t.z,f.z)); } template __forceinline Vec3 select ( const typename T::Bool& s, const Vec3& t, const Vec3& f ) { return Vec3(select(s,t.x,f.x),select(s,t.y,f.y),select(s,t.z,f.z)); } template __forceinline Vec3 lerp(const Vec3& v0, const Vec3& v1, const T& t) { return madd(Vec3(T(1.0f)-t),v0,t*v1); } template __forceinline int maxDim ( const Vec3& a ) { const Vec3 b = abs(a); if (b.x > b.y) { if (b.x > b.z) return 0; else return 2; } else { if (b.y > b.z) return 1; else return 2; } } //////////////////////////////////////////////////////////////////////////////// /// Comparison Operators //////////////////////////////////////////////////////////////////////////////// template __forceinline Vec3 eq_mask( const Vec3& a, const Vec3& b ) { return Vec3(a.x==b.x,a.y==b.y,a.z==b.z); } template __forceinline Vec3 neq_mask(const Vec3& a, const Vec3& b ) { return Vec3(a.x!=b.x,a.y!=b.y,a.z!=b.z); } template __forceinline Vec3 lt_mask( const Vec3& a, const Vec3& b ) { return Vec3(a.x< b.x,a.y< b.y,a.z< b.z); } template __forceinline Vec3 le_mask( const Vec3& a, const Vec3& b ) { return Vec3(a.x<=b.x,a.y<=b.y,a.z<=b.z); } template __forceinline Vec3 gt_mask( const Vec3& a, const Vec3& b ) { return Vec3(a.x> b.x,a.y> b.y,a.z> b.z); } template __forceinline Vec3 ge_mask( const Vec3& a, const Vec3& b ) { return Vec3(a.x>=b.x,a.y>=b.y,a.z>=b.z); } //////////////////////////////////////////////////////////////////////////////// /// Euclidean Space Operators //////////////////////////////////////////////////////////////////////////////// template __forceinline T sqr ( const Vec3& a ) { return dot(a,a); } template __forceinline T dot ( const Vec3& a, const Vec3& b ) { return madd(a.x,b.x,madd(a.y,b.y,a.z*b.z)); } template __forceinline T length ( const Vec3& a ) { return sqrt(sqr(a)); } template __forceinline T rcp_length( const Vec3& a ) { return rsqrt(sqr(a)); } template __forceinline Vec3 normalize( const Vec3& a ) { return a*rsqrt(sqr(a)); } template __forceinline T distance ( const Vec3& a, const Vec3& b ) { return length(a-b); } template __forceinline Vec3 cross ( const Vec3& a, const Vec3& b ) { return Vec3(msub(a.y,b.z,a.z*b.y), msub(a.z,b.x,a.x*b.z), msub(a.x,b.y,a.y*b.x)); } template __forceinline Vec3 stable_triangle_normal( const Vec3& a, const Vec3& b, const Vec3& c ) { const T ab_x = a.z*b.y, ab_y = a.x*b.z, ab_z = a.y*b.x; const T bc_x = b.z*c.y, bc_y = b.x*c.z, bc_z = b.y*c.x; const Vec3 cross_ab(msub(a.y,b.z,ab_x), msub(a.z,b.x,ab_y), msub(a.x,b.y,ab_z)); const Vec3 cross_bc(msub(b.y,c.z,bc_x), msub(b.z,c.x,bc_y), msub(b.x,c.y,bc_z)); const auto sx = abs(ab_x) < abs(bc_x); const auto sy = abs(ab_y) < abs(bc_y); const auto sz = abs(ab_z) < abs(bc_z); return Vec3(select(sx,cross_ab.x,cross_bc.x), select(sy,cross_ab.y,cross_bc.y), select(sz,cross_ab.z,cross_bc.z)); } template __forceinline T sum ( const Vec3& a ) { return a.x+a.y+a.z; } template __forceinline T halfArea ( const Vec3& d ) { return madd(d.x,(d.y+d.z),d.y*d.z); } template __forceinline T area ( const Vec3& d ) { return 2.0f*halfArea(d); } template __forceinline Vec3 normalize_safe( const Vec3& a ) { const T d = dot(a,a); return select(d == T( zero ), a , a*rsqrt(d) ); } template __forceinline T sqr_point_to_line_distance(const Vec3& P, const Vec3& Q0, const Vec3& Q1) { const Vec3 N = cross(P-Q0,Q1-Q0); const Vec3 D = Q1-Q0; return dot(N,N)*rcp(dot(D,D)); } template __forceinline T sqr_point_to_line_distance(const Vec3& PmQ0, const Vec3& Q1mQ0) { const Vec3 N = cross(PmQ0,Q1mQ0); const Vec3 D = Q1mQ0; return dot(N,N)*rcp(dot(D,D)); } //////////////////////////////////////////////////////////////////////////////// /// Output Operators //////////////////////////////////////////////////////////////////////////////// template __forceinline embree_ostream operator<<(embree_ostream cout, const Vec3& a) { return cout << "(" << a.x << ", " << a.y << ", " << a.z << ")"; } typedef Vec3 Vec3b; typedef Vec3 Vec3i; typedef Vec3 Vec3f; } #include "vec3ba.h" #include "vec3ia.h" #include "vec3fa.h" //////////////////////////////////////////////////////////////////////////////// /// SSE / AVX / MIC specializations //////////////////////////////////////////////////////////////////////////////// #if defined(__SSE__) || defined(__ARM_NEON) #include "../simd/sse.h" #endif #if defined(__AVX__) #include "../simd/avx.h" #endif #if defined(__AVX512F__) #include "../simd/avx512.h" #endif namespace embree { template __forceinline Vec3 broadcast(const Vec3& a, const size_t k) { return Vec3(Out(a.x[k]), Out(a.y[k]), Out(a.z[k])); } template<> __forceinline Vec3::Vec3(const Vec3fa& a) { x = a.x; y = a.y; z = a.z; } #if defined(__AVX__) template<> __forceinline Vec3::Vec3(const Vec3fa& a) { x = a.x; y = a.y; z = a.z; } #elif defined(__SSE__) || defined(__ARM_NEON) template<> __forceinline Vec3::Vec3(const Vec3fa& a) { const vfloat4 v = vfloat4(a.m128); x = shuffle<0,0,0,0>(v); y = shuffle<1,1,1,1>(v); z = shuffle<2,2,2,2>(v); } #endif #if defined(__SSE__) || defined(__ARM_NEON) template<> __forceinline Vec3 broadcast(const Vec3& a, const size_t k) { return Vec3(vfloat4::broadcast(&a.x[k]), vfloat4::broadcast(&a.y[k]), vfloat4::broadcast(&a.z[k])); } template __forceinline Vec3 shuffle(const Vec3& b) { return Vec3(shuffle(b.x), shuffle(b.y), shuffle(b.z)); } #endif #if defined(__AVX__) template<> __forceinline Vec3::Vec3(const Vec3fa& a) { x = a.x; y = a.y; z = a.z; } template<> __forceinline Vec3 broadcast(const Vec3& a, const size_t k) { return Vec3(vfloat8::broadcast(&a.x[k]), vfloat8::broadcast(&a.y[k]), vfloat8::broadcast(&a.z[k])); } template<> __forceinline Vec3 broadcast(const Vec3& a, const size_t k) { return Vec3(vfloat8::broadcast(&a.x[k]), vfloat8::broadcast(&a.y[k]), vfloat8::broadcast(&a.z[k])); } template __forceinline Vec3 shuffle(const Vec3& b) { return Vec3(shuffle(b.x), shuffle(b.y), shuffle(b.z)); } #endif #if defined(__AVX512F__) template<> __forceinline Vec3::Vec3(const Vec3fa& a) : x(a.x), y(a.y), z(a.z) {} #endif }