1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**
* @file SampleSet.cpp
*
* @author <a href="mailto:mellmann@informatik.hu-berlin.de">Heinrich Mellmann</a>
* Implementation of class SampleSet
*/
#include <algorithm>
#include "SampleSet.h"
#include "Tools/Debug/NaoTHAssert.h"
#include "Tools/Math/Common.h"

void SampleSet::sort(bool descending) 
{
  if(samples.size() > 1) {
    quicksort(descending?1:-1, 0, ((int)samples.size())-1);
  }
}

void SampleSet::quicksort(int d, int low, int high) 
{
  int i = low;
  int j = high;

  Sample help;

  /* compare value */
  double z = samples[(low + high) / 2].likelihood*d;

  /* partition */
  do {
    /* find member above ... */
    while(samples[i].likelihood*d > z) i++;

    /* find element below ... */
    while(samples[j].likelihood*d < z) j--;

    if(i <= j) 
    {
      /* swap two elements */
      help = samples[i];
      samples[i] = samples[j]; 
      samples[j] = help;

      i++; 
      j--;
    }//end if
  } while(i <= j);

  /* recurse */
  if(low < j) {
   quicksort(d, low, j);
  }

  if(i < high) {
    quicksort(d, i, high);
  }
}//end quicksort


void SampleSet::normalize(double offset)
{
  double sum = 0.0;
  for(size_t i = 0; i < samples.size(); i++) {
    samples[i].likelihood = samples[i].likelihood;<--- Redundant assignment of 'samples[i].likelihood' to itself.
    sum += samples[i].likelihood;
  }

  if(sum == 0) { 
    return; 
  }

  double offset_sum = 1.0+offset*(double)samples.size();

  for(size_t i = 0; i < samples.size(); i++) {
    samples[i].likelihood = ((samples[i].likelihood/sum) + offset)/offset_sum;
  }
}//end normalize


void SampleSet::resetLikelihood()
{
  double likelihood = 1.0/static_cast<double>(samples.size());
  setLikelihood(likelihood);
}

void SampleSet::setLikelihood(double v)
{
  for(size_t i = 0; i < samples.size(); i++) {
    samples[i].likelihood = v;
  }
}

Sample SampleSet::meanOfLargestCluster(Moments2<2>& moments) const
{
  ASSERT(samples.size() > 0);

  // TODO: make it better
  std::vector<int> cluster(samples.size());
  int maxIndex = 0;
  double maxNumber = 0;

  for(size_t i = 0; i < samples.size(); i++)
  {
    if(samples[i].cluster >= 0 && samples[i].cluster < (int)samples.size())
    {
      int idx = samples[i].cluster;
      cluster[idx]++;

      if(maxNumber < cluster[idx]) {
        maxIndex = idx;
        maxNumber = cluster[idx];
      }
    }//end if
  }//end for

  return meanOfCluster(moments, maxIndex);
}//end meanOfLargestCluster


Sample SampleSet::meanOfCluster(Moments2<2>& moments, int idx) const 
{
  ASSERT(samples.size() > 0);

  Vector2d averageTranslation;
  Vector2d averageRotation;
  double numberOfSamples = 0;

  // calculate the covariance of the largest cluster
  for(size_t i = 0; i < samples.size(); i++)
  {
    const Sample& sample = samples[i];
    if(sample.cluster == idx)
    {
      averageTranslation += sample.translation;
      averageRotation.x += cos(sample.rotation);
      averageRotation.y += sin(sample.rotation);
      moments.add(sample.translation);
      numberOfSamples++;
    }
  }

  Sample result;
  result.cluster = idx;

  if(numberOfSamples > 0) {
    result.translation = averageTranslation/numberOfSamples;
    result.rotation = (averageRotation/numberOfSamples).angle();
  }

  return result;
}//end meanOfCluster

const Sample& SampleSet::getMostLikelySample() const
{
  ASSERT(samples.size() > 0);

  double maxLikelihood = samples[0].likelihood;
  int maxIdx = 0;

  for(size_t i = 1; i < samples.size(); i++)
  {
    if(maxLikelihood < samples[i].likelihood) {
      maxLikelihood = samples[i].likelihood;
      maxIdx = (int)i;
    }
  }

  return samples[maxIdx];
}

void SampleSet::drawCluster(DrawingCanvas2D& canvas, unsigned int clusterId) const
{
  ASSERT(samples.size() > 0);

  for (size_t i = 0; i < samples.size(); i++)
  {
    if (samples[i].cluster == (int)clusterId) {
      canvas.pen("FFFFFF", 20);
    } else { 
      canvas.pen("000000", 20);
    }

    canvas.drawArrow(samples[i].translation.x, samples[i].translation.y, 
          samples[i].translation.x + 100*cos(samples[i].rotation), 
          samples[i].translation.y + 100*sin(samples[i].rotation));
  }
}

void SampleSet::drawImportance(DrawingCanvas2D& canvas, bool /*arrows*/) const
{
  ASSERT(samples.size() > 0);

  // normalize the colors (black: less importent, red more importent)
  double minValue = samples[0].likelihood;
  double maxValue = samples[0].likelihood;
  for (size_t i = 1; i < samples.size(); i++)
  {
    maxValue = std::max(samples[i].likelihood, maxValue);
    minValue = std::min(samples[i].likelihood, minValue);
  }
  double colorDiff = log(maxValue) - log(minValue);

  for (size_t i = 0; i < samples.size(); i++)
  {
    Color color;
    if(colorDiff > 0) {
      color[0] = Math::clamp((log(samples[i].likelihood) - log(minValue))/colorDiff,0.0,1.0);
    }
    
    canvas.pen(color, 20);

    canvas.drawArrow(samples[i].translation.x, samples[i].translation.y, 
          samples[i].translation.x + 100*cos(samples[i].rotation), 
          samples[i].translation.y + 100*sin(samples[i].rotation));
  }
}