forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkPVWebGLExporter.cxx
135 lines (118 loc) · 4.4 KB
/
vtkPVWebGLExporter.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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkPVWebGLExporter.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 "vtkPVWebGLExporter.h"
#include "vtkBase64Utilities.h"
#include "vtkCamera.h"
#include "vtkExporter.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRendererCollection.h"
#include "vtkWebGLExporter.h"
#include "vtkWebGLObject.h"
#include <string>
#include <sstream>
#include <fstream>
vtkStandardNewMacro(vtkPVWebGLExporter);
// ---------------------------------------------------------------------------
vtkPVWebGLExporter::vtkPVWebGLExporter()
{
this->FileName = NULL;
}
// ---------------------------------------------------------------------------
vtkPVWebGLExporter::~vtkPVWebGLExporter()
{
this->SetFileName(NULL);
}
// ---------------------------------------------------------------------------
void vtkPVWebGLExporter::WriteData()
{
// make sure the user specified a FileName or FilePointer
if (this->FileName == NULL)
{
vtkErrorMacro(<< "Please specify FileName to use");
return;
}
vtkNew<vtkWebGLExporter> exporter;
exporter->SetMaxAllowedSize(65000);
// We use the camera focal point to be the center of rotation
double centerOfRotation[3];
vtkRenderer *ren = this->RenderWindow->GetRenderers()->GetFirstRenderer();
vtkCamera *cam = ren->GetActiveCamera();
cam->GetFocalPoint(centerOfRotation);
exporter->SetCenterOfRotation( static_cast<float>(centerOfRotation[0]),
static_cast<float>(centerOfRotation[1]),
static_cast<float>(centerOfRotation[2]));
exporter->parseScene(this->RenderWindow->GetRenderers(), "1", VTK_PARSEALL);
// Write meta-data file
std::string baseFileName = this->FileName;
baseFileName.erase(baseFileName.size()-6,6);
std::string metadatFile = this->FileName;
FILE *fp = fopen(metadatFile.c_str(),"w");
if (!fp)
{
vtkErrorMacro(<< "unable to open JSON MetaData file " << metadatFile.c_str());
return;
}
fputs(exporter->GenerateMetadata(), fp);
fclose(fp);
// Write binary objects
vtkNew<vtkBase64Utilities> base64;
int nbObjects = exporter->GetNumberOfObjects();
for(int idx=0; idx < nbObjects; ++idx)
{
vtkWebGLObject* obj = exporter->GetWebGLObject(idx);
if(obj->isVisible())
{
int nbParts = obj->GetNumberOfParts();
for(int part = 0; part < nbParts; ++part)
{
// Manage binary content
std::stringstream filePath;
filePath << baseFileName.c_str() << "_" << obj->GetMD5().c_str() << "_" << part;
std::fstream binaryFile;
binaryFile.open( filePath.str().c_str(),
std::ios_base::out | std::ios_base::binary);
binaryFile.write((const char *)obj->GetBinaryData(part), obj->GetBinarySize(part));
binaryFile.close();
// Manage Base64
std::stringstream filePathBase64;
filePathBase64 << baseFileName.c_str() << "_" << obj->GetMD5().c_str() << "_" << part << ".base64";
std::fstream base64File;
unsigned char* output = new unsigned char[obj->GetBinarySize(part)*2];
int size = base64->Encode(
obj->GetBinaryData(part), obj->GetBinarySize(part), output, false);
base64File.open(filePathBase64.str().c_str(), std::ios_base::out);
base64File.write((const char *)output, size);
base64File.close();
delete[] output;
}
}
}
// Write HTML file
std::string htmlFile = baseFileName;
htmlFile += ".html";
exporter->exportStaticScene(this->RenderWindow->GetRenderers(), 300, 300, htmlFile.c_str());
}
// ---------------------------------------------------------------------------
void vtkPVWebGLExporter::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
if (this->FileName)
{
os << indent << "FileName: " << this->FileName << "\n";
}
else
{
os << indent << "FileName: (null)\n";
}
}