Robotics Library  0.7.0
L2Squared.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_L2SQUARED_H
28 #define RL_MATH_METRICS_L2SQUARED_H
29 
30 namespace rl
31 {
32  namespace math
33  {
34  namespace metrics
35  {
36  template<typename T>
37  struct L2Squared
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 distance;
65  }
66 
67  Distance operator()(const Distance& lhs, const Distance& rhs, const Size& index) const
68  {
69  Distance tmp = lhs - rhs;
70  return tmp * tmp;
71  }
72  };
73 
74  template<typename Scalar, int Rows, int Options, int MaxRows, int MaxCols>
75  struct L2Squared< ::Eigen::Matrix<Scalar, Rows, 1, Options, MaxRows, MaxCols>>
76  {
77  typedef Scalar Distance;
78 
79  typedef int Size;
80 
81  typedef ::Eigen::Matrix<Scalar, Rows, 1, Options, MaxRows, MaxCols> Value;
82 
83  Distance operator()(const Value& lhs, const Value& rhs) const
84  {
85  return (lhs - rhs).squaredNorm();
86  }
87 
88  Distance operator()(const Distance& lhs, const Distance& rhs, const Size& index) const
89  {
90  Distance tmp = lhs - rhs;
91  return tmp * tmp;
92  }
93  };
94 
95  template<typename Scalar, int Rows, int Options, int MaxRows, int MaxCols>
96  struct L2Squared< ::Eigen::Matrix<Scalar, Rows, 1, Options, MaxRows, MaxCols>*>
97  {
98  typedef Scalar Distance;
99 
100  typedef int Size;
101 
102  typedef ::Eigen::Matrix<Scalar, Rows, 1, Options, MaxRows, MaxCols>* Value;
103 
104  Distance operator()(const Value& lhs, const Value& rhs) const
105  {
106  return (*lhs - *rhs).squaredNorm();
107  }
108 
109  Distance operator()(const Distance& lhs, const Distance& rhs, const Size& index) const
110  {
111  Distance tmp = lhs - rhs;
112  return tmp * tmp;
113  }
114  };
115 
116  template<typename Scalar, int Rows, int Options, int MaxRows, int MaxCols>
117  struct L2Squared<const ::Eigen::Matrix<Scalar, Rows, 1, Options, MaxRows, MaxCols>*>
118  {
119  typedef Scalar Distance;
120 
121  typedef int Size;
122 
123  typedef const ::Eigen::Matrix<Scalar, Rows, 1, Options, MaxRows, MaxCols>* Value;
124 
125  Distance operator()(const Value& lhs, const Value& rhs) const
126  {
127  return (*lhs - *rhs).squaredNorm();
128  }
129 
130  Distance operator()(const Distance& lhs, const Distance& rhs, const Size& index) const
131  {
132  Distance tmp = lhs - rhs;
133  return tmp * tmp;
134  }
135  };
136  }
137  }
138 }
139 
140 #endif // RL_MATH_METRICS_L2SQUARED_H
rl::math::metrics::L2Squared< ::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > >::operator()
Distance operator()(const Distance &lhs, const Distance &rhs, const Size &index) const
Definition: L2Squared.h:88
rl::math::Matrix
::Eigen::Matrix< Real, ::Eigen::Dynamic, ::Eigen::Dynamic > Matrix
Definition: Matrix.h:42
rl::math::metrics::L2Squared< ::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > * >::Value
::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > * Value
Definition: L2Squared.h:102
rl::math::metrics::L2Squared< ::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > * >::operator()
Distance operator()(const Value &lhs, const Value &rhs) const
Definition: L2Squared.h:104
rl::math::metrics::L2Squared< ::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > >::Size
int Size
Definition: L2Squared.h:79
rl::math::metrics::L2Squared< const ::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > * >::operator()
Distance operator()(const Value &lhs, const Value &rhs) const
Definition: L2Squared.h:125
rl::math::metrics::L2Squared< const ::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > * >::Distance
Scalar Distance
Definition: L2Squared.h:119
rl::math::metrics::L2Squared::Distance
T::value_type Distance
Definition: L2Squared.h:39
rl::math::metrics::L2Squared< const ::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > * >::Size
int Size
Definition: L2Squared.h:121
rl::math::metrics::L2Squared< ::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > * >::Size
int Size
Definition: L2Squared.h:100
rl::math::metrics::L2Squared::operator()
Distance operator()(const Distance &lhs, const Distance &rhs, const Size &index) const
Definition: L2Squared.h:67
rl::math::metrics::L2Squared::Size
T::size_type Size
Definition: L2Squared.h:41
rl::math::metrics::L2Squared< ::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > * >::operator()
Distance operator()(const Distance &lhs, const Distance &rhs, const Size &index) const
Definition: L2Squared.h:109
rl::math::metrics::L2Squared< ::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > * >::Distance
Scalar Distance
Definition: L2Squared.h:98
rl::math::metrics::L2Squared
Definition: L2Squared.h:38
rl::math::metrics::L2Squared::Value
T Value
Definition: L2Squared.h:43
rl::math::metrics::L2Squared< ::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > >::Value
::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > Value
Definition: L2Squared.h:81
rl::math::metrics::L2Squared< ::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > >::Distance
Scalar Distance
Definition: L2Squared.h:77
rl::math::metrics::L2Squared< const ::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > * >::operator()
Distance operator()(const Distance &lhs, const Distance &rhs, const Size &index) const
Definition: L2Squared.h:130
distance
Scalar distance(const Transform< Scalar, Dim, Mode, Options > &other, const Scalar &weight=1) const
Definition: TransformAddons.h:33
rl::math::metrics::L2Squared< ::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > >::operator()
Distance operator()(const Value &lhs, const Value &rhs) const
Definition: L2Squared.h:83
rl::math::metrics::L2Squared::operator()
Distance operator()(const Value &lhs, const Value &rhs) const
Definition: L2Squared.h:45
rl::math::metrics::L2Squared< const ::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > * >::Value
const ::Eigen::Matrix< Scalar, Rows, 1, Options, MaxRows, MaxCols > * Value
Definition: L2Squared.h:123
rl
Robotics Library.
Definition: AnalogInput.cpp:30