// ======================================================================== // // Copyright 2009-2017 Intel Corporation // // // // Licensed under the Apache License, Version 2.0 (the "License"); // // you may not use this file except in compliance with the License. // // You may obtain a copy of the License at // // // // http://www.apache.org/licenses/LICENSE-2.0 // // // // Unless required by applicable law or agreed to in writing, software // // distributed under the License is distributed on an "AS IS" BASIS, // // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // // See the License for the specific language governing permissions and // // limitations under the License. // // ======================================================================== // #pragma once #include "linearspace2.h" #include "linearspace3.h" #include "quaternion.h" #include "bbox.h" namespace embree { #define VectorT typename L::Vector #define ScalarT typename L::Vector::Scalar //////////////////////////////////////////////////////////////////////////////// // Affine Space //////////////////////////////////////////////////////////////////////////////// template struct AffineSpaceT { L l; /*< linear part of affine space */ VectorT p; /*< affine part of affine space */ //////////////////////////////////////////////////////////////////////////////// // Constructors, Assignment, Cast, Copy Operations //////////////////////////////////////////////////////////////////////////////// __forceinline AffineSpaceT ( ) { } __forceinline AffineSpaceT ( const AffineSpaceT& other ) { l = other.l; p = other.p; } __forceinline AffineSpaceT ( const L & other ) { l = other ; p = VectorT(zero); } __forceinline AffineSpaceT& operator=( const AffineSpaceT& other ) { l = other.l; p = other.p; return *this; } __forceinline AffineSpaceT( const VectorT& vx, const VectorT& vy, const VectorT& vz, const VectorT& p ) : l(vx,vy,vz), p(p) {} __forceinline AffineSpaceT( const L& l, const VectorT& p ) : l(l), p(p) {} template __forceinline AffineSpaceT( const AffineSpaceT& s ) : l(s.l), p(s.p) {} //////////////////////////////////////////////////////////////////////////////// // Constants //////////////////////////////////////////////////////////////////////////////// __forceinline AffineSpaceT( ZeroTy ) : l(zero), p(zero) {} __forceinline AffineSpaceT( OneTy ) : l(one), p(zero) {} /*! return matrix for scaling */ static __forceinline AffineSpaceT scale(const VectorT& s) { return L::scale(s); } /*! return matrix for translation */ static __forceinline AffineSpaceT translate(const VectorT& p) { return AffineSpaceT(one,p); } /*! return matrix for rotation, only in 2D */ static __forceinline AffineSpaceT rotate(const ScalarT& r) { return L::rotate(r); } /*! return matrix for rotation around arbitrary point (2D) or axis (3D) */ static __forceinline AffineSpaceT rotate(const VectorT& u, const ScalarT& r) { return L::rotate(u,r); } /*! return matrix for rotation around arbitrary axis and point, only in 3D */ static __forceinline AffineSpaceT rotate(const VectorT& p, const VectorT& u, const ScalarT& r) { return translate(+p) * rotate(u,r) * translate(-p); } /*! return matrix for looking at given point, only in 3D */ static __forceinline AffineSpaceT lookat(const VectorT& eye, const VectorT& point, const VectorT& up) { VectorT Z = normalize(point-eye); VectorT U = normalize(cross(up,Z)); VectorT V = normalize(cross(Z,U)); return AffineSpaceT(L(U,V,Z),eye); } }; //////////////////////////////////////////////////////////////////////////////// // Unary Operators //////////////////////////////////////////////////////////////////////////////// template __forceinline AffineSpaceT operator -( const AffineSpaceT& a ) { return AffineSpaceT(-a.l,-a.p); } template __forceinline AffineSpaceT operator +( const AffineSpaceT& a ) { return AffineSpaceT(+a.l,+a.p); } template __forceinline AffineSpaceT rcp( const AffineSpaceT& a ) { L il = rcp(a.l); return AffineSpaceT(il,-(il*a.p)); } //////////////////////////////////////////////////////////////////////////////// // Binary Operators //////////////////////////////////////////////////////////////////////////////// template __forceinline const AffineSpaceT operator +( const AffineSpaceT& a, const AffineSpaceT& b ) { return AffineSpaceT(a.l+b.l,a.p+b.p); } template __forceinline const AffineSpaceT operator -( const AffineSpaceT& a, const AffineSpaceT& b ) { return AffineSpaceT(a.l-b.l,a.p-b.p); } template __forceinline const AffineSpaceT operator *( const ScalarT & a, const AffineSpaceT& b ) { return AffineSpaceT(a*b.l,a*b.p); } template __forceinline const AffineSpaceT operator *( const AffineSpaceT& a, const AffineSpaceT& b ) { return AffineSpaceT(a.l*b.l,a.l*b.p+a.p); } template __forceinline const AffineSpaceT operator /( const AffineSpaceT& a, const AffineSpaceT& b ) { return a * rcp(b); } template __forceinline const AffineSpaceT operator /( const AffineSpaceT& a, const ScalarT & b ) { return a * rcp(b); } template __forceinline AffineSpaceT& operator *=( AffineSpaceT& a, const AffineSpaceT& b ) { return a = a * b; } template __forceinline AffineSpaceT& operator *=( AffineSpaceT& a, const ScalarT & b ) { return a = a * b; } template __forceinline AffineSpaceT& operator /=( AffineSpaceT& a, const AffineSpaceT& b ) { return a = a / b; } template __forceinline AffineSpaceT& operator /=( AffineSpaceT& a, const ScalarT & b ) { return a = a / b; } template __forceinline const VectorT xfmPoint (const AffineSpaceT& 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))); } template __forceinline const VectorT xfmVector(const AffineSpaceT& m, const VectorT& v) { return xfmVector(m.l,v); } template __forceinline const VectorT xfmNormal(const AffineSpaceT& m, const VectorT& n) { return xfmNormal(m.l,n); } __forceinline const BBox xfmBounds(const AffineSpaceT >& m, const BBox& b) { BBox3fa dst = empty; const Vec3fa p0(b.lower.x,b.lower.y,b.lower.z); dst.extend(xfmPoint(m,p0)); const Vec3fa p1(b.lower.x,b.lower.y,b.upper.z); dst.extend(xfmPoint(m,p1)); const Vec3fa p2(b.lower.x,b.upper.y,b.lower.z); dst.extend(xfmPoint(m,p2)); const Vec3fa p3(b.lower.x,b.upper.y,b.upper.z); dst.extend(xfmPoint(m,p3)); const Vec3fa p4(b.upper.x,b.lower.y,b.lower.z); dst.extend(xfmPoint(m,p4)); const Vec3fa p5(b.upper.x,b.lower.y,b.upper.z); dst.extend(xfmPoint(m,p5)); const Vec3fa p6(b.upper.x,b.upper.y,b.lower.z); dst.extend(xfmPoint(m,p6)); const Vec3fa p7(b.upper.x,b.upper.y,b.upper.z); dst.extend(xfmPoint(m,p7)); return dst; } //////////////////////////////////////////////////////////////////////////////// /// Comparison Operators //////////////////////////////////////////////////////////////////////////////// template __forceinline bool operator ==( const AffineSpaceT& a, const AffineSpaceT& b ) { return a.l == b.l && a.p == b.p; } template __forceinline bool operator !=( const AffineSpaceT& a, const AffineSpaceT& b ) { return a.l != b.l || a.p != b.p; } //////////////////////////////////////////////////////////////////////////////// /// Select //////////////////////////////////////////////////////////////////////////////// template __forceinline AffineSpaceT select ( const typename L::Vector::Scalar::Bool& s, const AffineSpaceT& t, const AffineSpaceT& f ) { return AffineSpaceT(select(s,t.l,f.l),select(s,t.p,f.p)); } /*! blending */ template __forceinline AffineSpaceT lerp(const AffineSpaceT& l0, const AffineSpaceT& l1, const float t) { return AffineSpaceT(lerp(l0.l,l1.l,t),lerp(l0.p,l1.p,t)); } //////////////////////////////////////////////////////////////////////////////// // Output Operators //////////////////////////////////////////////////////////////////////////////// template static std::ostream& operator<<(std::ostream& cout, const AffineSpaceT& m) { return cout << "{ l = " << m.l << ", p = " << m.p << " }"; } //////////////////////////////////////////////////////////////////////////////// // Template Instantiations //////////////////////////////////////////////////////////////////////////////// typedef AffineSpaceT AffineSpace2f; typedef AffineSpaceT AffineSpace3f; typedef AffineSpaceT AffineSpace3fa; typedef AffineSpaceT OrthonormalSpace3f; //////////////////////////////////////////////////////////////////////////////// /*! Template Specialization for 2D: return matrix for rotation around point (rotation around arbitrarty vector is not meaningful in 2D) */ template<> __forceinline AffineSpace2f AffineSpace2f::rotate(const Vec2f& p, const float& r) { return translate(+p) * AffineSpace2f(LinearSpace2f::rotate(r)) * translate(-p); } #undef VectorT #undef ScalarT }