HyperspaceExplorer 0.7.1
|
00001 /* 00002 * File: MultithreadedTransformation.h 00003 * Author: lene 00004 * 00005 * Created on March 30, 2012, 5:59 PM 00006 */ 00007 00008 #ifndef MULTITHREADEDTRANSFORMATION_H 00009 #define MULTITHREADEDTRANSFORMATION_H 00010 00011 #include "Transformation.h" 00012 #include "SinglethreadedTransformation.h" 00013 #include "Multithreading.h" 00014 00015 #if DEBUG_TRANSFORMATION 00016 #include <QDebug> 00017 #include <QThreadPool> 00018 #include <typeinfo> 00019 #endif 00020 00022 00033 template <unsigned N, unsigned P, typename NUM = double> 00034 class MultithreadedTransformation: public TransformationImpl< N, P, NUM > { 00035 00036 public: 00037 00039 typedef typename FunctionValueGrid< N, P, NUM >::value_storage_type value_storage_type; 00040 00042 00046 virtual value_storage_type transform(const value_storage_type &x) const { 00047 return Multithreading::mapped(x, functor_); 00048 } 00049 00050 private: 00051 00053 00057 MultithreadedTransformation(const VecMath::Rotation<N, NUM> &rotation, 00058 const VecMath::Vector<N, NUM> &translation, 00059 const VecMath::Vector<N, NUM> &scale): 00060 TransformationImpl<N, P, NUM>(rotation, translation, scale), 00061 functor_(rotation, translation, scale) { } 00062 00063 void perform_transformation(const VecMath::Vector<N, NUM>& x, const VecMath::Vector<N, NUM>& xtrans); 00064 00065 friend class TransformationFactory; 00066 friend class MultithreadedTransformation<N, P+1, NUM>; 00067 00068 struct MapFunctor { 00069 00070 typedef typename MultithreadedTransformation<N, P-1, NUM>::value_storage_type result_type; 00071 00072 MapFunctor(const VecMath::Rotation<N, NUM> &rotation, 00073 const VecMath::Vector<N, NUM> &translation, 00074 const VecMath::Vector<N, NUM> &scale): 00075 matrix_(rotation), translation_(translation), scale_(scale) { } 00076 00083 typename MultithreadedTransformation<N, P-1, NUM>::value_storage_type operator() ( 00084 const VecMath::MultiDimensionalVector< VecMath::Vector<N, NUM>, P-1 > &arg 00085 ) { 00086 SinglethreadedTransformation<N, P-1, NUM> xform(matrix_, translation_, scale_); 00087 return xform.transform(arg); 00088 } 00089 00090 VecMath::Rotation<N, NUM> matrix_; 00091 VecMath::Vector<N, NUM> translation_; 00092 VecMath::Vector<N, NUM> scale_; 00093 00094 } functor_; 00095 00096 }; 00097 00099 00107 template <unsigned N, typename NUM> 00108 class MultithreadedTransformation<N, 1, NUM>: public TransformationImpl< N, 1, NUM > { 00109 00110 public: 00111 00113 typedef typename FunctionValueGrid< N, 1, NUM >::value_storage_type value_storage_type; 00114 00116 00120 virtual value_storage_type transform(const value_storage_type &x) const { 00121 # if DEBUG_TRANSFORMATION 00122 qDebug() << "MultithreadedTransformationPolicy<" << N << ", " << 1 << ", " << typeid(NUM).name() << ">::transform(" 00123 << x.toString().c_str() 00124 << ")"; 00125 # endif 00126 return Multithreading::partitionedMapped(x, functor_); 00127 } 00128 00129 private: 00130 00132 00136 MultithreadedTransformation(const VecMath::Rotation<N, NUM> &rotation, 00137 const VecMath::Vector<N, NUM> &translation, 00138 const VecMath::Vector<N, NUM> &scale): 00139 TransformationImpl<N, 1, NUM>(rotation, translation, scale), 00140 functor_(rotation, translation, scale) { } 00141 00142 void perform_transformation(const VecMath::Vector<N, NUM>& x, const VecMath::Vector<N, NUM>& xtrans); 00143 00144 friend class TransformationFactory; 00145 friend class MultithreadedTransformation<N, 2, NUM>; 00146 00147 struct MapFunctor { 00148 00149 typedef VecMath::Vector<N, NUM> result_type; 00150 00151 MapFunctor(const VecMath::Rotation<N, NUM> &rotation, 00152 const VecMath::Vector<N, NUM> &translation, 00153 const VecMath::Vector<N, NUM> &scale): 00154 matrix_(rotation), translation_(translation), scale_(scale) { } 00155 00162 VecMath::Vector<N, NUM> operator() (const VecMath::Vector<N, NUM> &arg) const { 00163 return Transformation<N, 1, NUM>::perform(arg, matrix_, translation_, scale_); 00164 } 00165 00166 VecMath::Matrix<N, NUM> matrix_; 00167 VecMath::Vector<N, NUM> translation_; 00168 VecMath::Vector<N, NUM> scale_; 00169 00170 } functor_; 00171 00172 }; 00173 00174 #endif /* MULTITHREADEDTRANSFORMATION_H */ 00175