cheshirekow  v0.1.0
Distance.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Josh Bialkowski (jbialk@mit.edu)
3  *
4  * This file is part of mpblocks.
5  *
6  * mpblocks is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * mpblocks is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with mpblocks. If not, see <http://www.gnu.org/licenses/>.
18  */
27 #ifndef MPBLOCKS_KD_TREE_R2_S1_DISTANCE_HPP_
28 #define MPBLOCKS_KD_TREE_R2_S1_DISTANCE_HPP_
29 
30 
31 
32 namespace mpblocks {
33 namespace kd_tree {
34 namespace r2_s1 {
35 
36 template <class Traits>
38  m_min(0),
39  m_max(1),
40  m_weight(1.0)
41 {}
42 
43 template <class Traits>
45  m_min(min),
46  m_max(max),
47  m_weight(weight)
48 {}
49 
50 template <class Traits>
52 {
53  m_min = min;
54  m_max = max;
55  m_weight = weight;
56 }
57 
58 
59 
60 
61 template <class Traits>
62 typename Traits::Format_t
64 {
65  // create a copy of one of the points where the s1 coordinate has been
66  // wrapped around. If s_1 is mapped to [a,b] and pb[2] is c then
67  // [a---c----b] -> [a-------b----(b+c-a)]
68  Point_t pb_wrap = pb;
69  pb_wrap[2] = m_max + (pb[2] - m_min);
70 
71  Point_t diff_a = (pb-pa);
72  Point_t diff_b = (pb_wrap-pa);
73 
74  Format_t d2a = diff_a[0]*diff_a[0]
75  + diff_a[1]*diff_a[1]
76  + m_weight * diff_a[2] * diff_a[2];
77 
78  Format_t d2b = diff_b[0]*diff_b[0]
79  + diff_b[1]*diff_b[1]
80  + m_weight * diff_b[2] * diff_b[2];
81 
82  return std::min( d2a,d2b );
83 }
84 
85 template <class Traits>
86 typename Traits::Format_t
88 {
89  Format_t dist2 = 0;
90  Format_t dist2i, dist2w;
91 
92  for (unsigned int i=0; i < 2; i++)
93  {
94  if (p[i] < h.minExt[i])
95  dist2i = h.minExt[i] - p[i];
96  else if(p[i] > h.maxExt[i])
97  dist2i = h.maxExt[i] - p[i];
98  else
99  continue;
100 
101  dist2 += dist2i * dist2i;
102  }
103 
104  // distance of p[2] to an interval [xa,xb] as in
105  // [x0-----p2----xa----xb----x1]
106  if( p[2] < h.minExt[2] )
107  {
108  dist2i = h.minExt[2] - p[2];
109  dist2w = (m_max + p[2] - m_min ) - h.maxExt[2];
110 
111  if( dist2w < dist2i )
112  dist2i = dist2w;
113 
114  dist2 += m_weight * dist2i * dist2i;
115  }
116  else if( p[2] > h.maxExt[2] )
117  {
118  dist2i = p[2] - h.maxExt[2];
119  dist2w = (m_max + h.minExt[2] - m_min) - p[2];
120 
121  if( dist2w < dist2i )
122  dist2i = dist2w;
123 
124  dist2 += m_weight * dist2i * dist2i;
125  }
126 
127  return dist2;
128 }
129 
130 
131 
132 } // namespace r2_s1
133 } // namespace kd_tree
134 } // namespace mpblocks
135 
136 
137 
138 #endif // DEFAULTDISTANCE_H_
Point_t maxExt
maximum extent of hyper-rectangle
Definition: HyperRect.h:30
Eigen::Matrix< Format_t, Traits::NDim, 1 > Point_t
Definition: Distance.h:44
void configure(Format_t min, Format_t max, Format_t weight)
Definition: Distance.hpp:51
double Format_t
number format (i.e. double, float)
Definition: Traits.h:38
Format_t operator()(const Point_t &pa, const Point_t &pb)
return the r2_s1 distance between two points
Definition: Distance.hpp:63
Point_t minExt
minimum extent of hyper-rectangle
Definition: HyperRect.h:29
an NDim dimensional hyperrectangle, represented as a min and max extent
Definition: HyperRect.h:23