5.1.2.1.4 : Summary table_particle.cpp


Here is the full table_particle.cpp file :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/***************************************
	Auteur : Pierre Aubert
	Mail : pierre.aubert@lapp.in2p3.fr
	Licence : CeCILL-C
****************************************/

#include "asterics_random.h"

#include "table_particle.h"

///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];
}

///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;
	}
}

///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;
}

///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;
	}
}

///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);
}


You can download it here.