Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

ShLibGeometryImpl.hpp

00001 // Sh: A GPU metaprogramming language.
00002 //
00003 // Copyright 2003-2005 Serious Hack Inc.
00004 // 
00005 // This software is provided 'as-is', without any express or implied
00006 // warranty. In no event will the authors be held liable for any damages
00007 // arising from the use of this software.
00008 // 
00009 // Permission is granted to anyone to use this software for any purpose,
00010 // including commercial applications, and to alter it and redistribute it
00011 // freely, subject to the following restrictions:
00012 // 
00013 // 1. The origin of this software must not be misrepresented; you must
00014 // not claim that you wrote the original software. If you use this
00015 // software in a product, an acknowledgment in the product documentation
00016 // would be appreciated but is not required.
00017 // 
00018 // 2. Altered source versions must be plainly marked as such, and must
00019 // not be misrepresented as being the original software.
00020 // 
00021 // 3. This notice may not be removed or altered from any source
00022 // distribution.
00024 #ifndef SHLIBGEOMETRYIMPL_HPP
00025 #define SHLIBGEOMETRYIMPL_HPP
00026 
00027 #include "ShLibClamp.hpp"
00028 #include "ShAttrib.hpp"
00029 #include "ShInstructions.hpp"
00030 
00031 namespace SH {
00032 
00033 template<typename T1, typename T2>
00034 ShGeneric<3, CT1T2> cross(const ShGeneric<3, T1>& left, const ShGeneric<3, T2>& right)
00035 {
00036   ShAttrib<3, SH_TEMP, CT1T2> t;
00037   shXPD(t, left, right);
00038   return t;
00039 }
00040 
00041 template<typename T1, typename T2>
00042 inline
00043 ShGeneric<3, CT1T2> operator^(const ShGeneric<3, T1>& left, const ShGeneric<3, T2>& right)
00044 {
00045   return cross(left, right);
00046 }
00047 
00048 template<int N, typename T>
00049 ShGeneric<N, T> normalize(const ShGeneric<N, T>& var)
00050 {
00051   ShAttrib<N, SH_TEMP, T> t;
00052   shNORM(t, var);
00053   return t;
00054 }
00055 
00056 template<int N, typename T1, typename T2>
00057 ShGeneric<1, CT1T2> dot(const ShGeneric<N, T1>& left, const ShGeneric<N, T2>& right)
00058 {
00059   ShAttrib<1, SH_TEMP, CT1T2> t;
00060   shDOT(t, left, right);
00061   return t;
00062 }
00063 
00064 template<int N, typename T1, typename T2>
00065 inline
00066 ShGeneric<1,  CT1T2> operator|(const ShGeneric<N, T1>& left, const ShGeneric<N, T2>& right)
00067 {
00068   return dot(left, right);
00069 }
00070 SH_SHLIB_CONST_N_OP_RETSIZE_BOTH(dot, 1);
00071 
00072 template<int N, typename T1, typename T2>
00073 ShGeneric<N, CT1T2> reflect(const ShGeneric<N, T1>& a, const ShGeneric<N, T2>& b)
00074 {
00075   ShGeneric<N, T2> bn = normalize(b);
00076   return 2 * dot(a, b) * b - a;
00077 }
00078 
00079 template<int N, typename T1, typename T2, typename T3>
00080 ShGeneric<N, CT1T2T3> refract(const ShGeneric<N, T1>& v, const ShGeneric<N, T2>& n,
00081                         const ShGeneric<1, T3>& theta)
00082 {
00083   ShGeneric<N, T1> vn = normalize(v);
00084   ShGeneric<N, T2> nn = normalize(n);
00085   ShGeneric<1, CT1T2T3> c = (-vn|nn);
00086   ShGeneric<1, CT1T2T3> k = c*c - ShDataTypeConstant<CT1T2T3, SH_HOST>::One;
00087   k = ShDataTypeConstant<CT1T2T3, SH_HOST>::One + theta*theta*k;
00088   k = clamp(k, ShDataTypeConstant<CT1T2T3, SH_HOST>::Zero, ShDataTypeConstant<CT1T2T3, SH_HOST>::One); 
00089   ShGeneric<1, CT1T2T3> a = theta;
00090   ShGeneric<1, CT1T2T3> b = theta*c - sqrt(k);
00091   return (a*vn + b*nn);
00092 }
00093 
00094 template<int N, typename T1, typename T2>
00095 inline
00096 ShGeneric<N, CT1T2> faceforward(const ShGeneric<N, T1>& a, const ShGeneric<N, T2>& b)
00097 {
00098   return (2 * (dot(a, b) > 0) - 1) * b;
00099 }
00100 
00101 template<typename T1, typename T2, typename T3>
00102 ShGeneric<4, CT1T2T3> lit(const ShGeneric<1, T1>& a,
00103                           const ShGeneric<1, T2>& b,
00104                           const ShGeneric<1, T3>& c)
00105 {
00106   ShAttrib<4, SH_TEMP, CT1T2T3> i;
00107   ShAttrib<4, SH_TEMP, CT1T2T3> r;
00108   shLIT(r, join(a, b, c, c));
00109   return r;
00110 }
00111 
00112 template<int N, typename T>
00113 inline
00114 ShGeneric<1, T> distance(const ShGeneric<N, T>& a, const ShGeneric<N, T>& b)
00115 {
00116   return length(a-b);
00117 }
00118 
00119 template<int N, typename T>
00120 inline
00121 ShGeneric<1, T> distance_1(const ShGeneric<N, T>& a, const ShGeneric<N, T>& b)
00122 {
00123   return length_1(a-b);
00124 }
00125 
00126 template<int N, typename T>
00127 inline
00128 ShGeneric<1, T> distance_inf(const ShGeneric<N, T>& a, const ShGeneric<N, T>& b)
00129 {
00130   return length_inf(a-b);
00131 }
00132 
00133 template<int N, typename T>
00134 inline
00135 ShGeneric<1, T> length(const ShGeneric<N, T>& a)
00136 {
00137   return sqrt(dot(a, a));
00138 }
00139 
00140 template<int N, typename T>
00141 inline
00142 ShGeneric<1, T> length_1(const ShGeneric<N, T>& a)
00143 {
00144   return sum(abs(a));
00145 }
00146 
00147 template<int N, typename T>
00148 inline
00149 ShGeneric<1, T> length_inf(const ShGeneric<N, T>& a)
00150 {
00151   return max(abs(a));
00152 }
00153 
00154 }
00155 
00156 #endif

Generated on Wed Jun 15 18:12:40 2005 for Sh by  doxygen 1.4.3-20050530