Path: blob/master/thirdparty/embree/common/math/linearspace3.h
9912 views
// Copyright 2009-2021 Intel Corporation1// SPDX-License-Identifier: Apache-2.023#pragma once45#include "vec3.h"6#include "quaternion.h"78namespace embree9{10////////////////////////////////////////////////////////////////////////////////11/// 3D Linear Transform (3x3 Matrix)12////////////////////////////////////////////////////////////////////////////////1314template<typename T> struct LinearSpace315{16typedef T Vector;17typedef typename T::Scalar Scalar;1819/*! default matrix constructor */20__forceinline LinearSpace3 ( ) {}2122__forceinline LinearSpace3 ( const LinearSpace3& other ) { vx = other.vx; vy = other.vy; vz = other.vz; }23__forceinline LinearSpace3& operator=( const LinearSpace3& other ) { vx = other.vx; vy = other.vy; vz = other.vz; return *this; }2425template<typename L1> __forceinline LinearSpace3( const LinearSpace3<L1>& s ) : vx(s.vx), vy(s.vy), vz(s.vz) {}2627/*! matrix construction from column vectors */28__forceinline LinearSpace3(const Vector& vx, const Vector& vy, const Vector& vz)29: vx(vx), vy(vy), vz(vz) {}3031/*! construction from quaternion */32__forceinline LinearSpace3( const QuaternionT<Scalar>& q )33: vx((q.r*q.r + q.i*q.i - q.j*q.j - q.k*q.k), 2.0f*(q.i*q.j + q.r*q.k), 2.0f*(q.i*q.k - q.r*q.j))34, vy(2.0f*(q.i*q.j - q.r*q.k), (q.r*q.r - q.i*q.i + q.j*q.j - q.k*q.k), 2.0f*(q.j*q.k + q.r*q.i))35, vz(2.0f*(q.i*q.k + q.r*q.j), 2.0f*(q.j*q.k - q.r*q.i), (q.r*q.r - q.i*q.i - q.j*q.j + q.k*q.k)) {}3637/*! matrix construction from row mayor data */38__forceinline LinearSpace3(const Scalar& m00, const Scalar& m01, const Scalar& m02,39const Scalar& m10, const Scalar& m11, const Scalar& m12,40const Scalar& m20, const Scalar& m21, const Scalar& m22)41: vx(m00,m10,m20), vy(m01,m11,m21), vz(m02,m12,m22) {}4243/*! compute the determinant of the matrix */44__forceinline const Scalar det() const { return dot(vx,cross(vy,vz)); }4546/*! compute adjoint matrix */47__forceinline const LinearSpace3 adjoint() const { return LinearSpace3(cross(vy,vz),cross(vz,vx),cross(vx,vy)).transposed(); }4849/*! compute inverse matrix */50__forceinline const LinearSpace3 inverse() const { return adjoint()/det(); }5152/*! compute transposed matrix */53__forceinline const LinearSpace3 transposed() const { return LinearSpace3(vx.x,vx.y,vx.z,vy.x,vy.y,vy.z,vz.x,vz.y,vz.z); }5455/*! returns first row of matrix */56__forceinline Vector row0() const { return Vector(vx.x,vy.x,vz.x); }5758/*! returns second row of matrix */59__forceinline Vector row1() const { return Vector(vx.y,vy.y,vz.y); }6061/*! returns third row of matrix */62__forceinline Vector row2() const { return Vector(vx.z,vy.z,vz.z); }6364////////////////////////////////////////////////////////////////////////////////65/// Constants66////////////////////////////////////////////////////////////////////////////////6768__forceinline LinearSpace3( ZeroTy ) : vx(zero), vy(zero), vz(zero) {}69__forceinline LinearSpace3( OneTy ) : vx(one, zero, zero), vy(zero, one, zero), vz(zero, zero, one) {}7071/*! return matrix for scaling */72static __forceinline LinearSpace3 scale(const Vector& s) {73return LinearSpace3(s.x, 0, 0,740 , s.y, 0,750 , 0, s.z);76}7778/*! return matrix for rotation around arbitrary axis */79static __forceinline LinearSpace3 rotate(const Vector& _u, const Scalar& r) {80Vector u = normalize(_u);81Scalar s = sin(r), c = cos(r);82return LinearSpace3(u.x*u.x+(1-u.x*u.x)*c, u.x*u.y*(1-c)-u.z*s, u.x*u.z*(1-c)+u.y*s,83u.x*u.y*(1-c)+u.z*s, u.y*u.y+(1-u.y*u.y)*c, u.y*u.z*(1-c)-u.x*s,84u.x*u.z*(1-c)-u.y*s, u.y*u.z*(1-c)+u.x*s, u.z*u.z+(1-u.z*u.z)*c);85}8687public:8889/*! the column vectors of the matrix */90Vector vx,vy,vz;91};9293#if !defined(__SYCL_DEVICE_ONLY__)9495/*! compute transposed matrix */96template<> __forceinline const LinearSpace3<Vec3fa> LinearSpace3<Vec3fa>::transposed() const {97vfloat4 rx,ry,rz; transpose((vfloat4&)vx,(vfloat4&)vy,(vfloat4&)vz,vfloat4(zero),rx,ry,rz);98return LinearSpace3<Vec3fa>(Vec3fa(rx),Vec3fa(ry),Vec3fa(rz));99}100#endif101102template<typename T>103__forceinline const LinearSpace3<T> transposed(const LinearSpace3<T>& xfm) {104return xfm.transposed();105}106107////////////////////////////////////////////////////////////////////////////////108// Unary Operators109////////////////////////////////////////////////////////////////////////////////110111template<typename T> __forceinline LinearSpace3<T> operator -( const LinearSpace3<T>& a ) { return LinearSpace3<T>(-a.vx,-a.vy,-a.vz); }112template<typename T> __forceinline LinearSpace3<T> operator +( const LinearSpace3<T>& a ) { return LinearSpace3<T>(+a.vx,+a.vy,+a.vz); }113template<typename T> __forceinline LinearSpace3<T> rcp ( const LinearSpace3<T>& a ) { return a.inverse(); }114115/* constructs a coordinate frame form a normalized normal */116template<typename T> __forceinline LinearSpace3<T> frame(const T& N)117{118const T dx0(0,N.z,-N.y);119const T dx1(-N.z,0,N.x);120const T dx = normalize(select(dot(dx0,dx0) > dot(dx1,dx1),dx0,dx1));121const T dy = normalize(cross(N,dx));122return LinearSpace3<T>(dx,dy,N);123}124125/* constructs a coordinate frame from a normal and approximate x-direction */126template<typename T> __forceinline LinearSpace3<T> frame(const T& N, const T& dxi)127{128if (abs(dot(dxi,N)) > 0.99f) return frame(N); // fallback in case N and dxi are very parallel129const T dx = normalize(cross(dxi,N));130const T dy = normalize(cross(N,dx));131return LinearSpace3<T>(dx,dy,N);132}133134/* clamps linear space to range -1 to +1 */135template<typename T> __forceinline LinearSpace3<T> clamp(const LinearSpace3<T>& space) {136return LinearSpace3<T>(clamp(space.vx,T(-1.0f),T(1.0f)),137clamp(space.vy,T(-1.0f),T(1.0f)),138clamp(space.vz,T(-1.0f),T(1.0f)));139}140141////////////////////////////////////////////////////////////////////////////////142// Binary Operators143////////////////////////////////////////////////////////////////////////////////144145template<typename T> __forceinline LinearSpace3<T> operator +( const LinearSpace3<T>& a, const LinearSpace3<T>& b ) { return LinearSpace3<T>(a.vx+b.vx,a.vy+b.vy,a.vz+b.vz); }146template<typename T> __forceinline LinearSpace3<T> operator -( const LinearSpace3<T>& a, const LinearSpace3<T>& b ) { return LinearSpace3<T>(a.vx-b.vx,a.vy-b.vy,a.vz-b.vz); }147148template<typename T> __forceinline LinearSpace3<T> operator*(const typename T::Scalar & a, const LinearSpace3<T>& b) { return LinearSpace3<T>(a*b.vx, a*b.vy, a*b.vz); }149template<typename T> __forceinline T operator*(const LinearSpace3<T>& a, const T & b) { return madd(T(b.x),a.vx,madd(T(b.y),a.vy,T(b.z)*a.vz)); }150template<typename T> __forceinline LinearSpace3<T> operator*(const LinearSpace3<T>& a, const LinearSpace3<T>& b) { return LinearSpace3<T>(a*b.vx, a*b.vy, a*b.vz); }151152template<typename T> __forceinline LinearSpace3<T> operator/(const LinearSpace3<T>& a, const typename T::Scalar & b) { return LinearSpace3<T>(a.vx/b, a.vy/b, a.vz/b); }153template<typename T> __forceinline LinearSpace3<T> operator/(const LinearSpace3<T>& a, const LinearSpace3<T>& b) { return a * rcp(b); }154155template<typename T> __forceinline LinearSpace3<T>& operator *=( LinearSpace3<T>& a, const LinearSpace3<T>& b ) { return a = a * b; }156template<typename T> __forceinline LinearSpace3<T>& operator /=( LinearSpace3<T>& a, const LinearSpace3<T>& b ) { return a = a / b; }157158template<typename T> __forceinline T xfmPoint (const LinearSpace3<T>& s, const T & a) { return madd(T(a.x),s.vx,madd(T(a.y),s.vy,T(a.z)*s.vz)); }159template<typename T> __forceinline T xfmVector(const LinearSpace3<T>& s, const T & a) { return madd(T(a.x),s.vx,madd(T(a.y),s.vy,T(a.z)*s.vz)); }160template<typename T> __forceinline T xfmNormal(const LinearSpace3<T>& s, const T & a) { return xfmVector(s.inverse().transposed(),a); }161162////////////////////////////////////////////////////////////////////////////////163/// Comparison Operators164////////////////////////////////////////////////////////////////////////////////165166template<typename T> __forceinline bool operator ==( const LinearSpace3<T>& a, const LinearSpace3<T>& b ) { return a.vx == b.vx && a.vy == b.vy && a.vz == b.vz; }167template<typename T> __forceinline bool operator !=( const LinearSpace3<T>& a, const LinearSpace3<T>& b ) { return a.vx != b.vx || a.vy != b.vy || a.vz != b.vz; }168169////////////////////////////////////////////////////////////////////////////////170/// Select171////////////////////////////////////////////////////////////////////////////////172173template<typename T> __forceinline LinearSpace3<T> select ( const typename T::Scalar::Bool& s, const LinearSpace3<T>& t, const LinearSpace3<T>& f ) {174return LinearSpace3<T>(select(s,t.vx,f.vx),select(s,t.vy,f.vy),select(s,t.vz,f.vz));175}176177/*! blending */178template<typename T>179__forceinline LinearSpace3<T> lerp(const LinearSpace3<T>& l0, const LinearSpace3<T>& l1, const float t)180{181return LinearSpace3<T>(lerp(l0.vx,l1.vx,t),182lerp(l0.vy,l1.vy,t),183lerp(l0.vz,l1.vz,t));184}185186////////////////////////////////////////////////////////////////////////////////187/// Output Operators188////////////////////////////////////////////////////////////////////////////////189190template<typename T> static embree_ostream operator<<(embree_ostream cout, const LinearSpace3<T>& m) {191return cout << "{ vx = " << m.vx << ", vy = " << m.vy << ", vz = " << m.vz << "}";192}193194/*! Shortcuts for common linear spaces. */195typedef LinearSpace3<Vec3f> LinearSpace3f;196typedef LinearSpace3<Vec3fa> LinearSpace3fa;197typedef LinearSpace3<Vec3fx> LinearSpace3fx;198typedef LinearSpace3<Vec3ff> LinearSpace3ff;199200template<int N> using LinearSpace3vf = LinearSpace3<Vec3<vfloat<N>>>;201typedef LinearSpace3<Vec3<vfloat<4>>> LinearSpace3vf4;202typedef LinearSpace3<Vec3<vfloat<8>>> LinearSpace3vf8;203typedef LinearSpace3<Vec3<vfloat<16>>> LinearSpace3vf16;204205/*! blending */206template<typename T, typename S>207__forceinline LinearSpace3<T> lerp(const LinearSpace3<T>& l0,208const LinearSpace3<T>& l1,209const S& t)210{211return LinearSpace3<T>(lerp(l0.vx,l1.vx,t),212lerp(l0.vy,l1.vy,t),213lerp(l0.vz,l1.vz,t));214}215216}217218219