-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransferfunction1d.cpp
More file actions
311 lines (268 loc) · 8.34 KB
/
transferfunction1d.cpp
File metadata and controls
311 lines (268 loc) · 8.34 KB
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
#include "transferfunction1d.h"
#include <GL/glew.h>
#include <fstream>
#include <cstdlib>
namespace vr
{
TransferFunction1D::TransferFunction1D (int max_value)
: m_built (false), m_interpolation_type (TFInterpolationType::LINEAR)
{
max_density = max_value;
m_cpt_rgb.clear ();
m_cpt_alpha.clear ();
m_transferfunction = NULL;
extinction_coef_type = false;
}
TransferFunction1D::~TransferFunction1D ()
{
m_cpt_rgb.clear ();
m_cpt_alpha.clear ();
if (m_transferfunction)
m_transferfunction;
if (m_gradients)
m_gradients;
}
const char* TransferFunction1D::GetNameClass ()
{
return "TrasnferFunction1D";
}
void TransferFunction1D::SetExtinctionCoefficientInput(bool s)
{
extinction_coef_type = s;
}
void TransferFunction1D::AddRGBControlPoint (TransferControlPoint rgb)
{
m_cpt_rgb.push_back (rgb);
}
void TransferFunction1D::AddAlphaControlPoint (TransferControlPoint alpha)
{
m_cpt_alpha.push_back (alpha);
}
void TransferFunction1D::ClearControlPoints ()
{
m_cpt_rgb.clear ();
m_cpt_alpha.clear ();
}
void TransferFunction1D::Build (TFInterpolationType type)
{
if (m_transferfunction)
delete[] m_transferfunction;
m_transferfunction = new glm::dvec4[max_density + 1];
if (type == TFInterpolationType::LINEAR)
BuildLinear();
else
BuildLinear();
//else if (type == TFInterpolationType::CUBIC)
//{
// std::vector<TransferControlPoint> tempColorKnots = m_cpt_rgb;
// std::vector<TransferControlPoint> tempAlphaKnots = m_cpt_alpha;
//
// Cubic* colorCubic = Cubic::CalculateCubicSpline((int)m_cpt_rgb.size () - 1, tempColorKnots);
// Cubic* alphaCubic = Cubic::CalculateCubicSpline((int)m_cpt_alpha.size () - 1, tempAlphaKnots);
//
// int numTF = 0;
// for (int i = 0; i < (int)m_cpt_rgb.size () - 1; i++)
// {
// int steps = m_cpt_rgb[i + 1].m_isoValue - m_cpt_rgb[i].m_isoValue;
// for (int j = 0; j < steps; j++)
// {
// float k = (float)j / (float)(steps);
// m_transferfunction[numTF++] = colorCubic[i].GetPointOnSpline (k);
// }
// }
// m_tflenght = numTF;
//
// numTF = 0;
// for (int i = 0; i < (int)m_cpt_alpha.size () - 1; i++)
// {
// int steps = m_cpt_alpha[i + 1].m_isoValue - m_cpt_alpha[i].m_isoValue;
// for (int j = 0; j < steps; j++)
// {
// float k = (float)j / (float)(steps);
// m_transferfunction[numTF++].w = alphaCubic[i].GetPointOnSpline (k).w;
// }
// }
//}
printf ("lqc: Transfer Function 1D Built!\n");
m_built = true;
}
glm::vec4 TransferFunction1D::Get (double value, double max_data_value)
{
if (!m_built)
Build (m_interpolation_type);
if (max_data_value >= 0)
value = value * (double(max_density) / max_data_value);
if (value < 0.0f || value > (float)max_density)
return glm::vec4 (0);
// range: [0, max_density]
if (fabs(value - (float)max_density) < 0.000001)
{
return glm::vec4(m_transferfunction[max_density]);
}
else
{
glm::dvec4 v1 = m_transferfunction[(int)value];
glm::dvec4 v2 = m_transferfunction[((int)value) + 1];
double t = value - (int)value;
return glm::vec4((1.0 - t)*v1 + t*v2);
}
}
float TransferFunction1D::GetOpc (double value, double max_input_value)
{
float val = Get(value, max_input_value).a;
if (extinction_coef_type)
return ExtinctionToMaterialOpacity(val);
return val;
}
float TransferFunction1D::GetExt (double value, double max_input_value)
{
float val = Get(value, max_input_value).a;
if (!extinction_coef_type)
return MaterialOpacityToExtinction(val);
return val;
}
void TransferFunction1D::PrintControlPoints ()
{
printf ("Print Transfer Function: Control Points\n");
int rgb_pts = (int)m_cpt_rgb.size ();
printf ("- Printing the RGB Control Points\n");
printf (" Format: \"Number: Red Green Blue, Isovalue\"\n");
for (int i = 0; i < rgb_pts; i++)
{
printf (" %d: %.2f %.2f %.2f, %d\n", i + 1, m_cpt_rgb[i].m_color.x, m_cpt_rgb[i].m_color.y, m_cpt_rgb[i].m_color.z, m_cpt_rgb[i].m_isoValue);
}
printf ("\n");
int alpha_pts = (int)m_cpt_alpha.size ();
printf ("- Printing the Alpha Control Points\n");
printf (" Format: \"Number: Alpha, Isovalue\"\n");
for (int i = 0; i < alpha_pts; i++)
{
printf (" %d: %.2f, %d\n", i + 1, m_cpt_alpha[i].m_color.w, m_cpt_alpha[i].m_isoValue);
}
printf ("\n");
}
void TransferFunction1D::PrintTransferFunction ()
{
printf ("Print Transfer Function: Control Points\n");
printf (" Format: \"IsoValue: Red Green Blue, Alpha\"\n");
for (int i = 0; i < max_density + 1; i++)
{
printf ("%d: %.2f %.2f %.2f, %.2f\n", i, m_transferfunction[i].x
, m_transferfunction[i].y, m_transferfunction[i].z, m_transferfunction[i].w);
}
}
bool TransferFunction1D::Save ()//char* filename, TFFormatType format)
{
/*
std::string filesaved;
filesaved.append (RESOURCE_LIBLQC_PATH);
filesaved.append ("TransferFunctions/");
filesaved.append (filename);
if (format == TFFormatType::LQC)
{
filesaved.append (".tf1d");
std::ofstream myfile (filesaved.c_str ());
if (myfile.is_open ())
{
myfile << 0 << "\n";
myfile << (int)m_cpt_rgb.size () << "\n";
for (int i = 0; i < (int)m_cpt_rgb.size (); i++)
{
myfile << m_cpt_rgb[i].m_color.x << " " <<
m_cpt_rgb[i].m_color.y << " " <<
m_cpt_rgb[i].m_color.z << " " <<
m_cpt_rgb[i].m_isoValue << " " << "\n";
}
myfile << (int)m_cpt_alpha.size () << "\n";
for (int i = 0; i < (int)m_cpt_alpha.size (); i++)
{
myfile << m_cpt_alpha[i].m_color.w << " " <<
m_cpt_alpha[i].m_isoValue << " " << "\n";
}
myfile.close ();
printf ("lqc: Transfer Function 1D Control Points Saved!\n");
}
else
{
printf ("lqc: Error on opening file at VRTransferFunction::Save().\n");
}
}
*/
return true;
}
bool TransferFunction1D::Load ()//std::string filename, TFFormatType format)
{
/*
std::string filesaved;
filesaved.append (RESOURCE_LIBLQC_PATH);
filesaved.append ("TransferFunctions/");
filesaved.append (filename);
if (format == TFFormatType::LQC)
{
filesaved.append (".tf1d");
std::ifstream myfile (filesaved.c_str ());
if (myfile.is_open ())
{
int init;
myfile >> init;
int cpt_rgb_size;
myfile >> cpt_rgb_size;
float r, g, b, a;
int isovalue;
for (int i = 0; i < cpt_rgb_size; i++)
{
myfile >> r >> g >> b >> isovalue;
m_cpt_rgb.push_back (TransferControlPoint (r, g, b, isovalue));
}
int cpt_alpha_size;
myfile >> cpt_alpha_size;
for (int i = 0; i < cpt_alpha_size; i++)
{
myfile >> a >> isovalue;
m_cpt_alpha.push_back (TransferControlPoint (a, isovalue));
}
myfile.close ();
printf ("lqc: Transfer Function 1D Control Points Loaded!\n");
return true;
}
else
printf ("lqc: Error on opening file at VRTransferFunction::AddControlPointsReadFile().\n");
}
return false;
*/
return true;
}
void TransferFunction1D::BuildLinear ()
{
for (int i = 0; i < (int)m_cpt_rgb.size() - 1; i++)
{
int i0 = m_cpt_rgb[i].m_isoValue;
int i1 = m_cpt_rgb[i + 1].m_isoValue;
glm::dvec3 diff = glm::dvec3(
m_cpt_rgb[i + 1].m_color.r - m_cpt_rgb[i].m_color.r,
m_cpt_rgb[i + 1].m_color.g - m_cpt_rgb[i].m_color.g,
m_cpt_rgb[i + 1].m_color.b - m_cpt_rgb[i].m_color.b
);
for (int x = i0; x <= i1; x++)
{
double k = (double)(x - i0) / (double)(i1 - i0);
m_transferfunction[x].r = m_cpt_rgb[i].m_color.r + diff.r * k;
m_transferfunction[x].g = m_cpt_rgb[i].m_color.g + diff.g * k;
m_transferfunction[x].b = m_cpt_rgb[i].m_color.b + diff.b * k;
}
}
for (int i = 0; i < (int)m_cpt_alpha.size() - 1; i++)
{
int i0 = m_cpt_alpha[i].m_isoValue;
int i1 = m_cpt_alpha[i + 1].m_isoValue;
double diff = double(
m_cpt_alpha[i + 1].m_color.a - m_cpt_alpha[i].m_color.a
);
for (int x = i0; x <= i1; x++)
{
double k = (double)(x - i0) / (double)(i1 - i0);
m_transferfunction[x].a = m_cpt_alpha[i].m_color.a + diff * k;
}
}
}
}