IndexedTreeDataStrategy.h
Go to the documentation of this file.
1#ifndef __GfxTL_INDEXEDTREEDATASTRATEGY_HEADER__
2#define __GfxTL_INDEXEDTREEDATASTRATEGY_HEADER__
3
4#include <list>
5#include <vector>
6
7namespace GfxTL
8{
9
10 template <class InheritedStrategy, class Data>
12 {
13 private:
14 template <class Data>
15 class IndexedTreeDataCore
16 {
17 public:
18 typedef Data DataType;
19 typedef typename Data::PointType PointType;
20 typedef typename PointType::ScalarType ScalarType;
21
22 Data&
23 ContainedData()
24 {
25 return _data;
26 }
27
28 const Data&
29 ContainedData() const
30 {
31 return _data;
32 }
33
35 PointTranslated(size_t i)
36 {
37 return _data.Point(i);
38 }
39
40 const PointType
41 PointTranslated(size_t i) const
42 {
43 return _data.Point(i);
44 }
45
47 operator[](size_t i)
48 {
49 return _data.Point(i);
50 }
51
52 const PointType
53 operator[](size_t i) const
54 {
55 return _data.Point(i);
56 }
57
58 private:
59 Data _data;
60 };
61
62 template <class Data>
63 class IndexedTreeDataCore<const Data>
64 {
65 public:
66 typedef const Data DataType;
67 typedef typename Data::PointType PointType;
68 typedef typename PointType::ScalarType ScalarType;
69
70 Data&
71 NonConstContainedData()
72 {
73 return _data;
74 }
75
76 const Data&
77 ContainedData() const
78 {
79 return _data;
80 }
81
82 const PointType
83 PointTranslated(size_t i) const
84 {
85 return _data.Point(i);
86 }
87
88 const PointType
89 operator[](size_t i) const
90 {
91 return _data.Point(i);
92 }
93
94 private:
95 Data _data;
96 };
97
98 template <class Data>
99 class IndexedTreeDataCore<Data*>
100 {
101 public:
102 typedef Data DataType;
103 typedef typename Data::PointType PointType;
104 typedef typename PointType::ScalarType ScalarType;
105
106 IndexedTreeDataCore() : _data(NULL)
107 {
108 }
109
110 Data&
111 ContainedData()
112 {
113 return *_data;
114 }
115
116 const Data&
117 ContainedData() const
118 {
119 return *_data;
120 }
121
122 void
123 ContainedDataPtr(Data* data)
124 {
125 _data = data;
126 }
127
128 PointType&
129 PointTranslated(size_t i)
130 {
131 return _data->Point(i);
132 }
133
134 const PointType
135 PointTranslated(size_t i) const
136 {
137 return _data->Point(i);
138 }
139
140 PointType&
141 operator[](size_t i)
142 {
143 return _data->Point(i);
144 }
145
146 const PointType
147 operator[](size_t i) const
148 {
149 return _data->Point(i);
150 }
151
152 private:
153 Data* _data;
154 };
155
156 template <class Data>
157 class IndexedTreeDataCore<const Data*>
158 {
159 public:
160 typedef const Data DataType;
161 typedef typename Data::PointType PointType;
162 typedef typename PointType::ScalarType ScalarType;
163
164 const Data&
165 ContainedData() const
166 {
167 return *_data;
168 }
169
170 void
171 ContainedDataPtr(const Data* data)
172 {
173 _data = data;
174 }
175
176 const PointType
177 PointTranslated(size_t i) const
178 {
179 return _data->Point(i);
180 }
181
182 const PointType
183 operator[](size_t i) const
184 {
185 return _data->Point(i);
186 }
187
188 private:
189 const Data* _data;
190 };
191
192 public:
193 typedef size_t HandleType;
194 typedef std::vector<HandleType> IndicesType;
195 typedef size_t CellIndicesType;
196 typedef typename IndexedTreeDataCore<Data>::DataType DataType;
197 typedef typename DataType::PointType PointType;
198 typedef typename PointType::ScalarType ScalarType;
199
200 template <class Point>
201 class CellData : public InheritedStrategy::CellData<Point>
202 {
203 public:
205
206 CellData() : _size(0)
207 {
208 }
209
210 CellData(const CellIndicesType& indices, size_t size) : _indices(indices), _size(size)
211 {
212 }
213
214 size_t
215 Size() const
216 {
217 return _size;
218 }
219
220 void
221 Size(size_t size)
222 {
223 _size = size;
224 }
225
226 const CellIndicesType
227 Points() const
228 {
229 return _indices;
230 }
231
232 const CellIndicesType
233 Normals() const
234 {
235 return _indices;
236 }
237
238 void
239 Data(const CellIndicesType indices, size_t size)
240 {
241 _indices = indices;
242 _size = size;
243 }
244
245 void
247 {
248 _indices = c._indices;
249 _size = c._size;
250 }
251
253 Indices() const
254 {
255 return _indices;
256 }
257
258 void
260 {
261 _indices = indices;
262 }
263
264 private:
265 CellIndicesType _indices;
266 size_t _size;
267 };
268
269 template <class Base>
271 public InheritedStrategy::StrategyBase<Base>,
272 public IndexedTreeDataCore<Data>
273 {
274 public:
275 typedef typename Base::CellType CellType;
276 typedef typename IndexedTreeDataCore<Data>::DataType DataType;
277 typedef typename DataType::PointType PointType;
278 typedef typename PointType::ScalarType ScalarType;
279 typedef
281 typedef
283 typedef typename InheritedStrategy::StrategyBase<Base> StrategyBaseType;
284
286 {
287 }
288
289 void
291 {
292 StrategyBaseType::Clear();
293 _indices.clear();
294 }
295
296 size_t
298 {
299 return _indices[i];
300 }
301
302 size_t
303 Size() const
304 {
305 return ContainedData().Size();
306 }
307
308 void
309 PointsInCell(const CellType& cell, std::vector<size_t>* points) const
310 {
311 points->clear();
312 for (size_t i = 0; i < cell.Size(); ++i)
313 {
314 points->push_back(Translate(cell.Points() + i));
315 }
316 }
317
318 void
320 {
321 cube->Bound(*this, Size());
322 }
323
324 template <class BV>
325 void
327 {
328 bv->Bound(*this, Size());
329 }
330
331 void
333 {
334 ContainedData().Add(data);
336 bc.Bound(data.Points(), data.Size());
338 }
339
340 template <class T>
341 void
342 Transform(const T& t)
343 {
344 ContainedData().Transform(t);
345 AACube<PointType> bc; // is infinite
347 }
348
349 template <class T>
350 void
351 Transform(size_t i, const T& t)
352 {
353 ContainedData().Transform(i, t);
354 AACube<PointType> bc; // is infinite
356 }
357
358 void
359 Translate(const PointType& translation)
360 {
361 ContainedData().Translate(translation);
362 AACube<PointType> bc; // is infinite
364 }
365
366 template <class TFunc>
367 void
368 TransformFunc(TFunc tfunc)
369 {
370 ContainedData().TransformFunc(tfunc);
371 AACube<PointType> bc; // is infinite
373 }
374
375 template <class BoundingVolume>
376 void
378 {
379 ContainedData().Remove(bv);
380 AACube<PointType> bc; // is infinite
382 }
383
384 virtual void
386 {
387 if (_indices.size() != ContainedData().Size())
388 {
389 size_t oldSize = _indices.size();
390 _indices.resize(ContainedData().Size());
391 for (size_t i = oldSize; i < _indices.size(); ++i)
392 {
393 _indices[i] = i;
394 }
395 }
396 }
397
398 protected:
399 template <class BoundingVolume>
400 void
402 {
403 _indices.resize(ContainedData().Size());
404 for (size_t i = 0; i < ContainedData().Size(); ++i)
405 {
406 _indices[i] = i;
407 }
408 IndicesType::iterator j = _indices.begin();
409 IndicesType::iterator k = _indices.begin() + _indices.size() - 1;
410 for (; j <= k;)
411 {
412 if (!bv.IsInside(ContainedData().Point(*j)))
413 {
414 HandleType q = *k;
415 *k = *j;
416 *j = q;
417 --k;
418 }
419 else
420 {
421 ++j;
422 }
423 }
424 cell->Data(0, j - _indices.begin());
425 }
426
427 void
429 unsigned int axis,
430 ScalarType s,
431 CellType* left,
432 CellType* right)
433 {
434 if (cell.Size() == 0)
435 {
436 right->Data(cell);
437 left->Data(cell);
438 return;
439 }
440 IndicesType::iterator j = _indices.begin() + cell.Indices();
441 IndicesType::iterator k = _indices.begin() + cell.Indices() + cell.Size() - 1;
442 for (; j <= k;)
443 {
444 if (ContainedData().Point(*j)[axis] > s)
445 {
446 HandleType q = *k;
447 *k = *j;
448 *j = q;
449 --k;
450 }
451 else
452 {
453 ++j;
454 }
455 }
456 left->Indices(cell.Indices());
457 left->Size(j - (_indices.begin() + cell.Indices()));
458 right->Indices(cell.Indices() + left->Size());
459 right->Size((cell.Indices() + cell.Size()) - right->Indices());
460 }
461
462 void
464 {
465 cube->BoundingCube(IndexPoints(data, data.Indices(), data.Size()), data.Size());
466 }
467
468 private:
469 IndicesType _indices;
470 };
471 };
472
473}; // namespace GfxTL
474
475#endif
uint8_t data[1]
constexpr T c
void Bound(const Points &points, size_t size)
Definition AACube.h:86
CellData(const CellIndicesType &indices, size_t size)
void Data(const CellIndicesType indices, size_t size)
void SplitAlongAxis(const CellType &cell, unsigned int axis, ScalarType s, CellType *left, CellType *right)
IndexedTreeDataStrategy< InheritedStrategy, Data >::HandleType HandleType
IndexedTreeDataCore< Data >::DataType DataType
void BoundingCube(const CellType &data, AACube< PointType > *cube)
IndexedTreeDataStrategy< InheritedStrategy, Data >::IndicesType IndicesType
InheritedStrategy::StrategyBase< Base > StrategyBaseType
void PointsInCell(const CellType &cell, std::vector< size_t > *points) const
virtual void RefreshWithNewTreeData(const AACube< PointType > &bc)
void RootCellData(const BoundingVolume &bv, CellType *cell)
IndexedTreeDataCore< Data >::DataType DataType
std::vector< HandleType > IndicesType
#define q
Definition AABox.h:10