Ajout de l'ensemble du workspace.
[GPU.git] / WCudaMSE / Tuto_Image / src / cpp / core / 02_Damier_Zoomable / b_moo / DamierMOO.cpp
1 #include <iostream>
2 #include <math.h>
3
4 #include "DamierMOO.h"
5
6 #include "OmpTools.h"
7 #include "MathTools.h"
8
9 #include "IndiceTools.h"
10
11
12 using std::cout;
13 using std::endl;
14 using std::string;
15
16 /*----------------------------------------------------------------------*\
17 |* Declaration *|
18 \*---------------------------------------------------------------------*/
19
20 /*--------------------------------------*\
21 |* Public *|
22 \*-------------------------------------*/
23
24 /*--------------------------------------*\
25 |* Private *|
26 \*-------------------------------------*/
27
28 /*----------------------------------------------------------------------*\
29 |* Implementation *|
30 \*---------------------------------------------------------------------*/
31
32 /*--------------------------------------*\
33 |* Public *|
34 \*-------------------------------------*/
35
36 /**
37 * variateurT: fait varier t in [0,2pi] par increment de dt,
38 * d'abord de mani�re croissante jusqua 2PI, puis de maniere decroissante jusqua 0, puis en boucle a l'infini selon ce procede
39 */
40 DamierMOO::DamierMOO(unsigned int w, unsigned int h, float dt, int n):variateurT(IntervalF(0, 2 * PI), dt)
41 {
42 // Inputs
43 this->n =n;
44 this->w=w;
45 this->h=h;
46
47 //Tools
48 this->isEntrelacement=true;
49
50 // OMP (facultatif)
51 const int NB_THREADS = OmpTools::setAndGetNaturalGranularity();
52 cout << "\n[DAMIER] nbThread = " << NB_THREADS << endl;;
53 }
54
55 DamierMOO::~DamierMOO(void)
56 {
57 // rien
58 }
59
60 /*--------------------------------------*\
61 |* Override *|
62 \*-------------------------------------*/
63
64 void DamierMOO::process(uchar4* ptrTabPixels, int w, int h, const DomaineMath& domaineMath)
65 {
66 if (isEntrelacement)
67 {
68 entrelacementOMP(ptrTabPixels,w,h,domaineMath); // Plus lent
69 }
70 else
71 {
72 forAutoOMP(ptrTabPixels,w,h,domaineMath); // Plus rapide
73 }
74
75 isEntrelacement=!isEntrelacement; // Pour tester que les deux implementations fonctionnent
76 }
77
78 void DamierMOO::animationStep()
79 {
80 variateurT.varierAndGet();
81 }
82
83 /*--------------*\
84 |* get *|
85 \*-------------*/
86
87 int DamierMOO::getN()
88 {
89 return n;
90 }
91
92 float DamierMOO::getT()
93 {
94 return variateurT.get();
95 }
96
97 /*--------------------------------------*\
98 |* Private *|
99 \*-------------------------------------*/
100
101
102 /**
103 * Code naturel et direct OMP
104 */
105 void DamierMOO::forAutoOMP(uchar4* ptrTabPixels, int w, int h, const DomaineMath& domaineMath)
106 {
107 DamierMath damierMath(n); // ici pour preparer cuda
108
109 #pragma omp parallel for
110 for (int i = 0; i < h; i++)
111 {
112 for (int j = 0; j < w; j++)
113 {
114 //int s = i * W + j;
115 int s=IndiceTools::toS(w,i,j);// i[0,H[ j[0,W[ --> s[0,W*H[
116
117 workPixel(&ptrTabPixels[s],i, j,s, domaineMath,&damierMath);
118 }
119 }
120 }
121
122 /**
123 * Code entrainement Cuda
124 */
125 void DamierMOO::entrelacementOMP(uchar4* ptrTabPixels, int w, int h, const DomaineMath& domaineMath)
126 {
127 DamierMath damierMath(n); // ici pour preparer cuda
128
129 const int WH=w*h;
130
131
132
133 #pragma omp parallel
134 {
135 const int NB_THREAD = OmpTools::getNbThread(); // dans region parallel
136
137 const int TID = OmpTools::getTid();
138 int s = TID; // in [0,...
139
140 int i;
141 int j;
142 while (s < WH)
143 {
144 IndiceTools::toIJ(s,w,&i,&j); // s[0,W*H[ --> i[0,H[ j[0,W[
145
146 workPixel(&ptrTabPixels[s],i, j,s, domaineMath,&damierMath);
147
148 s += NB_THREAD;
149 }
150 }
151 }
152
153 /*--------------------------------------*\
154 |* Private *|
155 \*-------------------------------------*/
156
157 /**
158 * i in [1,h]
159 * j in [1,w]
160 * code commun �
161 * entrelacementOMP
162 * forAutoOMP
163 */
164 void DamierMOO::workPixel(uchar4* ptrColorIJ,int i, int j,int s, const DomaineMath& domaineMath,DamierMath* ptrDamierMath)
165 {
166 DamierMath damierMath(n); // ici pour preparer cuda
167
168 // (i,j) domaine ecran dans N2
169 // (x,y) domaine math dans R2
170
171 double x;
172 double y;
173
174 domaineMath.toXY(i, j, &x, &y); // fill (x,y) from (i,j)
175
176 float t=variateurT.get();
177 ptrDamierMath->colorXY(ptrColorIJ,x, y, domaineMath,t); // in [01]
178 }
179
180 /*----------------------------------------------------------------------*\
181 |* End *|
182 \*---------------------------------------------------------------------*/
183