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

Generated on Mon Jan 24 18:36:32 2005 for Sh by  doxygen 1.4.1