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>
33TTKWRLEXPORTER_EXPORT vtkPolyData *ttkWRLExporterPolyData_ =
nullptr;
36TTKWRLEXPORTER_EXPORT
void vtkVRMLExporter::WriteAnActor(vtkActor *anActor,
41 debugInfo.
printWrn(
"Using TTK fix for VRML export...");
44 vtkPointData *pntData;
46 vtkDataArray *normals =
nullptr;
47 vtkDataArray *tcoords =
nullptr;
52#ifdef VTK_CELL_ARRAY_V2
53 vtkIdType
const *indx =
nullptr;
55 vtkIdType *indx =
nullptr;
57 int pointDataWritten = 0;
58 vtkPolyDataMapper *pm;
59 vtkUnsignedCharArray *colors;
65 if(anActor->GetMapper() ==
nullptr) {
69 if(anActor->GetVisibility() == 0) {
74 trans = vtkTransform::New();
75 trans->SetMatrix(anActor->vtkProp3D::GetMatrix());
77 fprintf(fp,
" Transform {\n");
78 tempd = trans->GetPosition();
79 fprintf(fp,
" translation %g %g %g\n", tempd[0], tempd[1], tempd[2]);
80 tempd = trans->GetOrientationWXYZ();
81 fprintf(fp,
" rotation %g %g %g %g\n", tempd[1], tempd[2], tempd[3],
82 tempd[0] * vtkMath::Pi() / 180.0);
83 tempd = trans->GetScale();
84 fprintf(fp,
" scale %g %g %g\n", tempd[0], tempd[1], tempd[2]);
85 fprintf(fp,
" children [\n");
88 vtkDataObject *inputDO = anActor->GetMapper()->GetInputDataObject(0, 0);
90 if(inputDO ==
nullptr) {
95 if(inputDO->IsA(
"vtkCompositeDataSet")) {
96 vtkCompositeDataGeometryFilter *gf = vtkCompositeDataGeometryFilter::New();
97 gf->SetInputConnection(anActor->GetMapper()->GetInputConnection(0, 0));
101 }
else if(inputDO->GetDataObjectType() != VTK_POLY_DATA) {
102 vtkGeometryFilter *gf = vtkGeometryFilter::New();
103 gf->SetInputConnection(anActor->GetMapper()->GetInputConnection(0, 0));
105 pd = gf->GetOutput();
108 anActor->GetMapper()->Update();
109 pd =
static_cast<vtkPolyData *
>(inputDO);
113 ttkWRLExporterPolyData_ =
static_cast<vtkPolyData *
>(pd);
116 pm = vtkPolyDataMapper::New();
117 pm->SetInputData(pd);
118 pm->SetScalarRange(anActor->GetMapper()->GetScalarRange());
119 pm->SetScalarVisibility(anActor->GetMapper()->GetScalarVisibility());
120 pm->SetLookupTable(anActor->GetMapper()->GetLookupTable());
121 pm->SetScalarMode(anActor->GetMapper()->GetScalarMode());
123 if(pm->GetScalarMode() == VTK_SCALAR_MODE_USE_POINT_FIELD_DATA
124 || pm->GetScalarMode() == VTK_SCALAR_MODE_USE_CELL_FIELD_DATA) {
125 if(anActor->GetMapper()->GetArrayAccessMode() == VTK_GET_ARRAY_BY_ID) {
126 pm->ColorByArrayComponent(anActor->GetMapper()->GetArrayId(),
127 anActor->GetMapper()->GetArrayComponent());
129 pm->ColorByArrayComponent(anActor->GetMapper()->GetArrayName(),
130 anActor->GetMapper()->GetArrayComponent());
134 points = pd->GetPoints();
135 pntData = pd->GetPointData();
136 normals = pntData->GetNormals();
137 tcoords = pntData->GetTCoords();
138 colors = pm->MapScalars(1.0);
141 if(pd->GetNumberOfPolys() > 0) {
142 WriteShapeBegin(anActor, fp, pd, pntData, colors);
143 fprintf(fp,
" geometry IndexedFaceSet {\n");
145 fprintf(fp,
" solid FALSE\n");
147 if(!pointDataWritten) {
148 this->WritePointData(points, normals, tcoords, colors, fp);
149 pointDataWritten = 1;
151 fprintf(fp,
" coord USE VTKcoordinates\n");
154 fprintf(fp,
" normal USE VTKnormals\n");
158 fprintf(fp,
" texCoord USE VTKtcoords\n");
162 fprintf(fp,
" color USE VTKcolors\n");
166 fprintf(fp,
" coordIndex [\n");
168 cells = pd->GetPolys();
170 for(cells->InitTraversal(); cells->GetNextCell(npts, indx);) {
173 for(i = 0; i < npts; i++) {
175 fprintf(fp,
"%i, ",
static_cast<int>(indx[i]));
178 fprintf(fp,
"-1,\n");
187 if(pd->GetNumberOfStrips() > 0) {
188 WriteShapeBegin(anActor, fp, pd, pntData, colors);
189 fprintf(fp,
" geometry IndexedFaceSet {\n");
191 if(!pointDataWritten) {
192 this->WritePointData(points, normals, tcoords, colors, fp);
193 pointDataWritten = 1;
195 fprintf(fp,
" coord USE VTKcoordinates\n");
198 fprintf(fp,
" normal USE VTKnormals\n");
202 fprintf(fp,
" texCoord USE VTKtcoords\n");
206 fprintf(fp,
" color USE VTKcolors\n");
210 fprintf(fp,
" coordIndex [\n");
211 cells = pd->GetStrips();
213 for(cells->InitTraversal(); cells->GetNextCell(npts, indx);) {
214 for(i = 2; i < npts; i++) {
224 fprintf(fp,
" %i, %i, %i, -1,\n",
225 static_cast<int>(indx[i1]),
static_cast<int>(indx[i2]),
226 static_cast<int>(indx[i]));
236 if(pd->GetNumberOfLines() > 0) {
237 WriteShapeBegin(anActor, fp, pd, pntData, colors);
238 fprintf(fp,
" geometry IndexedLineSet {\n");
240 if(!pointDataWritten) {
241 this->WritePointData(points,
nullptr,
nullptr, colors, fp);
243 fprintf(fp,
" coord USE VTKcoordinates\n");
246 fprintf(fp,
" color USE VTKcolors\n");
250 fprintf(fp,
" coordIndex [\n");
252 cells = pd->GetLines();
254 for(cells->InitTraversal(); cells->GetNextCell(npts, indx);) {
257 for(i = 0; i < npts; i++) {
259 fprintf(fp,
"%i, ",
static_cast<int>(indx[i]));
262 fprintf(fp,
"-1,\n");
271 if(pd->GetNumberOfVerts() > 0) {
272 WriteShapeBegin(anActor, fp, pd, pntData, colors);
273 fprintf(fp,
" geometry PointSet {\n");
274 cells = pd->GetVerts();
275 fprintf(fp,
" coord Coordinate {");
276 fprintf(fp,
" point [");
278 for(cells->InitTraversal(); cells->GetNextCell(npts, indx);) {
281 for(i = 0; i < npts; i++) {
282 p = points->GetPoint(indx[i]);
283 fprintf(fp,
" %g %g %g,\n", p[0], p[1], p[2]);
291 fprintf(fp,
" color Color {");
292 fprintf(fp,
" color [");
294 for(cells->InitTraversal(); cells->GetNextCell(npts, indx);) {
297 for(i = 0; i < npts; i++) {
298 c = colors->GetPointer(4 * indx[i]);
299 fprintf(fp,
" %g %g %g,\n", c[0] / 255.0, c[1] / 255.0,
318TTKWRLEXPORTER_EXPORT
void vtkVRMLExporter::WriteData() {
321 vtkActorCollection *ac;
322 vtkActor *anActor, *aPart;
323 vtkLightCollection *lc;
330 if(!this->FilePointer && (this->FileName ==
nullptr)) {
331 vtkErrorMacro(<<
"Please specify FileName to use");
344 ren = this->RenderWindow->GetRenderers()->GetFirstRenderer();
347 if(ren->GetActors()->GetNumberOfItems() < 1) {
348 vtkErrorMacro(<<
"no actors found for writing VRML file.");
353 if(!this->FilePointer) {
354 fp = fopen(this->FileName,
"w");
357 vtkErrorMacro(<<
"unable to open VRML file " << this->FileName);
361 fp = this->FilePointer;
367 vtkDebugMacro(
"Writing VRML file");
368 fprintf(fp,
"#VRML V2.0 utf8\n");
369 fprintf(fp,
"# VRML file written by the visualization toolkit\n\n");
372 double background[3];
373 ren->GetBackground(background);
374 fprintf(fp,
" Background {\n ");
375 fprintf(fp,
" skyColor [%f %f %f, ]\n", background[0], background[1],
377 fprintf(fp,
" }\n ");
408 fp,
"DirectionalLight { ambientIntensity 1 intensity 0 # ambient light\n");
409 fprintf(fp,
" color %f %f %f }\n\n", ren->GetAmbient()[0],
410 ren->GetAmbient()[1], ren->GetAmbient()[2]);
414 lc = ren->GetLights();
415 vtkCollectionSimpleIterator lsit;
417 for(lc->InitTraversal(lsit); (aLight = lc->GetNextLight(lsit));) {
418 this->WriteALight(aLight, fp);
422 ac = ren->GetActors();
423 vtkAssemblyPath *apath;
424 vtkCollectionSimpleIterator ait;
426 for(ac->InitTraversal(ait); (anActor = ac->GetNextActor(ait));) {
427 for(anActor->InitPathTraversal(); (apath = anActor->GetNextPath());) {
428 aPart =
static_cast<vtkActor *
>(apath->GetLastNode()->GetViewProp());
429 this->WriteAnActor(aPart, fp);
433 if(!this->FilePointer) {
438TTKWRLEXPORTER_EXPORT
void
439 vtkVRMLExporter::WritePointData(vtkPoints *points,
440 vtkDataArray *normals,
441 vtkDataArray *tcoords,
442 vtkUnsignedCharArray *colors,
449 fprintf(fp,
" coord DEF VTKcoordinates Coordinate {\n");
450 fprintf(fp,
" point [\n");
451 for(
int i = 0; i < points->GetNumberOfPoints(); i++) {
452 p = points->GetPoint(i);
453 fprintf(fp,
" %g %g %g,\n", p[0], p[1], p[2]);
461 fprintf(fp,
" normal DEF VTKnormals Normal {\n");
462 fprintf(fp,
" vector [\n");
463 for(
int i = 0; i < normals->GetNumberOfTuples(); i++) {
464 p = normals->GetTuple(i);
465 fprintf(fp,
" %g %g %g,\n", p[0], p[1], p[2]);
473 fprintf(fp,
" texCoord DEF VTKtcoords TextureCoordinate {\n");
474 fprintf(fp,
" point [\n");
475 for(
int i = 0; i < tcoords->GetNumberOfTuples(); i++) {
476 p = tcoords->GetTuple(i);
477 fprintf(fp,
" %g %g,\n", p[0], p[1]);
483 if(ttkWRLExporterPolyData_) {
484 fprintf(fp,
" texCoordIndex[\n");
485 vtkCellArray *cells = ttkWRLExporterPolyData_->GetPolys();
487#ifdef VTK_CELL_ARRAY_V2
488 vtkIdType
const *indx =
nullptr;
490 vtkIdType *indx =
nullptr;
492 for(cells->InitTraversal(); cells->GetNextCell(npts, indx);) {
494 for(
int i = 0; i < npts; i++) {
495 fprintf(fp,
"%i, ",
static_cast<int>(indx[i]));
497 fprintf(fp,
"-1,\n");
506 fprintf(fp,
" color DEF VTKcolors Color {\n");
507 fprintf(fp,
" color [\n");
508 for(
int i = 0; i < colors->GetNumberOfTuples(); i++) {
509 c = colors->GetPointer(4 * i);
511 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)