Path: blob/master/thirdparty/embree/common/math/affinespace.h
9912 views
// Copyright 2009-2021 Intel Corporation1// SPDX-License-Identifier: Apache-2.023#pragma once45#include "linearspace2.h"6#include "linearspace3.h"7#include "quaternion.h"8#include "bbox.h"9#include "vec4.h"1011namespace embree12{13#define VectorT typename L::Vector14#define ScalarT typename L::Vector::Scalar1516////////////////////////////////////////////////////////////////////////////////17// Affine Space18////////////////////////////////////////////////////////////////////////////////1920template<typename L>21struct AffineSpaceT22{23L l; /*< linear part of affine space */24VectorT p; /*< affine part of affine space */2526////////////////////////////////////////////////////////////////////////////////27// Constructors, Assignment, Cast, Copy Operations28////////////////////////////////////////////////////////////////////////////////2930__forceinline AffineSpaceT ( ) { }31__forceinline AffineSpaceT ( const AffineSpaceT& other ) { l = other.l; p = other.p; }32__forceinline AffineSpaceT ( const L & other ) { l = other ; p = VectorT(zero); }33__forceinline AffineSpaceT& operator=( const AffineSpaceT& other ) { l = other.l; p = other.p; return *this; }3435__forceinline AffineSpaceT( const VectorT& vx, const VectorT& vy, const VectorT& vz, const VectorT& p ) : l(vx,vy,vz), p(p) {}36__forceinline AffineSpaceT( const L& l, const VectorT& p ) : l(l), p(p) {}3738template<typename L1> __forceinline AffineSpaceT( const AffineSpaceT<L1>& s ) : l(s.l), p(s.p) {}3940////////////////////////////////////////////////////////////////////////////////41// Constants42////////////////////////////////////////////////////////////////////////////////4344__forceinline AffineSpaceT( ZeroTy ) : l(zero), p(zero) {}45__forceinline AffineSpaceT( OneTy ) : l(one), p(zero) {}4647/*! return matrix for scaling */48static __forceinline AffineSpaceT scale(const VectorT& s) { return L::scale(s); }4950/*! return matrix for translation */51static __forceinline AffineSpaceT translate(const VectorT& p) { return AffineSpaceT(one,p); }5253/*! return matrix for rotation, only in 2D */54static __forceinline AffineSpaceT rotate(const ScalarT& r) { return L::rotate(r); }5556/*! return matrix for rotation around arbitrary point (2D) or axis (3D) */57static __forceinline AffineSpaceT rotate(const VectorT& u, const ScalarT& r) { return L::rotate(u,r); }5859/*! return matrix for rotation around arbitrary axis and point, only in 3D */60static __forceinline AffineSpaceT rotate(const VectorT& p, const VectorT& u, const ScalarT& r) { return translate(+p) * rotate(u,r) * translate(-p); }6162/*! return matrix for looking at given point, only in 3D */63static __forceinline AffineSpaceT lookat(const VectorT& eye, const VectorT& point, const VectorT& up) {64VectorT Z = normalize(point-eye);65VectorT U = normalize(cross(up,Z));66VectorT V = normalize(cross(Z,U));67return AffineSpaceT(L(U,V,Z),eye);68}6970};7172// template specialization to get correct identity matrix for type AffineSpace3fa73template<>74__forceinline AffineSpaceT<LinearSpace3ff>::AffineSpaceT( OneTy ) : l(one), p(0.f, 0.f, 0.f, 1.f) {}7576////////////////////////////////////////////////////////////////////////////////77// Unary Operators78////////////////////////////////////////////////////////////////////////////////7980template<typename L> __forceinline AffineSpaceT<L> operator -( const AffineSpaceT<L>& a ) { return AffineSpaceT<L>(-a.l,-a.p); }81template<typename L> __forceinline AffineSpaceT<L> operator +( const AffineSpaceT<L>& a ) { return AffineSpaceT<L>(+a.l,+a.p); }82template<typename L> __forceinline AffineSpaceT<L> rcp( const AffineSpaceT<L>& a ) { L il = rcp(a.l); return AffineSpaceT<L>(il,-(il*a.p)); }8384////////////////////////////////////////////////////////////////////////////////85// Binary Operators86////////////////////////////////////////////////////////////////////////////////8788template<typename L> __forceinline const AffineSpaceT<L> operator +( const AffineSpaceT<L>& a, const AffineSpaceT<L>& b ) { return AffineSpaceT<L>(a.l+b.l,a.p+b.p); }89template<typename L> __forceinline const AffineSpaceT<L> operator -( const AffineSpaceT<L>& a, const AffineSpaceT<L>& b ) { return AffineSpaceT<L>(a.l-b.l,a.p-b.p); }9091template<typename L> __forceinline const AffineSpaceT<L> operator *( const ScalarT & a, const AffineSpaceT<L>& b ) { return AffineSpaceT<L>(a*b.l,a*b.p); }92template<typename L> __forceinline const AffineSpaceT<L> operator *( const AffineSpaceT<L>& a, const AffineSpaceT<L>& b ) { return AffineSpaceT<L>(a.l*b.l,a.l*b.p+a.p); }93template<typename L> __forceinline const AffineSpaceT<L> operator /( const AffineSpaceT<L>& a, const AffineSpaceT<L>& b ) { return a * rcp(b); }94template<typename L> __forceinline const AffineSpaceT<L> operator /( const AffineSpaceT<L>& a, const ScalarT & b ) { return a * rcp(b); }9596template<typename L> __forceinline AffineSpaceT<L>& operator *=( AffineSpaceT<L>& a, const AffineSpaceT<L>& b ) { return a = a * b; }97template<typename L> __forceinline AffineSpaceT<L>& operator *=( AffineSpaceT<L>& a, const ScalarT & b ) { return a = a * b; }98template<typename L> __forceinline AffineSpaceT<L>& operator /=( AffineSpaceT<L>& a, const AffineSpaceT<L>& b ) { return a = a / b; }99template<typename L> __forceinline AffineSpaceT<L>& operator /=( AffineSpaceT<L>& a, const ScalarT & b ) { return a = a / b; }100101template<typename L> __forceinline VectorT xfmPoint (const AffineSpaceT<L>& m, const VectorT& p) { return madd(VectorT(p.x),m.l.vx,madd(VectorT(p.y),m.l.vy,madd(VectorT(p.z),m.l.vz,m.p))); }102template<typename L> __forceinline VectorT xfmVector(const AffineSpaceT<L>& m, const VectorT& v) { return xfmVector(m.l,v); }103template<typename L> __forceinline VectorT xfmNormal(const AffineSpaceT<L>& m, const VectorT& n) { return xfmNormal(m.l,n); }104105__forceinline const BBox<Vec3fa> xfmBounds(const AffineSpaceT<LinearSpace3<Vec3fa> >& m, const BBox<Vec3fa>& b)106{107BBox3fa dst = empty;108const Vec3fa p0(b.lower.x,b.lower.y,b.lower.z); dst.extend(xfmPoint(m,p0));109const Vec3fa p1(b.lower.x,b.lower.y,b.upper.z); dst.extend(xfmPoint(m,p1));110const Vec3fa p2(b.lower.x,b.upper.y,b.lower.z); dst.extend(xfmPoint(m,p2));111const Vec3fa p3(b.lower.x,b.upper.y,b.upper.z); dst.extend(xfmPoint(m,p3));112const Vec3fa p4(b.upper.x,b.lower.y,b.lower.z); dst.extend(xfmPoint(m,p4));113const Vec3fa p5(b.upper.x,b.lower.y,b.upper.z); dst.extend(xfmPoint(m,p5));114const Vec3fa p6(b.upper.x,b.upper.y,b.lower.z); dst.extend(xfmPoint(m,p6));115const Vec3fa p7(b.upper.x,b.upper.y,b.upper.z); dst.extend(xfmPoint(m,p7));116return dst;117}118119////////////////////////////////////////////////////////////////////////////////120/// Comparison Operators121////////////////////////////////////////////////////////////////////////////////122123template<typename L> __forceinline bool operator ==( const AffineSpaceT<L>& a, const AffineSpaceT<L>& b ) { return a.l == b.l && a.p == b.p; }124template<typename L> __forceinline bool operator !=( const AffineSpaceT<L>& a, const AffineSpaceT<L>& b ) { return a.l != b.l || a.p != b.p; }125126////////////////////////////////////////////////////////////////////////////////127/// Select128////////////////////////////////////////////////////////////////////////////////129130template<typename L> __forceinline AffineSpaceT<L> select ( const typename L::Vector::Scalar::Bool& s, const AffineSpaceT<L>& t, const AffineSpaceT<L>& f ) {131return AffineSpaceT<L>(select(s,t.l,f.l),select(s,t.p,f.p));132}133134////////////////////////////////////////////////////////////////////////////////135// Output Operators136////////////////////////////////////////////////////////////////////////////////137138template<typename L> static embree_ostream operator<<(embree_ostream cout, const AffineSpaceT<L>& m) {139return cout << "{ l = " << m.l << ", p = " << m.p << " }";140}141142////////////////////////////////////////////////////////////////////////////////143// Template Instantiations144////////////////////////////////////////////////////////////////////////////////145146typedef AffineSpaceT<LinearSpace2f> AffineSpace2f;147typedef AffineSpaceT<LinearSpace3f> AffineSpace3f;148typedef AffineSpaceT<LinearSpace3fa> AffineSpace3fa;149typedef AffineSpaceT<LinearSpace3fx> AffineSpace3fx;150typedef AffineSpaceT<LinearSpace3ff> AffineSpace3ff;151typedef AffineSpaceT<Quaternion3f > OrthonormalSpace3f;152153template<int N> using AffineSpace3vf = AffineSpaceT<LinearSpace3<Vec3<vfloat<N>>>>;154typedef AffineSpaceT<LinearSpace3<Vec3<vfloat<4>>>> AffineSpace3vf4;155typedef AffineSpaceT<LinearSpace3<Vec3<vfloat<8>>>> AffineSpace3vf8;156typedef AffineSpaceT<LinearSpace3<Vec3<vfloat<16>>>> AffineSpace3vf16;157158template<int N> using AffineSpace3vff = AffineSpaceT<LinearSpace3<Vec4<vfloat<N>>>>;159typedef AffineSpaceT<LinearSpace3<Vec4<vfloat<4>>>> AffineSpace3vfa4;160typedef AffineSpaceT<LinearSpace3<Vec4<vfloat<8>>>> AffineSpace3vfa8;161typedef AffineSpaceT<LinearSpace3<Vec4<vfloat<16>>>> AffineSpace3vfa16;162163//////////////////////////////////////////////////////////////////////////////164/// Interpolation165//////////////////////////////////////////////////////////////////////////////166template<typename T, typename R>167__forceinline AffineSpaceT<T> lerp(const AffineSpaceT<T>& M0,168const AffineSpaceT<T>& M1,169const R& t)170{171return AffineSpaceT<T>(lerp(M0.l,M1.l,t),lerp(M0.p,M1.p,t));172}173174// slerp interprets the 16 floats of the matrix M = D * R * S as components of175// three matrizes (D, R, S) that are interpolated individually.176template<typename T> __forceinline AffineSpaceT<LinearSpace3<Vec3<T>>>177slerp(const AffineSpaceT<LinearSpace3<Vec4<T>>>& M0,178const AffineSpaceT<LinearSpace3<Vec4<T>>>& M1,179const T& t)180{181QuaternionT<T> q0(M0.p.w, M0.l.vx.w, M0.l.vy.w, M0.l.vz.w);182QuaternionT<T> q1(M1.p.w, M1.l.vx.w, M1.l.vy.w, M1.l.vz.w);183QuaternionT<T> q = slerp(q0, q1, t);184185AffineSpaceT<LinearSpace3<Vec3<T>>> S = lerp(M0, M1, t);186AffineSpaceT<LinearSpace3<Vec3<T>>> D(one);187D.p.x = S.l.vx.y;188D.p.y = S.l.vx.z;189D.p.z = S.l.vy.z;190S.l.vx.y = 0;191S.l.vx.z = 0;192S.l.vy.z = 0;193194AffineSpaceT<LinearSpace3<Vec3<T>>> R = LinearSpace3<Vec3<T>>(q);195return D * R * S;196}197198// this is a specialized version for Vec3fa because that does199// not play along nicely with the other templated Vec3/Vec4 types200__forceinline AffineSpace3fa slerp(const AffineSpace3ff& M0,201const AffineSpace3ff& M1,202const float& t)203{204Quaternion3f q0(M0.p.w, M0.l.vx.w, M0.l.vy.w, M0.l.vz.w);205Quaternion3f q1(M1.p.w, M1.l.vx.w, M1.l.vy.w, M1.l.vz.w);206Quaternion3f q = slerp(q0, q1, t);207208AffineSpace3fa S = lerp(M0, M1, t);209AffineSpace3fa D(one);210D.p.x = S.l.vx.y;211D.p.y = S.l.vx.z;212D.p.z = S.l.vy.z;213S.l.vx.y = 0;214S.l.vx.z = 0;215S.l.vy.z = 0;216217AffineSpace3fa R = LinearSpace3fa(q);218return D * R * S;219}220221__forceinline AffineSpace3fa quaternionDecompositionToAffineSpace(const AffineSpace3ff& qd)222{223// compute affine transform from quaternion decomposition224Quaternion3f q(qd.p.w, qd.l.vx.w, qd.l.vy.w, qd.l.vz.w);225AffineSpace3fa M = qd;226AffineSpace3fa D(one);227D.p.x = M.l.vx.y;228D.p.y = M.l.vx.z;229D.p.z = M.l.vy.z;230M.l.vx.y = 0;231M.l.vx.z = 0;232M.l.vy.z = 0;233AffineSpace3fa R = LinearSpace3fa(q);234return D * R * M;235}236237__forceinline void quaternionDecomposition(const AffineSpace3ff& qd, Vec3fa& T, Quaternion3f& q, AffineSpace3fa& S)238{239q = Quaternion3f(qd.p.w, qd.l.vx.w, qd.l.vy.w, qd.l.vz.w);240S = qd;241T.x = qd.l.vx.y;242T.y = qd.l.vx.z;243T.z = qd.l.vy.z;244S.l.vx.y = 0;245S.l.vx.z = 0;246S.l.vy.z = 0;247}248249__forceinline AffineSpace3fx quaternionDecomposition(Vec3fa const& T, Quaternion3f const& q, AffineSpace3fa const& S)250{251AffineSpace3ff M = S;252M.l.vx.w = q.i;253M.l.vy.w = q.j;254M.l.vz.w = q.k;255M.p.w = q.r;256M.l.vx.y = T.x;257M.l.vx.z = T.y;258M.l.vy.z = T.z;259return M;260}261262struct __aligned(16) QuaternionDecomposition263{264float scale_x = 1.f;265float scale_y = 1.f;266float scale_z = 1.f;267float skew_xy = 0.f;268float skew_xz = 0.f;269float skew_yz = 0.f;270float shift_x = 0.f;271float shift_y = 0.f;272float shift_z = 0.f;273float quaternion_r = 1.f;274float quaternion_i = 0.f;275float quaternion_j = 0.f;276float quaternion_k = 0.f;277float translation_x = 0.f;278float translation_y = 0.f;279float translation_z = 0.f;280};281282__forceinline QuaternionDecomposition quaternionDecomposition(AffineSpace3ff const& M)283{284QuaternionDecomposition qd;285qd.scale_x = M.l.vx.x;286qd.scale_y = M.l.vy.y;287qd.scale_z = M.l.vz.z;288qd.shift_x = M.p.x;289qd.shift_y = M.p.y;290qd.shift_z = M.p.z;291qd.translation_x = M.l.vx.y;292qd.translation_y = M.l.vx.z;293qd.translation_z = M.l.vy.z;294qd.skew_xy = M.l.vy.x;295qd.skew_xz = M.l.vz.x;296qd.skew_yz = M.l.vz.y;297qd.quaternion_r = M.p.w;298qd.quaternion_i = M.l.vx.w;299qd.quaternion_j = M.l.vy.w;300qd.quaternion_k = M.l.vz.w;301return qd;302}303304////////////////////////////////////////////////////////////////////////////////305/*306* ! Template Specialization for 2D: return matrix for rotation around point307* (rotation around arbitrarty vector is not meaningful in 2D)308*/309template<> __forceinline310AffineSpace2f AffineSpace2f::rotate(const Vec2f& p, const float& r) {311return translate(+p)*AffineSpace2f(LinearSpace2f::rotate(r))*translate(-p);312}313314////////////////////////////////////////////////////////////////////////////////315// Similarity Transform316//317// checks, if M is a similarity transformation, i.e if there exists a factor D318// such that for all x,y: distance(Mx, My) = D * distance(x, y)319////////////////////////////////////////////////////////////////////////////////320__forceinline bool similarityTransform(const AffineSpace3fa& M, float* D)321{322if (D) *D = 0.f;323if (abs(dot(M.l.vx, M.l.vy)) > 1e-5f) return false;324if (abs(dot(M.l.vx, M.l.vz)) > 1e-5f) return false;325if (abs(dot(M.l.vy, M.l.vz)) > 1e-5f) return false;326327const float D_x = dot(M.l.vx, M.l.vx);328const float D_y = dot(M.l.vy, M.l.vy);329const float D_z = dot(M.l.vz, M.l.vz);330331if (abs(D_x - D_y) > 1e-5f ||332abs(D_x - D_z) > 1e-5f ||333abs(D_y - D_z) > 1e-5f)334return false;335336if (D) *D = sqrtf(D_x);337return true;338}339340__forceinline void AffineSpace3fa_store_unaligned(const AffineSpace3fa &source, AffineSpace3fa* ptr)341{342Vec3fa::storeu(&ptr->l.vx, source.l.vx);343Vec3fa::storeu(&ptr->l.vy, source.l.vy);344Vec3fa::storeu(&ptr->l.vz, source.l.vz);345Vec3fa::storeu(&ptr->p, source.p);346}347348__forceinline AffineSpace3fa AffineSpace3fa_load_unaligned(AffineSpace3fa* ptr)349{350AffineSpace3fa space;351space.l.vx = Vec3fa::loadu(&ptr->l.vx);352space.l.vy = Vec3fa::loadu(&ptr->l.vy);353space.l.vz = Vec3fa::loadu(&ptr->l.vz);354space.p = Vec3fa::loadu(&ptr->p);355return space;356}357358#undef VectorT359#undef ScalarT360}361362363