forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkWebGLWidget.cxx
152 lines (128 loc) · 5.19 KB
/
vtkWebGLWidget.cxx
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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkWebGLWidget.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkWebGLWidget.h"
#include "vtkActor2D.h"
#include "vtkDiscretizableColorTransferFunction.h"
#include "vtkObjectFactory.h"
#include "vtkScalarBarActor.h"
#include "vtkWebGLExporter.h"
#include "vtkWebGLObject.h"
#include <sstream>
vtkStandardNewMacro(vtkWebGLWidget);
vtkWebGLWidget::vtkWebGLWidget()
{
this->binaryData = NULL;
this->iswidget = false;
this->binarySize = 0;
this->orientation = 1;
this->interactAtServer = false;
this->title = NULL;
}
vtkWebGLWidget::~vtkWebGLWidget()
{
if (this->binaryData) delete[] this->binaryData;
while(this->colors.size() !=0)
{
double* xrgb = this->colors.back();
this->colors.pop_back();
delete[] xrgb;
}
if (this->title) delete[] this->title;
}
unsigned char* vtkWebGLWidget::GetBinaryData(int vtkNotUsed(part))
{
this->hasChanged = false;
return this->binaryData;
}
int vtkWebGLWidget::GetBinarySize(int vtkNotUsed(part))
{
return this->binarySize;
}
void vtkWebGLWidget::GenerateBinaryData()
{
delete[] this->binaryData;
std::string oldMD5 = this->MD5;
size_t pos = 0;
//Calculate the size used
//NumOfColors, Type, Position, Size, Colors, Orientation, numberOfLabels
int total = static_cast<int>(sizeof(int) + 1 + 4*sizeof(float) + this->colors.size()*(sizeof(float)+3*sizeof(char)) + 1 + 1 + strlen(this->title));
this->binaryData = new unsigned char[total];
int colorSize = static_cast<int>(this->colors.size());
memset(this->binaryData, 0, total); //Fill array with 0
memcpy(&this->binaryData[pos], &colorSize, sizeof(int)); pos+=sizeof(int); //Binary Data Size
this->binaryData[pos++] = 'C'; //Object Type
memcpy(&this->binaryData[pos], &this->position, sizeof(float)*2); pos+=sizeof(float)*2; //Position (double[2])
memcpy(&this->binaryData[pos], &this->size, sizeof(float)*2); pos+=sizeof(float)*2; //Size (double[2])
unsigned char rgb[3];
for(size_t i=0; i<colors.size(); i++) //Array of Colors (double, char[3])
{
float v = (float)this->colors[i][0];
memcpy(&this->binaryData[pos], &v, sizeof(float)); pos+=sizeof(float);
rgb[0] = (unsigned char)((int)(this->colors[i][1]*255));
rgb[1] = (unsigned char)((int)(this->colors[i][2]*255));
rgb[2] = (unsigned char)((int)(this->colors[i][3]*255));
memcpy(&this->binaryData[pos], rgb, 3*sizeof(unsigned char)); pos+=sizeof(unsigned char)*3;
}
unsigned char aux; aux = (unsigned char)this->orientation;
memcpy(&this->binaryData[pos], &aux, 1); pos++;
aux = (unsigned char)this->numberOfLabels;
memcpy(&this->binaryData[pos], &aux, 1); pos++;
memcpy(&this->binaryData[pos], this->title, strlen(this->title)); pos+=strlen(this->title);
this->binarySize = total;
vtkWebGLExporter::ComputeMD5(this->binaryData, total, this->MD5);
this->hasChanged = this->MD5.compare(oldMD5) != 0;
}
int vtkWebGLWidget::GetNumberOfParts()
{
return 1;
}
void vtkWebGLWidget::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
void vtkWebGLWidget::GetDataFromColorMap(vtkActor2D *actor)
{
vtkScalarBarActor* scalarbar = vtkScalarBarActor::SafeDownCast(actor);
this->numberOfLabels = scalarbar->GetNumberOfLabels();
std::stringstream theTitle;
char* componentTitle = scalarbar->GetComponentTitle();
theTitle << scalarbar->GetTitle();
if (componentTitle && strlen(componentTitle) > 0)
{
theTitle << " ";
theTitle << componentTitle;
}
delete[] this->title;
std::string tmp = theTitle.str();
this->title = new char[tmp.length()+1];
strcpy(this->title, tmp.c_str());
this->hasTransparency = (scalarbar->GetUseOpacity() != 0);
this->orientation = scalarbar->GetOrientation();
//Colors
vtkDiscretizableColorTransferFunction* lookup = vtkDiscretizableColorTransferFunction::SafeDownCast(scalarbar->GetLookupTable());
int num = 5*lookup->GetSize();
double* range = lookup->GetRange();
double v, s; v = range[0]; s = (range[1]-range[0])/(num-1);
for(int i=0; i<num; i++)
{
double* xrgb = new double[4];
scalarbar->GetLookupTable()->GetColor(v, &xrgb[1]);
xrgb[0] = v;
this->colors.push_back(xrgb);
v += s;
}
this->textFormat = scalarbar->GetLabelFormat(); //Float Format ex.: %-#6.3g
this->textPosition = scalarbar->GetTextPosition(); //Orientacao dos textos; 1;
double *pos = scalarbar->GetPosition();
double *siz = scalarbar->GetPosition2();
this->position[0] = pos[0]; this->position[1] = pos[1];//Widget Position
this->size[0] = siz[0]; this->size[1] = siz[1]; //Widget Size
}