forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkX3DExporterFIWriter.cxx
715 lines (643 loc) · 20.3 KB
/
vtkX3DExporterFIWriter.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
/*=========================================================================
Program: Visualization Toolkit
Module: vtkX3DExporterFIWriter.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 "vtkX3DExporterFIWriter.h"
#include "vtkCellArray.h"
#include "vtkDataArray.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkPoints.h"
#include "vtkUnsignedCharArray.h"
#include "vtkX3D.h"
#include <sstream>
#include <cassert>
#include <vector>
//#define ENCODEASSTRING 1
using namespace vtkX3D;
/*======================================================================== */
struct NodeInfo
{
NodeInfo(int _nodeId)
{
this->nodeId = _nodeId;
this->isChecked = false;
this->attributesTerminated = true;
}
int nodeId;
bool attributesTerminated;
bool isChecked;
};
/*======================================================================== */
typedef std::vector<NodeInfo> vtkX3DExporterFINodeInfoStackBase;
class vtkX3DExporterFINodeInfoStack :
public vtkX3DExporterFINodeInfoStackBase
{
};
/*======================================================================== */
class vtkX3DExporterFIByteWriter
{
public:
~vtkX3DExporterFIByteWriter();
vtkX3DExporterFIByteWriter() = default;
// This is the current byte to fill
unsigned char CurrentByte;
// This is the current byte position. Range: 0-7
unsigned char CurrentBytePos;
// Opens the specified file in binary mode. Returns 0
// if failed
int OpenFile(const char* file);
int OpenStream();
// Puts a bitstring to the current byte bit by bit
void PutBits(const std::string &bitstring);
// Puts the integer value to the stream using count bits
// for encoding
void PutBits(unsigned int value, unsigned char count);
// Puts on bit to the current byte true = 1, false = 0
void PutBit(bool on);
// Puts whole bytes to the file stream. CurrentBytePos must
// be 0 for this
void PutBytes(const char* bytes, size_t length);
// Fills up the current byte with 0 values
void FillByte();
// Get stream info
std::string GetStringStream(int& size);
private:
unsigned char Append(unsigned int value, unsigned char count);
void TryFlush();
ostream* Stream;
int WriteToOutputString;
vtkX3DExporterFIByteWriter(const vtkX3DExporterFIByteWriter&) = delete;
void operator=(const vtkX3DExporterFIByteWriter&) = delete;
};
//----------------------------------------------------------------------------
vtkX3DExporterFIByteWriter::~vtkX3DExporterFIByteWriter()
{
delete this->Stream;
this->Stream = nullptr;
}
//----------------------------------------------------------------------------
int vtkX3DExporterFIByteWriter::OpenFile(const char* file)
{
this->WriteToOutputString = 0;
this->CurrentByte = 0;
this->CurrentBytePos = 0;
ofstream* fileStream = new ofstream();
fileStream->open (file, ios::out | ios::binary);
if(fileStream->fail())
{
delete fileStream;
return 0;
}
else
{
this->Stream = fileStream;
return 1;
}
}
//----------------------------------------------------------------------------
int vtkX3DExporterFIByteWriter::OpenStream()
{
this->WriteToOutputString = 1;
this->CurrentByte = 0;
this->CurrentBytePos = 0;
this->Stream = new std::ostringstream();
return 1;
}
//----------------------------------------------------------------------------
std::string vtkX3DExporterFIByteWriter::GetStringStream(int& size)
{
if (this->WriteToOutputString && this->Stream)
{
std::ostringstream *ostr =
static_cast<std::ostringstream*>(this->Stream);
size = static_cast<int>(ostr->str().size());
return ostr->str();
}
size = 0;
return nullptr;
}
//----------------------------------------------------------------------------
void vtkX3DExporterFIByteWriter::TryFlush()
{
if (this->CurrentBytePos == 8)
{
this->Stream->write((char*)(&(this->CurrentByte)), 1);
this->CurrentByte = 0;
this->CurrentBytePos = 0;
}
}
//----------------------------------------------------------------------------
void vtkX3DExporterFIByteWriter::FillByte()
{
while (this->CurrentBytePos !=0)
{
this->PutBit(0);
}
}
//----------------------------------------------------------------------------
void vtkX3DExporterFIByteWriter::PutBit(bool on)
{
assert(this->CurrentBytePos < 8);
if (on)
{
unsigned char pos = this->CurrentBytePos;
unsigned char mask = (unsigned char)(0x80 >> pos);
this->CurrentByte |= mask;
}
this->CurrentBytePos++;
TryFlush();
}
//----------------------------------------------------------------------------
unsigned char vtkX3DExporterFIByteWriter::Append(unsigned int value, unsigned char count)
{
assert(this->CurrentBytePos < 8);
while ((this->CurrentBytePos < 8) && count > 0)
{
// Value and der Stelle i
unsigned int mask = 1;
bool isSet = !(((mask << (count - 1)) & value) == 0);
if (isSet)
{
this->CurrentByte |= static_cast<unsigned char>(0x80 >> this->CurrentBytePos);
}
this->CurrentBytePos++;
count--;
}
TryFlush();
return count;
}
//----------------------------------------------------------------------------
void vtkX3DExporterFIByteWriter::PutBytes(const char* bytes, size_t length)
{
if(this->CurrentBytePos == 0)
{
this->Stream->write(bytes, length);
}
else
{
//vtkErrorMacro(<< "Wrong position in vtkX3DExporterFIByteWriter::PutBytes");
assert(false);
}
}
//----------------------------------------------------------------------------
void vtkX3DExporterFIByteWriter::PutBits(unsigned int value, unsigned char count)
{
// Can be optimized
while (count > 0)
{
count = this->Append(value, count);
}
}
//----------------------------------------------------------------------------
void vtkX3DExporterFIByteWriter::PutBits(const std::string &bitstring)
{
std::string::const_iterator I = bitstring.begin();
while(I != bitstring.end())
{
this->PutBit((*I) == '1');
++I;
}
}
#include "vtkX3DExporterFIWriterHelper.h"
/* ------------------------------------------------------------------------- */
vtkStandardNewMacro(vtkX3DExporterFIWriter);
//----------------------------------------------------------------------------
vtkX3DExporterFIWriter::~vtkX3DExporterFIWriter()
{
this->CloseFile();
delete this->InfoStack;
this->Compressor->Delete();
}
//----------------------------------------------------------------------------
vtkX3DExporterFIWriter::vtkX3DExporterFIWriter()
{
this->InfoStack = new vtkX3DExporterFINodeInfoStack();
this->Compressor = vtkZLibDataCompressor::New();
this->Compressor->SetCompressionLevel(5);
this->Writer = nullptr;
this->IsLineFeedEncodingOn = true;
this->Fastest = 0;
}
//----------------------------------------------------------------------------
void vtkX3DExporterFIWriter::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "Fastest: " << this->Fastest << endl;
}
//----------------------------------------------------------------------------
int vtkX3DExporterFIWriter::OpenFile(const char* file)
{
std::string t(file);
this->CloseFile();
// Delegate to vtkX3DExporterFIByteWriter
this->Writer = new vtkX3DExporterFIByteWriter();
this->WriteToOutputString = 0;
return this->Writer->OpenFile(file);
}
int vtkX3DExporterFIWriter::OpenStream()
{
// Delegate to vtkX3DExporterFIByteWriter
this->Writer = new vtkX3DExporterFIByteWriter();
this->WriteToOutputString = 1;
return this->Writer->OpenStream();
}
//----------------------------------------------------------------------------
void vtkX3DExporterFIWriter::CloseFile()
{
if(this->Writer != nullptr)
{
if(this->WriteToOutputString)
{
delete [] this->OutputString;
std::string tmpstr = this->Writer->GetStringStream(this->OutputStringLength);
const char* tmp = tmpstr.c_str();
this->OutputString = new char[this->OutputStringLength];
memcpy(this->OutputString, tmp, this->OutputStringLength);
}
delete this->Writer;
this->Writer = nullptr;
}
}
//----------------------------------------------------------------------------
void vtkX3DExporterFIWriter::StartDocument()
{
const char* external_voc = "urn:external-vocabulary";
// ITU 12.6: 1110000000000000
this->Writer->PutBits("1110000000000000");
// ITU 12.7 / 12.9: Version of standard: 1 as 16bit
this->Writer->PutBits("0000000000000001");
// ITU 12.8: The bit '0' (padding) shall then be appended to the bit stream
this->Writer->PutBit(0);
// ITU C.2.3
this->Writer->PutBit(0); // additional-data
this->Writer->PutBit(1); // initial-vocabulary
this->Writer->PutBit(0); // notations
this->Writer->PutBit(0); // unparsed-entities
this->Writer->PutBit(0); // character-encoding-scheme
this->Writer->PutBit(0); // standalone
this->Writer->PutBit(0); // and version
// ITU C.2.5: padding '000' for optional component initial-vocabulary
this->Writer->PutBits("000");
// ITU C.2.5.1: For each of the thirteen optional components:
// presence ? 1 : 0
this->Writer->PutBits("1000000000000"); // 'external-vocabulary'
// ITU C.2.5.2: external-vocabulary is present
this->Writer->PutBit(0);
// Write "urn:external-vocabulary"
// ITU C.22.3.1: Length is < 65
this->Writer->PutBit(0);
//Writer->PutBits("010110"); // = strlen(external_voc) - 1
this->Writer->PutBits(
static_cast<unsigned int>(strlen(external_voc) - 1), 6);
this->Writer->PutBytes(external_voc, strlen(external_voc));
}
//----------------------------------------------------------------------------
void vtkX3DExporterFIWriter::EndDocument()
{
// ITU C.2.12: The four bits '1111' (termination) are appended
this->Writer->PutBits("1111");
}
//----------------------------------------------------------------------------
void vtkX3DExporterFIWriter::StartNode(int elementID)
{
if (!this->InfoStack->empty())
{
this->CheckNode(false);
if (this->IsLineFeedEncodingOn)
{
vtkX3DExporterFIWriterHelper::EncodeLineFeed(this->Writer);
}
this->Writer->FillByte();
}
this->InfoStack->push_back(NodeInfo(elementID));
// ITU C.3.7.2: element is present
this->Writer->PutBit(0);
}
//----------------------------------------------------------------------------
void vtkX3DExporterFIWriter::EndNode()
{
assert(!this->InfoStack->empty());
this->CheckNode(false);
if (this->IsLineFeedEncodingOn)
{
vtkX3DExporterFIWriterHelper::EncodeLineFeed(this->Writer);
}
if(!this->InfoStack->back().attributesTerminated)
{
//cout << "Terminated in EndNode: could be wrong" << endl;
// ITU C.3.6.2: End of attribute
this->Writer->PutBits("1111");
}
// ITU C.3.8: The four bits '1111' (termination) are appended.
this->Writer->PutBits("1111");
this->InfoStack->pop_back();
}
//----------------------------------------------------------------------------
void vtkX3DExporterFIWriter::CheckNode(bool callerIsAttribute)
{
if(!this->InfoStack->back().isChecked)
{
if (callerIsAttribute) // Element has attributes
{
// ITU C.3.3: then the bit '1' (presence) is appended
this->Writer->PutBit(1);
this->InfoStack->back().attributesTerminated = false;
}
else // Element has no attributes
{
// ITU C.3.3: otherwise, the bit '0' (absence) is appended
this->Writer->PutBit(0);
}
// Write Node name (starting at third bit)
// ITU: C.18.4 If the alternative name-surrogate-index is present,
// it is encoded as described in C.27.
vtkX3DExporterFIWriterHelper::EncodeInteger3(this->Writer, this->InfoStack->back().nodeId + 1);
this->InfoStack->back().isChecked = true;
}
// Element has attributes and childs
else if (!callerIsAttribute && !this->InfoStack->back().attributesTerminated)
{
// ITU C.3.6.2: End of attribute
this->Writer->PutBits("1111");
this->InfoStack->back().attributesTerminated = true;
}
}
//----------------------------------------------------------------------------
void vtkX3DExporterFIWriter::StartAttribute(int attributeID, bool literal, bool addToTable)
{
this->CheckNode();
// ITU C.3.6.1: Start of attribute
this->Writer->PutBit(0);
// ITU C.4.3 The value of qualified-name is encoded as described in C.17.
vtkX3DExporterFIWriterHelper::EncodeInteger2(this->Writer, attributeID +1);
// ITU C.14.3: If the alternative literal-character-string is present,
//then the bit '0' (discriminant) is appended
// ITU C.14.4: If the alternative string-index is present,
// then the bit '1' (discriminant) is appended
this->Writer->PutBit(literal ? 0 : 1);
if (literal)
{
// ITU C.14.3.1 If the value of the component add-to-table is TRUE,
// then the bit '1' is appended to the bit stream;
this->Writer->PutBit(addToTable);
}
}
//----------------------------------------------------------------------------
void vtkX3DExporterFIWriter::EndAttribute()
{
}
//----------------------------------------------------------------------------
void vtkX3DExporterFIWriter::SetField(int attributeID, int type, const double* d)
{
this->StartAttribute(attributeID, true, false);
#ifdef ENCODEASSTRING
const double* loc = nullptr;
size_t size = 0;
double temp[4];
switch (type)
{
case(SFVEC3F):
case(SFCOLOR):
size = 3; loc = d;
break;
case(SFROTATION):
size = 4;
temp[0] = d[1];
temp[1] = d[2];
temp[2] = d[3];
temp[3] = vtkMath::RadiansFromDegrees( -d[0] );
loc = temp;
break;
default:
cerr << "UNKNOWN DATATYPE";
assert(false);
}
vtkX3DExporterFIWriterHelper::EncodeFloatFI(this->Writer, loc, size);
#else
std::ostringstream ss;
switch (type)
{
case(SFVEC3F):
case(SFCOLOR):
ss << static_cast<float>( d[0] )
<< " "
<< static_cast<float>( d[1] )
<< " "
<< static_cast<float>( d[2] );
break;
case(SFROTATION):
ss << static_cast<float>( d[1] )
<< " "
<< static_cast<float>( d[2] )
<< " "
<< static_cast<float>( d[3] )
<< " "
<< static_cast<float>( vtkMath::RadiansFromDegrees( -d[0] ) );
break;
default:
cout << "UNKNOWN DATATYPE";
assert(false);
}
vtkX3DExporterFIWriterHelper::EncodeCharacterString3(this->Writer, ss.str());
#endif
}
//----------------------------------------------------------------------------
void vtkX3DExporterFIWriter::SetField(int attributeID, int type, vtkDataArray* a)
{
this->StartAttribute(attributeID, true, false);
#ifdef ENCODEASSTRING
std::ostringstream ss;
switch(type)
{
case (MFVEC3F):
case (MFVEC2F):
vtkIdType i;
for (i = 0; i < a->GetNumberOfTuples(); i++)
{
double* d = a->GetTuple(i);
ss << d[0] << " " << d[1];
if (type == MFVEC3F) ss << " " << d[2];
ss << ",";
}
vtkX3DExporterFIWriterHelper::EncodeCharacterString3(this->Writer, ss.str());
break;
default:
cerr << "UNKNOWN DATATYPE";
assert(false);
}
#else
std::vector<double> values;
switch(type)
{
case (MFVEC3F):
case (MFVEC2F):
vtkIdType i;
for (i = 0; i < a->GetNumberOfTuples(); i++)
{
double* d = a->GetTuple(i);
values.push_back(d[0]);
values.push_back(d[1]);
if (type == MFVEC3F)
{
values.push_back(d[2]);
}
}
if (!this->Fastest && (values.size() > 15))
{
X3DEncoderFunctions::EncodeQuantizedzlibFloatArray(this->Writer,
&(values.front()), values.size(), this->Compressor);
}
else
{
vtkX3DExporterFIWriterHelper::EncodeFloatFI(this->Writer,
&(values.front()), values.size());
}
break;
default:
vtkErrorMacro("UNKNOWN DATATYPE");
assert(false);
}
#endif
}
//----------------------------------------------------------------------------
void vtkX3DExporterFIWriter::SetField(int attributeID, const double* values,
size_t size)
{
this->StartAttribute(attributeID, true, false);
if (!this->Fastest && (size > 15))
{
X3DEncoderFunctions::EncodeQuantizedzlibFloatArray(this->Writer,
values, size, this->Compressor);
}
else
{
vtkX3DExporterFIWriterHelper::EncodeFloatFI(this->Writer, values, size);
}
}
//----------------------------------------------------------------------------
void vtkX3DExporterFIWriter::SetField(int attributeID,
const int* values, size_t size, bool image)
{
this->StartAttribute(attributeID, true, false);
if (size > 15)
{
X3DEncoderFunctions::EncodeIntegerDeltaZ(this->Writer, values, size,
this->Compressor, image);
}
else
{
vtkX3DExporterFIWriterHelper::EncodeIntegerFI(this->Writer, values, size);
}
}
//----------------------------------------------------------------------------
void vtkX3DExporterFIWriter::SetField(int attributeID, int type, vtkCellArray* a)
{
vtkIdType npts = 0;
vtkIdType *indx = nullptr;
this->StartAttribute(attributeID, true, false);
#ifdef ENCODEASSTRING
switch(type)
{
case (MFINT32):
int i;
for (a->InitTraversal(); a->GetNextCell(npts,indx); )
{
for (i = 0; i < npts; i++)
{
// treating vtkIdType as int
ss << (int)indx[i] << " ";
}
ss << "-1";
}
vtkX3DExporterFIWriterHelper::EncodeCharacterString3(this->Writer, ss.str());
break;
default:
cerr << "UNKNOWN DATATYPE";
assert(false);
}
#else
std::vector<int> values;
switch(type)
{
case (MFINT32):
int i;
for (a->InitTraversal(); a->GetNextCell(npts,indx); )
{
for (i = 0; i < npts; i++)
{
values.push_back(indx[i]);
}
values.push_back(-1);
}
vtkX3DExporterFIWriterHelper::EncodeIntegerFI(this->Writer, &(values.front()), values.size());
break;
default:
cerr << "UNKNOWN DATATYPE";
assert(false);
}
#endif
}
//----------------------------------------------------------------------------
void vtkX3DExporterFIWriter::SetField(int attributeID, int value)
{
std::ostringstream ss;
this->StartAttribute(attributeID, true, false);
// Xj3D writes out single value fields in string encoding. Expected:
//FIEncoderFunctions::EncodeFloatFI<float>(this->Writer, &value, 1);
ss << value;
vtkX3DExporterFIWriterHelper::EncodeCharacterString3(this->Writer, ss.str());
}
//----------------------------------------------------------------------------
void vtkX3DExporterFIWriter::SetField(int attributeID, float value)
{
std::ostringstream ss;
this->StartAttribute(attributeID, true, false);
// Xj3D writes out single value fields in string encoding. Expected:
//FIEncoderFunctions::EncodeFloatFI<float>(this->Writer, &value, 1);
ss << value;
vtkX3DExporterFIWriterHelper::EncodeCharacterString3(this->Writer, ss.str());
}
//----------------------------------------------------------------------------
void vtkX3DExporterFIWriter::SetField(int vtkNotUsed(attributeID),
double vtkNotUsed(value))
{
cout << "Function not implemented yet." << endl;
assert(false);
}
//----------------------------------------------------------------------------
void vtkX3DExporterFIWriter::SetField(int attributeID, bool value)
{
this->StartAttribute(attributeID, false);
vtkX3DExporterFIWriterHelper::EncodeInteger2(this->Writer, value ? 2 : 1);
}
//----------------------------------------------------------------------------
void vtkX3DExporterFIWriter::SetField(int attributeID, const char* value,
bool vtkNotUsed(mfstring))
{
this->StartAttribute(attributeID, true, true);
vtkX3DExporterFIWriterHelper::EncodeCharacterString3(this->Writer, std::string(value));
}
//----------------------------------------------------------------------------
/*void vtkX3DExporterFIWriter::SetField(int attributeID, const std::string &value)
{
this->StartAttribute(attributeID, true, true);
vtkX3DExporterFIWriterHelper::EncodeCharacterString3(this->Writer, value);
}*/
//----------------------------------------------------------------------------
/*void vtkX3DExporterFIWriter::SetField(int attributeID, int type, std::string value)
{
assert(type == MFSTRING);
type++;
this->SetField(attributeID, value);
}*/
//----------------------------------------------------------------------------
void vtkX3DExporterFIWriter::Flush()
{
}