-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSierpinski.cu
356 lines (307 loc) · 7.98 KB
/
Sierpinski.cu
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#include <GL/glut.h>
#include <armadillo>
#include <cuda.h>
#include <cstdlib>
#include <stack>
#include <chrono>
#include <math.h>
#include <iostream>
//files.associations
using namespace std;
using namespace std::chrono;
// menu item
#define MENU_SMOOTH 1
#define MENU_FLAT 0
struct fractalLevel
{
arma::Mat<GLfloat> poly;
int iteration;
};
struct transformLevel
{
vector<int> trans;
int iteration;
};
arma::Mat<GLfloat> Triangle = {{0.0, 0.0, -1.0},
{1.0, 0.0, 1.0},
{0.0, 1.0, 1.0}};
arma::Mat<GLfloat> Triangle2 = {{1.0, 0.0, 0.0},
{0.0, 1.0, 0.0},
{0.0, 0.0, 1.0}};
vector<arma::Mat<GLfloat>> transfMat{
{{1.0, 0.5, 0.5},
{0.0, 0.5, 0.0},
{0.0, 0.0, 0.5}},
{{0.5, 0.0, 0.0},
{0.5, 1.0, 0.5},
{0.0, 0.0, 0.5}},
{{0.5, 0.0, 0.0},
{0.0, 0.5, 0.0},
{0.5, 0.5, 1.0}}
};
double colors[1000] = {0};
int iterations = 11, maxIteration = 15;
double zoom = 1;
int shading = GL_SMOOTH;
// Function prototypes
void generateColors();
double random(bool reset);
void keyboard(unsigned char key, int x, int y);
//void special(int key, int x, int y);
void mouse(int button, int state, int x, int y);
void menu(int item);
void display();
void init();
GLfloat **toGLfloatPoints(arma::Mat<GLfloat> armapoly, int n_row);
void drawPolygone(GLfloat **poly, int n_row);
void displayPolygone(arma::Mat<GLfloat> trgl);
void ifsRecursif(arma::Mat<GLfloat> poly, vector<arma::Mat<GLfloat>> TransfList, int iteration)
{
if (iteration == 0) displayPolygone(poly);
else
{
for (int i = 0; i < TransfList.size(); i++)
ifsRecursif(TransfList[i] * poly, TransfList, iteration - 1);
}
}
void ifsIteratif1(arma::Mat<GLfloat> poly, vector<arma::Mat<GLfloat>> TransfList, int iter)
{
stack<fractalLevel> stk;
fractalLevel level;
while (true)
{
while (iter > 0)
{
iter--;
level.iteration = iter;
for (int i = TransfList.size() - 1; i > 0; i--)
{
level.poly = TransfList[i] * poly;
stk.push(level);
}
poly = TransfList[0] * poly;
}
displayPolygone(poly);
if (stk.empty())
break;
else
{
level = stk.top();
stk.pop();
poly = level.poly;
iter = level.iteration;
}
}
}
void getListTransform(vector<vector<int>> &Tlist, int TlistSize, int iter)
{
stack<transformLevel> stk;
transformLevel level, l;
arma::Mat<GLfloat> t;
while (true)
{
while (iter > 0)
{
iter--;
level.iteration = iter;
for (int i = TlistSize - 1; i > 0; i--)
{
l = level;
l.trans.push_back(i);
stk.push(l);
}
level.trans.push_back(0);
}
Tlist.push_back(level.trans);
if (stk.empty())
break;
else
{
level = stk.top();
stk.pop();
iter = level.iteration;
}
}
}
void ifsIteratif2(arma::Mat<GLfloat> trgl, vector<arma::Mat<GLfloat>> TransfList, int iter)
{
vector<vector<int>> Tlist;
arma::Mat<GLfloat> t;
getListTransform(Tlist, TransfList.size(), iter);
for (int j = 0; j < Tlist.size(); j++)
{
t = trgl;
for (auto i = Tlist[j].cbegin(); i != Tlist[j].cend(); ++i)
{
t = TransfList[*i] * t;
}
displayPolygone(t);
}
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glScalef(zoom, zoom, zoom);
random(true);
cout <<"\n N = " << iterations <<"\n" << endl;
auto start = high_resolution_clock::now();
ifsRecursif(Triangle2, transfMat, iterations);
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start)*pow(10,-6);
cout <<"Recursive : " << duration.count() << endl;
start = high_resolution_clock::now();
ifsIteratif1(Triangle2, transfMat, iterations);
stop = high_resolution_clock::now();
duration = duration_cast<microseconds>(stop - start)*pow(10,-6);
cout <<"Iterative 1 : " << duration.count() << endl;
start = high_resolution_clock::now();
ifsIteratif2(Triangle2, transfMat, iterations);
stop = high_resolution_clock::now();
duration = duration_cast<microseconds>(stop - start)*pow(10,-6);
cout <<"Iterative 2 : " << duration.count() << endl;
glFlush();
}
int main(int argc, char **argv)
{
generateColors();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(600, 600);
glutInitWindowPosition(0, 0);
glutCreateWindow("Sierpinski Triangle");
glutPositionWindow(100, 100);
glutKeyboardFunc(keyboard);
//glutSpecialFunc(special);
glutMouseFunc(mouse);
glutCreateMenu(menu);
glutAddMenuEntry("Smooth shading", MENU_SMOOTH);
glutAddMenuEntry("Flat shading", MENU_FLAT);
glutAttachMenu(GLUT_RIGHT_BUTTON);
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
void displayPolygone(arma::Mat<GLfloat> trgl)
{
trgl = trgl.t();
GLfloat **poly = toGLfloatPoints(trgl, trgl.n_rows);
drawPolygone(poly, trgl.n_rows);
}
GLfloat **toGLfloatPoints(arma::Mat<GLfloat> armapoly, int n_row)
{
GLfloat **poly = (GLfloat **)malloc(n_row * sizeof(GLfloat *));
for (int i = 0; i < n_row; i++)
{
poly[i] = (GLfloat *)malloc(3 * sizeof(GLfloat));
for (int j = 0; j < 3; j++)
{
poly[i][j] = armapoly(i, j);
}
}
return poly;
}
void drawPolygone(GLfloat **poly, int n_row)
{
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glShadeModel(shading);
glBegin(GL_TRIANGLES);
for (int i = 0; i < n_row; i++)
{
glColor3f(random(false), random(false), random(false));
glVertex3fv(poly[i]);
}
glEnd();
}
void generateColors()
{
for (int i = 0; i < 1000; i++)
{
colors[i] = rand() / (double)RAND_MAX;
}
}
double random(bool reset)
{
static int curr = 0;
if (reset)
{
curr = 0;
return 0.0;
}
else
{
if (curr >= 1000)
curr = 0;
return colors[curr++];
}
}
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case '+':
if (iterations < maxIteration)
iterations += 1;
display();
break;
case '-':
if (iterations > 0)
iterations -= 1;
display();
break;
case 'q':
exit(0);
break;
}
}
void mouse(int button, int state, int x, int y)
{
if ((button == 3) || (button == 4)) // It's a wheel event
{
if (button == 3)
{
zoom += 0.5;
}
else if (button == 4)
{
if (zoom >= 1.5)
zoom -= 0.5;
else
zoom = 1;
}
display();
}
else
{ // normal button event
//if (button == GLUT_LEFT_BUTTON){
if (state == GLUT_UP)
{
generateColors();
display();
}
}
}
void menu(int item)
{
switch (item)
{
case MENU_FLAT:
shading = GL_FLAT;
display();
break;
case MENU_SMOOTH:
shading = GL_SMOOTH;
display();
break;
}
}
void init()
{
glClearColor(0.0, 0.0, 0.0, 1.0);
glColor3f(0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluPerspective(30, 1, 0.1, 500);
gluLookAt(2, 2, 2, 0, 0.2, 0, 0, 1, 0);
}