5.1.2.1.3 : Implementation table_particle.cpp
Implementation of table_particle.cpp file : We need to use the proper includes (random initialisation and TableParticle struct) :

1
2
3
#include "asterics_random.h"

#include "table_particle.h"


Let's implement the function to allocate the table of particles :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
///Allocate the tables of position, velocity and acceleration
/**	@param[out] tp : table of particles
 * 	@param nbParticle
*/
void allocTableParticle(TableParticle & tp, size_t nbParticle){
	tp.nbParticle = nbParticle;
	
	tp.tabPosX = new float[nbParticle];
	tp.tabPosY = new float[nbParticle];
	tp.tabPosZ = new float[nbParticle];
	
	tp.tabVitX = new float[nbParticle];
	tp.tabVitY = new float[nbParticle];
	tp.tabVitZ = new float[nbParticle];
	
	tp.tabAccX = new float[nbParticle];
	tp.tabAccY = new float[nbParticle];
	tp.tabAccZ = new float[nbParticle];
}


Let's implement the function to initialise the table of particles :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
///Init the tables of position, velocity and acceleration
/**	@param[out] tp : table of particles
*/
void initTableParticle(TableParticle & tp){
	unsigned int seed = initRandom();
	std::cout << "initTableParticle : seed = " << seed << std::endl;
	size_t nbParticle(tp.nbParticle);
	for(size_t i(0lu); i < nbParticle; ++i){
		tp.tabPosX[i] = randfloat(-1.0f, 1.0f);
		tp.tabPosY[i] = randfloat(-1.0f, 1.0f);
		tp.tabPosZ[i] = randfloat(-1.0f, 1.0f);
		
		tp.tabVitX[i] = randfloat(-0.005f, 0.005f);
		tp.tabVitY[i] = randfloat(-0.005f, 0.005f);
		tp.tabVitZ[i] = randfloat(-0.005f, 0.005f);
		
		tp.tabAccX[i] = 0.0f;
		tp.tabAccY[i] = 0.0f;
		tp.tabAccZ[i] = -9.81f;
	}
}


Let's implement the function to free the table of particles :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
///Free the table of particles
/**	@param[out] tp : table of particles
*/
void freeTableParticle(TableParticle & tp){
	delete [] tp.tabPosX;
	delete [] tp.tabPosY;
	delete [] tp.tabPosZ;
	delete [] tp.tabVitX;
	delete [] tp.tabVitY;
	delete [] tp.tabVitZ;
	delete [] tp.tabAccX;
	delete [] tp.tabAccY;
	delete [] tp.tabAccZ;
	tp.tabPosX = NULL;
	tp.tabPosY = NULL;
	tp.tabPosZ = NULL;
	tp.tabVitX = NULL;
	tp.tabVitY = NULL;
	tp.tabVitZ = NULL;
	tp.tabAccX = NULL;
	tp.tabAccY = NULL;
	tp.tabAccZ = NULL;
}


Let's implement a function to move the particles on a defined axis :

1
2
3
4
5
6
7
8
9
10
11
12
13
///Move the particles on one axis
/**	@param[out] tabPos : table of particle position to the updated
 * 	@param[out] tabVit : table of particle velocity to the updated
 * 	@param[out] tabAcc : table of particle acceleration to the updated
 * 	@param nbParticle : number of particles
 * 	@param dt : ellapsed time
*/
void moveParticleOnAxis(float * tabPos, float * tabVit, const float * tabAcc, size_t nbParticle, float dt){
	for(size_t i(0lu); i < nbParticle; ++i){
		tabVit[i] += tabAcc[i]*dt;
		tabPos[i] += tabVit[i]*dt;
	}
}


Let's implement the function to move the particles :

1
2
3
4
5
6
7
8
9
10
///Move the particles
/**	@param[out] tp : table of particle to the updated
 * 	@param dt : ellapsed time
*/
void moveParticle(TableParticle & tp, float dt){
	size_t nbParticle(tp.nbParticle);
	moveParticleOnAxis(tp.tabPosX, tp.tabVitX, tp.tabAccX, nbParticle, dt);
	moveParticleOnAxis(tp.tabPosY, tp.tabVitY, tp.tabAccY, nbParticle, dt);
	moveParticleOnAxis(tp.tabPosZ, tp.tabVitZ, tp.tabAccZ, nbParticle, dt);
}