Robotics Library  0.7.0
L2.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2009, Markus Rickert
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
7 //
8 // * Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright notice,
11 // this list of conditions and the following disclaimer in the documentation
12 // and/or other materials provided with the distribution.
13 //
14 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 // POSSIBILITY OF SUCH DAMAGE.
25 //
26 
27 #ifndef RL_MATH_METRICS_L2_H
28 #define RL_MATH_METRICS_L2_H
29 
30 namespace rl
31 {
32  namespace math
33  {
34  namespace metrics
35  {
36  template<typename T>
37  struct L2
38  {
39  typedef typename T::value_type Distance;
40 
41  typedef typename T::size_type Size;
42 
43  typedef T Value;
44 
45  Distance operator()(const Value& lhs, const Value& rhs) const
46  {
47  using ::std::begin;
48  using ::std::end;
49 
50  auto first1 = begin(lhs);
51  auto last1 = end(lhs);
52  auto first2 = begin(rhs);
53 
55 
56  while (first1 != last1)
57  {
58  Distance tmp = *first1 - *first2;
59  distance += tmp * tmp;
60  ++first1;
61  ++first2;
62  }
63 
64  return ::std::sqrt(distance);
65  }
66 
67  Distance operator()(const Distance& lhs, const Distance& rhs, const Size& index) const
68  {
69  return ::std::abs(lhs - rhs);
70  }
71  };
72 
73  template<typename Scalar, int Rows, int Options, int MaxRows, int MaxCols>
74  struct L2< ::Eigen::Matrix<Scalar, Rows, 1, Options, MaxRows, MaxCols>>
75  {
76  typedef Scalar Distance;
77 
78  typedef int Size;
79 
80  typedef ::Eigen::Matrix<Scalar, Rows, 1, Options, MaxRows, MaxCols> Value;
81 
82  Distance operator()(const Value& lhs, const Value& rhs) const
83  {
84  return (lhs - rhs).norm();
85  }
86 
87  Distance operator()(const Distance& lhs, const Distance& rhs, const Size& index) const
88  {
89  return ::std::abs(lhs - rhs);
90  }
91  };
92 
93  template<typename Scalar, int Rows, int Options, int MaxRows, int MaxCols>
94  struct L2< ::Eigen::Matrix<Scalar, Rows, 1, Options, MaxRows, MaxCols>*>
95  {
96  typedef Scalar Distance;
97 
98  typedef int Size;
99 
100  typedef ::Eigen::Matrix<Scalar, Rows, 1, Options, MaxRows, MaxCols>* Value;
101 
102  Distance operator()(const Value& lhs, const Value& rhs) const
103  {
104  return (*lhs - *rhs).norm();
105  }
106 
107  Distance operator()(const Distance& lhs, const Distance& rhs, const Size& index) const
108  {
109  return ::std::abs(lhs - rhs);
110  }
111  };
112 
113  template<typename Scalar, int Rows, int Options, int MaxRows, int MaxCols>
114  struct L2<const ::Eigen::Matrix<Scalar, Rows, 1, Options, MaxRows, MaxCols>*>
115  {
116  typedef Scalar Distance;
117 
118  typedef int Size;
119 
120  typedef const ::Eigen::Matrix<Scalar, Rows, 1, Options, MaxRows, MaxCols>* Value;
121 
122  Distance operator()(const Value& lhs, const Value& rhs) const
123  {
124  return (*lhs - *rhs).norm();
125  }
126 
127  Distance operator()(const Distance& lhs, const Distance& rhs, const Size& index) const
128  {
129  return ::std::abs(lhs - rhs);
130  }
131  };
132  }
133  }
134 }
135 
136 #endif // RL_MATH_METRICS_L2_H
rl::math::Matrix
::Eigen::Matrix< Real, ::Eigen::Dynamic, ::Eigen::Dynamic > Matrix
Definition: Matrix.h:42
rl::math::metrics::L2< const ::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > * >::Value
const ::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > * Value
Definition: L2.h:120
rl::math::metrics::L2::Distance
T::value_type Distance
Definition: L2.h:39
rl::math::metrics::L2< ::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > >::operator()
Distance operator()(const Value &lhs, const Value &rhs) const
Definition: L2.h:82
rl::math::metrics::L2< const ::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > * >::operator()
Distance operator()(const Distance &lhs, const Distance &rhs, const Size &index) const
Definition: L2.h:127
rl::math::metrics::L2< ::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > >::Distance
Scalar Distance
Definition: L2.h:76
rl::math::metrics::L2< ::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > >::Size
int Size
Definition: L2.h:78
rl::math::metrics::L2< ::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > >::Value
::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > Value
Definition: L2.h:80
rl::math::metrics::L2< ::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > >::operator()
Distance operator()(const Distance &lhs, const Distance &rhs, const Size &index) const
Definition: L2.h:87
rl::math::metrics::L2< const ::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > * >::operator()
Distance operator()(const Value &lhs, const Value &rhs) const
Definition: L2.h:122
rl::math::metrics::L2< const ::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > * >::Distance
Scalar Distance
Definition: L2.h:116
rl::math::metrics::L2< ::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > * >::Size
int Size
Definition: L2.h:98
rl::math::metrics::L2< const ::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > * >::Size
int Size
Definition: L2.h:118
rl::math::metrics::L2< ::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > * >::Value
::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > * Value
Definition: L2.h:100
rl::math::metrics::L2< ::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > * >::operator()
Distance operator()(const Distance &lhs, const Distance &rhs, const Size &index) const
Definition: L2.h:107
rl::math::metrics::L2
Definition: L2.h:38
rl::math::metrics::L2< ::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > * >::operator()
Distance operator()(const Value &lhs, const Value &rhs) const
Definition: L2.h:102
rl::math::metrics::L2::Value
T Value
Definition: L2.h:43
rl::math::metrics::L2::Size
T::size_type Size
Definition: L2.h:41
distance
Scalar distance(const Transform< Scalar, Dim, Mode, Options > &other, const Scalar &weight=1) const
Definition: TransformAddons.h:33
rl::math::metrics::L2< ::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > * >::Distance
Scalar Distance
Definition: L2.h:96
rl::math::metrics::L2::operator()
Distance operator()(const Value &lhs, const Value &rhs) const
Definition: L2.h:45
rl::math::metrics::L2::operator()
Distance operator()(const Distance &lhs, const Distance &rhs, const Size &index) const
Definition: L2.h:67
rl
Robotics Library.
Definition: AnalogInput.cpp:30