Candidate.h
Go to the documentation of this file.
1 #ifndef CANDIDATE_HEADER
2 #define CANDIDATE_HEADER
3 #include "ScoreComputer.h"
4 #include <MiscLib/RefCountPtr.h>
5 #include "PrimitiveShape.h"
6 #include <MiscLib/Vector.h>
7 #include <limits>
8 #include <MiscLib/RefCounted.h>
9 #include <MiscLib/Random.h>
10 #include <MiscLib/NoShrinkVector.h>
11 #include "Octree.h"
12 #include <algorithm>
13 
14 #ifndef DLL_LINKAGE
15 #define DLL_LINKAGE
16 #endif
17 
19 {
20 public:
21  Candidate();
22  Candidate(PrimitiveShape* shape, size_t level);
24  {
25  return m_shape;
26  }
27  void Shape(PrimitiveShape* shape)
28  {
29  m_shape = shape;
30  }
31  float LowerBound() const
32  {
33  return m_lowerBound;
34  }
35  float UpperBound() const
36  {
37  return m_upperBound;
38  }
40  {
41  return m_indices;
42  }
44  {
45  m_indices = indices;
46  }
47  size_t ComputedSubsets() const
48  {
49  return m_subset;
50  }
51  float ExpectedValue() const
52  {
53  return (m_lowerBound + m_upperBound) / 2.f;
54  }
55  void SetSubset(size_t subset)
56  {
57  m_subset = subset;
58  }
59  size_t Level() const
60  {
61  return m_level;
62  }
63  void Reset();
64  template< class ScoreVisitorT >
65  bool ImproveBounds(const MiscLib::Vector< ImmediateOctreeType* >& octrees,
66  const PointCloud& pc, ScoreVisitorT& scoreVisitor,
67  size_t currentSize, float bitmapEpsilon,
68  size_t maxSubset, size_t minPoints = 500);
69  template< class ScoreVisitorT >
70  void RecomputeBounds(const MiscLib::Vector< ImmediateOctreeType* >& octrees,
71  const PointCloud& pc, ScoreVisitorT& scoreVisitor, size_t currentSize,
72  float epsilon, float normalThresh, float bitmapEpsilon);
73  void Reindex(const MiscLib::Vector< int >& newIndices, int minInvalidIndex,
74  size_t mergedSubsets, const MiscLib::Vector< size_t >& subsetSizes,
75  const PointCloud& pc, size_t currentSize, float epsilon,
76  float normalThresh, float bitmapEpsilon);
77  void Reindex(const MiscLib::Vector< size_t >& reindex);
78  template< class ScoreVisitorT >
79  void GlobalScore(ScoreVisitorT& scoreVisitor,
80  const IndexedOctreeType& oct);
81  float WeightedScore(const PointCloud& pc, float epsilon,
82  float normalThresh) const;
83  void ConnectedComponent(const PointCloud& pc, float bitmapEpsilon, float* borderRatio = 0);
84  template< class ScoreVisitorT >
85  float GlobalWeightedScore(ScoreVisitorT& scoreVisitor, const IndexedOctreeType& oct,
86  const PointCloud& pc, float epsilon, float normalThresh,
87  float bitmapEpsilon);
88  inline bool operator<(const Candidate& c) const;
89  inline bool operator>(const Candidate& c) const;
90  inline bool operator<=(const Candidate& c) const;
91  inline bool operator>=(const Candidate& c) const;
92  inline void Clone(Candidate* c) const;
93  bool IsEquivalent(const Candidate& c, const PointCloud& pc,
94  float epsilon, float normalThresh) const;
95  size_t Size() const
96  {
97  return m_indices->size();
98  }
99 
100 private:
101  inline void GetBounds(size_t sampledPoints, size_t totalPoints);
102  inline void Induct(double totalSize, double sampleSize,
103  double totalCorrectSize, float* lower, float* upper) const;
104  inline void Deduct(double totalSize, double sampleSize,
105  double totalCorrectSize, float* lower, float* upper) const;
106 
107  void GetScore(const PointCloud& pc, float bitmapEpsilon, bool doFiltering);
108  void GetScoreMaxCCSize(const PointCloud& pc, float bitmapEpsilon, bool doFiltering);
109  void GetScoreMaxCCMinBorder(const PointCloud& pc, float bitmapEpsilon, bool doFiltering);
110 
111  float GetVariance(const PointCloud& pc);
112  float GetPseudoVariance(const PointCloud& pc);
113 
114 private:
116  size_t m_subset;
117  float m_lowerBound;
118  float m_upperBound;
120  size_t m_level;
121  bool m_hasConnectedComponent;
122  size_t m_score;
123 };
124 
125 bool Candidate::operator<(const Candidate& c) const
126 {
127  return ExpectedValue() < c.ExpectedValue();
128 }
129 
130 bool Candidate::operator>(const Candidate& c) const
131 {
132  return ExpectedValue() > c.ExpectedValue();
133 }
134 
136 {
137  return ExpectedValue() <= c.ExpectedValue();
138 }
139 
141 {
142  return ExpectedValue() >= c.ExpectedValue();
143 }
144 
146 {
147  c->m_shape = m_shape->Clone();
148  c->m_shape->Release();
149  c->m_subset = m_subset;
150  c->m_lowerBound = m_lowerBound;
151  c->m_upperBound = m_upperBound;
152  c->m_indices = new MiscLib::RefCounted< MiscLib::Vector< size_t > >(*m_indices);
153  c->m_indices->Release();
154  c->m_level = m_level;;
155  c->m_hasConnectedComponent = m_hasConnectedComponent;
156  c->m_score = m_score;
157 }
158 
159 void Candidate::GetBounds(size_t sampledPoints, size_t totalPoints)
160 {
161  Induct((double)totalPoints, (double)sampledPoints, (double)m_score, &m_lowerBound,
162  &m_upperBound);
163  m_lowerBound = std::max(0.f, m_lowerBound);
164 }
165 
166 void Candidate::Induct(double totalSize, double sampleSize,
167  double totalCorrectSize, float* lower, float* upper) const
168 {
169  Deduct(-2.0 - sampleSize, -2.0 - totalSize, -1.0 - totalCorrectSize,
170  lower, upper);
171  *lower = -1.f - *lower;
172  *upper = -1.f - *upper;
173 }
174 
175 void Candidate::Deduct(double totalSize, double sampleSize,
176  double totalCorrectSize, float* lower, float* upper) const
177 {
178  double nI = sampleSize * totalCorrectSize;
179  double denom = nI * (totalSize - sampleSize) *
180  (totalSize - totalCorrectSize);
181  double frac = denom / (totalSize - 1);
182  double dev = std::sqrt(frac);
183  *lower = float((nI - dev) / totalSize); // 95% quantile
184  *upper = float((nI + dev) / totalSize); // 95% quantile
185 }
186 
187 template< class ScoreVisitorT >
189  const PointCloud& pc, ScoreVisitorT& scoreVisitor,
190  size_t currentSize, float bitmapEpsilon,
191  size_t maxSubset, size_t minPoints)
192 {
193  if (m_subset >= maxSubset)
194  {
195  return false;
196  }
197  if (m_subset >= octrees.size())
198  {
199  return false;
200  }
201 
202  size_t sampledPoints = 0;
203  for (size_t i = 0; i < m_subset; ++i)
204  {
205  sampledPoints += octrees[i]->size();
206  }
207 
208  size_t newlySampledPoints = 0;
209  scoreVisitor.SetIndices(m_indices);
210  do
211  {
212  scoreVisitor.SetOctree(*octrees[m_subset]);
213  m_shape->Visit(&scoreVisitor);
214  newlySampledPoints += octrees[m_subset]->size();
215  sampledPoints += octrees[m_subset]->size();
216  ++m_subset;
217  }
218  while (m_subset < octrees.size() && newlySampledPoints < minPoints);
219 
220  m_score = m_indices->size();
221  GetBounds(sampledPoints, currentSize);
222 
223  // check if connected component is worthwhile
224  if (m_subset == 1)
225  {
226  return true;
227  }
228  if (m_hasConnectedComponent ||
229  (2.f * (m_upperBound - (m_lowerBound / .7f)) /
230  (m_upperBound + (m_lowerBound / .7f))) < .3f)
231  {
232  if (!m_hasConnectedComponent && m_indices->size() < 2)
233  {
234  return true;
235  }
236  m_hasConnectedComponent = true;
237  m_score = m_shape->ConnectedComponent(pc,
238  (4 << ((octrees.size() - m_subset) / 2)) * bitmapEpsilon,
239  m_indices, false);
240  m_indices->resize(m_score);
241  if (m_subset >= octrees.size())
242  {
243  GetScore(pc, bitmapEpsilon, true);
244  m_upperBound = m_lowerBound = m_score;
245  return true;
246  }
247  GetScore(pc, (2 << ((octrees.size() - m_subset) / 2)) * bitmapEpsilon, false);
248 
249  GetBounds(sampledPoints, currentSize);
250  }
251  return true;
252 }
253 
254 template< class ScoreVisitorT >
256  const PointCloud& pc, ScoreVisitorT& scoreVisitor, size_t currentSize,
257  float epsilon, float normalThresh, float bitmapEpsilon)
258 {
259  // run over indices and check if still unassigned
260  const MiscLib::Vector< int >& shapeIndex = scoreVisitor.GetShapeIndex();
261  size_t indicesSize = m_indices->size();
262  for (size_t i = 0; i < indicesSize;)
263  {
264  if (shapeIndex[(*m_indices)[i]] != -1)
265  {
266  --indicesSize;
267  std::swap((*m_indices)[i], (*m_indices)[indicesSize]);
268  }
269  else
270  {
271  ++i;
272  }
273  }
274 
275  m_score = indicesSize;
276 
277  if (m_hasConnectedComponent)
278  {
279  if (m_indices->size() != indicesSize) // dirty!
280  {
281  if ((float)indicesSize / m_indices->size() > 0.95)
282  {
283  m_indices->resize(indicesSize);
284 
285  GetScore(pc, m_subset >= octrees.size() ? bitmapEpsilon :
286  (4 << ((octrees.size() - m_subset) / 2)) * bitmapEpsilon,
287  m_subset >= octrees.size());
288 
289  if (m_subset >= octrees.size())
290  {
291  m_upperBound = m_lowerBound = (float)m_indices->size();
292  return;
293  }
294  // reevaluate bounds
295  }
296  else
297  {
298  m_subset = 0;
299  m_hasConnectedComponent = false;
300  m_indices->clear();
301  m_score = 0;
302  ImproveBounds(octrees, pc, scoreVisitor,
303  currentSize, bitmapEpsilon, 1);
304  return;
305  }
306  }
307  else if (m_subset > octrees.size())
308  {
309  return;
310  }
311  // reevaluate bounds
312  }
313  else
314  {
315  m_indices->resize(indicesSize);
316  m_score = m_indices->size();
317  }
318  size_t sampledPoints = 0,
319  endi = std::min(m_subset, octrees.size());
320  for (size_t i = 0; i < endi; ++i)
321  {
322  sampledPoints += octrees[i]->size();
323  }
324 
325  GetBounds(sampledPoints, currentSize);
326 }
327 
328 template< class ScoreVisitorT >
329 void Candidate::GlobalScore(ScoreVisitorT& scoreVisitor,
330  const IndexedOctreeType& oct)
331 {
332  m_indices->clear();
333  scoreVisitor.SetOctree(oct);
334  scoreVisitor.SetIndices(m_indices);
335  m_shape->Visit(&scoreVisitor);
336  m_lowerBound = m_upperBound = (float)m_indices->size();
337 }
338 
339 template< class ScoreVisitorT >
340 float Candidate::GlobalWeightedScore(ScoreVisitorT& scoreVisitor, const IndexedOctreeType& oct,
341  const PointCloud& pc, float epsilon, float normalThresh,
342  float bitmapEpsilon)
343 {
344  GlobalScore(scoreVisitor, oct);
345  ConnectedComponent(pc, bitmapEpsilon);
346  return WeightedScore(pc, epsilon, normalThresh);
347 }
348 
349 #endif
GfxTL::sqrt
VectorXD< D, T > sqrt(const VectorXD< D, T > &a)
Definition: VectorXD.h:662
MiscLib::RefCounted
Definition: RefCounted.h:10
Vector.h
RefCounted.h
Candidate::Clone
void Clone(Candidate *c) const
Definition: Candidate.h:145
MiscLib::Vector::resize
void resize(size_type s, const value_type &v)
Definition: Vector.h:222
Candidate::operator<=
bool operator<=(const Candidate &c) const
Definition: Candidate.h:135
PrimitiveShape::ConnectedComponent
virtual size_t ConnectedComponent(const PointCloud &pc, float epsilon, MiscLib::Vector< size_t > *indices, bool doFiltering=true, float *borderRatio=0)=0
Candidate::ImproveBounds
bool ImproveBounds(const MiscLib::Vector< ImmediateOctreeType * > &octrees, const PointCloud &pc, ScoreVisitorT &scoreVisitor, size_t currentSize, float bitmapEpsilon, size_t maxSubset, size_t minPoints=500)
Definition: Candidate.h:188
Candidate::UpperBound
float UpperBound() const
Definition: Candidate.h:35
Candidate::ConnectedComponent
void ConnectedComponent(const PointCloud &pc, float bitmapEpsilon, float *borderRatio=0)
Definition: Candidate.cpp:93
Candidate::operator<
bool operator<(const Candidate &c) const
Definition: Candidate.h:125
Candidate::operator>=
bool operator>=(const Candidate &c) const
Definition: Candidate.h:140
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
PrimitiveShape
PrimtiveShape is a shape primitive in conjunction with a parametrization.
Definition: PrimitiveShape.h:32
Candidate::Size
size_t Size() const
Definition: Candidate.h:95
Candidate::operator>
bool operator>(const Candidate &c) const
Definition: Candidate.h:130
Candidate::RecomputeBounds
void RecomputeBounds(const MiscLib::Vector< ImmediateOctreeType * > &octrees, const PointCloud &pc, ScoreVisitorT &scoreVisitor, size_t currentSize, float epsilon, float normalThresh, float bitmapEpsilon)
Definition: Candidate.h:255
MiscLib::Vector< size_t >
Candidate::Shape
PrimitiveShape * Shape()
Definition: Candidate.h:23
Candidate
Definition: Candidate.h:18
armarx::armem::client::util::swap
void swap(SubscriptionHandle &first, SubscriptionHandle &second)
Definition: SubscriptionHandle.cpp:66
NoShrinkVector.h
RefCountPtr.h
MiscLib::Vector::size
size_type size() const
Definition: Vector.h:212
Candidate::ExpectedValue
float ExpectedValue() const
Definition: Candidate.h:51
Candidate::ComputedSubsets
size_t ComputedSubsets() const
Definition: Candidate.h:47
Candidate::GlobalWeightedScore
float GlobalWeightedScore(ScoreVisitorT &scoreVisitor, const IndexedOctreeType &oct, const PointCloud &pc, float epsilon, float normalThresh, float bitmapEpsilon)
Definition: Candidate.h:340
pcl::graph::indices
pcl::PointIndices::Ptr indices(const PCG &g)
Retrieve the indices of the points of the point cloud stored in a point cloud graph that actually bel...
Definition: point_cloud_graph.h:737
armarx::operator>=
bool operator>=(const RemoteHandle< PrxTA > &fst, const RemoteHandle< PrxTB > &snd)
Definition: RemoteHandle.h:236
Candidate::Shape
void Shape(PrimitiveShape *shape)
Definition: Candidate.h:27
Candidate::Indices
void Indices(MiscLib::RefCounted< MiscLib::Vector< size_t > > *indices)
Definition: Candidate.h:43
Candidate::SetSubset
void SetSubset(size_t subset)
Definition: Candidate.h:55
max
T max(T t1, T t2)
Definition: gdiam.h:48
IceStormElection::operator<
bool operator<(const ReplicaObserver &lhs, const ReplicaObserver &rhs)
Definition: Election.h:1604
Candidate::GlobalScore
void GlobalScore(ScoreVisitorT &scoreVisitor, const IndexedOctreeType &oct)
Definition: Candidate.h:329
MiscLib::RefCountPtr< PrimitiveShape >
Candidate::Level
size_t Level() const
Definition: Candidate.h:59
PointCloud
Definition: PointCloud.h:69
Candidate::WeightedScore
float WeightedScore(const PointCloud &pc, float epsilon, float normalThresh) const
Definition: Candidate.cpp:81
float
#define float
Definition: 16_Level.h:22
Random.h
armarx::operator<=
bool operator<=(const RemoteHandle< PrxTA > &fst, const RemoteHandle< PrxTB > &snd)
Definition: RemoteHandle.h:228
DLL_LINKAGE
#define DLL_LINKAGE
Definition: basic.h:11
ScoreComputer.h
Octree.h
pc
Introduction Thank you for taking interest in our work and downloading this software This library implements the algorithm described in the paper R R R Klein Efficient RANSAC for Point Cloud Shape in Computer Graphics Blackwell June If you use this software you should cite the aforementioned paper in any resulting publication Please send comments or bug reports to Ruwen Roland BUT NOT LIMITED THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY OR CONSEQUENTIAL WHETHER IN STRICT OR EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE Example usage This section shows how to use the library to detect the shapes in a point cloud PointCloud pc
Definition: ReadMe.txt:68
min
T min(T t1, T t2)
Definition: gdiam.h:42
Candidate::LowerBound
float LowerBound() const
Definition: Candidate.h:31
Candidate::Indices
MiscLib::RefCounted< MiscLib::Vector< size_t > > * Indices()
Definition: Candidate.h:39
MiscLib::Vector::clear
void clear()
Definition: Vector.h:174
PrimitiveShape.h
PrimitiveShape::Clone
virtual PrimitiveShape * Clone() const =0
GfxTL::AACubeTree
Definition: AACubeTree.h:287
PrimitiveShape::Visit
virtual void Visit(PrimitiveShapeVisitor *visitor) const =0
armarx::operator>
bool operator>(const RemoteHandle< PrxTA > &fst, const RemoteHandle< PrxTB > &snd)
Definition: RemoteHandle.h:232