forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkVRMLImporter.cxx
1409 lines (1337 loc) · 42.2 KB
/
vtkVRMLImporter.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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*=========================================================================
Program: Visualization Toolkit
Module: vtkVRMLImporter.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.
=========================================================================*/
/* ======================================================================
Importer based on BNF Yacc and Lex parser definition from:
**************************************************
* VRML 2.0 Parser
* Copyright (C) 1996 Silicon Graphics, Inc.
*
* Author(s) : Gavin Bell
* Daniel Woods (first port)
**************************************************
Ported to VTK By: Thomas D. Citriniti
Rensselaer Polytechnic Institute
citrit@rpi.edu
=======================================================================*/
#include "vtkVRMLImporter.h"
#include "vtkActor.h"
#include "vtkCellArray.h"
#include "vtkConeSource.h"
#include "vtkCubeSource.h"
#include "vtkCylinderSource.h"
#include "vtkFloatArray.h"
#include "vtkIdTypeArray.h"
#include "vtkLight.h"
#include "vtkLookupTable.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkPointData.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkPolyDataMapper.h"
#include "vtkProperty.h"
#include "vtkRenderer.h"
#include "vtkSphereSource.h"
#include "vtkSystemIncludes.h"
#include "vtkTransform.h"
#include "vtkVRML.h"
#include <cassert>
#include <exception>
#include <sstream>
#include "vtkVRMLImporter_Yacc.h"
//----------------------------------------------------------------------------
class vtkVRMLImporterInternal
{
public:
vtkVRMLImporterInternal() : Heap(1) {}
vtkVRMLVectorType<vtkObject*> Heap;
};
//----------------------------------------------------------------------------
// Heap to manage memory leaks
vtkHeap *vtkVRMLAllocator::Heap = NULL;
//----------------------------------------------------------------------------
vtkStandardNewMacro(vtkVRMLImporter);
//----------------------------------------------------------------------------
vtkVRMLImporter::vtkVRMLImporter()
{
this->Internal = new vtkVRMLImporterInternal;
this->CurrentActor = NULL;
this->CurrentLight = NULL;
this->CurrentProperty = NULL;
this->CurrentSource = NULL;
this->CurrentPoints = NULL;
this->CurrentScalars = NULL;
this->CurrentNormals = NULL;
this->CurrentNormalCells = NULL;
this->CurrentTCoords = NULL;
this->CurrentTCoordCells = NULL;
this->CurrentMapper = NULL;
this->CurrentLut = NULL;
this->CurrentTransform = NULL;
this->FileName = NULL;
this->FileFD = NULL;
this->Parser = new vtkVRMLYaccData;
this->ShapeResolution = 12;
}
//----------------------------------------------------------------------------
vtkVRMLImporter::~vtkVRMLImporter()
{
if (this->CurrentTransform)
{
this->CurrentTransform->Delete();
this->CurrentTransform = NULL;
}
delete [] this->FileName;
this->FileName = NULL;
while (this->Internal->Heap.Count() > 0)
{
vtkObject* obj = this->Internal->Heap.Pop();
if (obj)
{
obj->Delete();
}
}
delete this->Internal;
this->Internal = NULL;
// According to Tom Citriniti the useList must not be deleted until the
// instance is destroyed. The importer was crashing when users asked for a
// DEF node from within the VRML file. This DEF mechanism allows you to
// name a node inside the VRML file and refer to it from other nodes or
// from scripts that can be associated with the VRML file. A vector of
// these is created in the importer and has to live until the class is
// deleted.
delete this->Parser->useList;
this->Parser->useList = NULL;
vtkVRMLAllocator::CleanUp();
delete this->Parser;
this->Parser = NULL;
}
//----------------------------------------------------------------------------
void vtkVRMLImporter::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
os << indent << "File Name: "
<< (this->FileName ? this->FileName : "(none)") << "\n";
os << "Defined names in File:" << endl;
if (this->Parser->useList)
{
for (int i = 0; i < this->Parser->useList->Count(); i++)
{
os << "\tName: " << (*this->Parser->useList)[i]->defName
<< " is a " << (*this->Parser->useList)[i]->defObject->GetClassName()
<< endl;
}
}
}
//----------------------------------------------------------------------------
// Open an import file. Returns zero if error.
int vtkVRMLImporter::OpenImportFile()
{
vtkDebugMacro(<< "Opening import file");
if (!this->FileName)
{
vtkErrorMacro(<< "No file specified!");
return 0;
}
this->FileFD = fopen(this->FileName, "r");
if (this->FileFD == NULL)
{
vtkErrorMacro(<< "Unable to open file: "<< this->FileName);
return 0;
}
return 1;
}
//----------------------------------------------------------------------------
vtkPoints* vtkVRMLImporter::PointsNew()
{
vtkPoints* pts = vtkPoints::New();
this->Internal->Heap.Push(pts);
return pts;
}
//----------------------------------------------------------------------------
vtkFloatArray* vtkVRMLImporter::FloatArrayNew()
{
vtkFloatArray* array = vtkFloatArray::New();
this->Internal->Heap.Push(array);
return array;
}
//----------------------------------------------------------------------------
vtkIdTypeArray* vtkVRMLImporter::IdTypeArrayNew()
{
vtkIdTypeArray* array = vtkIdTypeArray::New();
this->Internal->Heap.Push(array);
return array;
}
//----------------------------------------------------------------------------
void vtkVRMLImporter::DeleteObject(vtkObject* obj)
{
for (int i = 0; i < this->Internal->Heap.Count(); i++)
{
if (obj == this->Internal->Heap[i])
{
this->Internal->Heap[i] = 0;
}
}
obj->Delete();
}
//----------------------------------------------------------------------------
int vtkVRMLImporter::ImportBegin()
{
int ret = 1;
try
{
if (this->CurrentTransform)
{
this->CurrentTransform->Delete();
}
this->CurrentTransform = vtkTransform::New();
this->Parser->CurrentProtoStack = 0;
this->Parser->memyyInput_i = 0;
this->Parser->memyyInput_j = 0;
vtkVRMLAllocator::Initialize();
this->Parser->typeList = new vtkVRMLVectorType<VrmlNodeType*>;
this->Parser->typeList->Init();
this->Parser->useList = new vtkVRMLVectorType<vtkVRMLUseStruct*>;
this->Parser->useList->Init();
this->Parser->currentField = new vtkVRMLVectorType<VrmlNodeType::FieldRec*>;
this->Parser->currentField->Init();
if (!this->OpenImportFile())
{
throw std::exception();
}
// This is actually where it all takes place, Since VRML is a SG
// And is state based, I need to create actors, cameras, and lights
// as I go. The Import* routines are not used.
this->Parser->CurrentProtoStack = new vtkVRMLVectorType<VrmlNodeType*>;
// Lets redefine the YY_INPUT macro on Flex and get chars from memory
this->Parser->theyyInput = vtkVRMLYaccData::memyyInput;
// Crank up the yacc parser...
this->Parser->yydebug = 0;
this->Parser->yy_flex_debug = 0;
this->Parser->yyparse(this);
this->Parser->yyin = NULL;
this->Parser->yyResetLineNumber();
// Not sure why I have to do this but its not working when
// When I use the FileFD file pointer...
// File existence already checked.
this->Parser->yyin = fopen(this->FileName, "r");
if (!this->Parser->yyin)
{
throw std::exception();
}
// reset the lex input routine
this->Parser->theyyInput = vtkVRMLYaccData::defyyInput;
// For this little test application, pushing and popping the node
// namespace isn't really necessary. But each VRML .wrl file is
// a separate namespace for PROTOs (except for things predefined
// in the spec), and pushing/popping the namespace when reading each
// file is a good habit to get into:
this->Parser->pushNameSpace();
this->Parser->yyparse(this);
this->Parser->popNameSpace();
}
catch (const std::exception&)
{
vtkErrorMacro(<< "Unable to read VRML file! Error at line " <<
this->Parser->currentLineNumber);
ret = 0;
}
catch (std::string &s)
{
vtkErrorMacro(<< "Unable to read VRML file! Error at line " <<
this->Parser->currentLineNumber <<":" << s);
ret = 0;
}
if (this->Parser->yyin)
{
fclose(this->Parser->yyin);
this->Parser->yyin = NULL;
}
delete this->Parser->CurrentProtoStack;
this->Parser->CurrentProtoStack = NULL;
return ret;
}
//----------------------------------------------------------------------------
void vtkVRMLImporter::ImportEnd()
{
delete this->Parser->typeList;
this->Parser->typeList = NULL;
delete this->Parser->currentField;
this->Parser->currentField = NULL;
vtkDebugMacro(<<"Closing import file");
if (this->FileFD != NULL)
{
fclose(this->FileFD);
this->FileFD = NULL;
}
if (this->CurrentActor)
{
this->CurrentActor->Delete();
this->CurrentActor = NULL;
}
if (this->CurrentLight)
{
this->CurrentLight->Delete();
this->CurrentLight = NULL;
}
if (this->CurrentProperty)
{
this->CurrentProperty->Delete();
this->CurrentProperty = NULL;
}
if (this->CurrentSource)
{
this->CurrentSource->Delete();
this->CurrentSource = NULL;
}
if (this->CurrentPoints)
{
this->CurrentPoints->Delete();
this->CurrentPoints = NULL;
}
if (this->CurrentNormals)
{
this->CurrentNormals->Delete();
this->CurrentNormals = NULL;
}
if (this->CurrentTCoords)
{
this->CurrentTCoords->Delete();
this->CurrentTCoords = NULL;
}
if (this->CurrentTCoordCells)
{
this->CurrentTCoordCells->Delete();
this->CurrentTCoordCells = NULL;
}
if (this->CurrentNormalCells)
{
this->CurrentNormalCells->Delete();
this->CurrentNormalCells = NULL;
}
if (this->CurrentScalars)
{
this->CurrentScalars->Delete();
this->CurrentScalars = NULL;
}
if (this->CurrentMapper)
{
this->CurrentMapper->Delete();
this->CurrentMapper = NULL;
}
if (this->CurrentLut)
{
this->CurrentLut->Delete();
this->CurrentLut = NULL;
}
if (this->CurrentTransform)
{
this->CurrentTransform->Delete();
this->CurrentTransform = NULL;
}
}
//----------------------------------------------------------------------------
// Yacc/lex routines to add stuff to the renderer.
void vtkVRMLImporter::enterNode(const char *nodeType)
{
const VrmlNodeType *t = this->Parser->find(nodeType);
if (t == NULL)
{
std::stringstream str;
str << "Unknown node type " << nodeType;
this->Parser->yyerror(str.str().c_str());
throw str.str();
}
VrmlNodeType::FieldRec *fr = new VrmlNodeType::FieldRec;
fr->nodeType = t;
fr->fieldName = NULL;
*this->Parser->currentField += fr;
std::string nodeTypeName(fr->nodeType->getName());
if (nodeTypeName == "Appearance")
{
if (this->CurrentProperty)
{
this->CurrentProperty->Delete();
}
this->CurrentProperty = vtkProperty::New();
if (this->Parser->creatingDEF)
{
*this->Parser->useList +=
new vtkVRMLUseStruct(this->Parser->curDEFName, this->CurrentProperty);
this->Parser->creatingDEF = 0;
}
}
else if (nodeTypeName == "Box" ||
nodeTypeName == "Cone" ||
nodeTypeName == "Cylinder" ||
nodeTypeName == "Sphere")
{
if (this->CurrentSource)
{
this->CurrentSource->Delete();
}
if (nodeTypeName == "Box")
{
vtkCubeSource *cube = vtkCubeSource::New();
this->CurrentSource = cube;
}
else if (nodeTypeName == "Cone")
{
vtkConeSource *cone = vtkConeSource::New();
cone->SetResolution(this->ShapeResolution);
this->CurrentSource = cone;
}
else if (nodeTypeName == "Cylinder")
{
vtkCylinderSource *cyl = vtkCylinderSource::New();
cyl->SetResolution(this->ShapeResolution);
this->CurrentSource = cyl;
}
else if (nodeTypeName == "Sphere")
{
vtkSphereSource *sphere = vtkSphereSource::New();
sphere->SetPhiResolution(this->ShapeResolution);
sphere->SetThetaResolution(this->ShapeResolution);
this->CurrentSource = sphere;
}
this->CurrentSource->Update();
vtkNew<vtkPolyDataMapper> pmap;
pmap->SetInputConnection(this->CurrentSource->GetOutputPort());
this->CurrentActor->SetMapper(pmap.Get());
if (this->CurrentProperty)
{
this->CurrentActor->SetProperty(this->CurrentProperty);
}
if (this->Parser->creatingDEF)
{
*this->Parser->useList +=
new vtkVRMLUseStruct(this->Parser->curDEFName, pmap.Get());
this->Parser->creatingDEF = 0;
}
}
else if (nodeTypeName == "DirectionalLight")
{
if (this->CurrentLight)
{
this->CurrentLight->Delete();
}
this->CurrentLight = vtkLight::New();
this->Renderer->AddLight(this->CurrentLight);
if (this->Parser->creatingDEF)
{
*this->Parser->useList +=
new vtkVRMLUseStruct(this->Parser->curDEFName, this->CurrentLight);
this->Parser->creatingDEF = 0;
}
}
else if (nodeTypeName == "IndexedFaceSet" ||
nodeTypeName == "IndexedLineSet" ||
nodeTypeName == "PointSet")
{
if (this->CurrentMapper)
{
this->CurrentMapper->Delete();
}
this->CurrentMapper = vtkPolyDataMapper::New();
this->CurrentMapper->SetScalarVisibility(0);
this->CurrentActor->SetMapper(this->CurrentMapper);
if (this->CurrentProperty)
{
this->CurrentActor->SetProperty(this->CurrentProperty);
}
if (this->CurrentScalars)
{
this->CurrentScalars->Delete();
}
this->CurrentScalars = vtkFloatArray::New();
if (this->Parser->creatingDEF)
{
*this->Parser->useList +=
new vtkVRMLUseStruct(this->Parser->curDEFName, this->CurrentMapper);
this->Parser->creatingDEF = 0;
}
}
else if (nodeTypeName == "Shape")
{
if (this->CurrentActor)
{
this->CurrentActor->Delete();
}
this->CurrentActor = vtkActor::New();
if (this->CurrentProperty)
{
this->CurrentActor->SetProperty(this->CurrentProperty);
}
this->CurrentActor->SetOrientation(this->CurrentTransform->GetOrientation());
this->CurrentActor->SetPosition(this->CurrentTransform->GetPosition());
this->CurrentActor->SetScale(this->CurrentTransform->GetScale());
// Add actor to renderer
this->Renderer->AddActor(this->CurrentActor);
if (this->Parser->creatingDEF)
{
*this->Parser->useList +=
new vtkVRMLUseStruct(this->Parser->curDEFName, this->CurrentActor);
this->Parser->creatingDEF = 0;
}
}
else if (nodeTypeName == "Transform")
{
this->CurrentTransform->Push();
}
}
//----------------------------------------------------------------------------
void vtkVRMLImporter::exitNode()
{
VrmlNodeType::FieldRec *fr = this->Parser->currentField->Top();
assert(fr != NULL);
this->Parser->currentField->Pop();
std::string nodeTypeName = fr->nodeType->getName();
// Exiting this means we need to setup the color mode and
// normals and other fun stuff.
if (nodeTypeName == "IndexedFaceSet" ||
nodeTypeName == "IndexedLineSet" ||
nodeTypeName == "PointSet")
{
// if tcoords exactly correspond with vertices (or there aren't any)
// then can map straight through as usual
// if not then must rejig using face-correspondence
// (VRML supports per-face tcoords)
// a similar scheme is implemented in vtkOBJReader
bool tcoordsCorrespond;
if ((!this->CurrentTCoords || !this->CurrentTCoordCells) &&
(!this->CurrentNormals || !this->CurrentNormalCells))
{
tcoordsCorrespond = true; // there aren't any, can proceed
}
else if (this->CurrentTCoords &&
this->CurrentTCoords->GetNumberOfTuples() !=
this->CurrentPoints->GetNumberOfPoints())
{
tcoordsCorrespond = false; // false, must rejig
}
else if (this->CurrentNormals &&
this->CurrentNormals->GetNumberOfTuples() !=
this->CurrentPoints->GetNumberOfPoints())
{
tcoordsCorrespond = false; // false, must rejig
}
else
{
// the number of polygon faces and texture faces must be equal.
// if they are not then something is wrong
if (this->CurrentTCoordCells &&
this->CurrentTCoordCells->GetNumberOfCells() !=
this->CurrentMapper->GetInput()->GetPolys()->GetNumberOfCells())
{
vtkErrorMacro(
<<"Number of faces does not match texture faces, output may not be correct")
tcoordsCorrespond = true; // don't rejig
}
else if (this->CurrentNormalCells &&
this->CurrentNormalCells->GetNumberOfCells() !=
this->CurrentMapper->GetInput()->GetPolys()->GetNumberOfCells())
{
vtkErrorMacro(
<<"Number of faces does not match normal faces, output may not be correct")
tcoordsCorrespond = true; // don't rejig
}
else
{
// count of tcoords and points is the same, must run through indices to see if they
// correspond by index point-for-point
tcoordsCorrespond = true; // assume true until found otherwise
if (this->CurrentTCoords && this->CurrentTCoordCells)
{
vtkCellArray* polys = this->CurrentMapper->GetInput()->GetPolys();
polys->InitTraversal();
this->CurrentTCoordCells->InitTraversal();
vtkIdType npts, *pts;
while (polys->GetNextCell(npts, pts))
{
vtkIdType nTCoordPts, *tcoordPts;
this->CurrentTCoordCells->GetNextCell(nTCoordPts, tcoordPts);
if (npts != nTCoordPts)
{
vtkErrorMacro(
<<"Face size differs to texture face size, output may not be correct")
break;
}
for (vtkIdType j = 0; j < npts; j++)
{
if (pts[j] != tcoordPts[j])
{
tcoordsCorrespond = false; // have found an exception
break;
}
}
}
}
if (this->CurrentNormals && this->CurrentNormalCells)
{
vtkCellArray* polys = this->CurrentMapper->GetInput()->GetPolys();
polys->InitTraversal();
this->CurrentNormalCells->InitTraversal();
vtkIdType npts, *pts;
while (polys->GetNextCell(npts, pts))
{
vtkIdType nNormalPts, *normalPts;
this->CurrentNormalCells->GetNextCell(nNormalPts, normalPts);
if (npts != nNormalPts)
{
vtkErrorMacro(
<<"Face size differs to normal face size, output may not be correct")
break;
}
for (vtkIdType j = 0; j < npts; j++)
{
if (pts[j] != normalPts[j])
{
tcoordsCorrespond = false; // have found an exception
break;
}
}
}
}
}
}
if (tcoordsCorrespond) // no rejigging necessary
{
vtkPolyData *pd = this->CurrentMapper->GetInput();
if (pd == NULL)
{
pd = vtkPolyData::New();
this->CurrentMapper->SetInputData(pd);
pd->Delete();
}
pd->SetPoints(this->CurrentPoints);
// We always create a scalar object in the enternode method.
pd->GetPointData()->SetScalars(CurrentScalars);
if (this->CurrentNormals)
{
pd->GetPointData()->SetNormals(CurrentNormals);
this->CurrentNormals->Delete();
this->CurrentNormals = NULL;
}
if (this->CurrentTCoords)
{
pd->GetPointData()->SetTCoords(CurrentTCoords);
this->CurrentTCoords->Delete();
this->CurrentTCoords = NULL;
}
}
else // must rejig
{
vtkDebugMacro(
<<"Duplicating vertices so that tcoords and normals are correct");
vtkNew<vtkPoints> newPoints;
vtkNew<vtkFloatArray> newScalars;
if (this->CurrentScalars)
{
newScalars->SetNumberOfComponents(
this->CurrentScalars->GetNumberOfComponents());
}
vtkNew<vtkFloatArray> newTCoords;
newTCoords->SetNumberOfComponents(2);
vtkNew<vtkFloatArray> newNormals;
newNormals->SetNumberOfComponents(3);
vtkNew<vtkCellArray> newPolys;
// for each poly, copy its vertices into newPoints (and point at them)
// also copy its tcoords into newTCoords
// also copy its normals into newNormals
// also copy its scalar into newScalars
vtkPolyData *pd = this->CurrentMapper->GetInput();
vtkCellArray *polys = pd->GetPolys();
polys->InitTraversal();
if (this->CurrentTCoordCells)
{
this->CurrentTCoordCells->InitTraversal();
}
if (this->CurrentNormalCells)
{
this->CurrentNormalCells->InitTraversal();
}
vtkIdType npts, *pts;
for (vtkIdType i = 0; polys->GetNextCell(npts, pts); i++)
{
vtkIdType n_tcoord_pts = 0, *tcoord_pts = 0;
if (this->CurrentTCoordCells)
{
this->CurrentTCoordCells->GetNextCell(n_tcoord_pts, tcoord_pts);
}
vtkIdType n_normal_pts = 0, *normal_pts = 0;
if (this->CurrentNormalCells)
{
this->CurrentNormalCells->GetNextCell(n_normal_pts, normal_pts);
}
// If some vertices have tcoords and not others
// then we must do something else VTK will complain.
// (crash on render attempt)
// Easiest solution is to delete polys that don't have complete tcoords
// (if there are any tcoords in the dataset)
if (this->CurrentTCoords && npts != n_tcoord_pts &&
this->CurrentTCoords->GetNumberOfTuples() > 0)
{
// skip this poly
vtkDebugMacro(<<"Skipping poly "<< i + 1 <<" (1-based index)");
}
else if (this->CurrentNormals && npts != n_normal_pts &&
this->CurrentNormals->GetNumberOfTuples() > 0)
{
// skip this poly
vtkDebugMacro(<<"Skipping poly "<< i + 1<< " (1-based index)");
}
else
{
// copy the corresponding points, tcoords and normals across
for (vtkIdType j = 0; j < npts; j++)
{
// copy the tcoord for this point across (if there is one)
if (this->CurrentTCoords && n_tcoord_pts > 0)
{
newTCoords->InsertNextTuple(
this->CurrentTCoords->GetTuple(tcoord_pts[j]));
}
// copy the normal for this point across (if any)
if (this->CurrentNormals && n_normal_pts > 0)
{
newNormals->InsertNextTuple(
this->CurrentNormals->GetTuple(normal_pts[j]));
}
// copy the scalar for this point across
if (this->CurrentScalars)
{
newScalars->InsertNextTuple(
this->CurrentScalars->GetTuple(pts[j]));
}
// copy the vertex into the new structure and update
// the vertex index in the polys structure (pts is a pointer into it)
pts[j] = newPoints->InsertNextPoint(
this->CurrentPoints->GetPoint(pts[j]));
}
// copy this poly (pointing at the new points) into the new polys list
newPolys->InsertNextCell(npts, pts);
}
}
// use the new structures for the output
pd->SetPoints(newPoints.Get());
pd->SetPolys(newPolys.Get());
if (this->CurrentTCoords)
{
pd->GetPointData()->SetTCoords(newTCoords.Get());
}
if (this->CurrentNormals)
{
pd->GetPointData()->SetNormals(newNormals.Get());
}
if (this->CurrentScalars)
{
pd->GetPointData()->SetScalars(newScalars.Get());
}
}
if (this->CurrentLut)
{
this->CurrentScalars->InsertNextValue(this->CurrentLut->GetNumberOfColors());
this->CurrentMapper->SetLookupTable(CurrentLut);
this->CurrentMapper->SetScalarVisibility(1);
// set for per vertex coloring
this->CurrentLut->SetTableRange(0.0,
float(this->CurrentLut->GetNumberOfColors() - 1));
this->CurrentLut->Delete();
this->CurrentLut = NULL;
}
}
else if (nodeTypeName == "Shape")
{
if (this->CurrentProperty)
{
this->CurrentActor->SetProperty(this->CurrentProperty);
this->CurrentProperty->Delete();
this->CurrentProperty = 0;
}
}
// simply pop the current transform
else if (nodeTypeName == "Transform")
{
this->CurrentTransform->Pop();
}
delete fr;
}
//----------------------------------------------------------------------------
void vtkVRMLImporter::enterField(const char *fieldName)
{
VrmlNodeType::FieldRec *fr = this->Parser->currentField->Top();
assert(fr != NULL);
fr->fieldName = fieldName;
if (fr->nodeType != NULL)
{
// enterField is called when parsing eventIn and eventOut IS
// declarations, in which case we don't need to do anything special--
// the IS IDENTIFIER will be returned from the lexer normally.
if (fr->nodeType->hasEventIn(fieldName) ||
fr->nodeType->hasEventOut(fieldName))
{
return;
}
int type = fr->nodeType->hasField(fieldName);
if (type != 0)
{
// Let the lexer know what field type to expect:
this->Parser->expect(type);
}
else
{
vtkErrorMacro(<< "Error: Node's of type " << fr->nodeType->getName() <<
" do not have fields/eventIn/eventOut named " <<
fieldName);
// expect(ANY_FIELD);
}
}
// else expect(ANY_FIELD);
}
//----------------------------------------------------------------------------
void vtkVRMLImporter::exitField()
{
VrmlNodeType::FieldRec *fr = this->Parser->currentField->Top();
assert(fr != NULL);
std::string fieldName(fr->fieldName);
std::string nodeTypeName(fr->nodeType->getName());
// For the radius field
if (fieldName == "radius")
{
// Set the Sphere radius
if (nodeTypeName == "Sphere")
{
static_cast<vtkSphereSource*>(this->CurrentSource)->
SetRadius(this->Parser->yylval.sffloat);
}
// Set the Cylinder radius
else if (nodeTypeName == "Cylinder")
{
static_cast<vtkCylinderSource*>(this->CurrentSource)->
SetRadius(this->Parser->yylval.sffloat);
}
}
// For the ambientIntensity field
else if (fieldName == "ambientIntensity")
{
// Add to the current light
if (nodeTypeName == "DirectionalLight")
{
this->CurrentLight->SetIntensity(this->Parser->yylval.sffloat);
}
// or the current material
else if (nodeTypeName == "Material")
{
this->CurrentProperty->SetAmbient(this->Parser->yylval.sffloat);
}
}
// For diffuseColor field, only in material node
else if (fieldName == "diffuseColor")
{
this->CurrentProperty->SetDiffuseColor(
this->Parser->yylval.vec3f->GetPoint(0));
this->Parser->yylval.vec3f->Reset();
this->DeleteObject(this->Parser->yylval.vec3f);
this->Parser->yylval.vec3f = NULL;
}
// For emissiveColor field, only in material node
else if (fieldName == "emissiveColor")
{
this->CurrentProperty->SetAmbientColor(
this->Parser->yylval.vec3f->GetPoint(0));
this->Parser->yylval.vec3f->Reset();
this->DeleteObject(this->Parser->yylval.vec3f);
this->Parser->yylval.vec3f = NULL;
}
// For shininess field, only in material node
else if (fieldName == "shininess")
{
this->CurrentProperty->SetSpecularPower(this->Parser->yylval.sffloat);
}
// For specularcolor field, only in material node
else if (fieldName == "specularColor")
{
this->CurrentProperty->SetSpecularColor(
this->Parser->yylval.vec3f->GetPoint(0));
this->Parser->yylval.vec3f->Reset();
this->DeleteObject(this->Parser->yylval.vec3f);
this->Parser->yylval.vec3f = NULL;
}
// For transparency field, only in material node
else if (fieldName == "transparency")
{
this->CurrentProperty->SetOpacity(1.0 - this->Parser->yylval.sffloat);
}
// For the translation field of the Transform node
else if (fieldName == "translation" && nodeTypeName == "Transform")
{
this->CurrentTransform->Translate(this->Parser->yylval.vec3f->GetPoint(0));
this->Parser->yylval.vec3f->Reset();
this->DeleteObject(this->Parser->yylval.vec3f);
this->Parser->yylval.vec3f = NULL;
}
// For the scale field of the transform node
else if (fieldName == "scale" && nodeTypeName == "Transform")
{
this->CurrentTransform->Scale(this->Parser->yylval.vec3f->GetPoint(0));
this->Parser->yylval.vec3f->Reset();
this->DeleteObject(this->Parser->yylval.vec3f);
this->Parser->yylval.vec3f = NULL;
}
// For the size field of the current cube source
else if (fieldName == "size" && nodeTypeName == "Box")
{
vtkCubeSource* cube = static_cast<vtkCubeSource*>(this->CurrentSource);
double *len = this->Parser->yylval.vec3f->GetPoint(0);
cube->SetXLength(len[0]);
cube->SetYLength(len[1]);
cube->SetZLength(len[2]);
this->Parser->yylval.vec3f->Reset();
this->DeleteObject(this->Parser->yylval.vec3f);
this->Parser->yylval.vec3f = NULL;
}
// For the height field
else if (fieldName == "height")
{
// Set the current Cone height
if (nodeTypeName == "Cone")
{
static_cast<vtkConeSource*>(this->CurrentSource)->
SetHeight(this->Parser->yylval.sffloat);
}
// or set the current Cylinder height
else if (nodeTypeName == "Cylinder")
{
static_cast<vtkCylinderSource*>(this->CurrentSource)->
SetHeight(this->Parser->yylval.sffloat);
}
}
// For the bottomRadius field (only for Cone shapes)
else if (fieldName == "bottomRadius" && nodeTypeName == "Cone")
{
static_cast<vtkConeSource*>(this->CurrentSource)->
SetRadius(this->Parser->yylval.sffloat);
}
// Handle coordIndex for Indexed*Sets
else if (fieldName == "coordIndex")
{
vtkNew<vtkPolyData> pd;
vtkNew<vtkCellArray> cells;
vtkIdType index = 0, cnt = 0;
vtkIdType nbPoints = this->Parser->yylval.mfint32->GetMaxId();
for (vtkIdType i = 0; i <= nbPoints; i++)
{
if (this->Parser->yylval.mfint32->GetValue(i) == -1)
{
cells->InsertNextCell(cnt, this->Parser->yylval.mfint32->GetPointer(index));
index = i + 1;
cnt = 0;