Partio
PartioIterator.h
Go to the documentation of this file.
1/*
2PARTIO SOFTWARE
3Copyright 2010 Disney Enterprises, Inc. All rights reserved
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are
7met:
8
9* Redistributions of source code must retain the above copyright
10notice, this list of conditions and the following disclaimer.
11
12* Redistributions in binary form must reproduce the above copyright
13notice, this list of conditions and the following disclaimer in
14the documentation and/or other materials provided with the
15distribution.
16
17* The names "Disney", "Walt Disney Pictures", "Walt Disney Animation
18Studios" or the names of its contributors may NOT be used to
19endorse or promote products derived from this software without
20specific prior written permission from Walt Disney Pictures.
21
22Disclaimer: THIS SOFTWARE IS PROVIDED BY WALT DISNEY PICTURES AND
23CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
25FOR A PARTICULAR PURPOSE, NONINFRINGEMENT AND TITLE ARE DISCLAIMED.
26IN NO EVENT SHALL WALT DISNEY PICTURES, THE COPYRIGHT HOLDER OR
27CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND BASED ON ANY
31THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
34*/
35#ifndef _PartioParticleIterator_h_
36#define _PartioParticleIterator_h_
37
38#include <cassert>
39#include <vector>
40#include <iostream>
41#include "PartioAttribute.h"
42
43namespace Partio{
44
45class ParticlesData;
46struct ParticleAccessor;
47
49
53template<class T,int d>
54struct Data
55{
56 T x[d];
57
58 const T& operator[](const int i) const {return x[i];}
59 T& operator[](const int i) {return x[i];}
60};
64
65
66template<bool constant> class ParticleIterator;
67
69{
70 virtual void setupIteratorNextBlock(ParticleIterator<true>& iterator) const=0;
72 virtual void setupAccessor(ParticleIterator<true>& iterator,ParticleAccessor& accessor) const=0;
73 virtual void setupAccessor(ParticleIterator<false>& iterator,ParticleAccessor& accessor)=0;
74 virtual ~Provider(){}
75};
76
77template<bool constant>
79{
80 typedef Provider TYPE;
81};
82template<>
83struct PROVIDER<true>
84{
85 typedef const Provider TYPE;
86};
87
88// TODO: non copyable
90{
91 int stride;
93 int attributeIndex; // index of attribute opaque, do not touch
94 int count;
95private:
97
99
100public:
105
106 template<class TDATA,class TITERATOR> TDATA* raw(const TITERATOR& it)
107 {return reinterpret_cast<TDATA*>(basePointer+it.index*stride);}
108
109 template<class TDATA,class TITERATOR> const TDATA* raw(const TITERATOR& it) const
110 {return reinterpret_cast<const TDATA*>(basePointer+it.index*stride);}
111
112 template<class TDATA,class TITERATOR> TDATA& data(const TITERATOR& it)
113 {return *reinterpret_cast<TDATA*>(basePointer+it.index*stride);}
114
115 template<class TDATA,class TITERATOR> const TDATA& data(const TITERATOR& it) const
116 {return *reinterpret_cast<const TDATA*>(basePointer+it.index*stride);}
117
118 friend class ParticleIterator<true>;
119 friend class ParticleIterator<false>;
120};
121
122
123template<bool constant=false>
125{
126public:
127private:
129
132
133public:
135 size_t index;
136private:
137
139 size_t indexEnd;
140
143
144public:
149
152 :particles(other.particles),index(other.index),indexEnd(other.indexEnd),accessors(0)
153 {}
154
161
163 bool valid() const
164 {return particles;}
165
168 {
169 ParticleIterator newIt(*this);
170 index++;
171 return newIt;
172 }
173
176 {
177 index++;
178 // TODO: make particles==0 check unnecessary by using indexEnd=0 to signify invalid iterator
179 if((index>indexEnd) && particles) particles->setupIteratorNextBlock(*this);
180 return *this;
181 }
182
184 bool operator==(const ParticleIterator& other)
185 {
186 // TODO: this is really really expensive
187 // TODO: this needs a block or somethingt o say which segment it is
188 return particles==other.particles && index==other.index;
189 }
190
192 bool operator!=(const ParticleIterator& other)
193 {
194 if(other.particles!=particles) return true; // if not same delegate
195 else if(particles==0) return false; // if both are invalid iterators
196 else return !(*this==other);
197 }
198
199 void addAccessor(ParticleAccessor& newAccessor)
200 {
201 newAccessor.next=accessors;
202 accessors=&newAccessor;
203 if(particles) particles->setupAccessor(*this,newAccessor);
204 }
205
206
207 // TODO: add copy constructor that wipes out accessor linked list
208
209};
210
211template<class T,int d>
212std::ostream& operator<<(std::ostream& output,const Data<T,d>& v)
213{
214 output<<v[0];
215 for(int i=1;i<d;i++) output<< " " << v[i];
216 return output;
217}
218
219
220}
221
222#endif
Particle Collection Interface.
Definition PartioAttribute.h:97
Definition PartioIterator.h:125
bool operator==(const ParticleIterator &other)
Iterator comparison equals.
Definition PartioIterator.h:184
bool valid() const
Whether the iterator is valid.
Definition PartioIterator.h:163
PROVIDER< constant >::TYPE PROVIDER
Definition PartioIterator.h:128
ParticleIterator operator++(int)
Increment the iterator (postfix). Prefer the prefix form below to this one.
Definition PartioIterator.h:167
PROVIDER * particles
Definition PartioIterator.h:131
void addAccessor(ParticleAccessor &newAccessor)
Definition PartioIterator.h:199
size_t indexEnd
Definition PartioIterator.h:139
ParticleIterator(const ParticleIterator &other)
Copy constructor. NOTE: Invalidates any accessors that have been registered with it.
Definition PartioIterator.h:151
ParticleIterator & operator++()
Increment the iterator (prefix).
Definition PartioIterator.h:175
ParticleIterator(PROVIDER *particles, size_t index, size_t indexEnd)
Definition PartioIterator.h:158
bool operator!=(const ParticleIterator &other)
Iterator comparison not-equals.
Definition PartioIterator.h:192
size_t index
Definition PartioIterator.h:135
ParticleIterator()
Construct an invalid iterator.
Definition PartioIterator.h:146
ParticleAccessor * accessors
Definition PartioIterator.h:142
Particle Data Interface.
Definition Partio.h:103
Definition Partio.h:52
Data< int, 1 > DataI
Definition PartioIterator.h:61
Data< float, 3 > DataV
Definition PartioIterator.h:63
ParticleAttributeType
Definition PartioAttribute.h:47
Data< float, 1 > DataF
Definition PartioIterator.h:62
std::ostream & operator<<(std::ostream &output, const Data< T, d > &v)
Definition PartioIterator.h:212
Data.
Definition PartioIterator.h:55
T & operator[](const int i)
Definition PartioIterator.h:59
const T & operator[](const int i) const
Definition PartioIterator.h:58
int x[d]
Definition PartioIterator.h:56
Definition PartioIterator.h:79
Provider TYPE
Definition PartioIterator.h:80
const Provider TYPE
Definition PartioIterator.h:85
Definition PartioIterator.h:90
ParticleAttributeType type
Definition PartioIterator.h:96
char * basePointer
Definition PartioIterator.h:92
TDATA & data(const TITERATOR &it)
Definition PartioIterator.h:112
int count
Definition PartioIterator.h:94
const TDATA & data(const TITERATOR &it) const
Definition PartioIterator.h:115
int attributeIndex
Definition PartioIterator.h:93
ParticleAccessor(const ParticleAttribute &attr)
Definition PartioIterator.h:101
TDATA * raw(const TITERATOR &it)
Definition PartioIterator.h:106
const TDATA * raw(const TITERATOR &it) const
Definition PartioIterator.h:109
ParticleAccessor * next
Definition PartioIterator.h:98
int stride
Definition PartioIterator.h:91
Definition PartioIterator.h:69
virtual ~Provider()
Definition PartioIterator.h:74
virtual void setupAccessor(ParticleIterator< false > &iterator, ParticleAccessor &accessor)=0
virtual void setupIteratorNextBlock(ParticleIterator< true > &iterator) const =0
virtual void setupIteratorNextBlock(ParticleIterator< false > &iterator)=0
virtual void setupAccessor(ParticleIterator< true > &iterator, ParticleAccessor &accessor) const =0