TSimpleSpaceResultConsumer.h
Go to the documentation of this file.
1 #ifndef ialgo_TSimpleSpaceResultConsumer_included
2 #define ialgo_TSimpleSpaceResultConsumer_included
3 
4 
5 // Qt includes
6 #include <QtCore/QMultiMap>
7 
8 // ACF includes
9 #include <imath/TVector.h>
10 
11 // ACF-Solutions includes
12 #include <ialgo/TIHoughSpace.h>
13 
14 
15 namespace ialgo
16 {
17 
18 
22 template <int Dimensions, class Element = double>
24  public iimg::CGeneralBitmap,
25  virtual public TIHoughSpace<Dimensions, Element>::ResultsConsumer
26 {
27 public:
32  typedef QMultiMap<double, PosVector> PosMap;
33 
42  TSimpleSpaceResultConsumer(int maxPoints, int maxConsideredPoints, double minDistance, double minMaxRatio);
43 
44  // reimplemented (ialgo::TIHoughSpace::ResultsConsumer)
45  virtual QList<int> GetSupportedNeghboursCount() const;
46  virtual void OnProcessingBegin(
48  const Element& minValue);
49  virtual void OnProcessingEnd(const TIHoughSpace<Dimensions, Element>& space);
50  virtual bool OnMaximumFound(
52  const istd::TIndex<Dimensions>& position,
53  const Element& value,
54  const Element* neghboursPtr,
55  int neghboursCount,
56  Element& minValue);
57 
59 
60 private:
61  int m_maxPoints;
62  int m_maxConsideredPoints;
63  double m_minDistance;
64  double m_minMaxRatio;
65 
66  double m_maxValue;
67 };
68 
69 
70 // public methods
71 
72 // reimplemented (ialgo::TIHoughSpace::ResultsConsumer)
73 
74 template <int Dimensions, class Element>
75 TSimpleSpaceResultConsumer<Dimensions, Element>::TSimpleSpaceResultConsumer(int maxPoints, int maxConsideredPoints, double minDistance, double minMaxRatio)
76 : m_maxPoints(maxPoints),
77  m_maxConsideredPoints(maxConsideredPoints),
78  m_minDistance(minDistance),
79  m_minMaxRatio(minMaxRatio)
80 {
81 }
82 
83 
84 template <int Dimensions, class Element>
86 {
87  return QList<int>() << Dimensions * 2;
88 }
89 
90 
91 template <int Dimensions, class Element>
93  const TIHoughSpace<Dimensions, Element>& /*space*/,
94  const Element& /*minValue*/)
95 {
96  m_maxValue = 0;
97 }
98 
99 
100 template <int Dimensions, class Element>
102 {
103  PosMap finalPositions;
104 
105  if (m_maxPoints != 0){
106  // remove elements beeing to close to each other
107  for ( typename PosMap::ConstIterator pointIter = positions.constBegin();
108  pointIter != positions.constEnd();
109  ++pointIter){
110  const PosVector& point1 = pointIter.value();
111 
112  bool isToClose = false;
113 
114  for ( typename PosMap::ConstIterator searchClosedIter = finalPositions.constBegin();
115  searchClosedIter != finalPositions.constEnd();
116  ++searchClosedIter){
117  const PosVector& point2 = searchClosedIter.value();
118 
119  double dist2 = space.GetSpaceDistance2(point1, point2);
120  if (dist2 <= m_minDistance * m_minDistance){
121  isToClose = true;
122 
123  break;
124  }
125  }
126 
127  if (!isToClose){
128  finalPositions.insertMulti(-pointIter.key(), point1);
129 
130  if ((m_maxPoints >= 0) && (finalPositions.size() >= m_maxPoints)){
131  break;
132  }
133  }
134  }
135  }
136 
137  positions.swap(finalPositions);
138 }
139 
140 
141 template <int Dimensions, class Element>
143  const TIHoughSpace<Dimensions, Element>& /*space*/,
144  const istd::TIndex<Dimensions>& position,
145  const Element& value,
146  const Element* neghboursPtr,
147  int neghboursCount,
148  Element& minValue)
149 {
150  Q_ASSERT(neghboursCount == Dimensions * 2);
151  Q_UNUSED(neghboursCount);
152 
153  PosVector resultPos;
154 
155  for (int dimensionIndex = 0; dimensionIndex < Dimensions; ++dimensionIndex){
156  Element diffLeft = double(value) - double(neghboursPtr[dimensionIndex * 2]);
157  Element diffRight = double(value) - double(neghboursPtr[dimensionIndex * 2 + 1]);
158 
159  double normValue = diffLeft + diffRight;
160  double correction = (normValue > I_BIG_EPSILON)? diffLeft / normValue: 0.5;
161 
162  resultPos[dimensionIndex] = position[dimensionIndex] + correction;
163  }
164 
165  positions.insert(-value, resultPos);
166 
167  if (value > m_maxValue){
168  m_maxValue = double(value);
169  Element propValue = Element(value * m_minMaxRatio);
170  if (minValue < propValue){
171  minValue = propValue;
172 
173  // remove elements weeker than new calculated minValue
174  while (!positions.isEmpty()){
175  typename PosMap::Iterator lastIter = positions.end() - 1;
176 
177  if (-lastIter.key() < minValue){
178  positions.erase(lastIter);
179  }
180  else{
181  break;
182  }
183  }
184  }
185  }
186 
187  // try remove the last one if we have too many points
188  if (positions.count() > m_maxConsideredPoints){
189  typename PosMap::Iterator lastIter = positions.end() - 1;
190  double lastValue = -lastIter.key();
191  positions.erase(lastIter);
192 
193  if (lastValue > minValue){
194  minValue = lastValue;
195  }
196  }
197 
198  return false;
199 }
200 
201 
202 } // namespace ialgo
203 
204 
205 #endif // !ialgo_TSimpleSpaceResultConsumer_included
206 
207 
virtual bool OnMaximumFound(const TIHoughSpace< Dimensions, Element > &space, const istd::TIndex< Dimensions > &position, const Element &value, const Element *neghboursPtr, int neghboursCount, Element &minValue)
Will be called when some local maximum is reached.
QMultiMap< double, PosVector > PosMap
List of weights and positions.
virtual void OnProcessingBegin(const TIHoughSpace< Dimensions, Element > &space, const Element &minValue)
Called when processing is started.
Consumer of results generated for some multidimensional space (typically Hough space).
virtual double GetSpaceDistance2(const imath::TVector< Dimensions > &position1, const imath::TVector< Dimensions > &position2) const =0
Get square of distance between two hough space positions considering the space wrapping.
virtual void OnProcessingEnd(const TIHoughSpace< Dimensions, Element > &space)
Called when processing is finished.
imath::TVector< Dimensions > PosVector
TSimpleSpaceResultConsumer(int maxPoints, int maxConsideredPoints, double minDistance, double minMaxRatio)
Construct the result processor.
Template interface for Hough space.
Definition: TIHoughSpace.h:21
virtual QList< int > GetSupportedNeghboursCount() const
Get list of number of neighbours supprted by this consumer.

© 2007-2017 Witold Gantzke and Kirill Lepskiy