Developer Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
SplatCloudT.cc
1 /*===========================================================================*\
2 * *
3 * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39 * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43 * *
44 * $Revision$ *
45 * $LastChangedBy$ *
46 * $Date$ *
47 * *
48 \*===========================================================================*/
49 
50 //================================================================
51 //
52 // CLASS SplatCloud - TEMPLATE IMPLEMENTATION
53 //
54 //================================================================
55 
56 
57 #define SPLATCLOUDT_CC
58 
59 
60 //== INCLUDES ====================================================
61 
62 
63 #include "SplatCloud.hh"
64 
65 
66 //== IMPLEMENTATION ==============================================
67 
68 
69 template <typename T>
70 unsigned int SplatCloud::eraseSplatsByIndex( const T &_indices )
71 {
72  int maxIdx = numSplats_ - 1;
73 
74  // create vector of flags indicating which elements to delete (initialized to false)
75  std::vector<bool> flags( numSplats_, false );
76 
77  // set flags given by indices
78  typename T::const_iterator idxIter;
79  for( idxIter = _indices.begin(); idxIter != _indices.end(); ++idxIter )
80  {
81  // convert index to int
82  int idx = static_cast<int>( *idxIter );
83 
84  // if index is valid, set flag
85  if( (idx >= 0) && (idx <= maxIdx) )
86  flags[ idx ] = true;
87  }
88 
89  // delete elements and return number of deleted elements
90  return eraseSplatsByFlag( flags );
91 }
92 
93 
94 //----------------------------------------------------------------
95 
96 
97 template <typename T>
98 unsigned int SplatCloud::eraseSplatsByFlag( const std::vector<T> &_flags )
99 {
100  // check for right size of flags vector
101  if( _flags.size() != numSplats_ )
102  return 0; // error; no elements have been deleted so return 0
103 
104  // create index vector indicating which elements to keep
105  std::vector<int> indices;
106 
107  // reserve enough memory
108  indices.reserve( numSplats_ );
109 
110  // fill index vector
111  unsigned int i;
112  for( i=0; i<numSplats_; ++i )
113  {
114  if( !_flags[i] )
115  indices.push_back( i );
116  }
117 
118  // calculate number of elements to delete
119  unsigned int numDelete = numSplats_ - indices.size();
120 
121  // check if something has to be deleted
122  if( numDelete != 0 )
123  {
124  // keep only elements with given indices in data vector of all splat-properties
125  SplatPropertyMap::const_iterator splatPropertyIter;
126  for( splatPropertyIter = splatProperties_.begin(); splatPropertyIter != splatProperties_.end(); ++splatPropertyIter )
127  splatPropertyIter->second.property_->crop( indices );
128 
129  // update number of splats
130  numSplats_ = indices.size();
131  }
132 
133  // return number of deleted elements
134  return numDelete;
135 }
136 
137 
138 //----------------------------------------------------------------
139 
140 
141 template <typename T>
142 void SplatCloud::cropSplats( const T &_indices )
143 {
144  int maxIdx = numSplats_ - 1;
145 
146  // create vector of valid indices
147  std::vector<int> validIndices;
148  validIndices.reserve( _indices.size() );
149 
150  // set valid indices
151  typename T::const_iterator idxIter;
152  for( idxIter = _indices.begin(); idxIter != _indices.end(); ++idxIter )
153  {
154  // convert index to int
155  int idx = static_cast<int>( *idxIter );
156 
157  // if index is valid, add index to valid indices
158  if( (idx >= 0) && (idx <= maxIdx) )
159  validIndices.push_back( idx );
160  }
161 
162  // keep only elements with given indices in data vector of all splat-properties
163  SplatPropertyMap::const_iterator splatPropertyIter;
164  for( splatPropertyIter = splatProperties_.begin(); splatPropertyIter != splatProperties_.end(); ++splatPropertyIter )
165  splatPropertyIter->second.property_->crop( validIndices );
166 
167  // update number of splats
168  numSplats_ = validIndices.size();
169 }
170 
171 
172 //----------------------------------------------------------------
173 
174 
175 template <typename T>
176 /*virtual*/ void SplatCloud::SplatPropertyT<T>::crop( const std::vector<int> &_indices )
177 {
178  // create new data vector
179  std::vector<T> newData;
180  newData.reserve( _indices.size() );
181 
182  // fill new data vector by inserting elements of old data vector with given indices
183  std::vector<int>::const_iterator idxIter;
184  for( idxIter = _indices.begin(); idxIter != _indices.end(); ++idxIter )
185  newData.push_back( data_[ *idxIter ] );
186 
187  // set old data vector to new one (swap and discard old vector)
188  data_.swap( newData );
189 }
190 
191 
192 //----------------------------------------------------------------
193 
194 
195 template <typename T>
197 {
198  // try to find property map entry
199  SplatPropertyMap::iterator iter = splatProperties_.find( _handle );
200 
201  // check if a property with the same name is already present
202  if( iter != splatProperties_.end() )
203  {
204  // try to cast
205  SplatPropertyT<T> *oldProp = dynamic_cast<SplatPropertyT<T> *>( iter->second.property_ );
206 
207  // if found property is of the right type, increase number of requests
208  if( oldProp != 0 )
209  ++iter->second.numRequests_;
210 
211  // return pointer to old property
212  return oldProp;
213  }
214 
215  // create new property
216  SplatPropertyT<T> *newProp = new SplatPropertyT<T>( _handle, numSplats_ );
217 
218  // if creation went wrong, free memory and reset pointer
219  if( newProp->data_.size() != numSplats_ )
220  {
221  delete newProp;
222  newProp = 0;
223  }
224 
225  // if pointer is valid, insert new property map entry
226  if( newProp != 0 )
227  splatProperties_[ _handle ] = SplatPropertyMapEntry( newProp, 1 );
228 
229  // return pointer to new property
230  return newProp;
231 }
232 
233 
234 //----------------------------------------------------------------
235 
236 
237 template <typename T>
239 {
240  // try to find property map entry
241  CloudPropertyMap::iterator iter = cloudProperties_.find( _handle );
242 
243  // check if a property with the same name is already present
244  if( iter != cloudProperties_.end() )
245  {
246  // try to cast
247  CloudPropertyT<T> *oldProp = dynamic_cast<CloudPropertyT<T> *>( iter->second.property_ );
248 
249  // if found property is of the right type, increase number of requests
250  if( oldProp != 0 )
251  ++iter->second.numRequests_;
252 
253  // return pointer to old property
254  return oldProp;
255  }
256 
257  // create new property
258  CloudPropertyT<T> *newProp = new CloudPropertyT<T>( _handle );
259 
260  // if pointer is valid, insert new property map entry
261  if( newProp != 0 )
262  cloudProperties_[ _handle ] = CloudPropertyMapEntry( newProp, 1 );
263 
264  // return pointer to new property
265  return newProp;
266 }
267 
268 
269 //----------------------------------------------------------------
270 
271 
272 template <typename T>
274 {
275  // try to find property map entry
276  SplatPropertyMap::iterator iter = splatProperties_.find( _handle );
277 
278  // if *not* found, abort
279  if( iter == splatProperties_.end() )
280  return 0;
281 
282  // try to cast
283  SplatPropertyT<T> *prop = dynamic_cast<SplatPropertyT<T> *>( iter->second.property_ );
284 
285  // check if found property is of the right type
286  if( prop != 0 )
287  {
288  // decrease number of request
289  --iter->second.numRequests_;
290 
291  // check if property should be removed now
292  if( iter->second.numRequests_ == 0 )
293  {
294  // free memory of property and reset pointer
295  delete prop;
296  prop = 0;
297 
298  // remove property map entry
299  splatProperties_.erase( iter );
300  }
301  }
302 
303  // return pointer to property
304  return prop;
305 }
306 
307 
308 //----------------------------------------------------------------
309 
310 
311 template <typename T>
313 {
314  // try to find property map entry
315  CloudPropertyMap::iterator iter = cloudProperties_.find( _handle );
316 
317  // if *not* found, abort
318  if( iter == cloudProperties_.end() )
319  return 0;
320 
321  // try to cast
322  CloudPropertyT<T> *prop = dynamic_cast<CloudPropertyT<T> *>( iter->second.property_ );
323 
324  // check if found property is of the right type
325  if( prop != 0 )
326  {
327  // decrease number of request
328  --iter->second.numRequests_;
329 
330  // check if property should be removed now
331  if( iter->second.numRequests_ == 0 )
332  {
333  // free memory of property and reset pointer
334  delete prop;
335  prop = 0;
336 
337  // remove property map entry
338  cloudProperties_.erase( iter );
339  }
340  }
341 
342  // return pointer to property
343  return prop;
344 }
345 
346 
347 //----------------------------------------------------------------
348 
349 
350 template <typename T>
352 {
353  // try to find property map entry
354  SplatPropertyMap::const_iterator iter = splatProperties_.find( _handle );
355 
356  // if *not* found or cast fails return 0, otherwise return a valid pointer
357  return (iter == splatProperties_.end()) ? 0 : dynamic_cast<SplatPropertyT<T> *>( iter->second.property_ );
358 }
359 
360 
361 //----------------------------------------------------------------
362 
363 
364 template <typename T>
366 {
367  // try to find property map entry
368  SplatPropertyMap::const_iterator iter = splatProperties_.find( _handle );
369 
370  // if *not* found or cast fails return 0, otherwise return a valid pointer
371  return (iter == splatProperties_.end()) ? 0 : dynamic_cast<const SplatPropertyT<T> *>( iter->second.property_ );
372 }
373 
374 
375 //----------------------------------------------------------------
376 
377 
378 template <typename T>
380 {
381  // try to find property map entry
382  CloudPropertyMap::const_iterator iter = cloudProperties_.find( _handle );
383 
384  // if *not* found or cast fails return 0, otherwise return a valid pointer
385  return (iter == cloudProperties_.end()) ? 0 : dynamic_cast<CloudPropertyT<T> *>( iter->second.property_ );
386 }
387 
388 
389 //----------------------------------------------------------------
390 
391 
392 template <typename T>
394 {
395  // try to find property map entry
396  CloudPropertyMap::const_iterator iter = cloudProperties_.find( _handle );
397 
398  // if *not* found or cast fails return 0, otherwise return a valid pointer
399  return (iter == cloudProperties_.end()) ? 0 : dynamic_cast<const CloudPropertyT<T> *>( iter->second.property_ );
400 }
CloudPropertyMap cloudProperties_
Cloud-property map.
Definition: SplatCloud.hh:485
SplatPropertyMap splatProperties_
Splat-property map.
Definition: SplatCloud.hh:482
CloudPropertyT< T > * requestCloudProperty(const PropertyHandleT< T > &_handle)
Request a new property.
Definition: SplatCloudT.cc:238
Index & indices(int _idx)
Get a reference of the predefined property's value.
Definition: SplatCloud.hh:645
unsigned int eraseSplatsByFlag(const std::vector< T > &_flags)
Delete the elements with flag != 0 from the data vector of all splat-properties.
Definition: SplatCloudT.cc:98
SplatPropertyT< T > * releaseSplatProperty(const PropertyHandleT< T > &_handle)
Release a property.
Definition: SplatCloudT.cc:273
SplatPropertyT< T > * getSplatProperty(const PropertyHandleT< T > &_handle)
Get a pointer to a property.
Definition: SplatCloudT.cc:351
unsigned int eraseSplatsByIndex(const T &_indices)
Delete the elements with given indices from the data vector of all splat-properties.
Definition: SplatCloudT.cc:70
CloudPropertyT< T > * releaseCloudProperty(const PropertyHandleT< T > &_handle)
Release a property.
Definition: SplatCloudT.cc:312
virtual void crop(const std::vector< int > &_indices)
Keep only the elements with given indices in the data vector. The indices have to be valid...
Definition: SplatCloudT.cc:176
std::vector< T > data_
The actual stored data (one element per splat)
Definition: SplatCloud.hh:323
void cropSplats(const T &_indices)
Keep only the elements with given indices in the data vector of all splat-properties. The splats will be rearranged depending on the order of the indices.
Definition: SplatCloudT.cc:142
CloudPropertyT< T > * getCloudProperty(const PropertyHandleT< T > &_handle)
Get a pointer to a property.
Definition: SplatCloudT.cc:379
unsigned int numSplats_
Number of splats.
Definition: SplatCloud.hh:192
SplatPropertyT< T > * requestSplatProperty(const PropertyHandleT< T > &_handle)
Request a new property.
Definition: SplatCloudT.cc:196