Partio
Partio.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
41#ifndef _Partioh_
42#define _Partioh_
43
44#include <string>
45#include <vector>
46#include <map>
47#include <stdint.h>
48#include <string.h>
49#include "PartioAttribute.h"
50#include "PartioIterator.h"
51
52namespace Partio{
53
55typedef uint64_t ParticleIndex;
56
57class ParticlesData;
59// Particle Collection Interface
61
67{
68protected:
69 virtual ~ParticlesInfo() {}
70public:
71
75 virtual void release()=0;
76
78 virtual int numParticles() const=0;
79
81 virtual int numAttributes() const=0;
83 virtual int numFixedAttributes() const=0;
84
86 virtual bool attributeInfo(const char* attributeName,ParticleAttribute& attribute) const=0;
88 virtual bool fixedAttributeInfo(const char* attributeName,FixedAttribute& attribute) const=0;
89
91 virtual bool attributeInfo(const int index,ParticleAttribute& attribute) const=0;
93 virtual bool fixedAttributeInfo(const int index,FixedAttribute& attribute) const=0;
94};
95
96// Particle Data Interface
98
103{
104protected:
105 virtual ~ParticlesData() {}
106public:
107 friend void freeCached(ParticlesData* particles);
108
110
114 template<class T> inline void data(const ParticleAttribute& attribute,
115 const int indexCount,const ParticleIndex* particleIndices,const bool sorted,T* values)
116 {
117 assert(typeCheck<T>(attribute.type));
118 dataInternalMultiple(attribute,indexCount,particleIndices,sorted,(char*)values);
119 }
120
121 template<class T> inline const T* data(const ParticleAttribute& attribute,
122 const ParticleIndex particleIndex) const
123 {
124 // TODO: add type checking
125 return static_cast<T*>(dataInternal(attribute,particleIndex));
126 }
127
128 template<class T> inline const T* fixedData(const FixedAttribute& attribute) const
129 {
130 // TODO: add type checking
131 return static_cast<T*>(fixedDataInternal(attribute));
132 }
133
135 virtual const std::vector<std::string>& indexedStrs(const ParticleAttribute& attr) const=0;
137 virtual const std::vector<std::string>& fixedIndexedStrs(const FixedAttribute& attr) const=0;
138
140 virtual int lookupIndexedStr(const ParticleAttribute& attribute,const char* str) const=0;
142 virtual int lookupFixedIndexedStr(const FixedAttribute& attribute,const char* str) const=0;
143
148 virtual void dataAsFloat(const ParticleAttribute& attribute,const int indexCount,
149 const ParticleIndex* particleIndices,const bool sorted,float* values) const=0;
150
154 virtual void findPoints(const float bboxMin[3],const float bboxMax[3],
155 std::vector<ParticleIndex>& points) const=0;
156
162 virtual float findNPoints(const float center[3],int nPoints,const float maxRadius,
163 std::vector<ParticleIndex>& points,std::vector<float>& pointDistancesSquared) const=0;
164
168 virtual int findNPoints(const float center[3],int nPoints,const float maxRadius,
169 ParticleIndex *points, float *pointDistancesSquared, float *finalRadius2) const=0;
170
172 virtual const_iterator setupConstIterator(const int index=0) const=0;
173
176 {return setupConstIterator();}
177
180 {return const_iterator();}
181
182private:
183 virtual void* dataInternal(const ParticleAttribute& attribute,const ParticleIndex particleIndex) const=0;
184 virtual void* fixedDataInternal(const FixedAttribute& attribute) const=0;
185 virtual void dataInternalMultiple(const ParticleAttribute& attribute,const int indexCount,
186 const ParticleIndex* particleIndices,const bool sorted,char* values) const=0;
187};
188
189// Particle Mutable Data Interface
191
196{
197protected:
199
200public:
201
203
206 template<class T> inline T* dataWrite(const ParticleAttribute& attribute,
207 const ParticleIndex particleIndex) const
208 {
209 // TODO: add type checking
210 return static_cast<T*>(dataInternal(attribute,particleIndex));
211 }
212
215 template<class T> inline T* fixedDataWrite(const FixedAttribute& attribute) const
216 {
217 // TODO: add type checking
218 return static_cast<T*>(fixedDataInternal(attribute));
219 }
220
222 template<class T> inline void set(const ParticleAttribute& attribute,
223 const ParticleIndex particleIndex, const T* data) {
224 T* ptr = static_cast<T*>(dataInternal(attribute, particleIndex));
225 if (ptr) memcpy(ptr, data, attribute.count * TypeSize(attribute.type));
226 }
227
228 template<class T> inline void setFixed(const FixedAttribute& attribute, const T* data) {
229 T* ptr = static_cast<T*>(fixedDataInternal(attribute));
230 memcpy(ptr, data, attribute.count * TypeSize(attribute.type));
231 }
232
234 virtual int registerIndexedStr(const ParticleAttribute& attribute,const char* str)=0;
236 virtual int registerFixedIndexedStr(const FixedAttribute& attribute,const char* str)=0;
237
239 virtual void setIndexedStr(const ParticleAttribute& attribute,int indexedStringToken,const char* str)=0;
241 virtual void setFixedIndexedStr(const FixedAttribute& attribute,int indexedStringToken,const char* str)=0;
242
245 virtual void sort()=0;
246
248 virtual ParticleAttribute addAttribute(const char* attribute,ParticleAttributeType type,
249 const int count)=0;
250
252 virtual FixedAttribute addFixedAttribute(const char* attribute,ParticleAttributeType type,
253 const int count)=0;
254
257
260 virtual iterator addParticles(const int count)=0;
261
264 {return setupIterator();}
265
268 {return iterator();}
269
271 virtual iterator setupIterator(const int index=0)=0;
272
273private:
274 virtual void* dataInternal(const ParticleAttribute& attribute,const ParticleIndex particleIndex) const=0;
275 virtual void* fixedDataInternal(const FixedAttribute& attribute) const=0;
276};
277
280
282
286ParticlesDataMutable* cloneSchema(const ParticlesData&, const std::map<std::string, std::string>* attrNameMap = nullptr);
287
292ParticlesDataMutable* clone(const ParticlesData&, bool particles=true, const std::map<std::string, std::string>* attrNameMap = nullptr);
293
296ParticlesDataMutable* read(const char* filename,const bool verbose=true,std::ostream& errorStream=std::cerr);
297
300ParticlesInfo* readHeaders(const char* filename,const bool verbose=true,std::ostream& errorStream=std::cerr);
301
304void write(const char* filename,const ParticlesData&,const bool forceCompressed=false,bool verbose=true,std::ostream& errorStream=std::cerr);
305
307
313ParticlesData* readCached(const char* filename,const bool sort,const bool verbose=true,std::ostream& errorStream=std::cerr);
314
316
322
324
331
333void print(const ParticlesData* particles);
334
335ParticlesDataMutable* computeClustering(ParticlesDataMutable* particles, const int numNeighbors,const double radiusSearch,const double radiusInside,const int connections,const double density);
336
338
347void merge(ParticlesDataMutable& base, const ParticlesData& delta, const std::string& identifier=std::string());
348
349}
350#endif
Fixed Attribute Interface.
Definition PartioAttribute.h:124
ParticleAttributeType type
Type of attribute.
Definition PartioAttribute.h:127
int count
Number of entries, should be 3 if type is VECTOR.
Definition PartioAttribute.h:130
Particle Collection Interface.
Definition PartioAttribute.h:97
int count
Number of entries, should be 3 if type is VECTOR.
Definition PartioAttribute.h:103
ParticleAttributeType type
Type of attribute.
Definition PartioAttribute.h:100
Definition PartioIterator.h:125
Particle Mutable Data Interface.
Definition Partio.h:196
virtual ParticleIndex addParticle()=0
Add a particle to the particle set. Returns the offset to the particle.
void set(const ParticleAttribute &attribute, const ParticleIndex particleIndex, const T *data)
Set particle value for attribute.
Definition Partio.h:222
virtual FixedAttribute addFixedAttribute(const char *attribute, ParticleAttributeType type, const int count)=0
Adds a fixed attribute with the provided name, type and count.
void setFixed(const FixedAttribute &attribute, const T *data)
Definition Partio.h:228
virtual void setFixedIndexedStr(const FixedAttribute &attribute, int indexedStringToken, const char *str)=0
Returns a token for the given string. This allows efficient storage of string data.
virtual ParticleAttribute addAttribute(const char *attribute, ParticleAttributeType type, const int count)=0
Adds an attribute to the particle with the provided name, type and count.
iterator end()
Produce a ending iterator for the particles.
Definition Partio.h:267
ParticleIterator< false > iterator
Definition Partio.h:202
virtual int registerFixedIndexedStr(const FixedAttribute &attribute, const char *str)=0
Returns a token for the given string. This allows efficient storage of string data.
iterator begin()
Produce a beginning iterator for the particles.
Definition Partio.h:263
virtual void * dataInternal(const ParticleAttribute &attribute, const ParticleIndex particleIndex) const =0
T * dataWrite(const ParticleAttribute &attribute, const ParticleIndex particleIndex) const
Definition Partio.h:206
virtual iterator setupIterator(const int index=0)=0
Produce a const iterator.
virtual int registerIndexedStr(const ParticleAttribute &attribute, const char *str)=0
Returns a token for the given string. This allows efficient storage of string data.
virtual ~ParticlesDataMutable()
Definition Partio.h:198
virtual void * fixedDataInternal(const FixedAttribute &attribute) const =0
virtual iterator addParticles(const int count)=0
T * fixedDataWrite(const FixedAttribute &attribute) const
Definition Partio.h:215
virtual void setIndexedStr(const ParticleAttribute &attribute, int indexedStringToken, const char *str)=0
Returns a token for the given string. This allows efficient storage of string data.
Particle Data Interface.
Definition Partio.h:103
virtual void findPoints(const float bboxMin[3], const float bboxMax[3], std::vector< ParticleIndex > &points) const =0
virtual const std::vector< std::string > & fixedIndexedStrs(const FixedAttribute &attr) const =0
All indexed strings for an attribute.
virtual const std::vector< std::string > & indexedStrs(const ParticleAttribute &attr) const =0
All indexed strings for an attribute.
friend void freeCached(ParticlesData *particles)
virtual const_iterator setupConstIterator(const int index=0) const =0
Produce a const iterator.
const_iterator begin() const
Produce a beginning iterator for the particles.
Definition Partio.h:175
const_iterator end() const
Produce a ending iterator for the particles.
Definition Partio.h:179
ParticleIterator< true > const_iterator
Definition Partio.h:109
const T * data(const ParticleAttribute &attribute, const ParticleIndex particleIndex) const
Definition Partio.h:121
virtual int lookupFixedIndexedStr(const FixedAttribute &attribute, const char *str) const =0
Looks up the index for a given string for a given attribute, returns -1 if not found.
virtual void * fixedDataInternal(const FixedAttribute &attribute) const =0
virtual ~ParticlesData()
Definition Partio.h:105
virtual void dataInternalMultiple(const ParticleAttribute &attribute, const int indexCount, const ParticleIndex *particleIndices, const bool sorted, char *values) const =0
virtual void * dataInternal(const ParticleAttribute &attribute, const ParticleIndex particleIndex) const =0
virtual float findNPoints(const float center[3], int nPoints, const float maxRadius, std::vector< ParticleIndex > &points, std::vector< float > &pointDistancesSquared) const =0
virtual int lookupIndexedStr(const ParticleAttribute &attribute, const char *str) const =0
Looks up the index for a given string for a given attribute, returns -1 if not found.
void data(const ParticleAttribute &attribute, const int indexCount, const ParticleIndex *particleIndices, const bool sorted, T *values)
Definition Partio.h:114
virtual void dataAsFloat(const ParticleAttribute &attribute, const int indexCount, const ParticleIndex *particleIndices, const bool sorted, float *values) const =0
const T * fixedData(const FixedAttribute &attribute) const
Definition Partio.h:128
virtual int findNPoints(const float center[3], int nPoints, const float maxRadius, ParticleIndex *points, float *pointDistancesSquared, float *finalRadius2) const =0
Particle Collection Interface.
Definition Partio.h:67
virtual void release()=0
virtual bool attributeInfo(const int index, ParticleAttribute &attribute) const =0
Lookup an attribute by index and store a handle to the attribute.
virtual bool attributeInfo(const char *attributeName, ParticleAttribute &attribute) const =0
Lookup an attribute by name and store a handle to the attribute.
virtual int numAttributes() const =0
Number of per-particle attributes.
virtual bool fixedAttributeInfo(const int index, FixedAttribute &attribute) const =0
Lookup an attribute by index and store a handle to the attribute.
virtual ~ParticlesInfo()
Definition Partio.h:69
virtual int numFixedAttributes() const =0
Number of fixed attributes.
virtual int numParticles() const =0
Number of particles in the structure.
virtual bool fixedAttributeInfo(const char *attributeName, FixedAttribute &attribute) const =0
Lookup an attribute by name and store a handle to the attribute.
Definition Partio.h:52
void beginCachedAccess(ParticlesData *particles)
Begin accessing data in a cached file.
ParticlesDataMutable * computeClustering(ParticlesDataMutable *particles, const int numNeighbors, const double radiusSearch, const double radiusInside, const int connections, const double density)
bool typeCheck(const ParticleAttributeType &type)
Definition PartioAttribute.h:61
ParticlesInfo * readHeaders(const char *filename, const bool verbose=true, std::ostream &errorStream=std::cerr)
ParticlesDataMutable * create()
Provides an empty particle instance, freed with p->release()
void merge(ParticlesDataMutable &base, const ParticlesData &delta, const std::string &identifier=std::string())
Merges one particle set into another.
uint64_t ParticleIndex
Opaque random access method to a single particle. No number is implied or guaranteed.
Definition Partio.h:55
ParticlesDataMutable * clone(const ParticlesData &, bool particles=true, const std::map< std::string, std::string > *attrNameMap=nullptr)
int TypeSize(ParticleAttributeType attrType)
Definition PartioAttribute.h:75
ParticleAttributeType
Definition PartioAttribute.h:47
ParticlesData * readCached(const char *filename, const bool sort, const bool verbose=true, std::ostream &errorStream=std::cerr)
Cached (only one copy) read only way to read a particle file.
void write(const char *filename, const ParticlesData &, const bool forceCompressed=false, bool verbose=true, std::ostream &errorStream=std::cerr)
ParticlesDataMutable * createInterleave()
ParticlesDataMutable * cloneSchema(const ParticlesData &, const std::map< std::string, std::string > *attrNameMap=nullptr)
void print(const ParticlesData *particles)
Prints a subset of particle data in a textual form.
void endCachedAccess(ParticlesData *particles)
End accessing data in a cached file.
ParticlesDataMutable * read(const char *filename, const bool verbose=true, std::ostream &errorStream=std::cerr)