aboutsummaryrefslogtreecommitdiffhomepage
path: root/bench/geometry.cpp
blob: b187a515f58d1f6f64f79a75614a404376016c02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126

#include <iostream>
#include <Eigen/Geometry>
#include <bench/BenchTimer.h>

using namespace std;
using namespace Eigen;

#ifndef SCALAR
#define SCALAR float
#endif

#ifndef SIZE
#define SIZE 8
#endif

typedef SCALAR Scalar;
typedef NumTraits<Scalar>::Real RealScalar;
typedef Matrix<RealScalar,Dynamic,Dynamic> A;
typedef Matrix</*Real*/Scalar,Dynamic,Dynamic> B;
typedef Matrix<Scalar,Dynamic,Dynamic> C;
typedef Matrix<RealScalar,Dynamic,Dynamic> M;

template<typename Transformation, typename Data>
EIGEN_DONT_INLINE void transform(const Transformation& t, Data& data)
{
  EIGEN_ASM_COMMENT("begin");
  data = t * data;
  EIGEN_ASM_COMMENT("end");
}

template<typename Scalar, typename Data>
EIGEN_DONT_INLINE void transform(const Quaternion<Scalar>& t, Data& data)
{
  EIGEN_ASM_COMMENT("begin quat");
  for(int i=0;i<data.cols();++i)
    data.col(i) = t * data.col(i);
  EIGEN_ASM_COMMENT("end quat");
}

template<typename T> struct ToRotationMatrixWrapper
{
  enum {Dim = T::Dim};
  typedef typename T::Scalar Scalar;
  ToRotationMatrixWrapper(const T& o) : object(o) {}
  T object;
};

template<typename QType, typename Data>
EIGEN_DONT_INLINE void transform(const ToRotationMatrixWrapper<QType>& t, Data& data)
{
  EIGEN_ASM_COMMENT("begin quat via mat");
  data = t.object.toRotationMatrix() * data;
  EIGEN_ASM_COMMENT("end quat via mat");
}

template<typename Scalar, int Dim, typename Data>
EIGEN_DONT_INLINE void transform(const Transform<Scalar,Dim,Projective>& t, Data& data)
{
  data = (t * data.colwise().homogeneous()).template block<Dim,Data::ColsAtCompileTime>(0,0);
}

template<typename T> struct get_dim { enum { Dim = T::Dim }; };
template<typename S, int R, int C, int O, int MR, int MC>
struct get_dim<Matrix<S,R,C,O,MR,MC> > { enum { Dim = R }; };

template<typename Transformation, int N>
struct bench_impl
{
  static EIGEN_DONT_INLINE void run(const Transformation& t)
  {
    Matrix<typename Transformation::Scalar,get_dim<Transformation>::Dim,N> data;
    data.setRandom();
    bench_impl<Transformation,N-1>::run(t);
    BenchTimer timer;
    BENCH(timer,10,100000,transform(t,data));
    cout.width(9);
    cout << timer.best() << " ";
  }
};


template<typename Transformation>
struct bench_impl<Transformation,0>
{
  static EIGEN_DONT_INLINE void run(const Transformation&) {}
};

template<typename Transformation>
EIGEN_DONT_INLINE void bench(const std::string& msg, const Transformation& t)
{
  cout << msg << " ";
  bench_impl<Transformation,SIZE>::run(t);
  std::cout << "\n";
}

int main(int argc, char ** argv)
{
  Matrix<Scalar,3,4> mat34; mat34.setRandom();
  Transform<Scalar,3,Isometry> iso3(mat34);
  Transform<Scalar,3,Affine> aff3(mat34);
  Transform<Scalar,3,AffineCompact> caff3(mat34);
  Transform<Scalar,3,Projective> proj3(mat34);
  Quaternion<Scalar> quat;quat.setIdentity();
  ToRotationMatrixWrapper<Quaternion<Scalar> > quatmat(quat);
  Matrix<Scalar,3,3> mat33; mat33.setRandom();
  
  cout.precision(4);
  std::cout
     << "N          ";
  for(int i=0;i<SIZE;++i)
  {
    cout.width(9);
    cout << i+1 << " ";
  }
  cout << "\n";
  
  bench("matrix 3x3", mat33);
  bench("quaternion", quat);
  bench("quat-mat  ", quatmat);
  bench("isometry3 ", iso3);
  bench("affine3   ", aff3);
  bench("c affine3 ", caff3);
  bench("proj3     ", proj3);
}