1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkWebGLDataSet.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 "vtkWebGLDataSet.h"
17 
18 #include "vtkObjectFactory.h"
19 #include "vtkWebGLExporter.h"
20 
21 vtkStandardNewMacro(vtkWebGLDataSet);
22 
GetMD5()23 std::string vtkWebGLDataSet::GetMD5()
24 {
25   return this->MD5;
26 }
27 
vtkWebGLDataSet()28 vtkWebGLDataSet::vtkWebGLDataSet()
29 {
30   this->NumberOfVertices = 0;
31   this->NumberOfPoints = 0;
32   this->NumberOfIndexes = 0;
33   this->vertices = nullptr;
34   this->normals = nullptr;
35   this->indexes = nullptr;
36   this->points = nullptr;
37   this->tcoords = nullptr;
38   this->colors = nullptr;
39   this->binary = nullptr;
40   this->binarySize = 0;
41   this->hasChanged = false;
42 }
43 
~vtkWebGLDataSet()44 vtkWebGLDataSet::~vtkWebGLDataSet()
45 {
46   delete[] this->vertices;
47   delete[] this->normals;
48   delete[] this->indexes;
49   delete[] this->points;
50   delete[] this->tcoords;
51   delete[] this->colors;
52   delete[] this->binary;
53 }
54 
SetVertices(float * v,int size)55 void vtkWebGLDataSet::SetVertices(float* v, int size)
56 {
57   delete[] this->vertices;
58   this->vertices = v;
59   this->NumberOfVertices = size;
60   this->webGLType = wTRIANGLES;
61   this->hasChanged = true;
62 }
63 
SetIndexes(short * i,int size)64 void vtkWebGLDataSet::SetIndexes(short* i, int size)
65 {
66   delete[] this->indexes;
67   this->indexes = i;
68   this->NumberOfIndexes = size;
69   this->hasChanged = true;
70 }
71 
SetNormals(float * n)72 void vtkWebGLDataSet::SetNormals(float* n)
73 {
74   delete[] this->normals;
75   this->normals = n;
76   this->hasChanged = true;
77 }
78 
SetColors(unsigned char * c)79 void vtkWebGLDataSet::SetColors(unsigned char* c)
80 {
81   delete[] this->colors;
82   this->colors = c;
83   this->hasChanged = true;
84 }
85 
SetPoints(float * p,int size)86 void vtkWebGLDataSet::SetPoints(float* p, int size)
87 {
88   delete[] this->points;
89   this->points = p;
90   this->NumberOfPoints = size;
91   this->webGLType = wLINES;
92   this->hasChanged = true;
93 }
94 
SetTCoords(float * t)95 void vtkWebGLDataSet::SetTCoords(float* t)
96 {
97   delete[] this->tcoords;
98   this->tcoords = t;
99   this->hasChanged = true;
100 }
101 
GetBinaryData()102 unsigned char* vtkWebGLDataSet::GetBinaryData()
103 {
104   this->hasChanged = false;
105   return this->binary;
106 }
107 
GetBinarySize()108 int vtkWebGLDataSet::GetBinarySize()
109 {
110   return this->binarySize;
111 }
112 
SetMatrix(float * m)113 void vtkWebGLDataSet::SetMatrix(float* m)
114 {
115   this->Matrix = m;
116   this->hasChanged = true;
117 }
118 
GenerateBinaryData()119 void vtkWebGLDataSet::GenerateBinaryData()
120 {
121   if (this->NumberOfIndexes == 0 && this->webGLType != wPOINTS)
122   {
123     return;
124   }
125   int size = 0, pos = 0, total = 0;
126   delete[] this->binary;
127   this->binarySize = 0;
128 
129   if (this->webGLType == wLINES)
130   {
131     pos = sizeof(pos);
132     size = this->NumberOfPoints * sizeof(this->points[0]);
133 
134     // Calculate the size used by each data
135     total = sizeof(pos) + 1 + sizeof(this->NumberOfPoints) +
136       size * 3 // Size, Type, NumberOfPoints, Points
137       + sizeof(this->colors[0]) * this->NumberOfPoints * 4 +
138       sizeof(this->NumberOfIndexes) // Color, NumberOfIndex
139       + this->NumberOfIndexes * sizeof(this->indexes[0]) +
140       sizeof(this->Matrix[0]) * 16; // Index, Matrix
141     this->binary = new unsigned char[total];
142     memset(this->binary, 0, total);
143 
144     this->binary[pos++] = 'L';
145     memcpy(&this->binary[pos], &this->NumberOfPoints, sizeof(this->NumberOfPoints));
146     pos += sizeof(this->NumberOfPoints); // Points
147     memcpy(&this->binary[pos], this->points, size * 3);
148     pos += size * 3;
149     memcpy(&this->binary[pos], this->colors, sizeof(this->colors[0]) * this->NumberOfPoints * 4);
150     pos += sizeof(this->colors[0]) * this->NumberOfPoints * 4;
151     memcpy(&this->binary[pos], &this->NumberOfIndexes, sizeof(this->NumberOfIndexes));
152     pos += sizeof(this->NumberOfIndexes);
153     memcpy(&this->binary[pos], this->indexes, this->NumberOfIndexes * sizeof(this->indexes[0]));
154     pos += this->NumberOfIndexes * sizeof(this->indexes[0]);
155     memcpy(&this->binary[pos], this->Matrix, sizeof(this->Matrix[0]) * 16);
156     pos += sizeof(this->Matrix[0]) * 16; // Matrix
157 
158     memcpy(&this->binary[0], &pos, sizeof(pos));
159     this->binarySize = total;
160   }
161   else if (this->webGLType == wTRIANGLES)
162   {
163     pos = sizeof(pos);
164     size = sizeof(this->vertices[0]) * this->NumberOfVertices;
165 
166     // Calculate the size used by each data
167     total = sizeof(pos) + 1 + sizeof(this->NumberOfVertices) +
168       size * (3 + 3) // Size, Type, VertCount, Vert, Normal
169       + sizeof(this->colors[0]) * this->NumberOfVertices * 4 +
170       sizeof(this->NumberOfIndexes) // Color, IndicCount
171       + this->NumberOfIndexes * sizeof(this->indexes[0]) +
172       sizeof(this->Matrix[0]) * 16; // Index, Matrix
173     if (this->tcoords)
174       total += size * 2; // TCoord
175     this->binary = new unsigned char[total];
176     memset(this->binary, 0, total);
177 
178     this->binary[pos++] = 'M';
179     memcpy(&this->binary[pos], &this->NumberOfVertices, sizeof(this->NumberOfVertices));
180     pos += sizeof(this->NumberOfVertices); // VertCount
181     memcpy(&this->binary[pos], this->vertices, size * 3);
182     pos += size * 3; // Vertices
183     memcpy(&this->binary[pos], this->normals, size * 3);
184     pos += size * 3; // Normals
185     memcpy(&this->binary[pos], this->colors, sizeof(this->colors[0]) * this->NumberOfVertices * 4);
186     pos += sizeof(this->colors[0]) * this->NumberOfVertices * 4; // Colors
187     memcpy(&this->binary[pos], &this->NumberOfIndexes, sizeof(this->NumberOfIndexes));
188     pos += sizeof(this->NumberOfIndexes); // IndCount
189     memcpy(&this->binary[pos], this->indexes, this->NumberOfIndexes * sizeof(this->indexes[0]));
190     pos += this->NumberOfIndexes * sizeof(this->indexes[0]);
191     memcpy(&this->binary[pos], this->Matrix, sizeof(this->Matrix[0]) * 16);
192     pos += sizeof(this->Matrix[0]) * 16; // Matrix
193     if (this->tcoords)                   // TCoord
194     {
195       memcpy(&this->binary[pos], this->tcoords, size * 2);
196       pos += size * 2;
197     }
198 
199     memcpy(&this->binary[0], &pos, sizeof(pos));
200     this->binarySize = total;
201   }
202   else if (this->webGLType == wPOINTS)
203   {
204     pos = sizeof(pos);
205     size = this->NumberOfPoints * sizeof(this->points[0]);
206 
207     // Calculate the size used by each data
208     total = sizeof(pos) + 1 + sizeof(this->NumberOfPoints) +
209       size * 3 // Size, Type, NumberOfPoints, Points
210       + sizeof(this->colors[0]) * this->NumberOfPoints * 4 +
211       sizeof(this->Matrix[0]) * 16; // Color, Matrix
212     this->binary = new unsigned char[total];
213     memset(this->binary, 0, total);
214 
215     this->binary[pos++] = 'P';
216     memcpy(&this->binary[pos], &this->NumberOfPoints, sizeof(this->NumberOfPoints));
217     pos += sizeof(this->NumberOfPoints); // Points
218     memcpy(&this->binary[pos], this->points, size * 3);
219     pos += size * 3;
220     memcpy(&this->binary[pos], this->colors, sizeof(this->colors[0]) * this->NumberOfPoints * 4);
221     pos += sizeof(this->colors[0]) * this->NumberOfPoints * 4;
222     memcpy(&this->binary[pos], this->Matrix, sizeof(this->Matrix[0]) * 16);
223     pos += sizeof(this->Matrix[0]) * 16; // Matrix
224 
225     memcpy(&this->binary[0], &pos, sizeof(pos));
226     this->binarySize = total;
227   }
228   vtkWebGLExporter::ComputeMD5((const unsigned char*)this->binary, this->binarySize, this->MD5);
229   this->hasChanged = true;
230 }
231 
SetType(WebGLObjectTypes t)232 void vtkWebGLDataSet::SetType(WebGLObjectTypes t)
233 {
234   this->webGLType = t;
235 }
236 
HasChanged()237 bool vtkWebGLDataSet::HasChanged()
238 {
239   return this->hasChanged;
240 }
241 
PrintSelf(ostream & os,vtkIndent indent)242 void vtkWebGLDataSet::PrintSelf(ostream& os, vtkIndent indent)
243 {
244   this->Superclass::PrintSelf(os, indent);
245 }
246