-
Notifications
You must be signed in to change notification settings - Fork 1
/
transferfunction.cpp
109 lines (89 loc) · 3.36 KB
/
transferfunction.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
#include "transferfunction.h"
TransferFunction::TransferFunction(vtkSmartPointer<vtkPiecewiseFunction> otf, vtkSmartPointer<vtkColorTransferFunction> ctf, QObject *parent) : QObject(parent)
{
opacityTF = otf;
colorTF = ctf;
this->otf = QSharedPointer<ctkTransferFunction>(new ctkVTKPiecewiseFunction(opacityTF));
this->ctf = QSharedPointer<ctkTransferFunction>(new ctkVTKColorTransferFunction(colorTF));
connect(this->otf.data(), SIGNAL(changed()), this, SLOT(onOpacityTFChanged()));
connect(this->ctf.data(), SIGNAL(changed()), this, SLOT(onColorTFChanged()));
compositeTex = 0;
// initialize each table
opacityTF->GetTable(0.0, 1.0, TABLE_SIZE, opacityTable);
colorTF->GetTable(0.0, 1.0, TABLE_SIZE, colorTable);
CompositeTable();
channelDesc = cudaCreateChannelDesc(32, 32, 32, 32, cudaChannelFormatKindFloat);
CudaSafeCall(cudaMallocArray(&array, &channelDesc, TABLE_SIZE));
CudaSafeCall(cudaMemcpyToArray(array, 0, 0, compositeTable, sizeof(float) * TABLE_SIZE * 4, cudaMemcpyHostToDevice));
memset(&resourceDesc, 0, sizeof(resourceDesc));
resourceDesc.resType = cudaResourceTypeArray;
resourceDesc.res.array.array = array;
memset(&texDesc, 0, sizeof(texDesc));
texDesc.addressMode[0] = cudaAddressModeClamp;
texDesc.filterMode = cudaFilterModeLinear;
texDesc.normalizedCoords = true;
texDesc.readMode = cudaReadModeElementType;
CudaSafeCall(cudaCreateTextureObject(&compositeTex, &resourceDesc, &texDesc, NULL));
}
TransferFunction::~TransferFunction()
{
if(compositeTex)
CudaSafeCall(cudaDestroyTextureObject(compositeTex));
CudaSafeCall(cudaFreeArray(array));
}
void TransferFunction::SaveCurrentTFConfiguration()
{
std::ofstream output("tf.txt");
if(!output.good())
{
std::cerr<<"unable to open tf.txt"<<std::endl;
exit(0);
}
for(unsigned int i = 0; i < TABLE_SIZE * 4; ++i)
{
output<<compositeTable[i]<<',';
}
output.close();
}
void TransferFunction::onOpacityTFChanged()
{
//std::cout<<"Opacity changed"<<std::endl;
if(compositeTex)
{
CudaSafeCall(cudaDestroyTextureObject(compositeTex));
compositeTex = 0;
}
opacityTF->GetTable(0.0, 1.0, TABLE_SIZE, opacityTable);
size_t j = 3;
for(size_t i = 0; i < TABLE_SIZE; ++i)
{
compositeTable[j] = opacityTable[i];
j += 4;
}
//CompositeTable();
CudaSafeCall(cudaMemcpyToArray(array, 0, 0, compositeTable, sizeof(float) * TABLE_SIZE * 4, cudaMemcpyHostToDevice));
CudaSafeCall(cudaCreateTextureObject(&compositeTex, &resourceDesc, &texDesc, NULL));
Changed();
}
void TransferFunction::onColorTFChanged()
{
//std::cout<<"Color changed"<<std::endl;
if(compositeTex)
{
CudaSafeCall(cudaDestroyTextureObject(compositeTex));
compositeTex = 0;
}
colorTF->GetTable(0.0, 1.0, TABLE_SIZE, colorTable);
size_t j = 0, k = 0;
for(size_t i = 0; i < TABLE_SIZE; ++i)
{
compositeTable[j++] = colorTable[k++];
compositeTable[j++] = colorTable[k++];
compositeTable[j++] = colorTable[k++];
j++;
}
//CompositeTable();
CudaSafeCall(cudaMemcpyToArray(array, 0, 0, compositeTable, sizeof(float) * TABLE_SIZE * 4, cudaMemcpyHostToDevice));
CudaSafeCall(cudaCreateTextureObject(&compositeTex, &resourceDesc, &texDesc, NULL));
Changed();
}