forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkX3DExporterXMLWriter.cxx
365 lines (320 loc) · 10.2 KB
/
vtkX3DExporterXMLWriter.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*=========================================================================
Program: Visualization Toolkit
Module: vtkX3DExporterXMLWriter.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen, Kristian Sons
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 "vtkX3DExporterXMLWriter.h"
#include "vtkX3D.h"
#include "vtkObjectFactory.h"
#include "vtkDataArray.h"
#include "vtkUnsignedCharArray.h"
#include "vtkPoints.h"
#include "vtkCellArray.h"
#include "vtkMath.h"
#include <vtksys/ios/sstream>
#include <vtksys/ios/fstream>
#include <string>
#include <cassert>
using namespace vtkX3D;
struct XMLInfo {
XMLInfo(int _elementId)
{
this->elementId = _elementId;
this->endTagWritten = false;
}
int elementId;
bool endTagWritten;
};
typedef std::vector<XMLInfo> vtkX3DExporterXMLNodeInfoStackBase;
class vtkX3DExporterXMLNodeInfoStack: public vtkX3DExporterXMLNodeInfoStackBase
{};
//-----------------------------------------------------------------------------
vtkStandardNewMacro(vtkX3DExporterXMLWriter);
//-----------------------------------------------------------------------------
vtkX3DExporterXMLWriter::~vtkX3DExporterXMLWriter()
{
delete this->InfoStack;
if(this->OutputStream)
{
delete this->OutputStream;
this->OutputStream = NULL;
}
}
//-----------------------------------------------------------------------------
vtkX3DExporterXMLWriter::vtkX3DExporterXMLWriter()
{
this->OutputStream = NULL;
this->InfoStack = new vtkX3DExporterXMLNodeInfoStack();
this->Depth = 0;
this->ActTab = "";
}
//-----------------------------------------------------------------------------
void vtkX3DExporterXMLWriter::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
//----------------------------------------------------------------------------
int vtkX3DExporterXMLWriter::OpenFile(const char* file)
{
this->CloseFile();
this->WriteToOutputString = 0;
vtksys_ios::fstream* fileStream = new vtksys_ios::fstream();
fileStream->open (file, ios::out);
if(fileStream->fail())
{
delete fileStream;
return 0;
}
else
{
this->OutputStream = fileStream;
return 1;
}
}
//----------------------------------------------------------------------------
int vtkX3DExporterXMLWriter::OpenStream()
{
this->CloseFile();
this->WriteToOutputString = 1;
this->OutputStream = new vtksys_ios::ostringstream();
return 1;
}
//----------------------------------------------------------------------------
void vtkX3DExporterXMLWriter::CloseFile()
{
if(this->OutputStream != NULL)
{
if(this->WriteToOutputString)
{
vtksys_ios::ostringstream *ostr =
static_cast<vtksys_ios::ostringstream*>(this->OutputStream);
if(this->OutputString)
{
delete [] this->OutputString;
}
this->OutputStringLength = static_cast<int>(ostr->str().size());
this->OutputString = new char[ostr->str().size()];
memcpy(this->OutputString, ostr->str().c_str(),
this->OutputStringLength);
}
delete this->OutputStream;
this->OutputStream = NULL;
}
}
//-----------------------------------------------------------------------------
void vtkX3DExporterXMLWriter::StartDocument()
{
this->Depth = 0;
*this->OutputStream << "<?xml version=\"1.0\" encoding =\"UTF-8\"?>"
<< endl << endl;
}
//-----------------------------------------------------------------------------
void vtkX3DExporterXMLWriter::EndDocument()
{
assert(this->Depth == 0);
}
//-----------------------------------------------------------------------------
void vtkX3DExporterXMLWriter::StartNode(int elementID)
{
// End last tag, if this is the first child
if (!this->InfoStack->empty())
{
if (!this->InfoStack->back().endTagWritten)
{
*this->OutputStream << ">" << this->GetNewline();
this->InfoStack->back().endTagWritten = true;
}
}
this->InfoStack->push_back(XMLInfo(elementID));
*this->OutputStream << this->ActTab << "<" << x3dElementString[elementID];
this->AddDepth();
}
//-----------------------------------------------------------------------------
void vtkX3DExporterXMLWriter::EndNode()
{
assert(!this->InfoStack->empty());
this->SubDepth();
int elementID = this->InfoStack->back().elementId;
// There were no childs
if (!this->InfoStack->back().endTagWritten)
{
*this->OutputStream << "/>" << this->GetNewline();
}
else
{
*this->OutputStream << this->ActTab << "</" << x3dElementString[elementID]
<< ">" << this->GetNewline();
}
this->InfoStack->pop_back();
}
//-----------------------------------------------------------------------------
void vtkX3DExporterXMLWriter::SetField(int attributeID, int type, const double* d)
{
*this->OutputStream << " " << x3dAttributeString[attributeID] << "=\"";
switch (type)
{
case(SFVEC3F):
case(SFCOLOR):
*this->OutputStream << d[0]
<< " "
<< d[1]
<< " "
<< d[2];
break;
case(SFROTATION):
*this->OutputStream << d[1]
<< " "
<< d[2]
<< " "
<< d[3]
<< " "
<< vtkMath::RadiansFromDegrees( -d[0] );
break;
default:
*this->OutputStream << "UNKNOWN DATATYPE";
}
*this->OutputStream << "\"";
}
//-----------------------------------------------------------------------------
void vtkX3DExporterXMLWriter::SetField(int attributeID, int type, vtkDataArray* a)
{
*this->OutputStream << " " << x3dAttributeString[attributeID] << "=\"" << this->GetNewline();
switch (type)
{
case(MFVEC3F):
for (vtkIdType i = 0; i < a->GetNumberOfTuples(); i++)
{
double* d = a->GetTuple(i);
*this->OutputStream << this->ActTab << d[0] << " " << d[1] << " " << d[2] << "," << this->GetNewline();
}
break;
case(MFVEC2F):
for (vtkIdType i = 0; i < a->GetNumberOfTuples(); i++)
{
double* d = a->GetTuple(i);
*this->OutputStream << this->ActTab << d[0] << " " << d[1] << "," << this->GetNewline();
}
break;
default:
*this->OutputStream << "UNKNOWN DATATYPE";
}
*this->OutputStream << this->ActTab << "\"";
}
//-----------------------------------------------------------------------------
void vtkX3DExporterXMLWriter::SetField(int attributeID, const double* values, size_t size)
{
*this->OutputStream << " " << x3dAttributeString[attributeID] << "=\"" << this->GetNewline() << this->ActTab;
unsigned int i = 0;
while (i < size)
{
*this->OutputStream << values[i];
if ((i+1)%3)
{
*this->OutputStream << " ";
}
else
{
*this->OutputStream << "," << this->GetNewline() << this->ActTab;
}
i++;
}
*this->OutputStream << "\"";
}
//-----------------------------------------------------------------------------
void vtkX3DExporterXMLWriter::SetField(int attributeID, const int* values, size_t size, bool image)
{
*this->OutputStream << " " << x3dAttributeString[attributeID] << "=\"" << this->GetNewline() << this->ActTab;
unsigned int i = 0;
if (image)
{
assert(size > 2);
char buffer[20];
*this->OutputStream << values[0] << " "; // width
*this->OutputStream << values[1] << " "; // height
int bpp = values[2]; *this->OutputStream << bpp << "\n"; // bpp
i = 3;
unsigned int j = 0;
while (i < size)
{
sprintf(buffer,"0x%.8x",values[i]);
*this->OutputStream << buffer;
if (j%(8*bpp))
{
*this->OutputStream << " ";
}
else
{
*this->OutputStream << this->GetNewline();// << this->ActTab;
}
i++; j+=bpp;
}
*this->OutputStream << dec;
}
else
while (i < size)
{
*this->OutputStream << values[i] << " ";
if (values[i] == -1)
{
*this->OutputStream << this->GetNewline() << this->ActTab;
}
i++;
}
*this->OutputStream << "\"";
}
//-----------------------------------------------------------------------------
void vtkX3DExporterXMLWriter::SetField(int attributeID, int value)
{
*this->OutputStream << " " << x3dAttributeString[attributeID] << "=\"" << value << "\"";
}
//-----------------------------------------------------------------------------
void vtkX3DExporterXMLWriter::SetField(int attributeID, float value)
{
*this->OutputStream << " " << x3dAttributeString[attributeID] << "=\"" << value << "\"";
}
//-----------------------------------------------------------------------------
void vtkX3DExporterXMLWriter::SetField(int attributeID, double vtkNotUsed(value))
{
*this->OutputStream << " " << x3dAttributeString[attributeID] << "=\"" << "WHY DOUBLE?" << "\"";
assert(false);
}
//-----------------------------------------------------------------------------
void vtkX3DExporterXMLWriter::SetField(int attributeID, bool value)
{
*this->OutputStream << " "
<< x3dAttributeString[attributeID] << "=\"" << (value ? "true" : "false" ) << "\"";
}
//-----------------------------------------------------------------------------
void vtkX3DExporterXMLWriter::SetField(int attributeID, const char* value, bool mfstring)
{
if (mfstring)
{
*this->OutputStream << " "
<< x3dAttributeString[attributeID] << "='" << value << "'";
}
else
{
*this->OutputStream
<< " " << x3dAttributeString[attributeID] << "=\"" << value << "\"";
}
}
//-----------------------------------------------------------------------------
void vtkX3DExporterXMLWriter::Flush()
{
this->OutputStream->flush();
}
//-----------------------------------------------------------------------------
void vtkX3DExporterXMLWriter::AddDepth()
{
this->ActTab += " ";
}
//-----------------------------------------------------------------------------
void vtkX3DExporterXMLWriter::SubDepth()
{
this->ActTab.erase(0, 2);
}