C++ Tutorial

Here is a simple example of some operations supported by particles. You may want to look at the includes demo applications and tools in the source tree to see more examples.

## Intro

The C++ API is easy to use. You just need to include

    #include <Partio.h>

Everything Partio related is in the Partio namespace. To read a particle file in and print the number of particles you could do:

    Partio::ParticlesDataMutable* data=Partio::read("test.bgeo");
    std::cout<<"Number of particles "<<data->numParticles()<<std::endl;

Now we could additionally print out the names of all attributes in the particle file as

    for(int i=0;i<data->numAttributes();i++){
        Partio::ParticleAttribute attr;
        data->attributeInfo(i,attr);
        std::cout<<"attribute["<<i<<"] is "<<attr.name<<std::endl;
    }

Now we could convert the data into RenderMan ptc format as

    Partio::write("test.ptc",*data);

Whenever you are done with a particle set you need to release it as

    data->release();

## Simple processing

Suppose we wanted to average all the positions in a particle file. We could do that by iterating over each particle and getting the position attribute value as follows:

    Partio::ParticleAttribute posAttr;
    if(!data->attributeInfo("position",attr)
       || (attr.type != Partio::FLOAT && attr.type != Partio::VECTOR)
       || attr.count != 3){
       std::cerr<<"Failed to get proper position attribute"<<std::endl;
    }

    float avg[3]={0,0,0};

    for(int i=0;i<data->numParticles();i++){
        float *pos=data->data<float>(posAttr,i);
        for(int k=0;k<3;k++) avg[k]+=pos[k];
    }
    for(int k=0;k<3;k++) pos[k]/=data->numParticles();
    std::cerr<<"avg "<<pos[0]<<" "<<pos[1]<<" "<<pos[2]<<std::endl;