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 = nullptr;
32   this->iswidget = false;
33   this->binarySize = 0;
34   this->orientation = 1;
35   this->interactAtServer = false;
36   this->title = nullptr;
37 }
38 
~vtkWebGLWidget()39 vtkWebGLWidget::~vtkWebGLWidget()
40 {
41   delete[] this->binaryData;
42   while(!this->colors.empty())
43   {
44     double* xrgb = this->colors.back();
45     this->colors.pop_back();
46     delete[] xrgb;
47   }
48   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   delete[] this->binaryData;
65   std::string oldMD5 = this->MD5;
66 
67   size_t pos = 0;
68   //Calculate the size used
69   //NumOfColors, Type, Position, Size, Colors, Orientation, numberOfLabels
70   int total = static_cast<int>(sizeof(int) + 1 + 4*sizeof(float) + this->colors.size()*(sizeof(float)+3*sizeof(char)) + 1 + 1 + strlen(this->title));
71   this->binaryData = new unsigned char[total];
72   int colorSize = static_cast<int>(this->colors.size());
73 
74   memset(this->binaryData, 0, total);                                                         //Fill array with 0
75   memcpy(&this->binaryData[pos], &colorSize, sizeof(int)); pos+=sizeof(int);                  //Binary Data Size
76   this->binaryData[pos++] = 'C';                                                              //Object Type
77   memcpy(&this->binaryData[pos], &this->position, sizeof(float)*2); pos+=sizeof(float)*2;     //Position (double[2])
78   memcpy(&this->binaryData[pos], &this->size, sizeof(float)*2); pos+=sizeof(float)*2;         //Size (double[2])
79   unsigned char rgb[3];
80   for(size_t i=0; i<colors.size(); i++)                                                       //Array of Colors (double, char[3])
81   {
82     float v = (float)this->colors[i][0];
83     memcpy(&this->binaryData[pos], &v, sizeof(float)); pos+=sizeof(float);
84     rgb[0] = (unsigned char)((int)(this->colors[i][1]*255));
85     rgb[1] = (unsigned char)((int)(this->colors[i][2]*255));
86     rgb[2] = (unsigned char)((int)(this->colors[i][3]*255));
87     memcpy(&this->binaryData[pos], rgb, 3*sizeof(unsigned char)); pos+=sizeof(unsigned char)*3;
88   }
89   unsigned char aux; aux = (unsigned char)this->orientation;
90   memcpy(&this->binaryData[pos], &aux, 1); pos++;
91   aux = (unsigned char)this->numberOfLabels;
92   memcpy(&this->binaryData[pos], &aux, 1); pos++;
93   memcpy(&this->binaryData[pos], this->title, strlen(this->title)); pos+=strlen(this->title);
94 
95   this->binarySize = total;
96   vtkWebGLExporter::ComputeMD5(this->binaryData, total, this->MD5);
97   this->hasChanged = this->MD5.compare(oldMD5) != 0;
98 }
99 
GetNumberOfParts()100 int vtkWebGLWidget::GetNumberOfParts()
101 {
102   return 1;
103 }
104 
PrintSelf(ostream & os,vtkIndent indent)105 void vtkWebGLWidget::PrintSelf(ostream& os, vtkIndent indent)
106 {
107   this->Superclass::PrintSelf(os, indent);
108 }
109 
GetDataFromColorMap(vtkActor2D * actor)110 void vtkWebGLWidget::GetDataFromColorMap(vtkActor2D *actor)
111 {
112   vtkScalarBarActor* scalarbar = vtkScalarBarActor::SafeDownCast(actor);
113   this->numberOfLabels = scalarbar->GetNumberOfLabels();
114 
115   std::stringstream theTitle;
116   char* componentTitle = scalarbar->GetComponentTitle();
117 
118   theTitle << scalarbar->GetTitle();
119   if (componentTitle && strlen(componentTitle) > 0)
120   {
121     theTitle << " ";
122     theTitle << componentTitle;
123   }
124 
125   delete[] this->title;
126   std::string tmp = theTitle.str();
127   this->title = new char[tmp.length()+1];
128   strcpy(this->title, tmp.c_str());
129   this->hasTransparency = (scalarbar->GetUseOpacity() != 0);
130   this->orientation = scalarbar->GetOrientation();
131 
132   //Colors
133   vtkDiscretizableColorTransferFunction* lookup = vtkDiscretizableColorTransferFunction::SafeDownCast(scalarbar->GetLookupTable());
134   int num = 5*lookup->GetSize();
135   double* range = lookup->GetRange();
136   double v, s; v = range[0]; s = (range[1]-range[0])/(num-1);
137   for(int i=0; i<num; i++)
138   {
139     double* xrgb = new double[4];
140     scalarbar->GetLookupTable()->GetColor(v, &xrgb[1]);
141     xrgb[0] = v;
142     this->colors.push_back(xrgb);
143     v += s;
144   }
145 
146   this->textFormat = scalarbar->GetLabelFormat();        //Float Format ex.: %-#6.3g
147   this->textPosition = scalarbar->GetTextPosition();     //Orientacao dos textos; 1;
148   double *pos = scalarbar->GetPosition();
149   double *siz = scalarbar->GetPosition2();
150   this->position[0] = pos[0]; this->position[1] = pos[1];//Widget Position
151   this->size[0] = siz[0]; this->size[1] = siz[1];        //Widget Size
152 }
153