RSC  0.16.0
SequenceMonitor.cpp
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of RSC project
4  *
5  * Copyright (C) 2011 by Christian Emmerich <cemmeric at techfak dot uni-bielefeld dot de>
6  *
7  * This file may be licensed under the terms of the
8  * GNU Lesser General Public License Version 3 (the ``LGPL''),
9  * or (at your option) any later version.
10  *
11  * Software distributed under the License is distributed
12  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13  * express or implied. See the LGPL for the specific language
14  * governing rights and limitations.
15  *
16  * You should have received a copy of the LGPL along with this
17  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18  * or write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The development of this software was supported by:
22  * CoR-Lab, Research Institute for Cognition and Robotics
23  * Bielefeld University
24  *
25  * ============================================================ */
26 
27 #include "SequenceMonitor.h"
28 
29 using namespace std;
30 
31 namespace rsc {
32 namespace math {
33 
34 Metric::~Metric() {
35 }
36 
37 double EuclidDist::calc(const double* v1, const double* v2,
38  const unsigned int& dim) {
39  double sum = 0;
40  for (int i = dim - 1; i >= 0; i--)
41  sum += pow(v1[i] - v2[i], 2);
42  return sqrt(sum);
43 }
44 
45 double MaximumDist::calc(const double* v1, const double* v2,
46  const unsigned int& dim) {
47  double maxValue = 0.0;
48  for (int i = dim - 1; i >= 0; i--) {
49  double currValue = fabs(v1[i] - v2[i]);
50  if (currValue > maxValue) {
51  maxValue = currValue;
52  }
53  }
54  return maxValue;
55 }
56 
57 MetricCondition::MetricCondition(MetricPtr m) :
58  metric(m) {
59 }
60 
62 }
63 
64 BelowThreshold::BelowThreshold(const MetricPtr m, const double threshold) :
65  MetricCondition(m), threshold(threshold) {
66 }
67 
68 bool BelowThreshold::isFulfilled(const double* v1, const double* v2,
69  const unsigned int& dim) {
70  return (this->metric->calc(v1, v2, dim) < this->threshold);
71 }
72 
74  MetricCondition(m), threshold(threshold) {
75 }
76 
77 bool AboveThreshold::isFulfilled(const double* v1, const double* v2,
78  const unsigned int& dim) {
79  return (this->metric->calc(v1, v2, dim) > this->threshold);
80 }
81 
82 SequenceMonitor::SequenceMonitor(const unsigned int dim,
83  const unsigned int window, MetricConditionPtr condition) :
84  dim(dim), windowSize(window), metricCondition(condition) {
85  prev_v = new double[dim];
86  for (unsigned int i = 0; i < dim; i++)
87  prev_v[i] = 0;
88  resetCnt();
89 }
90 
92  delete[] prev_v;
93 }
94 
96  const unsigned int& dim) {
97  if (dim != this->dim) {
98  std::stringstream s;
99  s << "Given dimension " << dim
100  << " of argument vector does not match preliminarily defined dimension "
101  << this->dim;
102  throw std::domain_error(s.str());
103  }
104 
105  // test if condition is fulfilled for this element and copy current vector for next call
106  bool currfulfilled = this->metricCondition->isFulfilled(new_v,
107  this->prev_v, dim);
108  for (unsigned int i = 0; i < this->dim; i++)
109  this->prev_v[i] = new_v[i];
110 
111  // if condition is not fulfilled currently, start counter again from window size
112  if (!currfulfilled) {
113  resetCnt();
114  return false;
115  }
116 
117  // test if cnt reaches zero, otherwise condition is not fulfilled long enough
118  this->cnt--;
119  if (this->cnt > 0)
120  return false;
121 
122  // now the sequence fulfilled the condition long enough
123  return true;
124 }
125 
127  cnt = windowSize;
128 }
129 
130 }
131 }
bool isFulfilled(const double *v1, const double *v2, const unsigned int &dim)
tests whether two vectors fulfill the AboveThreshold - condition
int cnt
Counts whether the condition is fulfilled long enough.
bool isFulfilled(const double *v1, const double *v2, const unsigned int &dim)
tests whether two vectors fulfill the BelowUpperThreshold - condition
STL namespace.
double * prev_v
Previous sequence element.
BelowThreshold(const MetricPtr m, const double threshold)
boost::shared_ptr< MetricCondition > MetricConditionPtr
SequenceMonitor(const unsigned int dim, const unsigned int window, MetricConditionPtr condition)
Constructor.
Defines a interface for metric conditions.
bool isConditionFulfilled(double *new_v, const unsigned int &dim)
Test whether the difference of consecutive sequence members fulfills the condition for the given &#39;tim...
AboveThreshold(const MetricPtr m, const double threshold)
const unsigned int windowSize
boost::shared_ptr< Metric > MetricPtr
MetricConditionPtr metricCondition