TTK
Loading...
Searching...
No Matches
ttkWRLExporter.cpp
Go to the documentation of this file.
1// VTK includes
2#include <vtkActor.h>
3#include <vtkActorCollection.h>
4#include <vtkAssemblyPath.h>
5#include <vtkCamera.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>
12#include <vtkMapper.h>
13#include <vtkMath.h>
14#include <vtkObjectFactory.h>
15#include <vtkPointData.h>
16#include <vtkPoints.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
26// base code includes
27#include <Debug.h>
28
29#include <ttkWRLExporter.h>
30
32
33TTKWRLEXPORTER_EXPORT vtkPolyData *ttkWRLExporterPolyData_ = nullptr;
34
35// Over-ride the appropriate functions of the vtkVRMLExporter class.
36TTKWRLEXPORTER_EXPORT void vtkVRMLExporter::WriteAnActor(vtkActor *anActor,
37 FILE *fp) {
38
39 ttk::Debug debugInfo;
40 debugInfo.setDebugMsgPrefix("WRLExporter");
41 debugInfo.printWrn("Using TTK fix for VRML export...");
42
44 vtkPointData *pntData;
45 vtkPoints *points;
46 vtkDataArray *normals = nullptr;
47 vtkDataArray *tcoords = nullptr;
48 int i, i1, i2;
49 double *tempd;
50 vtkCellArray *cells;
51 vtkIdType npts = 0;
52#ifdef VTK_CELL_ARRAY_V2
53 vtkIdType const *indx = nullptr;
54#else
55 vtkIdType *indx = nullptr;
56#endif
57 int pointDataWritten = 0;
58 vtkPolyDataMapper *pm;
59 vtkUnsignedCharArray *colors;
60 double *p;
61 unsigned char *c;
62 vtkTransform *trans;
63
64 // see if the actor has a mapper. it could be an assembly
65 if(anActor->GetMapper() == nullptr) {
66 return;
67 }
68
69 if(anActor->GetVisibility() == 0) {
70 return;
71 }
72
73 // first stuff out the transform
74 trans = vtkTransform::New();
75 trans->SetMatrix(anActor->vtkProp3D::GetMatrix());
76
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");
86 trans->Delete();
87
88 vtkDataObject *inputDO = anActor->GetMapper()->GetInputDataObject(0, 0);
89
90 if(inputDO == nullptr) {
91 return;
92 }
93
94 // we really want polydata
95 if(inputDO->IsA("vtkCompositeDataSet")) {
96 vtkCompositeDataGeometryFilter *gf = vtkCompositeDataGeometryFilter::New();
97 gf->SetInputConnection(anActor->GetMapper()->GetInputConnection(0, 0));
98 gf->Update();
99 pd = gf->GetOutput();
100 gf->Delete();
101 } else if(inputDO->GetDataObjectType() != VTK_POLY_DATA) {
102 vtkGeometryFilter *gf = vtkGeometryFilter::New();
103 gf->SetInputConnection(anActor->GetMapper()->GetInputConnection(0, 0));
104 gf->Update();
105 pd = gf->GetOutput();
106 gf->Delete();
107 } else {
108 anActor->GetMapper()->Update();
109 pd = static_cast<vtkPolyData *>(inputDO);
110 }
111
112 // BUG fix here
113 ttkWRLExporterPolyData_ = static_cast<vtkPolyData *>(pd);
114 // end of BUG fix here
115
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());
122
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());
128 } else {
129 pm->ColorByArrayComponent(anActor->GetMapper()->GetArrayName(),
130 anActor->GetMapper()->GetArrayComponent());
131 }
132 }
133
134 points = pd->GetPoints();
135 pntData = pd->GetPointData();
136 normals = pntData->GetNormals();
137 tcoords = pntData->GetTCoords();
138 colors = pm->MapScalars(1.0);
139
140 // write out polys if any
141 if(pd->GetNumberOfPolys() > 0) {
142 WriteShapeBegin(anActor, fp, pd, pntData, colors);
143 fprintf(fp, " geometry IndexedFaceSet {\n");
144 // two sided lighting ? for now assume it is on
145 fprintf(fp, " solid FALSE\n");
146
147 if(!pointDataWritten) {
148 this->WritePointData(points, normals, tcoords, colors, fp);
149 pointDataWritten = 1;
150 } else {
151 fprintf(fp, " coord USE VTKcoordinates\n");
152
153 if(normals) {
154 fprintf(fp, " normal USE VTKnormals\n");
155 }
156
157 if(tcoords) {
158 fprintf(fp, " texCoord USE VTKtcoords\n");
159 }
160
161 if(colors) {
162 fprintf(fp, " color USE VTKcolors\n");
163 }
164 }
165
166 fprintf(fp, " coordIndex [\n");
167
168 cells = pd->GetPolys();
169
170 for(cells->InitTraversal(); cells->GetNextCell(npts, indx);) {
171 fprintf(fp, " ");
172
173 for(i = 0; i < npts; i++) {
174 // treating vtkIdType as int
175 fprintf(fp, "%i, ", static_cast<int>(indx[i]));
176 }
177
178 fprintf(fp, "-1,\n");
179 }
180
181 fprintf(fp, " ]\n");
182 fprintf(fp, " }\n");
183 WriteShapeEnd(fp);
184 }
185
186 // write out tstrips if any
187 if(pd->GetNumberOfStrips() > 0) {
188 WriteShapeBegin(anActor, fp, pd, pntData, colors);
189 fprintf(fp, " geometry IndexedFaceSet {\n");
190
191 if(!pointDataWritten) {
192 this->WritePointData(points, normals, tcoords, colors, fp);
193 pointDataWritten = 1;
194 } else {
195 fprintf(fp, " coord USE VTKcoordinates\n");
196
197 if(normals) {
198 fprintf(fp, " normal USE VTKnormals\n");
199 }
200
201 if(tcoords) {
202 fprintf(fp, " texCoord USE VTKtcoords\n");
203 }
204
205 if(colors) {
206 fprintf(fp, " color USE VTKcolors\n");
207 }
208 }
209
210 fprintf(fp, " coordIndex [\n");
211 cells = pd->GetStrips();
212
213 for(cells->InitTraversal(); cells->GetNextCell(npts, indx);) {
214 for(i = 2; i < npts; i++) {
215 if(i % 2) {
216 i1 = i - 1;
217 i2 = i - 2;
218 } else {
219 i1 = i - 2;
220 i2 = i - 1;
221 }
222
223 // treating vtkIdType as int
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]));
227 }
228 }
229
230 fprintf(fp, " ]\n");
231 fprintf(fp, " }\n");
232 WriteShapeEnd(fp);
233 }
234
235 // write out lines if any
236 if(pd->GetNumberOfLines() > 0) {
237 WriteShapeBegin(anActor, fp, pd, pntData, colors);
238 fprintf(fp, " geometry IndexedLineSet {\n");
239
240 if(!pointDataWritten) {
241 this->WritePointData(points, nullptr, nullptr, colors, fp);
242 } else {
243 fprintf(fp, " coord USE VTKcoordinates\n");
244
245 if(colors) {
246 fprintf(fp, " color USE VTKcolors\n");
247 }
248 }
249
250 fprintf(fp, " coordIndex [\n");
251
252 cells = pd->GetLines();
253
254 for(cells->InitTraversal(); cells->GetNextCell(npts, indx);) {
255 fprintf(fp, " ");
256
257 for(i = 0; i < npts; i++) {
258 // treating vtkIdType as int
259 fprintf(fp, "%i, ", static_cast<int>(indx[i]));
260 }
261
262 fprintf(fp, "-1,\n");
263 }
264
265 fprintf(fp, " ]\n");
266 fprintf(fp, " }\n");
267 WriteShapeEnd(fp);
268 }
269
270 // write out verts if any
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 [");
277
278 for(cells->InitTraversal(); cells->GetNextCell(npts, indx);) {
279 fprintf(fp, " ");
280
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]);
284 }
285 }
286
287 fprintf(fp, " ]\n");
288 fprintf(fp, " }\n");
289
290 if(colors) {
291 fprintf(fp, " color Color {");
292 fprintf(fp, " color [");
293
294 for(cells->InitTraversal(); cells->GetNextCell(npts, indx);) {
295 fprintf(fp, " ");
296
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,
300 c[2] / 255.0);
301 }
302 }
303
304 fprintf(fp, " ]\n");
305 fprintf(fp, " }\n");
306 }
307
308 fprintf(fp, " }\n");
309 WriteShapeEnd(fp);
310 }
311
312 fprintf(fp, " ]\n"); // close the original transforms children
313 fprintf(fp, " }\n"); // close the original transform
314
315 pm->Delete();
316}
317
318TTKWRLEXPORTER_EXPORT void vtkVRMLExporter::WriteData() {
319
320 vtkRenderer *ren;
321 vtkActorCollection *ac;
322 vtkActor *anActor, *aPart;
323 vtkLightCollection *lc;
324 vtkLight *aLight;
325 // // vtkCamera *cam;
326 // double *tempd;
327 FILE *fp;
328
329 // make sure the user specified a FileName or FilePointer
330 if(!this->FilePointer && (this->FileName == nullptr)) {
331 vtkErrorMacro(<< "Please specify FileName to use");
332 return;
333 }
334
335 // Always pick the first renderer
336 // first make sure there is only one renderer in this rendering window
337 // if (this->RenderWindow->GetRenderers()->GetNumberOfItems() > 1)
338 // {
339 // vtkErrorMacro(<< "VRML files only support one renderer per window.");
340 // return;
341 // }
342
343 // get the renderer
344 ren = this->RenderWindow->GetRenderers()->GetFirstRenderer();
345
346 // make sure it has at least one actor
347 if(ren->GetActors()->GetNumberOfItems() < 1) {
348 vtkErrorMacro(<< "no actors found for writing VRML file.");
349 return;
350 }
351
352 // try opening the files
353 if(!this->FilePointer) {
354 fp = fopen(this->FileName, "w");
355
356 if(!fp) {
357 vtkErrorMacro(<< "unable to open VRML file " << this->FileName);
358 return;
359 }
360 } else {
361 fp = this->FilePointer;
362 }
363
364 //
365 // Write header
366 //
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");
370
371 // Start write the Background
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],
376 background[2]);
377 fprintf(fp, " }\n ");
378 // End of Background
379
380 // BUG fix
381 // do the camera
382 // cam = ren->GetActiveCamera();
383 // fprintf(fp, " Viewpoint\n {\n fieldOfView %f\n",
384 // cam->GetViewAngle()*vtkMath::Pi() / 180.0);
385 // fprintf(fp, " position %f %f %f\n", cam->GetPosition()[0],
386 // cam->GetPosition()[1], cam->GetPosition()[2]);
387 // fprintf(fp, " description \"Default View\"\n");
388 // tempd = cam->GetOrientationWXYZ();
389 // fprintf(fp, " orientation %g %g %g %g\n }\n", tempd[1],
390 // tempd[2],
391 // tempd[3], tempd[0]*vtkMath::Pi() / 180.0);
392 //
393 // // do the lights first the ambient then the others
394 // fprintf(fp,
395 // " NavigationInfo {\n type [\"EXAMINE\",\"FLY\"]\n speed
396 // %f\n",
397 // this->Speed);
398 //
399 // if (ren->GetLights()->GetNumberOfItems() == 0){
400 // fprintf(fp, " headlight TRUE}\n\n");
401 // }
402 // else{
403 // fprintf(fp, " headlight FALSE}\n\n");
404 // }
405 // end of BUG fix
406
407 fprintf(
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]);
411
412 // make sure we have a default light
413 // if we dont then use a headlight
414 lc = ren->GetLights();
415 vtkCollectionSimpleIterator lsit;
416
417 for(lc->InitTraversal(lsit); (aLight = lc->GetNextLight(lsit));) {
418 this->WriteALight(aLight, fp);
419 }
420
421 // do the actors now
422 ac = ren->GetActors();
423 vtkAssemblyPath *apath;
424 vtkCollectionSimpleIterator ait;
425
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);
430 }
431 }
432
433 if(!this->FilePointer) {
434 fclose(fp);
435 }
436}
437
438TTKWRLEXPORTER_EXPORT void
439 vtkVRMLExporter::WritePointData(vtkPoints *points,
440 vtkDataArray *normals,
441 vtkDataArray *tcoords,
442 vtkUnsignedCharArray *colors,
443 FILE *fp) {
444
445 double *p;
446 unsigned char *c;
447
448 // write out the points
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]);
454 }
455 fprintf(fp, " ]\n");
456 fprintf(fp, " }\n");
457
458 // write out the point data
459 if(normals) {
460
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]);
466 }
467 fprintf(fp, " ]\n");
468 fprintf(fp, " }\n");
469 }
470
471 // write out the point data
472 if(tcoords) {
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]);
478 }
479 fprintf(fp, " ]\n");
480 fprintf(fp, " }\n");
481
482 // BUG fix here.
483 if(ttkWRLExporterPolyData_) {
484 fprintf(fp, " texCoordIndex[\n");
485 vtkCellArray *cells = ttkWRLExporterPolyData_->GetPolys();
486 vtkIdType npts = 0;
487#ifdef VTK_CELL_ARRAY_V2
488 vtkIdType const *indx = nullptr;
489#else
490 vtkIdType *indx = nullptr;
491#endif
492 for(cells->InitTraversal(); cells->GetNextCell(npts, indx);) {
493 fprintf(fp, " ");
494 for(int i = 0; i < npts; i++) {
495 fprintf(fp, "%i, ", static_cast<int>(indx[i]));
496 }
497 fprintf(fp, "-1,\n");
498 }
499 fprintf(fp, " ]\n");
500 }
501 // end of BUG fix here.
502 }
503
504 // write out the point data
505 if(colors) {
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);
510 fprintf(
511 fp, " %g %g %g,\n", c[0] / 255.0, c[1] / 255.0, c[2] / 255.0);
512 }
513 fprintf(fp, " ]\n");
514 fprintf(fp, " }\n");
515 }
516}
517
Minimalist debugging class.
Definition Debug.h:88
int printWrn(const std::string &msg, const debug::LineMode &lineMode=debug::LineMode::NEW, std::ostream &stream=std::cerr) const
Definition Debug.h:159
void setDebugMsgPrefix(const std::string &prefix)
Definition Debug.h:364