-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflame.cpp
139 lines (113 loc) · 3.47 KB
/
flame.cpp
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
#include "flame.h"
using namespace std;
Flame::Flame(unsigned long width, unsigned long height) :
m_alpha(width, vector<unsigned long>(height)), m_R(width, vector<float>(height)), m_G(width, vector<float>(height)),
m_B(width, vector<float>(height)), m_width(width), m_height(height), m_scale(100), m_image(0), m_finalTransform(0)
{
m_image = new unsigned char[m_height * m_width];
m_finalTransform = new GenFunction(1);
m_finalTransform->setFunction(1,0);
srand(time(0)); // And what if the user has already initialized the random generator ?
}
void Flame::render(unsigned long long nbIterations)
{
// We select a point in the bi-unit square ( [ -1, 1] ) : precision 10^-4
double x = (rand() % (2 * PRECISION) - PRECISION) / (2*PRECISION);
double y = (rand() % (2*PRECISION) - PRECISION) / (2 * PRECISION) ;
//We select a random normalized color.
float c_R = (rand() % PRECISION ) / PRECISION;
float c_G = ( rand() % PRECISION ) / PRECISION;
float c_B = ( rand() % PRECISION) / PRECISION;
int nbFunctions = m_functions.size();
// Always 20 + nbIterations iterations at least, the 20 first of which are not plotted.
for(unsigned i(0); i < 20 ; i++)
{
int numFonc = rand() % nbFunctions;
m_functions[numFonc]->apply(x,y);
m_finalTransform->apply(x,y);
}
for(unsigned long long i(0); i < nbIterations ; i++ )
{
/* Selecting one of the generating functions : equirepartition
* Will change : probability will depend on the weight of the function.
**/
int numFunc = rand() % nbFunctions;
GenFunction *curFunc = m_functions[numFunc];
curFunc->apply(x,y);
//Colors
updateColors(c_R, c_G, c_B, curFunc->getColor());
//The final transform, always called
m_finalTransform->apply(x, y);
// We also blend the colors with the color of the final transform
updateColors(c_R, c_G, c_B, m_finalTransform->getColor());
//Updating the alpha canal
m_alpha[toX(x)][toY(y)] += 1;
}
}
unsigned char *Flame::toImage()
{
unsigned char *image = new unsigned char[m_width * m_height];
//Copy m_image into image : memcpy ?
return image;
}
unsigned long Flame::getWidth() const
{
return m_width;
}
unsigned long Flame::getHeight() const
{
return m_height;
}
double Flame::getScaleFactor() const
{
return m_scale;
}
void Flame::setScaleFactor(double scaleFactor)
{
if (m_scale != 0)
{
m_scale = scaleFactor;
clear();// Some recalculations will be needed !
}
}
/** @todo Giving the histogram and the color array the new dimensions ; idem for the image array
*
*/
void Flame::setDimensions(unsigned long width, unsigned long height)
{
m_width = width;
m_height = height;
clear();
}
void Flame::setFinalTransform(GenFunction *finalTransform)
{
delete m_finalTransform;
m_finalTransform = finalTransform;
}
Flame::~Flame()
{
delete m_image;
delete m_finalTransform;
}
unsigned long Flame::toX(double x)
{
return floor(x * m_scale + m_width / 2);
}
unsigned long Flame::toY(double y)
{
return floor( - y * m_scale + m_height / 2 );
}
void Flame::clear()
{
m_alpha.clear();
m_R.clear();
m_G.clear();
m_B.clear();
delete m_image;
}
void Flame::updateColors(float &r, float &g, float &b, int col)
{
r = (r + GenFunction::toRed(col)/255)/2;
g = (g + GenFunction::toGreen(col)/255 )/2;
b =(b + GenFunction::toBlue(col)/255)/2;
}