forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkSVGExporter.cxx
345 lines (296 loc) · 10.4 KB
/
vtkSVGExporter.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*=========================================================================
Program: Visualization Toolkit
Module: vtkSVGExporter.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 "vtkSVGExporter.h"
#include "vtkBrush.h"
#include "vtkContext2D.h"
#include "vtkContextActor.h"
#include "vtkContextScene.h"
#include "vtkImageData.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkProp.h"
#include "vtkPropCollection.h"
#include "vtkRect.h"
#include "vtkRenderWindow.h"
#include "vtkRenderer.h"
#include "vtkRendererCollection.h"
#include "vtkSVGContextDevice2D.h"
#include "vtkTexture.h"
#include "vtkXMLDataElement.h"
#include <array>
#include <cstring>
#include <iomanip>
#include <sstream>
#include <string>
namespace
{
std::string ColorToString(const unsigned char* rgb)
{
std::ostringstream out;
out << "#";
for (int i = 0; i < 3; ++i)
{
out << std::setw(2) << std::right << std::setfill('0') << std::hex
<< static_cast<unsigned int>(rgb[i]);
}
return out.str();
}
} // end anon namespace
//------------------------------------------------------------------------------
vtkStandardNewMacro(vtkSVGExporter);
//------------------------------------------------------------------------------
void vtkSVGExporter::PrintSelf(std::ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
//------------------------------------------------------------------------------
vtkSVGExporter::vtkSVGExporter()
: Title(nullptr)
, Description(nullptr)
, FileName(nullptr)
, Device(nullptr)
, RootNode(nullptr)
, PageNode(nullptr)
, DefinitionNode(nullptr)
, SubdivisionThreshold(1.f)
, DrawBackground(true)
, TextAsPath(true)
{
this->SetTitle("VTK Exported Scene");
this->SetDescription("VTK Exported Scene");
}
//------------------------------------------------------------------------------
vtkSVGExporter::~vtkSVGExporter()
{
assert(!this->Device);
assert(!this->RootNode);
assert(!this->PageNode);
this->SetTitle(nullptr);
this->SetDescription(nullptr);
this->SetFileName(nullptr);
}
//------------------------------------------------------------------------------
void vtkSVGExporter::WriteData()
{
if (!this->FileName || !*this->FileName)
{
vtkErrorMacro("FileName not specified.");
return;
}
if (!this->RenderWindow)
{
vtkErrorMacro("No RenderWindow set -- nothing to export.");
return;
}
// These should be freed at the end of the last export.
assert(!this->Device);
assert(!this->RootNode);
assert(!this->DefinitionNode);
assert(!this->PageNode);
this->WriteSVG();
this->Device->Delete();
this->Device = nullptr;
this->RootNode->Delete();
this->RootNode = nullptr;
this->DefinitionNode = nullptr;
this->PageNode = nullptr;
}
//------------------------------------------------------------------------------
void vtkSVGExporter::WriteSVG()
{
this->PrepareDocument();
this->RenderContextActors();
// If we aren't using any defs, remove the node.
if (this->DefinitionNode->GetNumberOfNestedElements() == 0)
{
this->RootNode->RemoveNestedElement(this->DefinitionNode);
this->DefinitionNode = nullptr; // no delete -- the remove above clears ref
}
else
{
this->Device->GenerateDefinitions();
}
this->RootNode->PrintXML(this->FileName);
}
//------------------------------------------------------------------------------
void vtkSVGExporter::PrepareDocument()
{
const int* size = this->RenderWindow->GetSize();
this->RootNode = vtkXMLDataElement::New();
this->RootNode->SetName("svg");
this->RootNode->SetAttribute("xmlns", "http://www.w3.org/2000/svg");
this->RootNode->SetAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");
this->RootNode->SetAttribute("version", "1.1");
this->RootNode->SetIntAttribute("width", size[0]);
this->RootNode->SetIntAttribute("height", size[1]);
// Antialias everything by default (we disable this for adjacent polygons
// when possible, though):
this->RootNode->SetAttribute("shape-rendering", "geometricPrecision");
if (this->Title && this->Title[0])
{
vtkNew<vtkXMLDataElement> title;
title->SetName("title");
title->SetCharacterData(this->Title, static_cast<int>(std::strlen(this->Title)));
this->RootNode->AddNestedElement(title);
}
if (this->Description && this->Description[0])
{
vtkNew<vtkXMLDataElement> desc;
desc->SetName("desc");
desc->SetCharacterData(this->Description, static_cast<int>(std::strlen(this->Description)));
this->RootNode->AddNestedElement(desc);
}
this->DefinitionNode = vtkXMLDataElement::New();
this->RootNode->AddNestedElement(this->DefinitionNode);
this->DefinitionNode->Delete();
this->DefinitionNode->SetName("defs");
this->PageNode = vtkXMLDataElement::New();
this->RootNode->AddNestedElement(this->PageNode);
this->PageNode->Delete();
this->PageNode->SetName("g");
// Setup the page to not fill or stroke anything by default. Otherwise we'd
// have to have fill="none" or stroke="none" on every primitive, since we
// never do both at once in the Context2D API.
this->PageNode->SetAttribute("stroke", "none");
this->PageNode->SetAttribute("fill", "none");
this->Device = vtkSVGContextDevice2D::New();
this->Device->SetSVGContext(this->PageNode, this->DefinitionNode);
this->Device->SetTextAsPath(this->TextAsPath);
this->Device->SetSubdivisionThreshold(this->SubdivisionThreshold);
}
//------------------------------------------------------------------------------
void vtkSVGExporter::RenderContextActors()
{
vtkRendererCollection* renCol = this->RenderWindow->GetRenderers();
int numLayers = this->RenderWindow->GetNumberOfLayers();
for (int i = 0; i < numLayers; ++i)
{
vtkCollectionSimpleIterator renIt;
vtkRenderer* ren;
for (renCol->InitTraversal(renIt); (ren = renCol->GetNextRenderer(renIt));)
{
if (this->ActiveRenderer && ren != this->ActiveRenderer)
{
// If ActiveRenderer is specified then ignore all other renderers
continue;
}
if (ren->GetLayer() == i)
{
if (this->DrawBackground)
{
this->RenderBackground(ren);
}
vtkPropCollection* props = ren->GetViewProps();
vtkCollectionSimpleIterator propIt;
vtkProp* prop;
for (props->InitTraversal(propIt); (prop = props->GetNextProp(propIt));)
{
vtkContextActor* actor = vtkContextActor::SafeDownCast(prop);
if (actor)
{
this->RenderContextActor(actor, ren);
}
}
}
}
}
}
//------------------------------------------------------------------------------
void vtkSVGExporter::RenderBackground(vtkRenderer* ren)
{
if (ren->Transparent())
{
return;
}
int* renOrigin = ren->GetOrigin();
const int* renSize = ren->GetSize();
vtkRectf renRect(renOrigin[0], renOrigin[1], renSize[0], renSize[1]);
vtkNew<vtkContext2D> ctx;
ctx->Begin(this->Device);
this->Device->Begin(ren);
if (ren->GetTexturedBackground())
{
vtkTexture* tex = ren->GetBackgroundTexture();
vtkImageData* image = tex->GetInput();
ctx->DrawImage(renRect, image);
}
else if (ren->GetGradientBackground())
{
std::ostringstream gradIdStream;
gradIdStream << "bgGrad_" << ren;
const std::string gradId = gradIdStream.str();
std::array<double, 3> rgb1d;
std::array<double, 3> rgb2d;
ren->GetBackground(rgb1d.data());
ren->GetBackground2(rgb2d.data());
double a = ren->GetBackgroundAlpha();
std::array<unsigned char, 3> rgb1;
std::array<unsigned char, 3> rgb2;
for (size_t i = 0; i < 3; ++i)
{
rgb1[i] = static_cast<unsigned char>(rgb1d[i] * 255);
rgb2[i] = static_cast<unsigned char>(rgb2d[i] * 255);
}
const float canvasHeight = ren->GetVTKWindow()->GetSize()[1];
vtkNew<vtkXMLDataElement> gradient;
this->DefinitionNode->AddNestedElement(gradient);
gradient->SetName("linearGradient");
gradient->SetAttribute("id", gradId.c_str());
gradient->SetAttribute("gradientUnits", "objectBoundingBox");
gradient->SetIntAttribute("x1", 0);
gradient->SetIntAttribute("y1", 1);
gradient->SetIntAttribute("x2", 0);
gradient->SetIntAttribute("y2", 0);
vtkNew<vtkXMLDataElement> stop1;
gradient->AddNestedElement(stop1);
stop1->SetName("stop");
stop1->SetAttribute("offset", "0%");
stop1->SetAttribute("stop-color", ColorToString(rgb1.data()).c_str());
vtkNew<vtkXMLDataElement> stop2;
gradient->AddNestedElement(stop2);
stop2->SetName("stop");
stop2->SetAttribute("offset", "100%");
stop2->SetAttribute("stop-color", ColorToString(rgb2.data()).c_str());
vtkNew<vtkXMLDataElement> rect;
this->PageNode->AddNestedElement(rect);
rect->SetName("rect");
rect->SetAttribute("fill", (std::string("url(#") + gradId + ")").c_str());
rect->SetFloatAttribute("fill-opacity", static_cast<float>(a));
rect->SetFloatAttribute("x", renRect.GetLeft());
rect->SetFloatAttribute("y", canvasHeight - renRect.GetTop());
rect->SetFloatAttribute("width", renRect.GetWidth());
rect->SetFloatAttribute("height", renRect.GetHeight());
}
else
{
std::array<double, 3> rgb;
ren->GetBackground(rgb.data());
double a = ren->GetBackgroundAlpha();
ctx->GetBrush()->SetColor(static_cast<unsigned char>(rgb[0] * 255),
static_cast<unsigned char>(rgb[1] * 255), static_cast<unsigned char>(rgb[2] * 255),
static_cast<unsigned char>(a * 255));
// Draw the rect directly on the device. Context2D::DrawRect also strokes
// the path...
std::array<float, 8> poly = { { renRect.GetLeft(), renRect.GetBottom(), renRect.GetRight(),
renRect.GetBottom(), renRect.GetRight(), renRect.GetTop(), renRect.GetLeft(),
renRect.GetTop() } };
this->Device->DrawPolygon(poly.data(), 4);
}
ctx->End();
}
//------------------------------------------------------------------------------
void vtkSVGExporter::RenderContextActor(vtkContextActor* actor, vtkRenderer* ren)
{
vtkContextDevice2D* oldForceDevice = actor->GetForceDevice();
actor->SetForceDevice(this->Device);
actor->RenderOverlay(ren);
actor->SetForceDevice(oldForceDevice);
}