You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
3.2 KiB

  1. #include <iostream>
  2. #include <Eigen/Geometry>
  3. #include <bench/BenchTimer.h>
  4. using namespace std;
  5. using namespace Eigen;
  6. #ifndef SCALAR
  7. #define SCALAR float
  8. #endif
  9. #ifndef SIZE
  10. #define SIZE 8
  11. #endif
  12. typedef SCALAR Scalar;
  13. typedef NumTraits<Scalar>::Real RealScalar;
  14. typedef Matrix<RealScalar,Dynamic,Dynamic> A;
  15. typedef Matrix</*Real*/Scalar,Dynamic,Dynamic> B;
  16. typedef Matrix<Scalar,Dynamic,Dynamic> C;
  17. typedef Matrix<RealScalar,Dynamic,Dynamic> M;
  18. template<typename Transformation, typename Data>
  19. EIGEN_DONT_INLINE void transform(const Transformation& t, Data& data)
  20. {
  21. EIGEN_ASM_COMMENT("begin");
  22. data = t * data;
  23. EIGEN_ASM_COMMENT("end");
  24. }
  25. template<typename Scalar, typename Data>
  26. EIGEN_DONT_INLINE void transform(const Quaternion<Scalar>& t, Data& data)
  27. {
  28. EIGEN_ASM_COMMENT("begin quat");
  29. for(int i=0;i<data.cols();++i)
  30. data.col(i) = t * data.col(i);
  31. EIGEN_ASM_COMMENT("end quat");
  32. }
  33. template<typename T> struct ToRotationMatrixWrapper
  34. {
  35. enum {Dim = T::Dim};
  36. typedef typename T::Scalar Scalar;
  37. ToRotationMatrixWrapper(const T& o) : object(o) {}
  38. T object;
  39. };
  40. template<typename QType, typename Data>
  41. EIGEN_DONT_INLINE void transform(const ToRotationMatrixWrapper<QType>& t, Data& data)
  42. {
  43. EIGEN_ASM_COMMENT("begin quat via mat");
  44. data = t.object.toRotationMatrix() * data;
  45. EIGEN_ASM_COMMENT("end quat via mat");
  46. }
  47. template<typename Scalar, int Dim, typename Data>
  48. EIGEN_DONT_INLINE void transform(const Transform<Scalar,Dim,Projective>& t, Data& data)
  49. {
  50. data = (t * data.colwise().homogeneous()).template block<Dim,Data::ColsAtCompileTime>(0,0);
  51. }
  52. template<typename T> struct get_dim { enum { Dim = T::Dim }; };
  53. template<typename S, int R, int C, int O, int MR, int MC>
  54. struct get_dim<Matrix<S,R,C,O,MR,MC> > { enum { Dim = R }; };
  55. template<typename Transformation, int N>
  56. struct bench_impl
  57. {
  58. static EIGEN_DONT_INLINE void run(const Transformation& t)
  59. {
  60. Matrix<typename Transformation::Scalar,get_dim<Transformation>::Dim,N> data;
  61. data.setRandom();
  62. bench_impl<Transformation,N-1>::run(t);
  63. BenchTimer timer;
  64. BENCH(timer,10,100000,transform(t,data));
  65. cout.width(9);
  66. cout << timer.best() << " ";
  67. }
  68. };
  69. template<typename Transformation>
  70. struct bench_impl<Transformation,0>
  71. {
  72. static EIGEN_DONT_INLINE void run(const Transformation&) {}
  73. };
  74. template<typename Transformation>
  75. EIGEN_DONT_INLINE void bench(const std::string& msg, const Transformation& t)
  76. {
  77. cout << msg << " ";
  78. bench_impl<Transformation,SIZE>::run(t);
  79. std::cout << "\n";
  80. }
  81. int main(int argc, char ** argv)
  82. {
  83. Matrix<Scalar,3,4> mat34; mat34.setRandom();
  84. Transform<Scalar,3,Isometry> iso3(mat34);
  85. Transform<Scalar,3,Affine> aff3(mat34);
  86. Transform<Scalar,3,AffineCompact> caff3(mat34);
  87. Transform<Scalar,3,Projective> proj3(mat34);
  88. Quaternion<Scalar> quat;quat.setIdentity();
  89. ToRotationMatrixWrapper<Quaternion<Scalar> > quatmat(quat);
  90. Matrix<Scalar,3,3> mat33; mat33.setRandom();
  91. cout.precision(4);
  92. std::cout
  93. << "N ";
  94. for(int i=0;i<SIZE;++i)
  95. {
  96. cout.width(9);
  97. cout << i+1 << " ";
  98. }
  99. cout << "\n";
  100. bench("matrix 3x3", mat33);
  101. bench("quaternion", quat);
  102. bench("quat-mat ", quatmat);
  103. bench("isometry3 ", iso3);
  104. bench("affine3 ", aff3);
  105. bench("c affine3 ", caff3);
  106. bench("proj3 ", proj3);
  107. }