File: vtkPOVExporter.cxx

package info (click to toggle)
vtk6 6.3.0%2Bdfsg2-8.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 118,972 kB
  • sloc: cpp: 1,442,790; ansic: 113,395; python: 72,383; tcl: 46,998; xml: 8,119; yacc: 4,525; java: 4,239; perl: 3,108; lex: 1,694; sh: 1,093; asm: 154; makefile: 68; objc: 17
file content (685 lines) | stat: -rw-r--r-- 21,219 bytes parent folder | download | duplicates (3)
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
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkPOVExporter.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.

=========================================================================*/
/*=========================================================================

  Program:   VTK/ParaView Los Alamos National Laboratory Modules (PVLANL)
  Module:    vtkPOVExporter.cxx

Copyright (c) 2007, Los Alamos National Security, LLC

All rights reserved.

Copyright 2007. Los Alamos National Security, LLC.
This software was produced under U.S. Government contract DE-AC52-06NA25396
for Los Alamos National Laboratory (LANL), which is operated by
Los Alamos National Security, LLC for the U.S. Department of Energy.
The U.S. Government has rights to use, reproduce, and distribute this software.
NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY, LLC MAKES ANY WARRANTY,
EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE.
If software is modified to produce derivative works, such modified software
should be clearly marked, so as not to confuse it with the version available
from LANL.

Additionally, redistribution and use in source and binary forms, with or
without modification, are permitted provided that the following conditions
are met:
-   Redistributions of source code must retain the above copyright notice,
    this list of conditions and the following disclaimer.
-   Redistributions in binary form must reproduce the above copyright notice,
    this list of conditions and the following disclaimer in the documentation
    and/or other materials provided with the distribution.
-   Neither the name of Los Alamos National Security, LLC, Los Alamos National
    Laboratory, LANL, the U.S. Government, nor the names of its contributors
    may be used to endorse or promote products derived from this software
    without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY LOS ALAMOS NATIONAL SECURITY, LLC AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL LOS ALAMOS NATIONAL SECURITY, LLC OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

=========================================================================*/

#include "vtkPOVExporter.h"

#include "vtkAssemblyPath.h"
#include "vtkCamera.h"
#include "vtkCellArray.h"
#include "vtkCompositeDataGeometryFilter.h"
#include "vtkCompositeDataSet.h"
#include "vtkFloatArray.h"
#include "vtkGeometryFilter.h"
#include "vtkLightCollection.h"
#include "vtkLight.h"
#include "vtkMapper.h"
#include "vtkMatrix4x4.h"
#include "vtkPointData.h"
#include "vtkPolyData.h"
#include "vtkProperty.h"
#include "vtkRendererCollection.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkSmartPointer.h"
#include "vtkTexture.h"
#include "vtkTypeTraits.h"
#include "vtkUnsignedCharArray.h"

#include <vtksys/ios/sstream>

#include "vtkObjectFactory.h"

vtkStandardNewMacro(vtkPOVExporter);

//Can't use printf("%d", a_vtkIdType) because vtkIdType is not always int.
//This internal class holds format strings vtkPOVExporter can use instead.
class vtkPOVInternals
{
public:
  vtkPOVInternals()
  {
    this->CountFormat = new char[100]; //"\t\t%d,\n"
    strcpy(this->CountFormat, "\t\t");
    strcat(this->CountFormat, vtkTypeTraits<vtkIdType>::ParseFormat());
    strcat(this->CountFormat, ",\n");

    char *triFormat = new char[100]; //"%d, %d, %d"
    strcpy(triFormat, vtkTypeTraits<vtkIdType>::ParseFormat());
    strcat(triFormat, ", ");
    strcat(triFormat, vtkTypeTraits<vtkIdType>::ParseFormat());
    strcat(triFormat, ", ");
    strcat(triFormat, vtkTypeTraits<vtkIdType>::ParseFormat());

    this->TriangleFormat1 = new char[100]; //"\t\t<%d, %d, %d>,"
    strcpy(this->TriangleFormat1, "\t\t<");
    strcat(this->TriangleFormat1, triFormat);
    strcat(this->TriangleFormat1, ">,");

    this->TriangleFormat2 = new char[100]; //" %d, %d, %d,\n"
    strcpy(this->TriangleFormat2, " ");
    strcat(this->TriangleFormat2, triFormat);
    strcat(this->TriangleFormat2, ",\n");

    delete[] triFormat;
  }

  ~vtkPOVInternals()
  {
    delete[] this->CountFormat;
    delete[] this->TriangleFormat1;
    delete[] this->TriangleFormat2;
  }

  char *CountFormat;
  char *TriangleFormat1;
  char *TriangleFormat2;
};

#define VTKPOV_CNTFMT this->Internals->CountFormat
#define VTKPOV_TRIFMT1 this->Internals->TriangleFormat1
#define VTKPOV_TRIFMT2 this->Internals->TriangleFormat2

//============================================================================
vtkPOVExporter::vtkPOVExporter()
{
  this->FileName = NULL;
  this->FilePtr = NULL;
  this->Internals = new vtkPOVInternals;
}

vtkPOVExporter::~vtkPOVExporter()
{
  delete[] this->FileName;
  delete this->Internals;
}

void vtkPOVExporter::WriteData()
{
  // make sure user specified a filename
  if (this->FileName == NULL)
    {
    vtkErrorMacro(<< "Please specify file name to create");
    return;
    }

  //get the renderer
  vtkRenderer *renderer =
    this->RenderWindow->GetRenderers()->GetFirstRenderer();
  // make sure it has at least one actor
  if (renderer->GetActors()->GetNumberOfItems() < 1)
    {
    vtkErrorMacro(<< "no actors found for writing .pov file.");
    return;
    }

  // try opening the file
  this->FilePtr = fopen(this->FileName, "w");
  if (this->FilePtr == NULL)
    {
    vtkErrorMacro (<< "Cannot open " << this->FileName);
    return;
    }


  // write header
  this->WriteHeader(renderer);

  // write camera
  this->WriteCamera(renderer->GetActiveCamera());

  // write lights
  vtkLightCollection *lc = renderer->GetLights();
  vtkCollectionSimpleIterator sit;
  lc->InitTraversal(sit);
  if (lc->GetNextLight(sit) == NULL)
    {
    vtkWarningMacro(<< "No light defined, creating one at camera position");
    renderer->CreateLight();
    }
  vtkLight *light;
  for (lc->InitTraversal(sit); (light = lc->GetNextLight(sit));)
    {
    if (light->GetSwitch())
      {
      this->WriteLight(light);
      }
    }

  // write actors
  vtkActorCollection *ac = renderer->GetActors();
  vtkAssemblyPath *apath;
  vtkCollectionSimpleIterator ait;
  vtkActor *anActor, *aPart;
  for (ac->InitTraversal(ait); (anActor = ac->GetNextActor(ait)); )
    {
    for (anActor->InitPathTraversal(); (apath = anActor->GetNextPath()); )
      {
      aPart = static_cast<vtkActor *>(apath->GetLastNode()->GetViewProp());
      this->WriteActor(aPart);
      }
    }

  fclose(this->FilePtr);
}

void vtkPOVExporter::WriteHeader(vtkRenderer *renderer)
{
  fprintf(this->FilePtr, "// POVRay file exported by vtkPOVExporter\n");
  fprintf(this->FilePtr, "//\n");

  // width and height of output image,
  //and other default command line args to POVRay
  int *size = renderer->GetSize();
  fprintf(this->FilePtr, "// +W%d +H%d\n\n", size[0], size[1]);

  // global settings
  fprintf(this->FilePtr, "global_settings {\n");
  fprintf(this->FilePtr, "\tambient_light color rgb <1.0, 1.0, 1.0>\n");
  fprintf(this->FilePtr, "\tassumed_gamma 2\n");
  fprintf(this->FilePtr, "}\n\n");

  // background
  double *color = renderer->GetBackground();
  fprintf(this->FilePtr, "background { color rgb <%f, %f, %f>}\n\n",
          color[0], color[1], color[2]);
}

void vtkPOVExporter::WriteCamera(vtkCamera *camera)
{
  fprintf(this->FilePtr, "camera {\n");
  if (camera->GetParallelProjection())
    {
    fprintf(this->FilePtr, "\torthographic\n");
    }
  else
    {
    fprintf(this->FilePtr, "\tperspective\n");
    }

  double *position = camera->GetPosition();
  fprintf(this->FilePtr, "\tlocation <%f, %f, %f>\n",
          position[0], position[1], position[2]);

  double *up = camera->GetViewUp();
  // the camera up vector is called "sky" in POVRay
  fprintf(this->FilePtr, "\tsky <%f, %f, %f>\n", up[0], up[1], up[2]);

  // make POVRay to use left handed system to right handed
  // TODO: aspect ratio
  fprintf(this->FilePtr, "\tright <-1, 0, 0>\n");
  //fprintf(this->FilePtr, "\tup <-1, 0, 0>\n");

  fprintf(this->FilePtr, "\tangle %f\n", camera->GetViewAngle());

  double *focal = camera->GetFocalPoint();
  fprintf(this->FilePtr, "\tlook_at <%f, %f, %f>\n",
          focal[0], focal[1], focal[2]);

  fprintf(this->FilePtr, "}\n\n");
}

void vtkPOVExporter::WriteLight(vtkLight *light)
{
  fprintf(this->FilePtr, "light_source {\n");

  double *position = light->GetPosition();
  fprintf(this->FilePtr, "\t<%f, %f, %f>\n",
          position[0], position[1], position[2]);

  double *color = light->GetDiffuseColor();
  fprintf(this->FilePtr, "\tcolor <%f, %f, %f>*%f\n",
          color[0], color[1], color[2],
          light->GetIntensity());

  if (light->GetPositional())
    {
    fprintf(this->FilePtr, "\tspotlight\n");
    fprintf(this->FilePtr, "\tradius %f\n", light->GetConeAngle());
    fprintf(this->FilePtr, "\tfalloff %f\n", light->GetExponent());
    }
  else
    {
    fprintf(this->FilePtr, "\tparallel\n");
    }
  double *focal    = light->GetFocalPoint();
  fprintf(this->FilePtr, "\tpoint_at <%f, %f, %f>\n",
          focal[0], focal[1], focal[2]);

  fprintf(this->FilePtr, "}\n\n");
}

void vtkPOVExporter::WriteActor(vtkActor *actor)
{
  if (actor->GetMapper() == NULL)
    {
    return;
    }

  // write geometry, first ask the pipeline to update data
  vtkDataSet *dataset = NULL;
  vtkSmartPointer<vtkDataSet> tempDS;

  vtkDataObject* dObj = actor->GetMapper()->GetInputDataObject(0, 0);
  vtkCompositeDataSet* cd = vtkCompositeDataSet::SafeDownCast(dObj);
  if (cd)
    {
    vtkCompositeDataGeometryFilter* gf = vtkCompositeDataGeometryFilter::New();
    gf->SetInputConnection(actor->GetMapper()->GetInputConnection(0, 0));
    gf->Update();
    tempDS = gf->GetOutput();
    gf->Delete();

    dataset = tempDS;
    }
  else
    {
    dataset = actor->GetMapper()->GetInput();
    }

  if (dataset == NULL)
    {
    return;
    }
  actor->GetMapper()->GetInputAlgorithm()->Update();

  // convert non polygon data to polygon data if needed
  vtkGeometryFilter *geometryFilter = NULL;
  vtkPolyData *polys = NULL;;
  if (dataset->GetDataObjectType() != VTK_POLY_DATA)
    {
    geometryFilter = vtkGeometryFilter::New();
    geometryFilter->SetInputConnection(
      actor->GetMapper()->GetInputConnection(0, 0));
    geometryFilter->Update();
    polys = geometryFilter->GetOutput();
    }
  else
    {
    polys = static_cast<vtkPolyData *>(dataset);
    }

  // we only export Polygons and Triangle Strips
  if ((polys->GetNumberOfPolys() == 0) && (polys->GetNumberOfStrips() == 0))
    {
      return;
    }

  // write point coordinates
  vtkPoints *points = polys->GetPoints();

  // we use mesh2 since it maps better to how VTK stores
  // polygons/triangle strips
  fprintf(this->FilePtr, "mesh2 {\n");

  fprintf(this->FilePtr, "\tvertex_vectors {\n");
  fprintf(this->FilePtr, VTKPOV_CNTFMT, points->GetNumberOfPoints());
  for (vtkIdType i = 0; i < points->GetNumberOfPoints(); i++)
    {
    double *pos = points->GetPoint(i);
    fprintf(this->FilePtr, "\t\t<%f, %f, %f>,\n", pos[0], pos[1], pos[2]);
    }
  fprintf(this->FilePtr, "\t}\n");

  // write vertex normal
  vtkPointData *pointData = polys->GetPointData();
  if (pointData->GetNormals())
    {
    vtkDataArray *normals = pointData->GetNormals();
    fprintf(this->FilePtr, "\tnormal_vectors {\n");
    fprintf(this->FilePtr, VTKPOV_CNTFMT, normals->GetNumberOfTuples());
    for (vtkIdType i = 0; i < normals->GetNumberOfTuples(); i++)
      {
      double *normal = normals->GetTuple(i);
      fprintf(this->FilePtr, "\t\t<%f, %f, %f>,\n",
              normal[0], normal[1], normal[2]);
      }
    fprintf(this->FilePtr, "\t}\n");
    }

  // TODO: write texture coordinates (uv vectors)

  // write vertex texture, ask mapper to generate color for each vertex if
  // the scalar data visibility is on
  bool scalar_visible = false;
  if (actor->GetMapper()->GetScalarVisibility())
    {
    vtkUnsignedCharArray *color_array = actor->GetMapper()->MapScalars(1.0);
    if (color_array != NULL)
      {
      scalar_visible = true;
      fprintf(this->FilePtr, "\ttexture_list {\n");
      fprintf(this->FilePtr, VTKPOV_CNTFMT, color_array->GetNumberOfTuples());
      for (vtkIdType i = 0; i < color_array->GetNumberOfTuples(); i++) {
        unsigned char *color = color_array->GetPointer(4*i);
        fprintf(this->FilePtr,
                "\t\ttexture { pigment {color rgbf <%f, %f, %f, %f> } },\n",
                color[0]/255.0,
                color[1]/255.0,
                color[2]/255.0,
                1.0 - color[3]/255.0);
        }
      fprintf(this->FilePtr, "\t}\n");
      }
    }

  // write polygons
  if (polys->GetNumberOfPolys() > 0)
    {
    this->WritePolygons(polys, scalar_visible);
    }

  // write triangle strips
  if (polys->GetNumberOfStrips() > 0)
    {
    this->WriteTriangleStrips(polys, scalar_visible);
    }

  // write transformation for the actor, it is column major and looks like transposed
  vtkMatrix4x4 *matrix = actor->GetMatrix();
  fprintf(this->FilePtr, "\tmatrix < %f, %f, %f,\n",
          matrix->GetElement(0, 0),
          matrix->GetElement(1, 0),
          matrix->GetElement(2, 0));
  fprintf(this->FilePtr, "\t\t %f, %f, %f,\n",
          matrix->GetElement(0, 1),
          matrix->GetElement(1, 1),
          matrix->GetElement(2, 1));
  fprintf(this->FilePtr, "\t\t %f, %f, %f,\n",
          matrix->GetElement(0, 2),
          matrix->GetElement(1, 2),
          matrix->GetElement(2, 2));
  fprintf(this->FilePtr, "\t\t %f, %f, %f >\n",
          matrix->GetElement(0, 3),
          matrix->GetElement(1, 3),
          matrix->GetElement(2, 3));

  // write property
  this->WriteProperty(actor->GetProperty());

  // done with this actor
  fprintf(this->FilePtr, "}\n\n");

  if (geometryFilter)
    {
    geometryFilter->Delete();
    }
}

void vtkPOVExporter::WritePolygons(vtkPolyData *polys, bool scalar_visible)
{
  // write polygons with on the fly triangulation,
  // assuming polygon are simple and can be triangulated into "fans"
  vtkIdType numtriangles = 0;
  vtkCellArray *cells = polys->GetPolys();
  vtkIdType npts = 0, *pts = 0;

  // first pass,
  // calculate how many triangles there will be after triangulation
  for (cells->InitTraversal(); cells->GetNextCell(npts, pts);)
    {
    // the number of triangles for each polygon will be # of vertex - 2
    numtriangles += (npts - 2);
    }

  // second pass, triangulate and write face indices
  fprintf(this->FilePtr, "\tface_indices {\n");
  fprintf(this->FilePtr, VTKPOV_CNTFMT, numtriangles);
  for (cells->InitTraversal(); cells->GetNextCell(npts, pts);)
    {
    vtkIdType triangle[3];
    // the first triangle
    triangle[0] = pts[0];
    triangle[1] = pts[1];
    triangle[2] = pts[2];

    fprintf(this->FilePtr, VTKPOV_TRIFMT1,
            triangle[0], triangle[1], triangle[2]);
    if (scalar_visible)
      {
      fprintf(this->FilePtr, VTKPOV_TRIFMT2,
              triangle[0], triangle[1], triangle[2]);
      }
    else
      {
      fprintf(this->FilePtr, "\n");
      }

  // the rest of triangles
    for (vtkIdType i = 3; i < npts; i++)
      {
      triangle[1] = triangle[2];
      triangle[2] = pts[i];
      fprintf(this->FilePtr, VTKPOV_TRIFMT1,
              triangle[0], triangle[1], triangle[2]);
      if (scalar_visible)
        {
        fprintf(this->FilePtr, VTKPOV_TRIFMT2,
                triangle[0], triangle[1], triangle[2]);
        }
      else
        {
        fprintf(this->FilePtr, "\n");
        }
      }
    }
  fprintf(this->FilePtr, "\t}\n");

  // third pass, the same thing as 2nd pass but for normal_indices
  if (polys->GetPointData()->GetNormals())
    {
    fprintf(this->FilePtr, "\tnormal_indices {\n");
    fprintf(this->FilePtr, VTKPOV_CNTFMT, numtriangles);
    for (cells->InitTraversal(); cells->GetNextCell(npts, pts);)
      {
      vtkIdType triangle[3];
      // the first triangle
      triangle[0] = pts[0];
      triangle[1] = pts[1];
      triangle[2] = pts[2];

      fprintf(this->FilePtr, VTKPOV_TRIFMT1,
              triangle[0], triangle[1], triangle[2]);
      fprintf(this->FilePtr, "\n");

      // the rest of triangles
      for (vtkIdType i = 3; i < npts; i++)
        {
        triangle[1] = triangle[2];
        triangle[2] = pts[i];
        fprintf(this->FilePtr, VTKPOV_TRIFMT1,
                triangle[0], triangle[1], triangle[2]);
        fprintf(this->FilePtr, "\n");
        }
      }
    fprintf(this->FilePtr, "\t}\n");
    }

  // TODO: 4th pass, texture indices

}

void vtkPOVExporter::WriteTriangleStrips(
  vtkPolyData *polys, bool scalar_visible)
{
  // convert triangle strips into triangles
  vtkIdType numtriangles = 0;
  vtkCellArray *cells = polys->GetStrips();
  vtkIdType npts = 0, *pts = 0;

  // first pass, calculate how many triangles there will be after conversion
  for (cells->InitTraversal(); cells->GetNextCell(npts, pts);)
    {
    // the number of triangles for each polygon will be # of vertex - 2
    numtriangles += (npts - 2);
    }

  // second pass, convert to triangles and write face indices
  fprintf(this->FilePtr, "\tface_indices {\n");
  fprintf(this->FilePtr, VTKPOV_CNTFMT, numtriangles);
  for (cells->InitTraversal(); cells->GetNextCell(npts, pts);)
    {
    vtkIdType triangle[3];
    // the first triangle
    triangle[0] = pts[0];
    triangle[1] = pts[1];
    triangle[2] = pts[2];

    fprintf(this->FilePtr, VTKPOV_TRIFMT1,
            triangle[0], triangle[1], triangle[2]);
    if (scalar_visible)
      {
      fprintf(this->FilePtr, VTKPOV_TRIFMT2,
              triangle[0], triangle[1], triangle[2]);
      }
    else
      {
      fprintf(this->FilePtr, "\n");
      }

  // the rest of triangles
    for (vtkIdType i = 3; i < npts; i++)
      {
      triangle[0] = triangle[1];
      triangle[1] = triangle[2];
      triangle[2] = pts[i];
      fprintf(this->FilePtr, VTKPOV_TRIFMT1,
              triangle[0], triangle[1], triangle[2]);
      if (scalar_visible)
        {
        fprintf(this->FilePtr, VTKPOV_TRIFMT2,
                triangle[0], triangle[1], triangle[2]);
        }
      else
        {
        fprintf(this->FilePtr, "\n");
        }
      }
    }
  fprintf(this->FilePtr, "\t}\n");

  // third pass, the same thing as 2nd pass but for normal_indices
  if (polys->GetPointData()->GetNormals())
    {
    fprintf(this->FilePtr, "\tnormal_indices {\n");
    fprintf(this->FilePtr, VTKPOV_CNTFMT, numtriangles);
    for (cells->InitTraversal(); cells->GetNextCell(npts, pts);)
      {
      vtkIdType triangle[3];
      // the first triangle
      triangle[0] = pts[0];
      triangle[1] = pts[1];
      triangle[2] = pts[2];

      fprintf(this->FilePtr, VTKPOV_TRIFMT1,
              triangle[0], triangle[1], triangle[2]);
      fprintf(this->FilePtr, "\n");

      // the rest of triangles
      for (vtkIdType i = 3; i < npts; i++)
        {
        triangle[0] = triangle[1];
        triangle[1] = triangle[2];
        triangle[2] = pts[i];
        fprintf(this->FilePtr, VTKPOV_TRIFMT1,
                triangle[0], triangle[1], triangle[2]);
        fprintf(this->FilePtr, "\n");
        }
      }
    fprintf(this->FilePtr, "\t}\n");
    }

  // TODO: 4th pass, texture indices
}

void vtkPOVExporter::WriteProperty(vtkProperty *property)
{
  fprintf(this->FilePtr, "\ttexture {\n");

  /* write color */
  fprintf(this->FilePtr, "\t\tpigment {\n");
  double *color   = property->GetColor();
  fprintf(this->FilePtr, "\t\t\tcolor rgbf <%f, %f, %f %f>\n",
          color[0], color[1], color[2], 1.0 - property->GetOpacity());
  fprintf(this->FilePtr, "\t\t}\n");

  /* write ambient, diffuse and specular coefficients */
  fprintf(this->FilePtr, "\t\tfinish {\n\t\t\t");
  fprintf(this->FilePtr, "ambient %f  ", property->GetAmbient());
  fprintf(this->FilePtr, "diffuse %f  ", property->GetDiffuse());
  fprintf(this->FilePtr, "phong %f  ", property->GetSpecular());
  fprintf(this->FilePtr, "phong_size %f  ", property->GetSpecularPower());
  fprintf(this->FilePtr, "\n\t\t}\n");

  fprintf(this->FilePtr, "\t}\n");
}

void vtkPOVExporter::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);
  if (this->FileName)
    {
    os << indent << "FileName: " << this->FileName << "\n";
    }
  else
    {
    os << indent << "FileName: (null)\n";
    }
}