Partio
Partio.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 
41 #ifndef _Partioh_
42 #define _Partioh_
43 
44 #include <string>
45 #include <vector>
46 #include <map>
47 #include <stdint.h>
48 #include "PartioAttribute.h"
49 #include "PartioIterator.h"
50 
51 namespace Partio{
52 
54 typedef uint64_t ParticleIndex;
55 
56 class ParticlesData;
58 // Particle Collection Interface
60 
66 {
67 protected:
68  virtual ~ParticlesInfo() {}
69 public:
70  friend void freeCached(ParticlesData* particles);
71 
75  virtual void release() const=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 {
104 protected:
105  virtual ~ParticlesData() {}
106 public:
107 
109 
113  template<class T> inline void data(const ParticleAttribute& attribute,
114  const int indexCount,const ParticleIndex* particleIndices,const bool sorted,T* values)
115  {
116  assert(typeCheck<T>(attribute.type));
117  dataInternalMultiple(attribute,indexCount,particleIndices,sorted,(char*)values);
118  }
119 
120  template<class T> inline const T* data(const ParticleAttribute& attribute,
121  const ParticleIndex particleIndex) const
122  {
123  // TODO: add type checking
124  return static_cast<T*>(dataInternal(attribute,particleIndex));
125  }
126 
127  template<class T> inline const T* fixedData(const FixedAttribute& attribute) const
128  {
129  // TODO: add type checking
130  return static_cast<T*>(fixedDataInternal(attribute));
131  }
132 
134  virtual const std::vector<std::string>& indexedStrs(const ParticleAttribute& attr) const=0;
136  virtual const std::vector<std::string>& fixedIndexedStrs(const FixedAttribute& attr) const=0;
137 
139  virtual int lookupIndexedStr(const ParticleAttribute& attribute,const char* str) const=0;
141  virtual int lookupFixedIndexedStr(const FixedAttribute& attribute,const char* str) const=0;
142 
147  virtual void dataAsFloat(const ParticleAttribute& attribute,const int indexCount,
148  const ParticleIndex* particleIndices,const bool sorted,float* values) const=0;
149 
153  virtual void findPoints(const float bboxMin[3],const float bboxMax[3],
154  std::vector<ParticleIndex>& points) const=0;
155 
161  virtual float findNPoints(const float center[3],int nPoints,const float maxRadius,
162  std::vector<ParticleIndex>& points,std::vector<float>& pointDistancesSquared) const=0;
163 
167  virtual int findNPoints(const float center[3],int nPoints,const float maxRadius,
168  ParticleIndex *points, float *pointDistancesSquared, float *finalRadius2) const=0;
169 
171  virtual const_iterator setupConstIterator(const int index=0) const=0;
172 
175  {return setupConstIterator();}
176 
179  {return const_iterator();}
180 
181 private:
182  virtual void* dataInternal(const ParticleAttribute& attribute,const ParticleIndex particleIndex) const=0;
183  virtual void* fixedDataInternal(const FixedAttribute& attribute) const=0;
184  virtual void dataInternalMultiple(const ParticleAttribute& attribute,const int indexCount,
185  const ParticleIndex* particleIndices,const bool sorted,char* values) const=0;
186 };
187 
188 // Particle Mutable Data Interface
190 
195 {
196 protected:
198 
199 public:
200 
202 
205  template<class T> inline T* dataWrite(const ParticleAttribute& attribute,
206  const ParticleIndex particleIndex) const
207  {
208  // TODO: add type checking
209  return static_cast<T*>(dataInternal(attribute,particleIndex));
210  }
211 
214  template<class T> inline T* fixedDataWrite(const FixedAttribute& attribute) const
215  {
216  // TODO: add type checking
217  return static_cast<T*>(fixedDataInternal(attribute));
218  }
219 
221  virtual int registerIndexedStr(const ParticleAttribute& attribute,const char* str)=0;
223  virtual int registerFixedIndexedStr(const FixedAttribute& attribute,const char* str)=0;
224 
226  virtual void setIndexedStr(const ParticleAttribute& attribute,int indexedStringToken,const char* str)=0;
228  virtual void setFixedIndexedStr(const FixedAttribute& attribute,int indexedStringToken,const char* str)=0;
229 
232  virtual void sort()=0;
233 
235  virtual ParticleAttribute addAttribute(const char* attribute,ParticleAttributeType type,
236  const int count)=0;
237 
239  virtual FixedAttribute addFixedAttribute(const char* attribute,ParticleAttributeType type,
240  const int count)=0;
241 
243  virtual ParticleIndex addParticle()=0;
244 
247  virtual iterator addParticles(const int count)=0;
248 
251  {return setupIterator();}
252 
255  {return iterator();}
256 
258  virtual iterator setupIterator(const int index=0)=0;
259 
260 private:
261  virtual void* dataInternal(const ParticleAttribute& attribute,const ParticleIndex particleIndex) const=0;
262  virtual void* fixedDataInternal(const FixedAttribute& attribute) const=0;
263 };
264 
266 ParticlesDataMutable* create();
267 
268 ParticlesDataMutable* createInterleave();
269 
272 ParticlesDataMutable* cloneSchema(const ParticlesData&);
273 
277 ParticlesDataMutable* clone(const ParticlesData&, bool particles=true);
278 
281 ParticlesDataMutable* read(const char* filename,const bool verbose=true,std::ostream& errorStream=std::cerr);
282 
285 ParticlesInfo* readHeaders(const char* filename,const bool verbose=true,std::ostream& errorStream=std::cerr);
286 
289 void write(const char* filename,const ParticlesData&,const bool forceCompressed=false,bool verbose=true,std::ostream& errorStream=std::cerr);
290 
292 
298 ParticlesData* readCached(const char* filename,const bool sort,const bool verbose=true,std::ostream& errorStream=std::cerr);
299 
301 
306 void beginCachedAccess(ParticlesData* particles);
307 
309 
315 void endCachedAccess(ParticlesData* particles);
316 
318 void print(const ParticlesData* particles);
319 
320 ParticlesDataMutable* computeClustering(ParticlesDataMutable* particles, const int numNeighbors,const double radiusSearch,const double radiusInside,const int connections,const double density);
321 
323 
332 void merge(ParticlesDataMutable& base, const ParticlesData& delta, const std::string& identifier=std::string());
333 
334 }
335 #endif
Fixed Attribute Interface.
Definition: PartioAttribute.h:123
virtual int numAttributes() const =0
Number of per-particle attributes.
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.
void merge(ParticlesDataMutable &base, const ParticlesData &delta, const std::string &identifier=std::string())
Merges one particle set into another.
ParticleIterator< true > const_iterator
Definition: Partio.h:108
virtual int registerIndexedStr(const ParticleAttribute &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:250
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.
ParticlesDataMutable * create()
Provides an empty particle instance, freed with p->release()
virtual void dataInternalMultiple(const ParticleAttribute &attribute, const int indexCount, const ParticleIndex *particleIndices, const bool sorted, char *values) const =0
Particle Collection Interface.
Definition: Partio.h:65
ParticleIterator< false > iterator
Definition: Partio.h:201
virtual iterator setupIterator(const int index=0)=0
Produce a const iterator.
void data(const ParticleAttribute &attribute, const int indexCount, const ParticleIndex *particleIndices, const bool sorted, T *values)
Definition: Partio.h:113
const T * data(const ParticleAttribute &attribute, const ParticleIndex particleIndex) const
Definition: Partio.h:120
virtual iterator addParticles(const int count)=0
virtual void dataAsFloat(const ParticleAttribute &attribute, const int indexCount, const ParticleIndex *particleIndices, const bool sorted, float *values) const =0
virtual FixedAttribute addFixedAttribute(const char *attribute, ParticleAttributeType type, const int count)=0
Adds a fixed attribute with the provided name, type and count.
T * fixedDataWrite(const FixedAttribute &attribute) const
Definition: Partio.h:214
Particle Data Interface.
Definition: Partio.h:102
const_iterator begin() const
Produce a beginning iterator for the particles.
Definition: Partio.h:174
const T * fixedData(const FixedAttribute &attribute) const
Definition: Partio.h:127
iterator end()
Produce a ending iterator for the particles.
Definition: Partio.h:254
virtual void release() const =0
virtual bool attributeInfo(const char *attributeName, ParticleAttribute &attribute) const =0
Lookup an attribute by name and store a handle to the attribute.
Definition: PartioIterator.h:66
const_iterator end() const
Produce a ending iterator for the particles.
Definition: Partio.h:178
uint64_t ParticleIndex
Opaque random access method to a single particle. No number is implied or guaranteed.
Definition: Partio.h:54
virtual void * dataInternal(const ParticleAttribute &attribute, const ParticleIndex particleIndex) 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 endCachedAccess(ParticlesData *particles)
End accessing data in a cached file.
virtual ~ParticlesDataMutable()
Definition: Partio.h:197
ParticlesDataMutable * clone(const ParticlesData &, bool particles=true)
virtual bool fixedAttributeInfo(const char *attributeName, FixedAttribute &attribute) const =0
Lookup an attribute by name and store a handle to the attribute.
virtual const_iterator setupConstIterator(const int index=0) const =0
Produce a const iterator.
T * dataWrite(const ParticleAttribute &attribute, const ParticleIndex particleIndex) const
Definition: Partio.h:205
friend void freeCached(ParticlesData *particles)
Particle Mutable Data Interface.
Definition: Partio.h:194
ParticlesDataMutable * computeClustering(ParticlesDataMutable *particles, const int numNeighbors, const double radiusSearch, const double radiusInside, const int connections, const double density)
ParticlesDataMutable * cloneSchema(const ParticlesData &)
virtual int registerFixedIndexedStr(const FixedAttribute &attribute, const char *str)=0
Returns a token for the given string. This allows efficient storage of string data.
void write(const char *filename, const ParticlesData &, const bool forceCompressed=false, bool verbose=true, std::ostream &errorStream=std::cerr)
ParticlesDataMutable * createInterleave()
ParticlesDataMutable * read(const char *filename, const bool verbose=true, std::ostream &errorStream=std::cerr)
ParticlesInfo * readHeaders(const char *filename, const bool verbose=true, std::ostream &errorStream=std::cerr)
void beginCachedAccess(ParticlesData *particles)
Begin accessing data in a cached file.
virtual ~ParticlesData()
Definition: Partio.h:105
virtual int numFixedAttributes() const =0
Number of fixed attributes.
Particle Collection Interface.
Definition: PartioAttribute.h:96
Definition: Partio.h:51
virtual ParticleIndex addParticle()=0
Add a particle to the particle set. Returns the offset to the particle.
virtual int numParticles() const =0
Number of particles in the structure.
virtual ~ParticlesInfo()
Definition: Partio.h:68
virtual const std::vector< std::string > & fixedIndexedStrs(const FixedAttribute &attr) const =0
All indexed strings for an attribute.
virtual void * dataInternal(const ParticleAttribute &attribute, const ParticleIndex particleIndex) const =0
virtual const std::vector< std::string > & indexedStrs(const ParticleAttribute &attr) const =0
All indexed strings for an attribute.
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.
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.
virtual void * fixedDataInternal(const FixedAttribute &attribute) 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 void * fixedDataInternal(const FixedAttribute &attribute) const =0
void print(const ParticlesData *particles)
Prints a subset of particle data in a textual form.
virtual void findPoints(const float bboxMin[3], const float bboxMax[3], std::vector< ParticleIndex > &points) const =0
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.
ParticleAttributeType type
Type of attribute.
Definition: PartioAttribute.h:100