SplatCloudT.cc 8.88 KB
Newer Older
Jan Möbius's avatar
Jan Möbius committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//================================================================
//
//  CLASS SplatCloud - TEMPLATE IMPLEMENTATION
//
//================================================================


#define SPLATCLOUDT_CC


//== INCLUDES ====================================================


#include "SplatCloud.hh"


//== IMPLEMENTATION ==============================================


template <typename T>
unsigned int SplatCloud::eraseSplatsByIndex( const T &_indices )
{
  int maxIdx = numSplats_ - 1;

  // create vector of flags indicating which elements to delete (initialized to false)
  std::vector<bool> flags( numSplats_, false );

  // set flags given by indices
  typename T::const_iterator idxIter;
  for( idxIter = _indices.begin(); idxIter != _indices.end(); ++idxIter )
  {
    // convert index to int
    int idx = static_cast<int>( *idxIter );

    // if index is valid, set flag
    if( (idx >= 0) && (idx <= maxIdx) )
      flags[ idx ] = true;
  }

  // delete elements and return number of deleted elements
  return eraseSplatsByFlag( flags );
}


//----------------------------------------------------------------


template <typename T>
unsigned int SplatCloud::eraseSplatsByFlag( const std::vector<T> &_flags )
{
  // check for right size of flags vector
  if( _flags.size() != numSplats_ )
    return 0; // error; no elements have been deleted so return 0

  // create index vector indicating which elements to keep
  std::vector<int> indices;

  // reserve enough memory
  indices.reserve( numSplats_ );

  // fill index vector
  unsigned int i;
  for( i=0; i<numSplats_; ++i )
  {
    if( !_flags[i] )
      indices.push_back( i );
  }

  // calculate number of elements to delete
  unsigned int numDelete = numSplats_ - indices.size();

  // check if something has to be deleted
  if( numDelete != 0 )
  {
    // keep only elements with given indices in data vector of all splat-properties
76
    SplatPropertyMap::const_iterator splatPropertyIter;
Jan Möbius's avatar
Jan Möbius committed
77
    for( splatPropertyIter = splatProperties_.begin(); splatPropertyIter != splatProperties_.end(); ++splatPropertyIter )
78
      splatPropertyIter->second.property_->crop( indices );
Jan Möbius's avatar
Jan Möbius committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112

    // update number of splats
    numSplats_ = indices.size();
  }

  // return number of deleted elements
  return numDelete;
}


//----------------------------------------------------------------


template <typename T>
/*virtual*/ void SplatCloud::SplatPropertyT<T>::crop( const std::vector<int> &_indices )
{
  // create new data vector
  std::vector<T> newData;
  newData.reserve( _indices.size() );

  // fill new data vector by inserting elements of old data vector with given indices
  std::vector<int>::const_iterator idxIter;
  for( idxIter = _indices.begin(); idxIter != _indices.end(); ++idxIter )
    newData.push_back( data_[ *idxIter ] );

  // set old data vector to new one (swap and discard old vector)
  data_.swap( newData );
}


//----------------------------------------------------------------


template <typename T>
Jan Möbius's avatar
Jan Möbius committed
113
SplatCloud::SplatPropertyT<T> *SplatCloud::requestSplatProperty( const SplatCloud::PropertyHandleT<T> &_handle )
Jan Möbius's avatar
Jan Möbius committed
114
{
115
116
  // try to find property map entry
  SplatPropertyMap::iterator iter = splatProperties_.find( _handle );
Jan Möbius's avatar
Jan Möbius committed
117

118
  // check if a property with the same name is already present
Jan Möbius's avatar
Jan Möbius committed
119
120
  if( iter != splatProperties_.end() )
  {
121
    // try to cast
Jan Möbius's avatar
Jan Möbius committed
122
    SplatPropertyT<T> *oldProp = dynamic_cast<SplatPropertyT<T> *>( iter->second.property_ );
123

Jan Möbius's avatar
Jan Möbius committed
124
125
126
    // if found property is of the right type, increase number of requests
    if( oldProp != 0 )
      ++iter->second.numRequests_;
127

Jan Möbius's avatar
Jan Möbius committed
128
129
    // return pointer to old property
    return oldProp;
Jan Möbius's avatar
Jan Möbius committed
130
131
132
  }

  // create new property
Jan Möbius's avatar
Jan Möbius committed
133
  SplatPropertyT<T> *newProp = new SplatPropertyT<T>( _handle, numSplats_ );
Jan Möbius's avatar
Jan Möbius committed
134

Jan Möbius's avatar
Jan Möbius committed
135
136
  // if creation went wrong, free memory and reset pointer
  if( newProp->data_.size() != numSplats_ )
Jan Möbius's avatar
Jan Möbius committed
137
  {
Jan Möbius's avatar
Jan Möbius committed
138
139
    delete newProp;
    newProp = 0;
Jan Möbius's avatar
Jan Möbius committed
140
141
  }

Jan Möbius's avatar
Jan Möbius committed
142
143
144
145
146
147
  // if pointer is valid, insert new property map entry
  if( newProp != 0 )
    splatProperties_[ _handle ] = SplatPropertyMapEntry( newProp, 1 );

  // return pointer to new property
  return newProp;
Jan Möbius's avatar
Jan Möbius committed
148
149
150
151
152
153
154
}


//----------------------------------------------------------------


template <typename T>
Jan Möbius's avatar
Jan Möbius committed
155
SplatCloud::CloudPropertyT<T> *SplatCloud::requestCloudProperty( const SplatCloud::PropertyHandleT<T> &_handle )
Jan Möbius's avatar
Jan Möbius committed
156
{
157
158
  // try to find property map entry
  CloudPropertyMap::iterator iter = cloudProperties_.find( _handle );
Jan Möbius's avatar
Jan Möbius committed
159

160
  // check if a property with the same name is already present
Jan Möbius's avatar
Jan Möbius committed
161
162
  if( iter != cloudProperties_.end() )
  {
163
    // try to cast
Jan Möbius's avatar
Jan Möbius committed
164
    CloudPropertyT<T> *oldProp = dynamic_cast<CloudPropertyT<T> *>( iter->second.property_ );
165

Jan Möbius's avatar
Jan Möbius committed
166
167
168
    // if found property is of the right type, increase number of requests
    if( oldProp != 0 )
      ++iter->second.numRequests_;
169

Jan Möbius's avatar
Jan Möbius committed
170
171
    // return pointer to old property
    return oldProp;
Jan Möbius's avatar
Jan Möbius committed
172
173
174
  }

  // create new property
Jan Möbius's avatar
Jan Möbius committed
175
  CloudPropertyT<T> *newProp = new CloudPropertyT<T>( _handle );
Jan Möbius's avatar
Jan Möbius committed
176

Jan Möbius's avatar
Jan Möbius committed
177
178
179
  // if pointer is valid, insert new property map entry
  if( newProp != 0 )
    cloudProperties_[ _handle ] = CloudPropertyMapEntry( newProp, 1 );
Jan Möbius's avatar
Jan Möbius committed
180

Jan Möbius's avatar
Jan Möbius committed
181
182
  // return pointer to new property
  return newProp;
Jan Möbius's avatar
Jan Möbius committed
183
184
185
186
187
188
189
}


//----------------------------------------------------------------


template <typename T>
Jan Möbius's avatar
Jan Möbius committed
190
SplatCloud::SplatPropertyT<T> *SplatCloud::releaseSplatProperty( const SplatCloud::PropertyHandleT<T> &_handle )
Jan Möbius's avatar
Jan Möbius committed
191
{
192
193
  // try to find property map entry
  SplatPropertyMap::iterator iter = splatProperties_.find( _handle );
Jan Möbius's avatar
Jan Möbius committed
194

Jan Möbius's avatar
Jan Möbius committed
195
196
197
  // if *not* found, abort
  if( iter == splatProperties_.end() )
    return 0;
Jan Möbius's avatar
Jan Möbius committed
198

Jan Möbius's avatar
Jan Möbius committed
199
200
  // try to cast
  SplatPropertyT<T> *prop = dynamic_cast<SplatPropertyT<T> *>( iter->second.property_ );
201

Jan Möbius's avatar
Jan Möbius committed
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
  // check if found property is of the right type
  if( prop != 0 )
  {
    // decrease number of request
    --iter->second.numRequests_;

    // check if property should be removed now
    if( iter->second.numRequests_ == 0 )
    {
      // free memory of property and reset pointer
      delete prop;
      prop = 0;

      // remove property map entry
      splatProperties_.erase( iter );
    }
  }
Jan Möbius's avatar
Jan Möbius committed
219

Jan Möbius's avatar
Jan Möbius committed
220
221
  // return pointer to property
  return prop;
Jan Möbius's avatar
Jan Möbius committed
222
223
224
225
226
227
228
}


//----------------------------------------------------------------


template <typename T>
Jan Möbius's avatar
Jan Möbius committed
229
SplatCloud::CloudPropertyT<T> *SplatCloud::releaseCloudProperty( const SplatCloud::PropertyHandleT<T> &_handle )
Jan Möbius's avatar
Jan Möbius committed
230
{
231
232
  // try to find property map entry
  CloudPropertyMap::iterator iter = cloudProperties_.find( _handle );
Jan Möbius's avatar
Jan Möbius committed
233

Jan Möbius's avatar
Jan Möbius committed
234
235
236
  // if *not* found, abort
  if( iter == cloudProperties_.end() )
    return 0;
237

Jan Möbius's avatar
Jan Möbius committed
238
239
  // try to cast
  CloudPropertyT<T> *prop = dynamic_cast<CloudPropertyT<T> *>( iter->second.property_ );
240

Jan Möbius's avatar
Jan Möbius committed
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
  // check if found property is of the right type
  if( prop != 0 )
  {
    // decrease number of request
    --iter->second.numRequests_;

    // check if property should be removed now
    if( iter->second.numRequests_ == 0 )
    {
      // free memory of property and reset pointer
      delete prop;
      prop = 0;

      // remove property map entry
      cloudProperties_.erase( iter );
    }
  }
Jan Möbius's avatar
Jan Möbius committed
258

Jan Möbius's avatar
Jan Möbius committed
259
260
  // return pointer to property
  return prop;
Jan Möbius's avatar
Jan Möbius committed
261
262
263
264
265
266
267
}


//----------------------------------------------------------------


template <typename T>
Jan Möbius's avatar
Jan Möbius committed
268
SplatCloud::SplatPropertyT<T> *SplatCloud::getSplatProperty( const SplatCloud::PropertyHandleT<T> &_handle )
Jan Möbius's avatar
Jan Möbius committed
269
{
270
271
  // try to find property map entry
  SplatPropertyMap::const_iterator iter = splatProperties_.find( _handle );
Jan Möbius's avatar
Jan Möbius committed
272

Jan Möbius's avatar
Jan Möbius committed
273
274
  // if *not* found or cast fails return 0, otherwise return a valid pointer
  return (iter == splatProperties_.end()) ? 0 : dynamic_cast<SplatPropertyT<T> *>( iter->second.property_ );
Jan Möbius's avatar
Jan Möbius committed
275
276
277
278
279
280
281
}


//----------------------------------------------------------------


template <typename T>
Jan Möbius's avatar
Jan Möbius committed
282
const SplatCloud::SplatPropertyT<T> *SplatCloud::getSplatProperty( const SplatCloud::PropertyHandleT<T> &_handle ) const
Jan Möbius's avatar
Jan Möbius committed
283
{
284
285
  // try to find property map entry
  SplatPropertyMap::const_iterator iter = splatProperties_.find( _handle );
Jan Möbius's avatar
Jan Möbius committed
286

Jan Möbius's avatar
Jan Möbius committed
287
288
  // if *not* found or cast fails return 0, otherwise return a valid pointer
  return (iter == splatProperties_.end()) ? 0 : dynamic_cast<const SplatPropertyT<T> *>( iter->second.property_ );
Jan Möbius's avatar
Jan Möbius committed
289
290
291
292
293
294
295
}


//----------------------------------------------------------------


template <typename T>
Jan Möbius's avatar
Jan Möbius committed
296
SplatCloud::CloudPropertyT<T> *SplatCloud::getCloudProperty( const SplatCloud::PropertyHandleT<T> &_handle )
Jan Möbius's avatar
Jan Möbius committed
297
{
298
299
  // try to find property map entry
  CloudPropertyMap::const_iterator iter = cloudProperties_.find( _handle );
Jan Möbius's avatar
Jan Möbius committed
300

Jan Möbius's avatar
Jan Möbius committed
301
302
  // if *not* found or cast fails return 0, otherwise return a valid pointer
  return (iter == cloudProperties_.end()) ? 0 : dynamic_cast<CloudPropertyT<T> *>( iter->second.property_ );
Jan Möbius's avatar
Jan Möbius committed
303
304
305
306
307
308
309
}


//----------------------------------------------------------------


template <typename T>
Jan Möbius's avatar
Jan Möbius committed
310
const SplatCloud::CloudPropertyT<T> *SplatCloud::getCloudProperty( const SplatCloud::PropertyHandleT<T> &_handle ) const
Jan Möbius's avatar
Jan Möbius committed
311
{
312
313
  // try to find property map entry
  CloudPropertyMap::const_iterator iter = cloudProperties_.find( _handle );
Jan Möbius's avatar
Jan Möbius committed
314

Jan Möbius's avatar
Jan Möbius committed
315
316
  // if *not* found or cast fails return 0, otherwise return a valid pointer
  return (iter == cloudProperties_.end()) ? 0 : dynamic_cast<const CloudPropertyT<T> *>( iter->second.property_ );
Jan Möbius's avatar
Jan Möbius committed
317
}