3#include <vtkActorCollection.h>
4#include <vtkAssemblyPath.h>
6#include <vtkCellArray.h>
7#include <vtkCompositeDataGeometryFilter.h>
8#include <vtkDataArray.h>
9#include <vtkDataObject.h>
10#include <vtkGeometryFilter.h>
11#include <vtkLightCollection.h>
14#include <vtkObjectFactory.h>
15#include <vtkPointData.h>
17#include <vtkPolyDataMapper.h>
18#include <vtkRenderWindow.h>
19#include <vtkRenderer.h>
20#include <vtkRendererCollection.h>
21#include <vtkSmartPointer.h>
22#include <vtkTransform.h>
23#include <vtkUnsignedCharArray.h>
24#include <vtkVRMLExporter.h>
25#include <vtkVersionMacros.h>
34TTKWRLEXPORTER_EXPORT vtkPolyData *ttkWRLExporterPolyData_ =
nullptr;
37TTKWRLEXPORTER_EXPORT
void vtkVRMLExporter::WriteAnActor(vtkActor *anActor,
42 debugInfo.
printWrn(
"Using TTK fix for VRML export...");
45 vtkPointData *pntData;
47 vtkDataArray *normals =
nullptr;
48 vtkDataArray *tcoords =
nullptr;
53#ifdef VTK_CELL_ARRAY_V2
54 vtkIdType
const *indx =
nullptr;
56 vtkIdType *indx =
nullptr;
58 int pointDataWritten = 0;
59 vtkPolyDataMapper *pm;
60 vtkUnsignedCharArray *colors;
66 if(anActor->GetMapper() ==
nullptr) {
70 if(anActor->GetVisibility() == 0) {
75 trans = vtkTransform::New();
76 trans->SetMatrix(anActor->vtkProp3D::GetMatrix());
78 fprintf(fp,
" Transform {\n");
79 tempd = trans->GetPosition();
80 fprintf(fp,
" translation %g %g %g\n", tempd[0], tempd[1], tempd[2]);
81 tempd = trans->GetOrientationWXYZ();
82 fprintf(fp,
" rotation %g %g %g %g\n", tempd[1], tempd[2], tempd[3],
83 tempd[0] * vtkMath::Pi() / 180.0);
84 tempd = trans->GetScale();
85 fprintf(fp,
" scale %g %g %g\n", tempd[0], tempd[1], tempd[2]);
86 fprintf(fp,
" children [\n");
89 vtkDataObject *inputDO = anActor->GetMapper()->GetInputDataObject(0, 0);
91 if(inputDO ==
nullptr) {
96 if(inputDO->IsA(
"vtkCompositeDataSet")) {
97 vtkCompositeDataGeometryFilter *gf = vtkCompositeDataGeometryFilter::New();
98 gf->SetInputConnection(anActor->GetMapper()->GetInputConnection(0, 0));
100 pd = gf->GetOutput();
102 }
else if(inputDO->GetDataObjectType() != VTK_POLY_DATA) {
103 vtkGeometryFilter *gf = vtkGeometryFilter::New();
104 gf->SetInputConnection(anActor->GetMapper()->GetInputConnection(0, 0));
106 pd = gf->GetOutput();
109 anActor->GetMapper()->Update();
110 pd =
static_cast<vtkPolyData *
>(inputDO);
114 ttkWRLExporterPolyData_ =
static_cast<vtkPolyData *
>(pd);
117 pm = vtkPolyDataMapper::New();
118 pm->SetInputData(pd);
119 pm->SetScalarRange(anActor->GetMapper()->GetScalarRange());
120 pm->SetScalarVisibility(anActor->GetMapper()->GetScalarVisibility());
121 pm->SetLookupTable(anActor->GetMapper()->GetLookupTable());
122 pm->SetScalarMode(anActor->GetMapper()->GetScalarMode());
124 if(pm->GetScalarMode() == VTK_SCALAR_MODE_USE_POINT_FIELD_DATA
125 || pm->GetScalarMode() == VTK_SCALAR_MODE_USE_CELL_FIELD_DATA) {
126 if(anActor->GetMapper()->GetArrayAccessMode() == VTK_GET_ARRAY_BY_ID) {
127 pm->ColorByArrayComponent(anActor->GetMapper()->GetArrayId(),
128 anActor->GetMapper()->GetArrayComponent());
130 pm->ColorByArrayComponent(anActor->GetMapper()->GetArrayName(),
131 anActor->GetMapper()->GetArrayComponent());
135 points = pd->GetPoints();
136 pntData = pd->GetPointData();
137 normals = pntData->GetNormals();
138 tcoords = pntData->GetTCoords();
139 colors = pm->MapScalars(1.0);
142 if(pd->GetNumberOfPolys() > 0) {
143 WriteShapeBegin(anActor, fp, pd, pntData, colors);
144 fprintf(fp,
" geometry IndexedFaceSet {\n");
146 fprintf(fp,
" solid FALSE\n");
148 if(!pointDataWritten) {
150#if VTK_VERSION_NUMBER >= VTK_VERSION_CHECK(9, 4, 20250513)
151 this->WritePointData(points, normals, tcoords, colors,
false, fp);
153 this->WritePointData(points, normals, tcoords, colors, fp);
155 pointDataWritten = 1;
157 fprintf(fp,
" coord USE VTKcoordinates\n");
160 fprintf(fp,
" normal USE VTKnormals\n");
164 fprintf(fp,
" texCoord USE VTKtcoords\n");
168 fprintf(fp,
" color USE VTKcolors\n");
172 fprintf(fp,
" coordIndex [\n");
174 cells = pd->GetPolys();
176 for(cells->InitTraversal(); cells->GetNextCell(npts, indx);) {
179 for(i = 0; i < npts; i++) {
181 fprintf(fp,
"%i, ",
static_cast<int>(indx[i]));
184 fprintf(fp,
"-1,\n");
193 if(pd->GetNumberOfStrips() > 0) {
194 WriteShapeBegin(anActor, fp, pd, pntData, colors);
195 fprintf(fp,
" geometry IndexedFaceSet {\n");
197 if(!pointDataWritten) {
199#if VTK_VERSION_NUMBER >= VTK_VERSION_CHECK(9, 4, 20250513)
200 this->WritePointData(points, normals, tcoords, colors,
false, fp);
202 this->WritePointData(points, normals, tcoords, colors, fp);
205 pointDataWritten = 1;
207 fprintf(fp,
" coord USE VTKcoordinates\n");
210 fprintf(fp,
" normal USE VTKnormals\n");
214 fprintf(fp,
" texCoord USE VTKtcoords\n");
218 fprintf(fp,
" color USE VTKcolors\n");
222 fprintf(fp,
" coordIndex [\n");
223 cells = pd->GetStrips();
225 for(cells->InitTraversal(); cells->GetNextCell(npts, indx);) {
226 for(i = 2; i < npts; i++) {
236 fprintf(fp,
" %i, %i, %i, -1,\n",
237 static_cast<int>(indx[i1]),
static_cast<int>(indx[i2]),
238 static_cast<int>(indx[i]));
248 if(pd->GetNumberOfLines() > 0) {
249 WriteShapeBegin(anActor, fp, pd, pntData, colors);
250 fprintf(fp,
" geometry IndexedLineSet {\n");
252 if(!pointDataWritten) {
254#if VTK_VERSION_NUMBER >= VTK_VERSION_CHECK(9, 4, 20250513)
255 this->WritePointData(points,
nullptr,
nullptr, colors,
false, fp);
257 this->WritePointData(points,
nullptr,
nullptr, colors, fp);
260 fprintf(fp,
" coord USE VTKcoordinates\n");
263 fprintf(fp,
" color USE VTKcolors\n");
267 fprintf(fp,
" coordIndex [\n");
269 cells = pd->GetLines();
271 for(cells->InitTraversal(); cells->GetNextCell(npts, indx);) {
274 for(i = 0; i < npts; i++) {
276 fprintf(fp,
"%i, ",
static_cast<int>(indx[i]));
279 fprintf(fp,
"-1,\n");
288 if(pd->GetNumberOfVerts() > 0) {
289 WriteShapeBegin(anActor, fp, pd, pntData, colors);
290 fprintf(fp,
" geometry PointSet {\n");
291 cells = pd->GetVerts();
292 fprintf(fp,
" coord Coordinate {");
293 fprintf(fp,
" point [");
295 for(cells->InitTraversal(); cells->GetNextCell(npts, indx);) {
298 for(i = 0; i < npts; i++) {
299 p = points->GetPoint(indx[i]);
300 fprintf(fp,
" %g %g %g,\n", p[0], p[1], p[2]);
308 fprintf(fp,
" color Color {");
309 fprintf(fp,
" color [");
311 for(cells->InitTraversal(); cells->GetNextCell(npts, indx);) {
314 for(i = 0; i < npts; i++) {
315 c = colors->GetPointer(4 * indx[i]);
316 fprintf(fp,
" %g %g %g,\n", c[0] / 255.0, c[1] / 255.0,
335TTKWRLEXPORTER_EXPORT
void vtkVRMLExporter::WriteData() {
338 vtkActorCollection *ac;
339 vtkActor *anActor, *aPart;
340 vtkLightCollection *lc;
347 if(!this->FilePointer && (this->FileName ==
nullptr)) {
348 vtkErrorMacro(<<
"Please specify FileName to use");
361 ren = this->RenderWindow->GetRenderers()->GetFirstRenderer();
364 if(ren->GetActors()->GetNumberOfItems() < 1) {
365 vtkErrorMacro(<<
"no actors found for writing VRML file.");
370 if(!this->FilePointer) {
371 fp = fopen(this->FileName,
"w");
374 vtkErrorMacro(<<
"unable to open VRML file " << this->FileName);
378 fp = this->FilePointer;
384 vtkDebugMacro(
"Writing VRML file");
385 fprintf(fp,
"#VRML V2.0 utf8\n");
386 fprintf(fp,
"# VRML file written by the visualization toolkit\n\n");
389 double background[3];
390 ren->GetBackground(background);
391 fprintf(fp,
" Background {\n ");
392 fprintf(fp,
" skyColor [%f %f %f, ]\n", background[0], background[1],
394 fprintf(fp,
" }\n ");
425 fp,
"DirectionalLight { ambientIntensity 1 intensity 0 # ambient light\n");
426 fprintf(fp,
" color %f %f %f }\n\n", ren->GetAmbient()[0],
427 ren->GetAmbient()[1], ren->GetAmbient()[2]);
431 lc = ren->GetLights();
432 vtkCollectionSimpleIterator lsit;
434 for(lc->InitTraversal(lsit); (aLight = lc->GetNextLight(lsit));) {
435 this->WriteALight(aLight, fp);
439 ac = ren->GetActors();
440 vtkAssemblyPath *apath;
441 vtkCollectionSimpleIterator ait;
443 for(ac->InitTraversal(ait); (anActor = ac->GetNextActor(ait));) {
444 for(anActor->InitPathTraversal(); (apath = anActor->GetNextPath());) {
445 aPart =
static_cast<vtkActor *
>(apath->GetLastNode()->GetViewProp());
446 this->WriteAnActor(aPart, fp);
450 if(!this->FilePointer) {
455TTKWRLEXPORTER_EXPORT
void
456 vtkVRMLExporter::WritePointData(vtkPoints *points,
457 vtkDataArray *normals,
458 vtkDataArray *tcoords,
459 vtkUnsignedCharArray *colors,
460#
if VTK_VERSION_NUMBER >= VTK_VERSION_CHECK(9, 4, 20250513)
461 bool vtkNotUsed(cellData),
469 fprintf(fp,
" coord DEF VTKcoordinates Coordinate {\n");
470 fprintf(fp,
" point [\n");
471 for(
int i = 0; i < points->GetNumberOfPoints(); i++) {
472 p = points->GetPoint(i);
473 fprintf(fp,
" %g %g %g,\n", p[0], p[1], p[2]);
481 fprintf(fp,
" normal DEF VTKnormals Normal {\n");
482 fprintf(fp,
" vector [\n");
483 for(
int i = 0; i < normals->GetNumberOfTuples(); i++) {
484 p = normals->GetTuple(i);
485 fprintf(fp,
" %g %g %g,\n", p[0], p[1], p[2]);
493 fprintf(fp,
" texCoord DEF VTKtcoords TextureCoordinate {\n");
494 fprintf(fp,
" point [\n");
495 for(
int i = 0; i < tcoords->GetNumberOfTuples(); i++) {
496 p = tcoords->GetTuple(i);
497 fprintf(fp,
" %g %g,\n", p[0], p[1]);
503 if(ttkWRLExporterPolyData_) {
504 fprintf(fp,
" texCoordIndex[\n");
505 vtkCellArray *cells = ttkWRLExporterPolyData_->GetPolys();
507#ifdef VTK_CELL_ARRAY_V2
508 vtkIdType
const *indx =
nullptr;
510 vtkIdType *indx =
nullptr;
512 for(cells->InitTraversal(); cells->GetNextCell(npts, indx);) {
514 for(
int i = 0; i < npts; i++) {
515 fprintf(fp,
"%i, ",
static_cast<int>(indx[i]));
517 fprintf(fp,
"-1,\n");
526 fprintf(fp,
" color DEF VTKcolors Color {\n");
527 fprintf(fp,
" color [\n");
528 for(
int i = 0; i < colors->GetNumberOfTuples(); i++) {
529 c = colors->GetPointer(4 * i);
531 fp,
" %g %g %g,\n", c[0] / 255.0, c[1] / 255.0, c[2] / 255.0);
Minimalist debugging class.
int printWrn(const std::string &msg, const debug::LineMode &lineMode=debug::LineMode::NEW, std::ostream &stream=std::cerr) const
void setDebugMsgPrefix(const std::string &prefix)