PartioIterator.h
Go to the documentation of this file.
/*
PARTIO SOFTWARE
Copyright 2010 Disney Enterprises, Inc. All rights reserved
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
* The names "Disney", "Walt Disney Pictures", "Walt Disney Animation
Studios" or the names of its contributors may NOT be used to
endorse or promote products derived from this software without
specific prior written permission from Walt Disney Pictures.
Disclaimer: THIS SOFTWARE IS PROVIDED BY WALT DISNEY PICTURES AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE, NONINFRINGEMENT AND TITLE ARE DISCLAIMED.
IN NO EVENT SHALL WALT DISNEY PICTURES, THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND BASED ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*/
#ifndef _PartioParticleIterator_h_
#define _PartioParticleIterator_h_
#include <cassert>
#include <vector>
#include <iostream>
#include "PartioAttribute.h"
namespace Partio{
class ParticlesData;
struct ParticleAccessor;
template<class T,int d>
struct Data
{
T x[d];
const T& operator[](const int i) const {return x[i];}
T& operator[](const int i) {return x[i];}
};
typedef Data<int,1> DataI;
typedef Data<float,1> DataF;
typedef Data<float,3> DataV;
template<bool constant> class ParticleIterator;
struct Provider
{
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;
virtual void setupAccessor(ParticleIterator<false>& iterator,ParticleAccessor& accessor)=0;
virtual ~Provider(){}
};
template<bool constant>
struct PROVIDER
{
typedef Provider TYPE;
};
template<>
struct PROVIDER<true>
{
typedef const Provider TYPE;
};
// TODO: non copyable
struct ParticleAccessor
{
int stride;
char* basePointer;
int attributeIndex; // index of attribute opaque, do not touch
int count;
private:
ParticleAttributeType type;
ParticleAccessor* next;
public:
ParticleAccessor(const ParticleAttribute& attr)
:stride(0),basePointer(0),attributeIndex(attr.attributeIndex),
count(attr.count),type(attr.type),next(0)
{}
template<class TDATA,class TITERATOR> TDATA* raw(const TITERATOR& it)
{return reinterpret_cast<TDATA*>(basePointer+it.index*stride);}
template<class TDATA,class TITERATOR> const TDATA* raw(const TITERATOR& it) const
{return reinterpret_cast<const TDATA*>(basePointer+it.index*stride);}
template<class TDATA,class TITERATOR> TDATA& data(const TITERATOR& it)
{return *reinterpret_cast<TDATA*>(basePointer+it.index*stride);}
template<class TDATA,class TITERATOR> const TDATA& data(const TITERATOR& it) const
{return *reinterpret_cast<const TDATA*>(basePointer+it.index*stride);}
friend class ParticleIterator<true>;
friend class ParticleIterator<false>;
};
template<bool constant=false>
class ParticleIterator
{
public:
private:
typedef typename PROVIDER<constant>::TYPE PROVIDER;
PROVIDER* particles;
public:
size_t index;
private:
size_t indexEnd;
ParticleAccessor* accessors;
public:
ParticleIterator()
:particles(0),index(0),indexEnd(0),accessors(0)
{}
ParticleIterator(const ParticleIterator& other)
:particles(other.particles),index(other.index),indexEnd(other.indexEnd),accessors(0)
{}
ParticleIterator(PROVIDER* particles,size_t index,size_t indexEnd)
:particles(particles),index(index),indexEnd(indexEnd)
{}
bool valid() const
{return particles;}
ParticleIterator operator++(int)
{
ParticleIterator newIt(*this);
index++;
return newIt;
}
ParticleIterator& operator++()
{
index++;
// TODO: make particles==0 check unnecessary by using indexEnd=0 to signify invalid iterator
if((index>indexEnd) && particles) particles->setupIteratorNextBlock(*this);
return *this;
}
bool operator==(const ParticleIterator& other)
{
// TODO: this is really really expensive
// TODO: this needs a block or somethingt o say which segment it is
return particles==other.particles && index==other.index;
}
bool operator!=(const ParticleIterator& other)
{
if(other.particles!=particles) return true; // if not same delegate
else if(particles==0) return false; // if both are invalid iterators
else return !(*this==other);
}
void addAccessor(ParticleAccessor& newAccessor)
{
newAccessor.next=accessors;
accessors=&newAccessor;
if(particles) particles->setupAccessor(*this,newAccessor);
}
// TODO: add copy constructor that wipes out accessor linked list
};
template<class T,int d>
std::ostream& operator<<(std::ostream& output,const Data<T,d>& v)
{
output<<v[0];
for(int i=1;i<d;i++) output<< " " << v[i];
return output;
}
}
#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:184
Partio::ParticleIterator::valid
bool valid() const
Whether the iterator is valid.
Definition: PartioIterator.h:163
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:167
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:199
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:175
Partio::ParticleIterator::ParticleIterator
ParticleIterator(PROVIDER *particles, size_t index, size_t indexEnd)
Definition: PartioIterator.h:158
Partio::ParticleIterator::operator!=
bool operator!=(const ParticleIterator &other)
Iterator comparison not-equals.
Definition: PartioIterator.h:192
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::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:212
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