// Copyright 2009-2021 Intel Corporation // SPDX-License-Identifier: Apache-2.0 #pragma once #include "math.h" namespace embree { struct Vec2fa; //////////////////////////////////////////////////////////////////////////////// /// Generic 2D vector Class //////////////////////////////////////////////////////////////////////////////// template struct Vec2 { enum { N = 2 }; union { struct { T x, y; }; #if !(defined(__WIN32__) && _MSC_VER == 1800) // workaround for older VS 2013 compiler T components[N]; #endif }; typedef T Scalar; //////////////////////////////////////////////////////////////////////////////// /// Construction //////////////////////////////////////////////////////////////////////////////// __forceinline Vec2( ) {} __forceinline explicit Vec2( const T& a ) : x(a), y(a) {} __forceinline Vec2( const T& x, const T& y ) : x(x), y(y) {} __forceinline Vec2( const Vec2& other ) { x = other.x; y = other.y; } __forceinline Vec2( const Vec2fa& other ); template __forceinline Vec2( const Vec2& a ) : x(T(a.x)), y(T(a.y)) {} template __forceinline Vec2& operator =( const Vec2& other ) { x = other.x; y = other.y; return *this; } __forceinline Vec2& operator =( const Vec2& other ) { x = other.x; y = other.y; return *this; } //////////////////////////////////////////////////////////////////////////////// /// Constants //////////////////////////////////////////////////////////////////////////////// __forceinline Vec2( ZeroTy ) : x(zero), y(zero) {} __forceinline Vec2( OneTy ) : x(one), y(one) {} __forceinline Vec2( PosInfTy ) : x(pos_inf), y(pos_inf) {} __forceinline Vec2( NegInfTy ) : x(neg_inf), y(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 < 2); return (&x)[axis]; } __forceinline T& operator [](const size_t axis) { assert(axis < 2); return (&x)[axis]; } #else __forceinline const T& operator [](const size_t axis) const { assert(axis < 2); return components[axis]; } __forceinline T& operator [](const size_t axis ) { assert(axis < 2); return components[axis]; } #endif }; //////////////////////////////////////////////////////////////////////////////// /// Unary Operators //////////////////////////////////////////////////////////////////////////////// template __forceinline Vec2 operator +( const Vec2& a ) { return Vec2(+a.x, +a.y); } template __forceinline Vec2 operator -( const Vec2& a ) { return Vec2(-a.x, -a.y); } template __forceinline Vec2 abs ( const Vec2& a ) { return Vec2(abs (a.x), abs (a.y)); } template __forceinline Vec2 rcp ( const Vec2& a ) { return Vec2(rcp (a.x), rcp (a.y)); } template __forceinline Vec2 rsqrt ( const Vec2& a ) { return Vec2(rsqrt(a.x), rsqrt(a.y)); } template __forceinline Vec2 sqrt ( const Vec2& a ) { return Vec2(sqrt (a.x), sqrt (a.y)); } template __forceinline Vec2 frac ( const Vec2& a ) { return Vec2(frac (a.x), frac (a.y)); } //////////////////////////////////////////////////////////////////////////////// /// Binary Operators //////////////////////////////////////////////////////////////////////////////// template __forceinline Vec2 operator +( const Vec2& a, const Vec2& b ) { return Vec2(a.x + b.x, a.y + b.y); } template __forceinline Vec2 operator +( const Vec2& a, const T& b ) { return Vec2(a.x + b , a.y + b ); } template __forceinline Vec2 operator +( const T& a, const Vec2& b ) { return Vec2(a + b.x, a + b.y); } template __forceinline Vec2 operator -( const Vec2& a, const Vec2& b ) { return Vec2(a.x - b.x, a.y - b.y); } template __forceinline Vec2 operator -( const Vec2& a, const T& b ) { return Vec2(a.x - b , a.y - b ); } template __forceinline Vec2 operator -( const T& a, const Vec2& b ) { return Vec2(a - b.x, a - b.y); } template __forceinline Vec2 operator *( const Vec2& a, const Vec2& b ) { return Vec2(a.x * b.x, a.y * b.y); } template __forceinline Vec2 operator *( const T& a, const Vec2& b ) { return Vec2(a * b.x, a * b.y); } template __forceinline Vec2 operator *( const Vec2& a, const T& b ) { return Vec2(a.x * b , a.y * b ); } template __forceinline Vec2 operator /( const Vec2& a, const Vec2& b ) { return Vec2(a.x / b.x, a.y / b.y); } template __forceinline Vec2 operator /( const Vec2& a, const T& b ) { return Vec2(a.x / b , a.y / b ); } template __forceinline Vec2 operator /( const T& a, const Vec2& b ) { return Vec2(a / b.x, a / b.y); } template __forceinline Vec2 min(const Vec2& a, const Vec2& b) { return Vec2(min(a.x, b.x), min(a.y, b.y)); } template __forceinline Vec2 max(const Vec2& a, const Vec2& b) { return Vec2(max(a.x, b.x), max(a.y, b.y)); } //////////////////////////////////////////////////////////////////////////////// /// Ternary Operators //////////////////////////////////////////////////////////////////////////////// template __forceinline Vec2 madd ( const Vec2& a, const Vec2& b, const Vec2& c) { return Vec2( madd(a.x,b.x,c.x), madd(a.y,b.y,c.y) ); } template __forceinline Vec2 msub ( const Vec2& a, const Vec2& b, const Vec2& c) { return Vec2( msub(a.x,b.x,c.x), msub(a.y,b.y,c.y) ); } template __forceinline Vec2 nmadd ( const Vec2& a, const Vec2& b, const Vec2& c) { return Vec2(nmadd(a.x,b.x,c.x),nmadd(a.y,b.y,c.y) ); } template __forceinline Vec2 nmsub ( const Vec2& a, const Vec2& b, const Vec2& c) { return Vec2(nmsub(a.x,b.x,c.x),nmsub(a.y,b.y,c.y) ); } template __forceinline Vec2 madd ( const T& a, const Vec2& b, const Vec2& c) { return Vec2( madd(a,b.x,c.x), madd(a,b.y,c.y) ); } template __forceinline Vec2 msub ( const T& a, const Vec2& b, const Vec2& c) { return Vec2( msub(a,b.x,c.x), msub(a,b.y,c.y) ); } template __forceinline Vec2 nmadd ( const T& a, const Vec2& b, const Vec2& c) { return Vec2(nmadd(a,b.x,c.x),nmadd(a,b.y,c.y) ); } template __forceinline Vec2 nmsub ( const T& a, const Vec2& b, const Vec2& c) { return Vec2(nmsub(a,b.x,c.x),nmsub(a,b.y,c.y) ); } //////////////////////////////////////////////////////////////////////////////// /// Assignment Operators //////////////////////////////////////////////////////////////////////////////// template __forceinline Vec2& operator +=( Vec2& a, const Vec2& b ) { a.x += b.x; a.y += b.y; return a; } template __forceinline Vec2& operator -=( Vec2& a, const Vec2& b ) { a.x -= b.x; a.y -= b.y; return a; } template __forceinline Vec2& operator *=( Vec2& a, const T& b ) { a.x *= b ; a.y *= b ; return a; } template __forceinline Vec2& operator /=( Vec2& a, const T& b ) { a.x /= b ; a.y /= b ; return a; } //////////////////////////////////////////////////////////////////////////////// /// Reduction Operators //////////////////////////////////////////////////////////////////////////////// template __forceinline T reduce_add( const Vec2& a ) { return a.x + a.y; } template __forceinline T reduce_mul( const Vec2& a ) { return a.x * a.y; } template __forceinline T reduce_min( const Vec2& a ) { return min(a.x, a.y); } template __forceinline T reduce_max( const Vec2& a ) { return max(a.x, a.y); } //////////////////////////////////////////////////////////////////////////////// /// Comparison Operators //////////////////////////////////////////////////////////////////////////////// template __forceinline bool operator ==( const Vec2& a, const Vec2& b ) { return a.x == b.x && a.y == b.y; } template __forceinline bool operator !=( const Vec2& a, const Vec2& b ) { return a.x != b.x || a.y != b.y; } template __forceinline bool operator < ( const Vec2& a, const Vec2& b ) { if (a.x != b.x) return a.x < b.x; if (a.y != b.y) return a.y < b.y; return false; } //////////////////////////////////////////////////////////////////////////////// /// Shift Operators //////////////////////////////////////////////////////////////////////////////// template __forceinline Vec2 shift_right_1( const Vec2& a ) { return Vec2(shift_right_1(a.x),shift_right_1(a.y)); } //////////////////////////////////////////////////////////////////////////////// /// Euclidean Space Operators //////////////////////////////////////////////////////////////////////////////// template __forceinline T dot ( const Vec2& a, const Vec2& b ) { return madd(a.x,b.x,a.y*b.y); } template __forceinline Vec2 cross ( const Vec2& a ) { return Vec2(-a.y,a.x); } template __forceinline T length ( const Vec2& a ) { return sqrt(dot(a,a)); } template __forceinline Vec2 normalize( const Vec2& a ) { return a*rsqrt(dot(a,a)); } template __forceinline T distance ( const Vec2& a, const Vec2& b ) { return length(a-b); } template __forceinline T det ( const Vec2& a, const Vec2& b ) { return a.x*b.y - a.y*b.x; } template __forceinline Vec2 normalize_safe( const Vec2& a ) { const T d = dot(a,a); return select(d == T( zero ),a, a*rsqrt(d) ); } //////////////////////////////////////////////////////////////////////////////// /// Select //////////////////////////////////////////////////////////////////////////////// template __forceinline Vec2 select ( bool s, const Vec2& t, const Vec2& f ) { return Vec2(select(s,t.x,f.x),select(s,t.y,f.y)); } template __forceinline Vec2 select ( const Vec2& s, const Vec2& t, const Vec2& f ) { return Vec2(select(s.x,t.x,f.x),select(s.y,t.y,f.y)); } template __forceinline Vec2 select ( const typename T::Bool& s, const Vec2& t, const Vec2& f ) { return Vec2(select(s,t.x,f.x),select(s,t.y,f.y)); } template __forceinline Vec2 lerp(const Vec2& v0, const Vec2& v1, const T& t) { return madd(Vec2(T(1.0f)-t),v0,t*v1); } template __forceinline int maxDim ( const Vec2& a ) { const Vec2 b = abs(a); if (b.x > b.y) return 0; else return 1; } //////////////////////////////////////////////////////////////////////////////// /// Output Operators //////////////////////////////////////////////////////////////////////////////// template __forceinline embree_ostream operator<<(embree_ostream cout, const Vec2& a) { return cout << "(" << a.x << ", " << a.y << ")"; } //////////////////////////////////////////////////////////////////////////////// /// Default template instantiations //////////////////////////////////////////////////////////////////////////////// typedef Vec2 Vec2b; typedef Vec2 Vec2i; typedef Vec2 Vec2f; } #include "vec2fa.h" #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 Vec2::Vec2(const Vec2fa& a) : x(a.x), y(a.y) {} #if defined(__SSE__) || defined(__ARM_NEON) template<> __forceinline Vec2::Vec2(const Vec2fa& a) : x(a.x), y(a.y) {} #endif #if defined(__AVX__) template<> __forceinline Vec2::Vec2(const Vec2fa& a) : x(a.x), y(a.y) {} #endif #if defined(__AVX512F__) template<> __forceinline Vec2::Vec2(const Vec2fa& a) : x(a.x), y(a.y) {} #endif }