DynamicTimeWarp.cpp
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2014, JSK, The University of Tokyo.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of the JSK, The University of Tokyo nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 /* Author: Dave Coleman
36  */
37 
38 #include <ompl/tools/lightning/DynamicTimeWarp.h>
39 
40 #include <utility>
41 
42 namespace // anonymous
43 {
47  inline double min3(double n1, double n2, double n3)
48  {
49  return std::min(n1, std::min(n2, n3));
50  }
51 }
52 
53 ompl::tools::DynamicTimeWarp::DynamicTimeWarp(base::SpaceInformationPtr si) : si_(std::move(si)), table_(1, 1)
54 {
55  table_(0, 0) = 0.;
56 }
57 
59  const og::PathGeometric &path2) const
60 {
61  // Get lengths
62  std::size_t n = path1.getStateCount();
63  std::size_t m = path2.getStateCount();
64  std::size_t nrows = table_.size1(), ncols = table_.size2();
65 
66  // Intialize table
67  if (nrows <= n || ncols <= m)
68  {
69  table_.resize(n + 1, m + 1, false);
70  for (std::size_t i = nrows; i <= n; ++i)
71  table_(i, 0) = std::numeric_limits<double>::infinity();
72  for (std::size_t i = ncols; i <= m; ++i)
73  table_(0, i) = std::numeric_limits<double>::infinity();
74  }
75 
76  // Do calculations
77  double cost;
78  for (std::size_t i = 1; i <= n; ++i)
79  for (std::size_t j = 1; j <= m; ++j)
80  {
81  cost = si_->distance(path1.getState(i - 1), path2.getState(j - 1));
82  table_(i, j) = cost + min3(table_(i - 1, j), table_(i, j - 1), table_(i - 1, j - 1));
83  }
84 
85  return table_(n, m);
86 }
87 
89 {
90  // Copy the path but not the states
91  og::PathGeometric newPath1 = path1;
92  og::PathGeometric newPath2 = path2;
93 
94  // Interpolate both paths so that we have an even discretization of samples
95  newPath1.interpolate();
96  newPath2.interpolate();
97 
98  // compute the DTW between two vectors and divide by total path length of the longer path
99  double max_states = std::max(newPath1.getStateCount(), newPath2.getStateCount());
100 
101  // Prevent division by zero
102  if (max_states == 0)
103  return std::numeric_limits<double>::max(); // the worse score possible
104 
105  return calcDTWDistance(newPath1, newPath2) / max_states;
106 }
std::size_t getStateCount() const
Get the number of states (way-points) that make up this path.
STL namespace.
base::State * getState(unsigned int index)
Get the state located at index along the path.
double getPathsScore(const og::PathGeometric &path1, const og::PathGeometric &path2) const
Use dynamic time warping to compare the similarity of two paths Note: this will interpolate both of t...
void interpolate(unsigned int count)
Insert a number of states in a path so that the path is made up of exactly count states. States are inserted uniformly (more states on longer segments). Changes are performed only if a path has less than count states.
double calcDTWDistance(const og::PathGeometric &path1, const og::PathGeometric &path2) const
Use Dynamic Timewarping to score two paths.
Definition of a geometric path.
Definition: PathGeometric.h:60
base::SpaceInformationPtr si_
The created space information.
Definition: LightningDB.h:156