snark
traits.h
1 // This file is part of snark, a generic and flexible library
2 // for robotics research.
3 //
4 // Copyright (C) 2011 The University of Sydney
5 //
6 // snark is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 3 of the License, or (at your option) any later version.
10 //
11 // snark is distributed in the hope that it will be useful, but WITHOUT ANY
12 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
14 // for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with snark. If not, see <http://www.gnu.org/licenses/>.
18 
19 #ifndef SNARK_MATH_TRAITS_H_
20 #define SNARK_MATH_TRAITS_H_
21 
22 #include <boost/array.hpp>
23 #include <Eigen/Core>
24 
25 namespace snark{ namespace math{
26 
27 template < typename T >
28 struct traits
29 {
30  enum { size = 1 };
31  T zero() { return T( 0 ); }
32  T one() { return T( 1 ); }
33  T identity() { return T( 1 ); }
34 };
35 
36 template < typename T, int Rows, int Columns >
37 struct traits< ::Eigen::Matrix< T, Rows, Columns > >
38 {
39  enum { rows = Rows, columns = Columns, size = rows * columns };
40 
41  static const ::Eigen::Matrix< T, Rows, Columns >& zero()
42  {
43  static ::Eigen::Matrix< T, Rows, Columns > z = ::Eigen::Matrix< T, Rows, Columns >::Zero();
44  return z;
45  }
46 
47  static const ::Eigen::Matrix< T, Rows, Columns >& indentity()
48  {
49  static ::Eigen::Matrix< T, Rows, Columns > i = ::Eigen::Matrix< T, Rows, Columns >::Identity();
50  return i;
51  }
52 };
53 
54 template < typename T, std::size_t Size >
55 class traits< boost::array< T, Size > >
56 {
57  public:
58  enum { size = Size };
59 
60  static const boost::array< T, Size >& zero() { static boost::array< T, Size > z = zero_(); return z; }
61 
62  private:
63  static boost::array< T, Size > zero_()
64  {
65  boost::array< T, Size > z;
66  for( std::size_t i = 0; i < Size; ++i ) { z[i] = math::traits< T >::zero(); }
67  return z;
68  }
69 };
70 
71 } } // namespace snark{ namespace math{
72 
73 #endif //SNARK_MATH_TRAITS_H_