5.8.5.3.2 : Le fichier source


Écrivons le fichier temporary_alloc.cpp :



Nous commençons, bien sur, par inclure le header que nous venons d'écrire :

1
#include "temporary_alloc.h"


Et nous implémentons notre fonction d'initialisation (avec la documentation, j'insiste) :

1
2
3
4
5
6
7
8
9
10
11
///Allocate temporary
/**	@param[out] tmpInU : Tensor temporary in U
 * 	@param[out] tmpInV : Tensor temporary in V
 * 	@param[out] tmpOutU : Tensor temporary out U
 * 	@param[out] tmpOutV : Tensor temporary out V
 * 	@param nbRow : number of rows of the matrices to be created
 * 	@param nbCol : number of columns of the matrices to be created
*/
void allocate_temporary(std::vector<float> & tmpInU, std::vector<float> & tmpInV, std::vector<float> & tmpOutU, std::vector<float> & tmpOutV,
			size_t nbRow, size_t nbCol)
{


Nous devons redimensionner nos vecteurs avant de les utiliser :

1
2
3
4
	tmpInU.resize(nbRow*nbCol);
	tmpInV.resize(nbRow*nbCol);
	tmpOutU.resize(nbRow*nbCol);
	tmpOutV.resize(nbRow*nbCol);


Puis, nous initialisons leurs valeurs par défault (1 pour U et 0 pour V) :

1
2
3
4
	std::fill(tmpInU.begin(), tmpInU.end(), 1.0f);
	std::fill(tmpOutU.begin(), tmpOutU.end(), 1.0f);
	std::fill(tmpInV.begin(), tmpInV.end(), 0.0f);
	std::fill(tmpOutV.begin(), tmpOutV.end(), 0.0f);


Maintenant nous pouvons injecter du V et retirer du U au centre de nos images. POur cela nous définissons quelques variable de contrôl qui nous permettrons de jouer sur la taille du rectangle de départ :

1
2
3
4
5
6
7
	size_t frac(16lu), numBegin(7lu), numEnd(8lu), rowShift(-4lu);
	for(size_t i(rowShift + (numBegin*nbRow)/frac); i < rowShift + (numEnd*nbRow)/frac; ++i){
		for(size_t j((numBegin*nbCol)/frac); j < (numEnd*nbCol)/frac; ++j){
			tmpInU[i*nbCol+ j] = 0.0f;
			tmpInV[i*nbCol+ j] = 1.0f;
		}
	}


Cela nous donne une image V initiale qui ressemble à la figure 19. Vous pouvez, bien sur, modifier cette fonction pour initialiser votre matrice V comme bon vous semble.

nothing

Figure 19 : Initialisation de la matrice V, écrite dans la fonction allocate_temporary. Blanc : 1, noir : 0.



Finalement, nous fermons cette implémentation de fonction :

1
}


Le fichier temporary_alloc.cpp complet :

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
/***************************************
	Auteur : Pierre Aubert
	Mail : pierre.aubert@lapp.in2p3.fr
	Licence : CeCILL-C
****************************************/

#include "temporary_alloc.h"
///Allocate temporary
/**	@param[out] tmpInU : Tensor temporary in U
 * 	@param[out] tmpInV : Tensor temporary in V
 * 	@param[out] tmpOutU : Tensor temporary out U
 * 	@param[out] tmpOutV : Tensor temporary out V
 * 	@param nbRow : number of rows of the matrices to be created
 * 	@param nbCol : number of columns of the matrices to be created
*/
void allocate_temporary(std::vector<float> & tmpInU, std::vector<float> & tmpInV, std::vector<float> & tmpOutU, std::vector<float> & tmpOutV,
			size_t nbRow, size_t nbCol)
{
	tmpInU.resize(nbRow*nbCol);
	tmpInV.resize(nbRow*nbCol);
	tmpOutU.resize(nbRow*nbCol);
	tmpOutV.resize(nbRow*nbCol);
	std::fill(tmpInU.begin(), tmpInU.end(), 1.0f);
	std::fill(tmpOutU.begin(), tmpOutU.end(), 1.0f);
	std::fill(tmpInV.begin(), tmpInV.end(), 0.0f);
	std::fill(tmpOutV.begin(), tmpOutV.end(), 0.0f);
	size_t frac(16lu), numBegin(7lu), numEnd(8lu), rowShift(-4lu);
	for(size_t i(rowShift + (numBegin*nbRow)/frac); i < rowShift + (numEnd*nbRow)/frac; ++i){
		for(size_t j((numBegin*nbCol)/frac); j < (numEnd*nbCol)/frac; ++j){
			tmpInU[i*nbCol+ j] = 0.0f;
			tmpInV[i*nbCol+ j] = 1.0f;
		}
	}
}


Vous pouvez le télécharger ici.