Partio

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