KernelCollection.cpp
Go to the documentation of this file.
1 // (C) Copyright Renaud Detry 2007-2015.
2 // Distributed under the GNU General Public License and under the
3 // BSD 3-Clause License (See accompanying file LICENSE.txt).
4 
5 /** @file */
6 
7 #include <fstream>
8 #include <cassert>
9 #include <algorithm>
10 #include <cmath>
11 
12 #include <queue>
13 #include <set>
14 #include <list>
15 
17 #include <nuklei/Common.h>
18 #include <nuklei/Indenter.h>
19 
20 
21 namespace nuklei {
22 
23  const int KernelCollection::HULL_KEY = 0;
24  const int KernelCollection::KDTREE_KEY = 1;
25  const int KernelCollection::NSTREE_KEY = 2;
26  const int KernelCollection::MESH_KEY = 3;
27  const int KernelCollection::AABBTREE_KEY = 4;
28  const int KernelCollection::VIEWCACHE_KEY = 5;
29 
30  std::istream& operator>>(std::istream &in, KernelCollection &v)
31  {
32  NUKLEI_TRACE_BEGIN();
33  NUKLEI_THROW("not implemented.");
34  return in;
35  NUKLEI_TRACE_END();
36  }
37 
38  void KernelCollection::invalidateHelperStructures()
39  {
40  totalWeight_ = boost::none;
41  maxLocCutPoint_ = boost::none;
42  deco_.clear();
43  }
44 
45  void KernelCollection::assertConsistency() const
46  {
47  NUKLEI_TRACE_BEGIN();
48  for (const_iterator i = begin(); i != end(); i++)
49  i->assertConsistency();
50  for (const_iterator i = begin(); i != end(); i++)
51  NUKLEI_ASSERT(i->polyType() == *kernelType_);
52  NUKLEI_TRACE_END();
53  }
54 
56  {
57  NUKLEI_TRACE_BEGIN();
58  kernels_.clear();
59  invalidateHelperStructures();
60  kernelType_ = boost::none;
61  NUKLEI_TRACE_END();
62  }
63 
65  {
66  NUKLEI_TRACE_BEGIN();
67  invalidateHelperStructures();
68  if (size() == 0)
69  kernelType_ = f.polyType();
70  else
71  NUKLEI_ASSERT(*kernelType_ == f.polyType());
72  kernels_.push_back(NUKLEI_RELEASE(f.clone()));
73  NUKLEI_TRACE_END();
74  }
75 
77  {
78  NUKLEI_TRACE_BEGIN();
79  for (const_iterator i = kv.begin(); i != kv.end(); ++i)
80  add(*i);
81  NUKLEI_TRACE_END();
82  }
83 
84  void KernelCollection::replace(const size_t idx, const kernel::base &k)
85  {
86  NUKLEI_TRACE_BEGIN();
87  NUKLEI_ASSERT(0 <= idx && idx < size());
88  NUKLEI_ASSERT(*kernelType_ == k.polyType());
89  kernels_.replace(idx, NUKLEI_RELEASE(k.clone()));
90  invalidateHelperStructures();
91  NUKLEI_TRACE_END();
92  }
93 
94  kernel::base::Type KernelCollection::kernelType() const
95  {
96  NUKLEI_TRACE_BEGIN();
97  if (empty())
98  NUKLEI_THROW("KernelCollection empty, hence undefined kernelType.");
99  NUKLEI_ASSERT(kernelType_);
100  return *kernelType_;
101  NUKLEI_TRACE_END();
102  }
103 
104 
106  KernelCollection::sampleBegin(size_t sampleSize)
107  {
108  // First create predicate
109  is_picked predicate(sampleSize, totalWeight());
110  // Then invalidate helper structures, as the user may use the iterator
111  // to change the data.
112  invalidateHelperStructures();
113  return sample_iterator(predicate, begin(), end());
114  }
115 
117  KernelCollection::sampleBegin(size_t sampleSize) const
118  {
119  is_picked predicate(sampleSize, totalWeight());
120  return const_sample_iterator(predicate, begin(), end());
121  }
122 
125  {
126  invalidateHelperStructures();
127  return nuklei_trsl::sort_iterator(begin(), end(),
128  std::greater<kernel::base>(), sortSize);
129  }
130 
132  KernelCollection::sortBegin(size_t sortSize) const
133  {
134  return nuklei_trsl::sort_iterator(begin(), end(),
135  std::greater<kernel::base>(), sortSize);
136  }
137 
138 
140  {
141  NUKLEI_TRACE_BEGIN();
142  totalWeight_ = 0;
143  maxLocCutPoint_ = 0;
144  for (Container::const_iterator i = kernels_.begin(); i != kernels_.end(); i++)
145  {
146  *totalWeight_ += i->getWeight();
147  *maxLocCutPoint_ = std::max(*maxLocCutPoint_, i->polyCutPoint());
148  }
149  NUKLEI_TRACE_END();
150  }
151 
153  {
154  NUKLEI_TRACE_BEGIN();
155  if (!totalWeight_)
156  NUKLEI_THROW("Undefined total weight. Call computeKernelStatistics() first.");
157  return *totalWeight_;
158  NUKLEI_TRACE_END();
159  };
160 
161  weight_t KernelCollection::maxLocCutPoint() const
162  {
163  NUKLEI_TRACE_BEGIN();
164  if (!maxLocCutPoint_)
165  NUKLEI_THROW("Undefined max cut point. Call computeKernelStatistics() first.");
166  return *maxLocCutPoint_;
167  NUKLEI_TRACE_END();
168  };
169 
171  {
172  NUKLEI_TRACE_BEGIN();
173 
174  if (size() == 0)
175  {
176  totalWeight_ = 0;
177  return;
178  }
179 
180  if (!totalWeight_)
182 
183  for (Container::iterator i = kernels_.begin(); i != kernels_.end(); ++i)
184  {
185  i->setWeight( i->getWeight() / *totalWeight_ );
186  }
187 
188  totalWeight_ = 1;
189 
190  NUKLEI_TRACE_END();
191  }
192 
194  {
195  coord_t w = coord_t(1)/size();
196  for (Container::iterator i = kernels_.begin(); i != kernels_.end(); ++i)
197  i->setWeight(w);
198  totalWeight_ = 1;
199  }
200 
202  {
203  NUKLEI_TRACE_BEGIN();
204  NUKLEI_ASSERT(!empty());
205 
206  kernel::base::ptr moments = kernels_.back().create();
207 
208  coord_t w = 0;
209  for (const_iterator i = begin(); i != end(); i++)
210  {
211  coord_t f = i->getWeight() / (w + i->getWeight());
212  moments = moments->polyLinearInterpolation(*i, f);
213  w += i->getWeight();
214  }
215 
216  moments->setWeight(w / size());
217  return moments;
218  NUKLEI_TRACE_END();
219  }
220 
221  kernel::base::ptr KernelCollection::deviation(const kernel::base &center) const
222  {
223  NUKLEI_TRACE_BEGIN();
224  NUKLEI_ASSERT(!empty());
225 
226  kernel::base::ptr dev = center.clone();
227 
228  coord_t w = 0;
229  for (const_iterator i = begin(); i != end(); i++)
230  {
231  coord_t f = i->getWeight() / (w + i->getWeight());
232  dev->polyUpdateWidth(*i, f);
233  w += i->getWeight();
234  }
235 
236  return dev;
237  NUKLEI_TRACE_END();
238  }
239 
241  {
242  NUKLEI_TRACE_BEGIN();
244  moments = deviation(*moments);
245  return moments;
246  NUKLEI_TRACE_END();
247  }
248 
250  {
251  NUKLEI_TRACE_BEGIN();
252  invalidateHelperStructures();
253  for (iterator i = begin(); i != end(); i++)
254  i->polyMakeTransformWith(t);
255  NUKLEI_TRACE_END();
256  }
257 
258  void KernelCollection::transformWith(const Vector3 &translation,
259  const Quaternion &rotation)
260  {
261  NUKLEI_TRACE_BEGIN();
262  kernel::se3 transfo;
263  transfo.loc_ = translation;
264  la::copyRotation(transfo.ori_, rotation);
265  transformWith(transfo);
266  NUKLEI_TRACE_END();
267  }
268 
269  std::vector<Vector3> KernelCollection::get3DPointCloud() const
270  {
271  std::vector<Vector3> proj;
272  for (const_iterator i = begin(); i != end(); ++i)
273  proj.push_back(i->getLoc());
274  return proj;
275  }
276 
278  {
279  NUKLEI_TRACE_BEGIN();
282  i = sampleBegin(sampleSize);
283  i != i.end(); ++i)
284  {
285  kernel::base::ptr k = i->polySample();
286  k->setWeight( 1.0/sampleSize );
287  s.add(*k);
288  }
289  return s;
290  NUKLEI_TRACE_END();
291  }
292 
294  int sampleSize)
295  {
296  NUKLEI_TRACE_BEGIN();
297  *this = sample(sampleSize);
298  NUKLEI_TRACE_END();
299  }
300 
302  {
303  NUKLEI_TRACE_BEGIN();
304  NUKLEI_ASSERT(!empty());
305  return *sampleBegin(1);
306  NUKLEI_TRACE_END();
307  }
308 
310  {
311  NUKLEI_TRACE_BEGIN();
312  for (iterator i = kernels_.begin(); i != kernels_.end(); i++)
313  {
314  i->setLocH(h);
315  maxLocCutPoint_ = i->polyCutPoint();
316  }
317  NUKLEI_TRACE_END();
318  }
319 
321  {
322  NUKLEI_TRACE_BEGIN();
323  for (iterator i = kernels_.begin(); i != kernels_.end(); i++)
324  i->setOriH(h);
325  NUKLEI_TRACE_END();
326  }
327 
328  void KernelCollection::clearDescriptors()
329  {
330  NUKLEI_TRACE_BEGIN();
331  for (iterator i = kernels_.begin(); i != kernels_.end(); ++i)
332  i->clearDescriptor();
333  NUKLEI_TRACE_END();
334  }
335 
336  void KernelCollection::setFlag(const bitfield_t flag)
337  {
338  for (iterator i = kernels_.begin(); i != kernels_.end(); ++i)
339  i->setFlag(flag);
340  }
341 
342 }
343 
Quaternion ori_
Kernel orientation.
Definition: Kernel.h:482
nuklei_trsl::ppfilter_iterator< is_picked, const_iterator > const_sample_iterator
Sample Iterator type.
Type
Explicit query of a kernel's type. See Type Queries for more info.
Definition: Kernel.h:53
bool empty() const
Returns true if empty.
nuklei_trsl::reorder_iterator< const_iterator > const_sort_iterator
Sort Iterator type.
std::vector< Vector3 > get3DPointCloud() const
Returns the locations of the contained kernels in an std::vector.
void add(const kernel::base &f)
Adds a copy of f.
nuklei_trsl::ppfilter_iterator< is_picked, iterator > sample_iterator
Sample Iterator type.
Public namespace.
Definition: Color.cpp:9
Container::const_iterator const_iterator
KernelCollection iterator.
double weight_t
Type for particle weights.
Definition: Definitions.h:31
kernel::base::ptr moments() const
Returns a kernel holding the mean and standard deviation in position and orientation of the data.
Definition: Kernel.h:404
nuklei_trsl::is_picked_systematic< kernel::base, weight_t, kernel::base::WeightAccessor > is_picked
Used internally.
This class acts as a vector-like container for kernels. It also provides methods related to kernel de...
Vector3 loc_
Kernel location.
Definition: Kernel.h:480
sort_iterator sortBegin(size_t sortSize)
Returns an iterator that iterates through the sortSize kernels of highest weight, in order of decreas...
void uniformizeWeights()
Sets all weights to , where is the total weight of the collection.
unsigned int bitfield_t
Type for bitfield.
Definition: Definitions.h:33
void setKernelLocH(coord_t h)
Sets the location bandwidth of all kernels.
weight_t totalWeight() const
Returns the sum of kernel weights.
void setKernelOriH(coord_t h)
Sets the orientation bandwidth of all kernels.
#define NUKLEI_ASSERT(expression)
Throws an Error if expression is not true.
Definition: Common.h:113
Polymorphic kernel class.
Definition: Kernel.h:45
NUKLEI_UNIQUE_PTR< kernel::base > ptr
NUKLEI_UNIQUE_PTR for kernel::base.
Definition: Kernel.h:50
double coord_t
Type for point coordinates.
Definition: Definitions.h:25
Container::iterator end()
Returns an iterator pointing to the last kernel.
KernelCollection sample(int sampleSize) const
Returns sampleSize samples from the density modeled by *this.
const kernel::base & randomKernel() const
Returns a kernel from the collection.
kernel::base::ptr mean() const
Returns a kernel holding the mean position and orientation of the data.
void transformWith(const kernel::se3 &t)
Transforms the data with t.
Container::iterator begin()
Returns an iterator pointing to the first kernel.
void replace(const size_t idx, const kernel::base &k)
Replaces the idx'th kernel with a copy of k.
sample_iterator sampleBegin(size_t sampleSize)
Returns an iterator that iterates through sampleSize kernels selected randomly.
void normalizeWeights()
Divides all weights by the total weight.
void resetWithSampleOf(const KernelCollection &kc, int sampleSize)
Deprecated. Use sample() instead.
Container::size_type size() const
Returns the number of kernels.
virtual Type polyType() const =0
Get the "kernel type", i.e., its domain of definition.
nuklei_trsl::reorder_iterator< iterator > sort_iterator
Sort Iterator type.
void computeKernelStatistics()
Computes the sum of all kernel weights (total weight), and the maximum kernel cut point.
Container::iterator iterator
KernelCollection iterator.
void clear()
Resets the class to its initial state.
virtual base::ptr clone() const =0
Clone the kernel.
#define NUKLEI_THROW(x)
Throws an Error.
Definition: Common.h:94
© Copyright 2007-2013 Renaud Detry.
Distributed under the terms of the GNU General Public License (GPL).
(See accompanying file LICENSE.txt or copy at http://www.gnu.org/copyleft/gpl.html.)
Revised Sun Sep 13 2020 19:10:06.