forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkJSONSceneExporter.cxx
1035 lines (900 loc) · 33.6 KB
/
vtkJSONSceneExporter.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: VisualizationJSONlkit
Module: vtkJSONSceneExporter.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 "vtkJSONSceneExporter.h"
#include "vtkAbstractVolumeMapper.h"
#include "vtkArchiver.h"
#include "vtkCamera.h"
#include "vtkCompositeDataIterator.h"
#include "vtkCompositeDataSet.h"
#include "vtkDataObject.h"
#include "vtkDataSet.h"
#include "vtkDiscretizableColorTransferFunction.h"
#include "vtkExporter.h"
#include "vtkImageData.h"
#include "vtkImageResize.h"
#include "vtkJPEGWriter.h"
#include "vtkJSONDataSetWriter.h"
#include "vtkMapper.h"
#include "vtkMolecule.h"
#include "vtkMoleculeMapper.h"
#include "vtkMoleculeToAtomBallFilter.h"
#include "vtkMoleculeToBondStickFilter.h"
#include "vtkNew.h"
#include "vtkObjectFactory.h"
#include "vtkPiecewiseFunction.h"
#include "vtkPolyDataNormals.h"
#include "vtkProp.h"
#include "vtkPropCollection.h"
#include "vtkProperty.h"
#include "vtkQuadricClustering.h"
#include "vtkRenderWindow.h"
#include "vtkRenderer.h"
#include "vtkRendererCollection.h"
#include "vtkScalarsToColors.h"
#include "vtkTexture.h"
#include "vtkVolume.h"
#include "vtkVolumeCollection.h"
#include "vtkVolumeProperty.h"
#include "vtksys/FStream.hxx"
#include "vtksys/SystemTools.hxx"
#include <fstream>
#include <sstream>
#include <string>
vtkStandardNewMacro(vtkJSONSceneExporter);
//------------------------------------------------------------------------------
vtkJSONSceneExporter::vtkJSONSceneExporter()
{
this->FileName = nullptr;
this->WriteTextures = false;
this->WriteTextureLODs = false;
this->TextureLODsBaseSize = 100000;
this->TextureLODsBaseUrl = nullptr;
this->WritePolyLODs = false;
this->PolyLODsBaseSize = 100000;
this->PolyLODsBaseUrl = nullptr;
}
//------------------------------------------------------------------------------
vtkJSONSceneExporter::~vtkJSONSceneExporter()
{
this->SetFileName(nullptr);
this->SetTextureLODsBaseUrl(nullptr);
this->SetPolyLODsBaseUrl(nullptr);
}
//------------------------------------------------------------------------------
void vtkJSONSceneExporter::WriteDataObject(
ostream& os, vtkDataObject* dataObject, vtkActor* actor = nullptr, vtkVolume* volume = nullptr)
{
// Skip if nothing to process
if (dataObject == nullptr)
{
return;
}
// Handle Dataset
if (dataObject->IsA("vtkDataSet"))
{
std::string texturesString;
std::string renderingSetup;
if (actor)
{
if (this->WriteTextures && actor->GetTexture())
{
// Write out the textures, add it to the textures string
texturesString += this->WriteTexture(actor->GetTexture());
}
if (this->WriteTextureLODs && actor->GetTexture())
{
// Write out the texture LODs, add it to the textures string
texturesString += this->WriteTextureLODSeries(actor->GetTexture());
}
renderingSetup = this->ExtractActorRenderingSetup(actor);
}
else if (volume)
{
renderingSetup = this->ExtractVolumeRenderingSetup(volume);
}
std::string addOnMeta = renderingSetup + texturesString + "\n";
std::string dsMeta =
this->WriteDataSet(vtkDataSet::SafeDownCast(dataObject), addOnMeta.c_str());
if (!dsMeta.empty())
{
os << dsMeta;
}
return;
}
// Handle composite
if (dataObject->IsA("vtkCompositeDataSet"))
{
vtkCompositeDataSet* composite = vtkCompositeDataSet::SafeDownCast(dataObject);
vtkSmartPointer<vtkCompositeDataIterator> iter;
iter.TakeReference(composite->NewIterator());
iter->SkipEmptyNodesOn();
iter->InitTraversal();
while (!iter->IsDoneWithTraversal())
{
this->WriteDataObject(os, iter->GetCurrentDataObject(), actor, volume);
iter->GoToNextItem();
}
return;
}
// Handle molecule
if (dataObject->IsA("vtkMolecule"))
{
vtkMolecule* molecule = vtkMolecule::SafeDownCast(dataObject);
// Create tubes for each bond
vtkNew<vtkMoleculeToBondStickFilter> stickFilter;
stickFilter->SetInputDataObject(molecule);
stickFilter->Update();
// Create spheres for each atom
vtkNew<vtkMoleculeToAtomBallFilter> ballFilter;
ballFilter->SetInputDataObject(molecule);
if (actor)
{
// Retrieve radius type and scale factor from mapper
vtkMoleculeMapper* mapper = vtkMoleculeMapper::SafeDownCast(actor->GetMapper());
switch (mapper->GetAtomicRadiusType())
{
case vtkMoleculeMapper::CovalentRadius:
{
ballFilter->SetRadiusSource(vtkMoleculeToAtomBallFilter::CovalentRadius);
break;
}
case vtkMoleculeMapper::VDWRadius:
{
ballFilter->SetRadiusSource(vtkMoleculeToAtomBallFilter::VDWRadius);
break;
}
case vtkMoleculeMapper::UnitRadius:
{
ballFilter->SetRadiusSource(vtkMoleculeToAtomBallFilter::UnitRadius);
break;
}
default:
{
// Default to Van Der Waals
ballFilter->SetRadiusSource(vtkMoleculeToAtomBallFilter::VDWRadius);
break;
}
}
ballFilter->SetRadiusScale(mapper->GetAtomicRadiusScaleFactor());
}
else
{
// Set default radius type and scale
ballFilter->SetRadiusSource(vtkMoleculeToAtomBallFilter::VDWRadius);
ballFilter->SetRadiusScale(0.3);
}
// Reduce resolution when the number of atoms is high
// The threshold value has been arbitrarily chosen
if (molecule->GetNumberOfAtoms() > 100)
{
ballFilter->SetResolution(20);
}
// Create vertex normals for a smoother appearance
vtkNew<vtkPolyDataNormals> normalFilter;
normalFilter->SetInputConnection(ballFilter->GetOutputPort());
normalFilter->Update();
// Write tubes and spheres
this->WriteDataObject(os, stickFilter->GetOutput(), actor, volume);
this->WriteDataObject(os, normalFilter->GetOutput(), actor, volume);
}
return;
}
//------------------------------------------------------------------------------
std::string vtkJSONSceneExporter::ExtractColorTransferFunctionSetup(
vtkColorTransferFunction* function)
{
std::stringstream configuration;
bool useAboveRangeColor = function->GetUseAboveRangeColor();
bool useBelowRangeColor = function->GetUseBelowRangeColor();
int colorSpace = function->GetColorSpace();
double aboveRangeColor[3] = { 0 };
double belowRangeColor[3] = { 0 };
double nanColor[3] = { 0 };
function->GetAboveRangeColor(aboveRangeColor);
function->GetBelowRangeColor(belowRangeColor);
function->GetNanColor(nanColor);
vtkIdType numberOfNodes = function->GetSize();
constexpr const char* INDENT = " ";
configuration << INDENT << " \"useAboveRangeColor\": " << (useAboveRangeColor ? "true" : "false")
<< ",\n"
<< INDENT << " \"useBelowRangeColor\": " << (useBelowRangeColor ? "true" : "false")
<< ",\n"
<< INDENT << " \"colorSpace\": " << colorSpace << ",\n";
if (useAboveRangeColor)
{
configuration << INDENT << " \"aboveRangeColor\": [" << aboveRangeColor[0] << ", "
<< aboveRangeColor[1] << ", " << aboveRangeColor[2] << "],\n";
}
if (useBelowRangeColor)
{
configuration << INDENT << " \"belowRangeColor\": [" << belowRangeColor[0] << ", "
<< belowRangeColor[1] << ", " << belowRangeColor[2] << "],\n";
}
configuration << INDENT << " \"nanColor\": [" << nanColor[0] << ", " << nanColor[1] << ", "
<< nanColor[2] << "],\n";
configuration << INDENT << " \"nodes\": [\n";
for (vtkIdType nodeId = 0; nodeId < numberOfNodes; ++nodeId)
{
double node[6];
function->GetNodeValue(nodeId, node);
configuration << INDENT << " [";
for (int i = 0; i < 6; ++i)
{
configuration << node[i] << (i == 5 ? "]" : ", ");
}
if (nodeId < numberOfNodes - 1)
{
configuration << ",";
}
configuration << "\n";
}
configuration << INDENT << " ]\n";
return configuration.str();
}
//------------------------------------------------------------------------------
std::string vtkJSONSceneExporter::ExtractPiecewiseFunctionSetup(vtkPiecewiseFunction* function)
{
bool clamping = function->GetClamping();
vtkIdType numberOfPoints = function->GetSize();
constexpr const char* INDENT = " ";
std::stringstream configuration;
configuration << INDENT << " \"clamping\": " << (clamping ? "true" : "false") << ",\n";
configuration << INDENT << " \"points\": [\n";
for (vtkIdType pointId = 0; pointId < numberOfPoints; ++pointId)
{
double point[4];
function->GetNodeValue(pointId, point);
configuration << INDENT << " [";
for (int i = 0; i < 4; ++i)
{
configuration << point[i] << (i < 3 ? ", " : "");
}
configuration << "]";
if (pointId < numberOfPoints - 1)
{
configuration << ",";
}
configuration << "\n";
}
configuration << INDENT << " ]\n";
return configuration.str();
}
//------------------------------------------------------------------------------
std::string vtkJSONSceneExporter::ExtractVolumeRenderingSetup(vtkVolume* volume)
{
vtkVolumeProperty* property = volume->GetProperty();
double* p3dPosition = volume->GetPosition();
double* p3dScale = volume->GetScale();
double* p3dOrigin = volume->GetOrigin();
double* p3dRotateWXYZ = volume->GetOrientationWXYZ();
int interpolationType = property->GetInterpolationType();
vtkTypeBool independentComponents = property->GetIndependentComponents();
int shade = property->GetShade();
double ambient = property->GetAmbient();
double diffuse = property->GetDiffuse();
double specular = property->GetSpecular();
double specularPower = property->GetSpecularPower();
constexpr const char* INDENT = " ";
std::stringstream renderingConfig;
renderingConfig << ",\n"
<< "\"volume\": {\n"
<< INDENT << " \"origin\": [" << p3dOrigin[0] << ", " << p3dOrigin[1] << ", "
<< p3dOrigin[2] << "],\n"
<< INDENT << " \"scale\": [" << p3dScale[0] << ", " << p3dScale[1] << ", "
<< p3dScale[2] << "],\n"
<< INDENT << " \"position\": [" << p3dPosition[0] << ", " << p3dPosition[1]
<< ", " << p3dPosition[2] << "]\n"
<< INDENT << "},\n"
<< INDENT << "\"volumeRotation\": [" << p3dRotateWXYZ[0] << ", "
<< p3dRotateWXYZ[1] << ", " << p3dRotateWXYZ[2] << ", " << p3dRotateWXYZ[3]
<< "],\n"
<< INDENT << "\"mapper\": {},\n"
<< INDENT << "\"property\": {\n"
<< INDENT << " \"interpolationType\": " << interpolationType << ",\n"
<< INDENT
<< " \"independentComponents\": " << (independentComponents ? "true" : "false")
<< ",\n"
<< INDENT << " \"ambient\": " << ambient << ",\n"
<< INDENT << " \"diffuse\": " << diffuse << ",\n"
<< INDENT << " \"specular\": " << specular << ",\n"
<< INDENT << " \"specularPower\": " << specularPower << ",\n"
<< INDENT << " \"shade\": " << shade << ",\n"
<< INDENT << " \"components\": [\n";
for (vtkIdType component = 0; component < VTK_MAX_VRCOMP; ++component)
{
renderingConfig << INDENT << " {\n";
int colorChannels = property->GetColorChannels(component);
renderingConfig << INDENT << " \"colorChannels\": " << colorChannels << ",\n";
if (colorChannels == 3)
{
renderingConfig << INDENT << " \"rgbTransferFunction\":\n"
<< INDENT << " {\n"
<< this->ExtractColorTransferFunctionSetup(
property->GetRGBTransferFunction(component))
<< INDENT << " },\n";
}
else if (colorChannels == 1)
{
renderingConfig << INDENT << " \"grayTransferFunction\":\n"
<< INDENT << " {\n"
<< this->ExtractPiecewiseFunctionSetup(
property->GetGrayTransferFunction(component))
<< INDENT << " },\n";
}
renderingConfig << INDENT << " \"scalarOpacity\":\n"
<< INDENT << " {\n"
<< this->ExtractPiecewiseFunctionSetup(property->GetScalarOpacity(component))
<< INDENT << " },\n";
double scalarOpacityUnitDistance = property->GetScalarOpacityUnitDistance(component);
renderingConfig << INDENT << " \"scalarOpacityUnitDistance\": " << scalarOpacityUnitDistance
<< "\n"
<< INDENT << " }";
if (component < VTK_MAX_VRCOMP - 1)
{
renderingConfig << ",";
}
renderingConfig << "\n";
}
renderingConfig << INDENT << " ]\n" << INDENT << "}\n";
return renderingConfig.str();
}
std::string vtkJSONSceneExporter::ExtractActorRenderingSetup(vtkActor* actor)
{
vtkMapper* mapper = actor->GetMapper();
// int scalarVisibility = mapper->GetScalarVisibility();
const char* colorArrayName = mapper->GetArrayName();
int colorMode = mapper->GetColorMode();
int scalarMode = mapper->GetScalarMode();
vtkProperty* property = actor->GetProperty();
int representation = property->GetRepresentation();
double* colorToUse = property->GetDiffuseColor();
if (representation == 1)
{
colorToUse = property->GetColor();
}
int pointSize = property->GetPointSize();
float opacity = property->GetOpacity();
int edgeVisibility = property->GetEdgeVisibility();
double* p3dPosition = actor->GetPosition();
double* p3dScale = actor->GetScale();
double* p3dOrigin = actor->GetOrigin();
double* p3dRotateWXYZ = actor->GetOrientationWXYZ();
constexpr const char* INDENT = " ";
std::stringstream renderingConfig;
renderingConfig << ",\n"
<< INDENT << "\"actor\": {\n"
<< INDENT << " \"origin\": [" << p3dOrigin[0] << ", " << p3dOrigin[1] << ", "
<< p3dOrigin[2] << "],\n"
<< INDENT << " \"scale\": [" << p3dScale[0] << ", " << p3dScale[1] << ", "
<< p3dScale[2] << "],\n"
<< INDENT << " \"position\": [" << p3dPosition[0] << ", " << p3dPosition[1]
<< ", " << p3dPosition[2] << "]\n"
<< INDENT << "},\n"
<< INDENT << "\"actorRotation\": [" << p3dRotateWXYZ[0] << ", "
<< p3dRotateWXYZ[1] << ", " << p3dRotateWXYZ[2] << ", " << p3dRotateWXYZ[3]
<< "],\n"
<< INDENT << "\"mapper\": {\n"
<< INDENT << " \"colorByArrayName\": \"" << colorArrayName << "\",\n"
<< INDENT << " \"colorMode\": " << colorMode << ",\n"
<< INDENT << " \"scalarMode\": " << scalarMode << "\n"
<< INDENT << "},\n"
<< INDENT << "\"property\": {\n"
<< INDENT << " \"representation\": " << representation << ",\n"
<< INDENT << " \"edgeVisibility\": " << edgeVisibility << ",\n"
<< INDENT << " \"diffuseColor\": [" << colorToUse[0] << ", " << colorToUse[1]
<< ", " << colorToUse[2] << "],\n"
<< INDENT << " \"pointSize\": " << pointSize << ",\n"
<< INDENT << " \"opacity\": " << opacity << "\n"
<< INDENT << "}";
return renderingConfig.str();
}
//------------------------------------------------------------------------------
std::string vtkJSONSceneExporter::CurrentDataSetPath() const
{
std::stringstream path;
path << this->FileName << "/" << this->DatasetCount + 1;
return vtksys::SystemTools::ConvertToOutputPath(path.str());
}
//------------------------------------------------------------------------------
std::string vtkJSONSceneExporter::WriteDataSet(vtkDataSet* dataset, const char* addOnMeta = nullptr)
{
if (!dataset)
{
return "";
}
std::string dsPath = this->CurrentDataSetPath();
++this->DatasetCount;
auto* polyData = vtkPolyData::SafeDownCast(dataset);
vtkSmartPointer<vtkPolyData> newDataset;
std::string polyLODsConfig;
if (this->WritePolyLODs && polyData)
{
newDataset = this->WritePolyLODSeries(polyData, polyLODsConfig);
// Write the smallest poly LOD to the vtkjs file
dataset = newDataset.Get();
}
vtkNew<vtkJSONDataSetWriter> dsWriter;
dsWriter->SetInputData(dataset);
dsWriter->GetArchiver()->SetArchiveName(dsPath.c_str());
dsWriter->Write();
if (!dsWriter->IsDataSetValid())
{
this->DatasetCount--;
return "";
}
std::stringstream meta;
if (this->DatasetCount > 1)
{
meta << ",\n";
}
else
{
meta << "\n";
}
constexpr const char* INDENT = " ";
meta << INDENT << "{\n"
<< INDENT << " \"name\": \"" << this->DatasetCount << "\",\n"
<< INDENT << " \"type\": \"vtkHttpDataSetReader\",\n"
<< INDENT << " \"vtkHttpDataSetReader\": { \"url\": \"" << this->DatasetCount << "\" }";
if (addOnMeta != nullptr)
{
meta << addOnMeta;
}
meta << polyLODsConfig;
meta << INDENT << "}";
return meta.str();
}
//------------------------------------------------------------------------------
void vtkJSONSceneExporter::WriteLookupTable(const char* name, vtkScalarsToColors* lookupTable)
{
if (lookupTable == nullptr)
{
return;
}
vtkDiscretizableColorTransferFunction* dctfn =
vtkDiscretizableColorTransferFunction::SafeDownCast(lookupTable);
if (dctfn != nullptr)
{
constexpr const char* INDENT = " ";
std::stringstream lutJSON;
lutJSON << "{\n"
<< INDENT << " \"clamping\": " << (dctfn->GetClamping() ? "true" : "false") << ",\n"
<< INDENT << " \"colorSpace\": " << dctfn->GetColorSpace() << ",\n"
<< INDENT << " \"hSVWrap\": " << (dctfn->GetHSVWrap() ? "true" : "false") << ",\n"
<< INDENT << " \"alpha\": " << dctfn->GetAlpha() << ",\n"
<< INDENT << " \"vectorComponent\": " << dctfn->GetVectorComponent() << ",\n"
<< INDENT << " \"vectorSize\": " << dctfn->GetVectorSize() << ",\n"
<< INDENT << " \"vectorMode\": " << dctfn->GetVectorMode() << ",\n"
<< INDENT << " \"indexedLookup\": " << dctfn->GetIndexedLookup() << ",\n"
<< INDENT << " \"nodes\": [";
// Fill nodes
vtkIdType nbNodes = dctfn->GetSize();
double node[6];
for (vtkIdType i = 0; i < nbNodes; i++)
{
dctfn->GetNodeValue(i, node);
if (i > 0)
{
lutJSON << ",";
}
lutJSON << "\n"
<< INDENT << INDENT << "[" << node[0] << ", " << node[1] << ", " << node[2] << ", "
<< node[3] << ", " << node[4] << ", " << node[5] << "]";
}
// Close node list
lutJSON << "\n ]\n }";
// Store in map...
this->LookupTables[name] = lutJSON.str();
}
}
//------------------------------------------------------------------------------
void vtkJSONSceneExporter::WritePropCollection(
vtkPropCollection* props, std::ostream& sceneComponents)
{
vtkIdType nbProps = props->GetNumberOfItems();
for (vtkIdType rpIdx = 0; rpIdx < nbProps; rpIdx++)
{
vtkProp* prop = vtkProp::SafeDownCast(props->GetItemAsObject(rpIdx));
// Skip non-visible actors
if (!prop || !prop->GetVisibility())
{
continue;
}
// Skip actors with no geometry
vtkActor* actor = vtkActor::SafeDownCast(prop);
if (actor)
{
vtkMapper* mapper = actor->GetMapper();
vtkDataObject* dataObject = mapper->GetInputDataObject(0, 0);
this->WriteDataObject(sceneComponents, dataObject, actor);
this->WriteLookupTable(mapper->GetArrayName(), mapper->GetLookupTable());
}
}
}
//------------------------------------------------------------------------------
void vtkJSONSceneExporter::WriteVolumeCollection(
vtkVolumeCollection* volumes, std::ostream& sceneComponents)
{
vtkVolume* volume = nullptr;
volumes->InitTraversal();
while ((volume = volumes->GetNextVolume()))
{
// Skip non-visible actors
if (!volume || !volume->GetVisibility())
{
continue;
}
vtkAbstractVolumeMapper* mapper = volume->GetMapper();
vtkDataObject* dataObject = mapper->GetInputDataObject(0, 0);
this->WriteDataObject(sceneComponents, dataObject, nullptr, volume);
}
}
//------------------------------------------------------------------------------
void vtkJSONSceneExporter::WriteData()
{
this->DatasetCount = 0;
this->TextureStrings.clear();
this->TextureLODStrings.clear();
this->FilesToZip.clear();
// make sure the user specified a FileName or FilePointer
if (this->FileName == nullptr)
{
vtkErrorMacro(<< "Please specify FileName to use");
return;
}
if (!vtksys::SystemTools::MakeDirectory(this->FileName))
{
vtkErrorMacro(<< "Can not create directory " << this->FileName);
return;
}
vtkRenderer* renderer = this->GetActiveRenderer();
if (!renderer)
{
renderer = this->RenderWindow->GetRenderers()->GetFirstRenderer();
}
vtkCamera* cam = renderer->GetActiveCamera();
std::stringstream sceneComponents;
vtkPropCollection* renProps = renderer->GetViewProps();
this->WritePropCollection(renProps, sceneComponents);
this->WriteVolumeCollection(renderer->GetVolumes(), sceneComponents);
std::stringstream sceneJsonFile;
sceneJsonFile << "{\n"
<< " \"version\": 1.0,\n"
<< " \"background\": [" << renderer->GetBackground()[0] << ", "
<< renderer->GetBackground()[1] << ", " << renderer->GetBackground()[2] << "],\n"
<< " \"camera\": {\n"
<< " \"focalPoint\": [" << cam->GetFocalPoint()[0] << ", "
<< cam->GetFocalPoint()[1] << ", " << cam->GetFocalPoint()[2] << "],\n"
<< " \"position\": [" << cam->GetPosition()[0] << ", " << cam->GetPosition()[1]
<< ", " << cam->GetPosition()[2] << "],\n"
<< " \"viewUp\": [" << cam->GetViewUp()[0] << ", " << cam->GetViewUp()[1] << ", "
<< cam->GetViewUp()[2] << "]\n"
<< " },\n"
<< " \"centerOfRotation\": [" << cam->GetFocalPoint()[0] << ", "
<< cam->GetFocalPoint()[1] << ", " << cam->GetFocalPoint()[2] << "],\n"
<< " \"scene\": [" << sceneComponents.str() << "\n ],\n"
<< " \"lookupTables\": {\n";
// Inject lookup table
size_t nbLuts = this->LookupTables.size();
for (auto const& lut : this->LookupTables)
{
sceneJsonFile << " \"" << lut.first.c_str() << "\": " << lut.second.c_str()
<< (--nbLuts ? "," : "") << "\n";
}
sceneJsonFile << " }\n"
<< "}\n";
// Write meta-data file
std::stringstream scenePath;
scenePath << this->FileName << "/index.json";
vtksys::ofstream file;
file.open(scenePath.str().c_str(), ios::out);
file << sceneJsonFile.str().c_str();
file.close();
}
namespace
{
//------------------------------------------------------------------------------
size_t getFileSize(const std::string& path)
{
// TODO: This function gives me slightly different sizes than what my
// filesystem gives me. Find out why.
// For instance, I get 240MB for a 230MB file.
// Maybe we can say "it's close enough" for now, though...
vtksys::SystemTools::Stat_t stat_buf;
int res = vtksys::SystemTools::Stat(path, &stat_buf);
if (res < 0)
{
std::cerr << "Failed to get size of file " << path.c_str() << std::endl;
return 0;
}
return stat_buf.st_size;
}
} // end anon namespace
//------------------------------------------------------------------------------
std::string vtkJSONSceneExporter::WriteTexture(vtkTexture* texture)
{
// If this texture has already been written, just re-use the one
// we have.
if (this->TextureStrings.find(texture) != this->TextureStrings.end())
{
return this->TextureStrings[texture];
}
std::string path = this->CurrentDataSetPath();
// Make sure it exists
if (!vtksys::SystemTools::MakeDirectory(path))
{
vtkErrorMacro(<< "Cannot create directory " << path);
return "";
}
path += "/texture.jpg";
path = vtksys::SystemTools::ConvertToOutputPath(path);
vtkSmartPointer<vtkImageData> image = texture->GetInput();
vtkNew<vtkJPEGWriter> writer;
writer->SetFileName(path.c_str());
writer->SetInputDataObject(image);
writer->Write();
constexpr const char* INDENT = " ";
std::stringstream config;
config << ",\n" << INDENT << "\"texture\": \"" << this->DatasetCount + 1 << "/texture.jpg\"";
this->TextureStrings[texture] = config.str();
return config.str();
}
//------------------------------------------------------------------------------
std::string vtkJSONSceneExporter::WriteTextureLODSeries(vtkTexture* texture)
{
// If this texture has already been written, just re-use the one
// we have.
if (this->TextureLODStrings.find(texture) != this->TextureLODStrings.end())
{
return this->TextureLODStrings[texture];
}
std::vector<std::string> files;
std::string name = "texture";
std::string ext = ".jpg";
vtkSmartPointer<vtkImageData> image = texture->GetInput();
int dims[3];
image->GetDimensions(dims);
// Write these into the parent directory of our file.
// This next line also converts the path to unix slashes.
std::string path = vtksys::SystemTools::GetParentDirectory(this->FileName);
path += "/";
path = vtksys::SystemTools::ConvertToOutputPath(path);
while (true)
{
// Name is "<name>_<dataset_number>-<width>x<height><ext>"
// For example, "texture_1-256x256.jpg"
std::stringstream full_name;
full_name << name << "_" << std::to_string(this->DatasetCount + 1) << "-"
<< std::to_string(dims[0]) << "x" << std::to_string(dims[1]) << ext;
std::string full_path = path + full_name.str();
vtkNew<vtkJPEGWriter> writer;
writer->SetFileName(full_path.c_str());
writer->SetInputDataObject(image);
writer->Write();
files.push_back(full_name.str());
size_t size = getFileSize(full_path);
if (size <= this->TextureLODsBaseSize || (dims[0] == 1 && dims[1] == 1))
{
// We are done...
break;
}
// Shrink the image and go again
vtkNew<vtkImageResize> shrink;
shrink->SetInputData(image);
dims[0] = dims[0] > 1 ? dims[0] / 2 : 1;
dims[1] = dims[1] > 1 ? dims[1] / 2 : 1;
shrink->SetOutputDimensions(dims[0], dims[1], 1);
shrink->Update();
image = shrink->GetOutput();
}
const char* url = this->TextureLODsBaseUrl;
std::string baseUrl = url ? url : "";
// Now, write out the config
constexpr const char* INDENT = " ";
std::stringstream config;
config << ",\n"
<< INDENT << "\"textureLODs\": {\n"
<< INDENT << " \"baseUrl\": \"" << baseUrl << "\",\n"
<< INDENT << " \"files\": [\n";
// Reverse the order of the files so the smallest comes first
std::reverse(files.begin(), files.end());
for (size_t i = 0; i < files.size(); ++i)
{
config << INDENT << " \"" << files[i] << "\"";
if (i != files.size() - 1)
{
config << ",\n";
}
else
{
config << "\n";
}
}
config << INDENT << " ]\n" << INDENT << "}";
this->TextureLODStrings[texture] = config.str();
return config.str();
}
//------------------------------------------------------------------------------
vtkSmartPointer<vtkPolyData> vtkJSONSceneExporter::WritePolyLODSeries(
vtkPolyData* dataset, std::string& polyLODsConfig)
{
vtkSmartPointer<vtkPolyData> polyData = dataset;
std::vector<std::string> files;
// Write these into the parent directory of our file.
// This next line also converts the path to unix slashes.
vtkNew<vtkJSONDataSetWriter> dsWriter;
std::string path = vtksys::SystemTools::GetParentDirectory(this->FileName) + "/";
path = vtksys::SystemTools::ConvertToOutputPath(path);
// If the new size is not at least 5% different from the old size,
// stop writing out the LODs, because the difference is too small.
size_t previousDataSize = 0;
double minDiffFraction = 0.05;
const size_t& baseSize = this->PolyLODsBaseSize;
int count = 0;
while (true)
{
// Squeeze the data, or we won't get an accurate memory size
polyData->Squeeze();
auto dataSize = polyData->GetActualMemorySize();
bool tooSimilar = false;
if (previousDataSize != 0)
{
double fraction = (static_cast<double>(previousDataSize) - dataSize) / previousDataSize;
if (fabs(fraction) < minDiffFraction)
{
tooSimilar = true;
}
}
previousDataSize = dataSize;
if (static_cast<size_t>(dataSize) * 1000 <= baseSize || tooSimilar)
{
// It is either now below the base size, or the size isn't
// changing much anymore.
// The latest "polyData" will be written into the .vtkjs directory
break;
}
// Write out the source LOD
// They are not zipped yet, but they should be zipped by subclasses
std::string name =
"sourceLOD_" + std::to_string(this->DatasetCount) + "_" + std::to_string(++count) + ".zip";
std::string full_path = path + name;
dsWriter->SetInputData(polyData);
dsWriter->GetArchiver()->SetArchiveName(full_path.c_str());
dsWriter->Write();
files.push_back(name);
this->FilesToZip.push_back(full_path);
// Now reduce the size of the data
double bounds[6];
polyData->GetBounds(bounds);
double length = polyData->GetLength();
double factors[3] = { (bounds[1] - bounds[0]) / length + 0.01,
(bounds[3] - bounds[2]) / length + 0.01, (bounds[5] - bounds[4]) / length + 0.01 };
double factorsCube = factors[0] * factors[1] * factors[2];
// Try to make a good first guess for B
// TODO: this first guess can probably be improved a lot.
double B = pow(100 * dataSize / factorsCube, 0.3333);
vtkNew<vtkQuadricClustering> cc;
cc->UseInputPointsOn();
cc->CopyCellDataOn();
cc->SetInputDataObject(polyData);
cc->SetAutoAdjustNumberOfDivisions(false);
// We will try to get the next size to be between 1/3 and 1/5
// of the original. The goal is to be approximately 1/4.
auto targetSize = dataSize / 4;
auto targetMin = dataSize / 5;
auto targetMax = dataSize / 3;
int maxAttempts = 100;
size_t previousSize = 0;
// If we fail to get to ~1/4 the size for some reason, just use
// the default divisions. Sometimes, a failure is caused by
// one of the factors being too big.
bool useDefaultDivisions = false;
for (int numAttempts = 0; numAttempts < maxAttempts; ++numAttempts)
{
int divs[3];
for (int i = 0; i < 3; ++i)
{
divs[i] = B * factors[i] + 1;
}
// The allocated memory is proportional to divs[0] * divs[1] * divs[2].
// Make sure this product is not too big, or we may run out of memory,
// or, worse, have an integer overflow.
// At the time of testing, a product of 1e8 requires more than 10 GB of
// memory to run. Let's set a limit here for now.
size_t maxProduct = 1e8;
if (static_cast<size_t>(divs[0]) * divs[1] * divs[2] > maxProduct)
{
// Too big. Just use the defaults.
useDefaultDivisions = true;
break;
}
cc->SetNumberOfXDivisions(divs[0]);
cc->SetNumberOfYDivisions(divs[1]);
cc->SetNumberOfZDivisions(divs[2]);
try
{
cc->Update();
}
catch (const std::bad_alloc&)
{
// Too many divisions, probably. Just use the defaults.
useDefaultDivisions = true;
break;
}
// Squeeze the data, or we won't get an accurate memory size
cc->GetOutput()->Squeeze();
auto newSize = cc->GetOutput()->GetActualMemorySize();
if (newSize == previousSize)
{
// This is not changing. Just use the default divisions.
useDefaultDivisions = true;
break;
}
previousSize = newSize;
if (newSize >= targetMin && newSize <= targetMax)
{
// We are within the tolerance!
break;
}
else
{
// Figure out the fraction that we are off, and change B
// accordingly
double fraction = newSize / static_cast<double>(targetSize);
B /= pow(fraction, 0.333);
}
}
if (useDefaultDivisions)
{
vtkNew<vtkQuadricClustering> defaultCC;
defaultCC->UseInputPointsOn();
defaultCC->CopyCellDataOn();
defaultCC->SetInputDataObject(polyData);
defaultCC->Update();
polyData = defaultCC->GetOutput();
}
else
{
polyData = cc->GetOutput();
}
}
// Write out the config
const char* url = this->PolyLODsBaseUrl;
std::string baseUrl = url ? url : "";