1 /*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkWebGLWidget.cxx
5
6 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 All rights reserved.
8 See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9
10 This software is distributed WITHOUT ANY WARRANTY; without even
11 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 PURPOSE. See the above copyright notice for more information.
13
14 =========================================================================*/
15
16 #include "vtkWebGLWidget.h"
17
18 #include "vtkActor2D.h"
19 #include "vtkDiscretizableColorTransferFunction.h"
20 #include "vtkObjectFactory.h"
21 #include "vtkScalarBarActor.h"
22 #include "vtkWebGLExporter.h"
23 #include "vtkWebGLObject.h"
24
25 #include <sstream>
26
27 vtkStandardNewMacro(vtkWebGLWidget);
28
vtkWebGLWidget()29 vtkWebGLWidget::vtkWebGLWidget()
30 {
31 this->binaryData = NULL;
32 this->iswidget = false;
33 this->binarySize = 0;
34 this->orientation = 1;
35 this->interactAtServer = false;
36 this->title = NULL;
37 }
38
~vtkWebGLWidget()39 vtkWebGLWidget::~vtkWebGLWidget()
40 {
41 if (this->binaryData) delete[] this->binaryData;
42 while(this->colors.size() !=0)
43 {
44 double* xrgb = this->colors.back();
45 this->colors.pop_back();
46 delete[] xrgb;
47 }
48 if (this->title) delete[] this->title;
49 }
50
GetBinaryData(int vtkNotUsed (part))51 unsigned char* vtkWebGLWidget::GetBinaryData(int vtkNotUsed(part))
52 {
53 this->hasChanged = false;
54 return this->binaryData;
55 }
56
GetBinarySize(int vtkNotUsed (part))57 int vtkWebGLWidget::GetBinarySize(int vtkNotUsed(part))
58 {
59 return this->binarySize;
60 }
61
GenerateBinaryData()62 void vtkWebGLWidget::GenerateBinaryData()
63 {
64 if (this->binaryData)
65 {
66 delete[] this->binaryData;
67 }
68 std::string oldMD5 = "qqehissorapaz";
69 oldMD5 = this->MD5;
70
71 size_t pos = 0;
72 //Calculate the size used
73 //NumOfColors, Type, Position, Size, Colors, Orientation, numberOfLabels
74 int total = static_cast<int>(sizeof(int) + 1 + 4*sizeof(float) + this->colors.size()*(sizeof(float)+3*sizeof(char)) + 1 + 1 + strlen(this->title));
75 this->binaryData = new unsigned char[total];
76 int colorSize = static_cast<int>(this->colors.size());
77
78 memset(this->binaryData, 0, total); //Fill array with 0
79 memcpy(&this->binaryData[pos], &colorSize, sizeof(int)); pos+=sizeof(int); //Binary Data Size
80 this->binaryData[pos++] = 'C'; //Object Type
81 memcpy(&this->binaryData[pos], &this->position, sizeof(float)*2); pos+=sizeof(float)*2; //Position (double[2])
82 memcpy(&this->binaryData[pos], &this->size, sizeof(float)*2); pos+=sizeof(float)*2; //Size (double[2])
83 unsigned char rgb[3];
84 for(size_t i=0; i<colors.size(); i++) //Array of Colors (double, char[3])
85 {
86 float v = (float)this->colors[i][0];
87 memcpy(&this->binaryData[pos], &v, sizeof(float)); pos+=sizeof(float);
88 rgb[0] = (unsigned char)((int)(this->colors[i][1]*255));
89 rgb[1] = (unsigned char)((int)(this->colors[i][2]*255));
90 rgb[2] = (unsigned char)((int)(this->colors[i][3]*255));
91 memcpy(&this->binaryData[pos], rgb, 3*sizeof(unsigned char)); pos+=sizeof(unsigned char)*3;
92 }
93 unsigned char aux; aux = (unsigned char)this->orientation;
94 memcpy(&this->binaryData[pos], &aux, 1); pos++;
95 aux = (unsigned char)this->numberOfLabels;
96 memcpy(&this->binaryData[pos], &aux, 1); pos++;
97 memcpy(&this->binaryData[pos], this->title, strlen(this->title)); pos+=strlen(this->title);
98
99 this->binarySize = total;
100 vtkWebGLExporter::ComputeMD5(this->binaryData, total, this->MD5);
101 this->hasChanged = this->MD5.compare(oldMD5) != 0;
102 }
103
GetNumberOfParts()104 int vtkWebGLWidget::GetNumberOfParts()
105 {
106 return 1;
107 }
108
PrintSelf(ostream & os,vtkIndent indent)109 void vtkWebGLWidget::PrintSelf(ostream& os, vtkIndent indent)
110 {
111 this->Superclass::PrintSelf(os, indent);
112 }
113
GetDataFromColorMap(vtkActor2D * actor)114 void vtkWebGLWidget::GetDataFromColorMap(vtkActor2D *actor)
115 {
116 vtkScalarBarActor* scalarbar = vtkScalarBarActor::SafeDownCast(actor);
117 this->numberOfLabels = scalarbar->GetNumberOfLabels();
118
119 std::stringstream theTitle;
120 char* componentTitle = scalarbar->GetComponentTitle();
121
122 theTitle << scalarbar->GetTitle();
123 if (componentTitle && strlen(componentTitle) > 0)
124 {
125 theTitle << " ";
126 theTitle << componentTitle;
127 }
128
129 if (this->title)
130 {
131 delete[] this->title;
132 }
133 std::string tmp = theTitle.str();
134 this->title = new char[tmp.length()+1];
135 strcpy(this->title, tmp.c_str());
136 this->hasTransparency = (scalarbar->GetUseOpacity() != 0);
137 this->orientation = scalarbar->GetOrientation();
138
139 //Colors
140 vtkDiscretizableColorTransferFunction* lookup = vtkDiscretizableColorTransferFunction::SafeDownCast(scalarbar->GetLookupTable());
141 int num = 5*lookup->GetSize();
142 double* range = lookup->GetRange();
143 double v, s; v = range[0]; s = (range[1]-range[0])/(num-1);
144 for(int i=0; i<num; i++)
145 {
146 double* xrgb = new double[4];
147 scalarbar->GetLookupTable()->GetColor(v, &xrgb[1]);
148 xrgb[0] = v;
149 this->colors.push_back(xrgb);
150 v += s;
151 }
152
153 this->textFormat = scalarbar->GetLabelFormat(); //Float Format ex.: %-#6.3g
154 this->textPosition = scalarbar->GetTextPosition(); //Orientacao dos textos; 1;
155 double *pos = scalarbar->GetPosition();
156 double *siz = scalarbar->GetPosition2();
157 this->position[0] = pos[0]; this->position[1] = pos[1];//Widget Position
158 this->size[0] = siz[0]; this->size[1] = siz[1]; //Widget Size
159 }
160