Candidate.cpp
Go to the documentation of this file.
1 #include "Candidate.h"
2 
4  : m_subset(0)
5  , m_lowerBound(0)
6  , m_upperBound(0)
7  , m_level(0)
8  , m_hasConnectedComponent(false)
9 {}
10 
11 Candidate::Candidate(PrimitiveShape* shape, size_t level)
12  : m_shape(shape)
13  , m_subset(0)
14  , m_lowerBound(0)
15  , m_upperBound(0)
16  , m_level(level)
17  , m_hasConnectedComponent(false)
18 {}
19 
21 {
22  m_indices->clear();
23  m_subset = 0;
24  m_lowerBound = 0;
25  m_upperBound = 0;
26  m_hasConnectedComponent = false;
27  m_score = 0;
28 }
29 
30 void Candidate::Reindex(const MiscLib::Vector< int >& newIndices, int minInvalidIndex,
31  size_t mergedSubsets, const MiscLib::Vector< size_t >& subsetSizes,
32  const PointCloud& pc, size_t currentSize, float epsilon, float normalThresh,
33  float bitmapEpsilon)
34 {
35  size_t i = 0, j = 0;
36  for (; i < m_indices->size(); ++i)
37  if (newIndices[(*m_indices)[i]] < minInvalidIndex)
38  {
39  (*m_indices)[j++] = newIndices[(*m_indices)[i]];
40  }
41  if (m_subset <= mergedSubsets)
42  {
43  m_hasConnectedComponent = false;
44  m_subset = 0;
45  m_indices->clear();
46  m_lowerBound = 0;
47  m_upperBound = 0; // forget this candidate
48  m_score = 0;
49  return;
50  }
51  else
52  {
53  m_indices->resize(j);
54  m_subset -= mergedSubsets;
55  if (m_subset >= subsetSizes.size())
56  // do connected component if all subsets have been computed
57  {
58  ConnectedComponent(pc, bitmapEpsilon);
59  }
60  }
61  size_t sampledPoints = 0,
62  endi = std::min(m_subset, subsetSizes.size());;
63  for (i = 0; i < endi; ++i)
64  {
65  sampledPoints += subsetSizes[i];
66  }
67 
68  GetBounds(sampledPoints, currentSize);
69 }
70 
72 {
73  size_t reindexSize = reindex.size();
74  for (size_t i = 0; i < m_indices->size(); ++i)
75  if (m_indices->at(i) < reindexSize)
76  {
77  m_indices->at(i) = reindex[m_indices->at(i)];
78  }
79 }
80 
81 float Candidate::WeightedScore(const PointCloud& pc, float epsilon,
82  float normalThresh) const
83 {
84  float score = 0;
85  #pragma omp parallel for schedule(static) reduction(+:score)
86  for (intptr_t i = 0; i < (intptr_t)m_indices->size(); ++i)
87  {
88  score += weigh(m_shape->Distance(pc[(*m_indices)[i]].pos), epsilon);
89  }
90  return score;
91 }
92 
93 void Candidate::ConnectedComponent(const PointCloud& pc, float bitmapEpsilon, float* borderRatio)
94 {
95  size_t oldSize = m_indices->size();
96  size_t connectedSize = m_shape->ConnectedComponent(pc, bitmapEpsilon, m_indices, true, borderRatio);
97  m_indices->resize(connectedSize);
98  m_lowerBound = m_upperBound = (float)m_indices->size();
99 }
100 
102  float epsilon, float normalThresh) const
103 {
104  if (m_shape->Identifier() != c.m_shape->Identifier())
105  {
106  return false;
107  }
108  size_t correct = 0;
109  size_t size = std::min(m_indices->size(), (size_t)9);
110  for (size_t i = 0; i < size; ++i)
111  {
112  std::pair< float, float > dn;
113  size_t idx = MiscLib::rn_rand() % m_indices->size();
114  c.m_shape->DistanceAndNormalDeviation(
115  pc[m_indices->at(idx)].pos,
116  pc[m_indices->at(idx)].normal, &dn);
117  if (dn.first < epsilon && abs(dn.second) > normalThresh)
118  {
119  ++correct;
120  }
121  }
122  size_t tested = size;
123  size = std::min(c.m_indices->size(), (size_t)9);
124  for (size_t i = 0; i < size; ++i)
125  {
126  std::pair< float, float > dn;
127  size_t idx = MiscLib::rn_rand() % c.m_indices->size();
129  pc[c.m_indices->at(idx)].pos,
130  pc[c.m_indices->at(idx)].normal, &dn);
131  if (dn.first < epsilon && abs(dn.second) > normalThresh)
132  {
133  ++correct;
134  }
135  }
136  tested += size;
137  return correct >= 2 * tested / 3;
138 }
139 
140 float Candidate::GetVariance(const PointCloud& pc)
141 {
142  float variance = 0.0f;
143  float dev;
144 
145  if (m_indices->size() > 0)
146  {
147  // first pass - get expectancy
148  float expectancy = 0.0f;
149  for (int i = 0; i < m_indices->size(); ++i)
150  {
151  expectancy += abs(m_shape->NormalDeviation(pc[(*m_indices)[i]].pos, pc[(*m_indices)[i]].normal));
152  }
153 
154  expectancy /= static_cast<float>(m_indices->size());
155 
156  // second pass - get real variance
157  for (int i = 0; i < m_indices->size(); ++i)
158  {
159  dev = abs(m_shape->NormalDeviation(pc[(*m_indices)[i]].pos, pc[(*m_indices)[i]].normal)) - expectancy;
160  variance += dev * dev;
161  }
162 
163  variance /= static_cast<float>(m_indices->size());
164  }
165  else
166  {
167  variance = 1.0f;
168  }
169 
170  return variance;
171 }
172 
173 float Candidate::GetPseudoVariance(const PointCloud& pc)
174 {
175  float variance = 0.0f;
176  float dev;
177 
178  for (int i = 0; i < m_indices->size(); ++i)
179  {
180  dev = abs(m_shape->NormalDeviation(pc[(*m_indices)[i]].pos, pc[(*m_indices)[i]].normal)) - 1.0f;
181  variance += dev * dev;
182  }
183  variance /= static_cast<float>(m_indices->size());
184 
185  return variance;
186 }
187 
188 void Candidate::GetScore(const PointCloud& pc, float bitmapEpsilon, bool doFiltering)
189 {
190  GetScoreMaxCCSize(pc, bitmapEpsilon, doFiltering);
191 }
192 
193 void Candidate::GetScoreMaxCCSize(const PointCloud& pc, float bitmapEpsilon, bool doFiltering)
194 {
195  size_t connectedSize = m_shape->ConnectedComponent(pc, bitmapEpsilon,
196  m_indices, doFiltering);
197  m_indices->resize(connectedSize);
198 
199  m_score = connectedSize;
200 }
201 
202 void Candidate::GetScoreMaxCCMinBorder(const PointCloud& pc, float bitmapEpsilon, bool doFiltering)
203 {
204  float borderRatio;
205  size_t connectedSize = m_shape->ConnectedComponent(pc, bitmapEpsilon,
206  m_indices, doFiltering, &borderRatio);
207  m_indices->resize(connectedSize);
208 
209  m_score = connectedSize * size_t((1.0f - borderRatio) * (1.0f - GetVariance(pc)));
210 }
PrimitiveShape::Identifier
virtual size_t Identifier() const =0
weigh
float weigh(const float d, const float eps)
Definition: ScoreComputer.h:10
MiscLib::Vector::resize
void resize(size_type s, const value_type &v)
Definition: Vector.h:222
PrimitiveShape::ConnectedComponent
virtual size_t ConnectedComponent(const PointCloud &pc, float epsilon, MiscLib::Vector< size_t > *indices, bool doFiltering=true, float *borderRatio=0)=0
Candidate::ConnectedComponent
void ConnectedComponent(const PointCloud &pc, float bitmapEpsilon, float *borderRatio=0)
Definition: Candidate.cpp:93
Candidate::Candidate
Candidate()
Definition: Candidate.cpp:3
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
PrimitiveShape
PrimtiveShape is a shape primitive in conjunction with a parametrization.
Definition: PrimitiveShape.h:32
Point::normal
Vec3f normal
Definition: PointCloud.h:27
PrimitiveShape::NormalDeviation
virtual float NormalDeviation(const Vec3f &p, const Vec3f &n) const =0
PrimitiveShape::Distance
virtual float Distance(const Vec3f &p) const =0
MiscLib::Vector< int >
Candidate
Definition: Candidate.h:18
MiscLib::Vector::size
size_type size() const
Definition: Vector.h:212
PrimitiveShape::DistanceAndNormalDeviation
virtual void DistanceAndNormalDeviation(const Vec3f &p, const Vec3f &n, std::pair< float, float > *dn) const =0
MiscLib::rn_rand
size_t rn_rand()
Definition: Random.h:20
armarx::abs
std::vector< T > abs(const std::vector< T > &v)
Definition: VectorHelpers.h:253
Candidate::IsEquivalent
bool IsEquivalent(const Candidate &c, const PointCloud &pc, float epsilon, float normalThresh) const
Definition: Candidate.cpp:101
Point::pos
Vec3f pos
Definition: PointCloud.h:26
MiscLib::Vector::at
T & at(size_type i)
Definition: Vector.h:308
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
Candidate::Reindex
void Reindex(const MiscLib::Vector< int > &newIndices, int minInvalidIndex, size_t mergedSubsets, const MiscLib::Vector< size_t > &subsetSizes, const PointCloud &pc, size_t currentSize, float epsilon, float normalThresh, float bitmapEpsilon)
Definition: Candidate.cpp:30
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.h
Candidate::Reset
void Reset()
Definition: Candidate.cpp:20
MiscLib::Vector::clear
void clear()
Definition: Vector.h:174