vtkIVExporter.cxx 19.8 KB
Newer Older
Ken Martin's avatar
Ken Martin committed
1 2 3 4 5
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkIVExporter.cxx

6
  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7 8
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
Ken Martin's avatar
Ken Martin committed
9

10 11
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12
     PURPOSE.  See the above copyright notice for more information.
Ken Martin's avatar
Ken Martin committed
13 14 15

=========================================================================*/
#include "vtkIVExporter.h"
16 17

#include "vtkAssemblyNode.h"
Andy Cedilnik's avatar
Andy Cedilnik committed
18
#include "vtkAssemblyPath.h"
19
#include "vtkCamera.h"
20
#include "vtkCellArray.h"
Ken Martin's avatar
Ken Martin committed
21
#include "vtkGeometryFilter.h"
22
#include "vtkImageData.h"
23
#include "vtkLight.h"
24
#include "vtkLightCollection.h"
Ken Martin's avatar
Ken Martin committed
25
#include "vtkMath.h"
26
#include "vtkObjectFactory.h"
Andy Cedilnik's avatar
Andy Cedilnik committed
27
#include "vtkPointData.h"
28
#include "vtkPolyData.h"
29 30
#include "vtkPolyDataMapper.h"
#include "vtkProperty.h"
31
#include "vtkRenderWindow.h"
32
#include "vtkRendererCollection.h"
33
#include "vtkTexture.h"
34
#include "vtkTransform.h"
35
#include "vtkUnsignedCharArray.h"
36

Brad King's avatar
Brad King committed
37
vtkStandardNewMacro(vtkIVExporter);
38

Ken Martin's avatar
Ken Martin committed
39 40 41 42 43 44 45
vtkIVExporter::vtkIVExporter()
{
  this->FileName = NULL;
}

vtkIVExporter::~vtkIVExporter()
{
Bill Lorensen's avatar
Bill Lorensen committed
46 47 48 49
  if ( this->FileName )
    {
    delete [] this->FileName;
    }
Ken Martin's avatar
Ken Martin committed
50 51 52 53
}

static char indent[256];
int indent_now = 0;
Bill Lorensen's avatar
Bill Lorensen committed
54
#define VTK_INDENT_MORE { indent[indent_now] = ' '; \
55 56
                                          indent_now += 4; \
                                      indent[indent_now] = 0; }
Bill Lorensen's avatar
Bill Lorensen committed
57
#define VTK_INDENT_LESS { indent[indent_now] = ' '; \
58 59
                                          indent_now -= 4; \
                                      indent[indent_now] = 0; }
Ken Martin's avatar
Ken Martin committed
60 61 62 63 64 65 66 67 68 69

void vtkIVExporter::WriteData()
{
  vtkRenderer *ren;
  FILE *fp;
  vtkActorCollection *ac;
  vtkActor *anActor, *aPart;
  vtkLightCollection *lc;
  vtkLight *aLight;
  vtkCamera *cam;
Ken Martin's avatar
Ken Martin committed
70
  double *tempd;
Ken Martin's avatar
Ken Martin committed
71
  
Bill Lorensen's avatar
Bill Lorensen committed
72 73 74 75
  for (int i=0;i<256;i++)
    {
    indent[i] = ' ';
    }
Ken Martin's avatar
Ken Martin committed
76 77 78 79 80 81 82 83 84 85
  indent[indent_now] = 0;

  // make sure the user specified a filename
  if ( this->FileName == NULL)
    {
    vtkErrorMacro(<< "Please specify FileName to use");
    return;
    }

  // first make sure there is only one renderer in this rendering window
86
  if (this->RenderWindow->GetRenderers()->GetNumberOfItems() > 1)
Ken Martin's avatar
Ken Martin committed
87 88 89 90 91 92
    {
    vtkErrorMacro(<< "OpenInventor files only support one renderer per window.");
    return;
    }

  // get the renderer
Ken Martin's avatar
Ken Martin committed
93
  ren = this->RenderWindow->GetRenderers()->GetFirstRenderer();
Ken Martin's avatar
Ken Martin committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
  
  // make sure it has at least one actor
  if (ren->GetActors()->GetNumberOfItems() < 1)
    {
    vtkErrorMacro(<< "no actors found for writing OpenInventor file.");
    return;
    }
    
  // try opening the files
  fp = fopen(this->FileName,"w");
  if (!fp)
    {
    vtkErrorMacro(<< "unable to open OpenInventor file " << this->FileName);
    return;
    }
  
  //
  //  Write header
  //
  vtkDebugMacro("Writing OpenInventor file");
  fprintf(fp,"#Inventor V2.0 ascii\n");
  fprintf(fp,"# OpenInventor file written by the visualization toolkit\n\n");

  fprintf(fp, "Separator {\n");
Bill Lorensen's avatar
Bill Lorensen committed
118
  VTK_INDENT_MORE;
Ken Martin's avatar
Ken Martin committed
119 120 121 122 123 124 125 126 127 128 129

  // do the camera
  cam = ren->GetActiveCamera();
  if (cam->GetParallelProjection()) 
    {
    fprintf(fp,"%sOrthographicCamera\n%s{\n", indent, indent);
    } 
  else 
    {
    // this assumes the aspect ratio is 1
    fprintf(fp,"%sPerspectiveCamera\n%s{\n%s    heightAngle %f\n",
130 131
            indent, indent, indent,
            cam->GetViewAngle()*3.1415926/180.0);
Ken Martin's avatar
Ken Martin committed
132
    }
Bill Lorensen's avatar
Bill Lorensen committed
133
  VTK_INDENT_MORE;
Ken Martin's avatar
Ken Martin committed
134 135 136 137
  fprintf(fp,"%snearDistance %f\n",indent, cam->GetClippingRange()[0]);
  fprintf(fp,"%sfarDistance %f\n",indent, cam->GetClippingRange()[1]);
  fprintf(fp,"%sfocalDistance %f\n",indent, cam->GetDistance());
  fprintf(fp,"%sposition %f %f %f\n", indent, cam->GetPosition()[0],
138
          cam->GetPosition()[1], cam->GetPosition()[2]);
Ken Martin's avatar
Ken Martin committed
139
  tempd = cam->GetOrientationWXYZ();
Ken Martin's avatar
Ken Martin committed
140
  fprintf(fp,"%sorientation %g %g %g %g\n%s}\n", indent,
Ken Martin's avatar
Ken Martin committed
141
          tempd[1], tempd[2], tempd[3], tempd[0]*3.1415926/180.0, indent);
Bill Lorensen's avatar
Bill Lorensen committed
142
  VTK_INDENT_LESS;
Ken Martin's avatar
Ken Martin committed
143 144 145 146 147 148 149 150

  // do the lights first the ambient then the others
  fprintf(fp,"# The following environment information is disabled\n");
  fprintf(fp,"# because a popular viewer (Template Graphics Software SceneViewer) has\n");
  fprintf(fp,"# trouble (access violations under Windows NT) with it.\n");
  fprintf(fp,"#%sEnvironment {\n", indent);
  // couldn't figure out a way to do headlight -- seems to be a property of the
  // viewer not the model
Bill Lorensen's avatar
Bill Lorensen committed
151
  VTK_INDENT_MORE;
Ken Martin's avatar
Ken Martin committed
152 153
  fprintf(fp,"#%sambientIntensity 1.0 # ambient light\n", indent);
  fprintf(fp,"#%sambientColor %f %f %f }\n\n", indent,
154
          ren->GetAmbient()[0], ren->GetAmbient()[1], ren->GetAmbient()[2]);
Bill Lorensen's avatar
Bill Lorensen committed
155
  VTK_INDENT_LESS;
Ken Martin's avatar
Ken Martin committed
156 157 158 159

  // make sure we have a default light
  // if we dont then use a headlight
  lc = ren->GetLights();
Ken Martin's avatar
Ken Martin committed
160 161
  vtkCollectionSimpleIterator lsit;
  for (lc->InitTraversal(lsit); (aLight = lc->GetNextLight(lsit)); )
Ken Martin's avatar
Ken Martin committed
162 163 164 165 166 167
    {
    this->WriteALight(aLight, fp);
    }

  // do the actors now
  ac = ren->GetActors();
Will Schroeder's avatar
Will Schroeder committed
168
  vtkAssemblyPath *apath;
Ken Martin's avatar
Ken Martin committed
169 170
  vtkCollectionSimpleIterator ait;
  for (ac->InitTraversal(ait); (anActor = ac->GetNextActor(ait)); )
Ken Martin's avatar
Ken Martin committed
171
    {
172
    for (anActor->InitPathTraversal(); (apath=anActor->GetNextPath()); )
Ken Martin's avatar
Ken Martin committed
173
      {
174
      aPart=static_cast<vtkActor *>(apath->GetLastNode()->GetViewProp());
Ken Martin's avatar
Ken Martin committed
175 176 177 178
      this->WriteAnActor(aPart, fp);
      }
    }

Bill Lorensen's avatar
Bill Lorensen committed
179
  VTK_INDENT_LESS;
Ken Martin's avatar
Ken Martin committed
180 181 182 183 184 185 186
  fprintf(fp, "}\n"); // close Separator

  fclose(fp);
}

void vtkIVExporter::WriteALight(vtkLight *aLight, FILE *fp)
{
Ken Martin's avatar
Ken Martin committed
187
  double *pos, *focus, *color;
Ken Martin's avatar
Ken Martin committed
188 189 190 191
  float dir[3];
  
  pos = aLight->GetPosition();
  focus = aLight->GetFocalPoint();
192
  color = aLight->GetDiffuseColor();
Ken Martin's avatar
Ken Martin committed
193 194 195 196 197 198 199 200

  dir[0] = focus[0] - pos[0];
  dir[1] = focus[1] - pos[1];
  dir[2] = focus[2] - pos[2];
  vtkMath::Normalize(dir);
    
  if (aLight->GetPositional())
    {
Ken Martin's avatar
Ken Martin committed
201
    double *attn;
Ken Martin's avatar
Ken Martin committed
202 203 204 205
    
    if (aLight->GetConeAngle() >= 180.0)
      {
      fprintf(fp,"%sPointLight {\n", indent);
Bill Lorensen's avatar
Bill Lorensen committed
206
      VTK_INDENT_MORE;
Ken Martin's avatar
Ken Martin committed
207 208 209 210
      }
    else
      { 
      fprintf(fp,"%sSpotLight {\n", indent);
211
          VTK_INDENT_MORE;
Ken Martin's avatar
Ken Martin committed
212 213
      fprintf(fp,"%sdirection %f %f %f\n", indent, dir[0], dir[1], dir[2]);
      fprintf(fp,"%scutOffAngle %f\n", indent, aLight->GetConeAngle());
214 215 216
          // the following ignores linear and quadratic attenuation values
          attn = aLight->GetAttenuationValues();
          fprintf(fp,"%sdropOffRate %f\n", indent, attn[0]);
Ken Martin's avatar
Ken Martin committed
217 218 219 220 221 222
      }
    fprintf(fp,"%slocation %f %f %f\n", indent, pos[0], pos[1], pos[2]);
    }
  else
    {
    fprintf(fp,"%sDirectionalLight {\n", indent);
223
        VTK_INDENT_MORE;
Ken Martin's avatar
Ken Martin committed
224 225 226 227 228 229 230 231 232 233 234 235 236
    fprintf(fp,"%sdirection %f %f %f\n", indent, dir[0], dir[1], dir[2]);
    }

  fprintf(fp,"%scolor %f %f %f\n", indent, color[0], color[1], color[2]);
  fprintf(fp,"%sintensity %f\n", indent, aLight->GetIntensity());
  if (aLight->GetSwitch())
    {
    fprintf(fp,"%son TRUE\n%s}\n", indent, indent);
    }
  else
    {
    fprintf(fp,"%son FALSE\n%s}\n", indent, indent);
    }
Bill Lorensen's avatar
Bill Lorensen committed
237
  VTK_INDENT_LESS;
Ken Martin's avatar
Ken Martin committed
238 239 240 241 242 243 244 245
}

void vtkIVExporter::WriteAnActor(vtkActor *anActor, FILE *fp)
{
  vtkDataSet *ds;
  vtkPolyData *pd;
  vtkGeometryFilter *gf = NULL;
  vtkPointData *pntData;
246
  vtkPoints *points;
247 248
  vtkDataArray *normals = NULL;
  vtkDataArray *tcoords = NULL;
Ken Martin's avatar
Ken Martin committed
249 250
  int i;
  vtkProperty *prop;
Ken Martin's avatar
Ken Martin committed
251
  double *tempd;
Ken Martin's avatar
Ken Martin committed
252
  vtkCellArray *cells;
253 254
  vtkIdType npts = 0;
  vtkIdType *indx = 0;
Ken Martin's avatar
Ken Martin committed
255 256
  float tempf2;
  vtkPolyDataMapper *pm;
257
  vtkUnsignedCharArray *colors;
Ken Martin's avatar
Ken Martin committed
258
  double *p;
Ken Martin's avatar
Ken Martin committed
259 260 261
  unsigned char *c;
  vtkTransform *trans;
  
262 263 264 265 266 267
  // see if the actor has a mapper. it could be an assembly
  if (anActor->GetMapper() == NULL)
    {
    return;
    }

Ken Martin's avatar
Ken Martin committed
268
  fprintf(fp,"%sSeparator {\n", indent);
Bill Lorensen's avatar
Bill Lorensen committed
269
  VTK_INDENT_MORE;
Ken Martin's avatar
Ken Martin committed
270 271

  // first stuff out the transform
272
  trans = vtkTransform::New();
273
  trans->SetMatrix(anActor->vtkProp3D::GetMatrix());
Ken Martin's avatar
Ken Martin committed
274 275
  
  fprintf(fp,"%sTransform {\n", indent);
Bill Lorensen's avatar
Bill Lorensen committed
276
  VTK_INDENT_MORE;
Ken Martin's avatar
Ken Martin committed
277 278 279 280 281 282 283
  tempd = trans->GetPosition();
  fprintf(fp,"%stranslation %g %g %g\n", indent, tempd[0], tempd[1], tempd[2]);
  tempd = trans->GetOrientationWXYZ();
  fprintf(fp,"%srotation %g %g %g %g\n", indent, tempd[1], tempd[2], 
          tempd[3], tempd[0]*3.1415926/180.0);
  tempd = trans->GetScale();
  fprintf(fp,"%sscaleFactor %g %g %g\n", indent, tempd[0], tempd[1], tempd[2]);
Ken Martin's avatar
Ken Martin committed
284
  fprintf(fp,"%s}\n", indent);
Bill Lorensen's avatar
Bill Lorensen committed
285
  VTK_INDENT_LESS;
Ken Martin's avatar
Ken Martin committed
286 287 288
  trans->Delete();
  
  // get the mappers input and matrix
289
  ds = anActor->GetMapper()->GetInput();
290 291

  vtkAlgorithmOutput* pdProducer = 0;
Ken Martin's avatar
Ken Martin committed
292
  // we really want polydata
293
  if ( ds->GetDataObjectType() != VTK_POLY_DATA )
Ken Martin's avatar
Ken Martin committed
294
    {
Bill Lorensen's avatar
Bill Lorensen committed
295
    gf = vtkGeometryFilter::New();
296 297
    gf->SetInputConnection(
      anActor->GetMapper()->GetInputConnection(0, 0));
Ken Martin's avatar
Ken Martin committed
298 299
    gf->Update();
    pd = gf->GetOutput();
300
    pdProducer = gf->GetOutputPort();
Ken Martin's avatar
Ken Martin committed
301 302 303
    }
  else
    {
Berk Geveci's avatar
Berk Geveci committed
304
    anActor->GetMapper()->GetInputAlgorithm()->Update();
305
    pd = static_cast<vtkPolyData *>(ds);
306
    pdProducer = anActor->GetMapper()->GetInputConnection(0, 0);
Ken Martin's avatar
Ken Martin committed
307 308
    }

309
  pm = vtkPolyDataMapper::New();
310
  pm->SetInputConnection(pdProducer);
Ken Martin's avatar
Ken Martin committed
311 312 313 314 315 316 317 318
  pm->SetScalarRange(anActor->GetMapper()->GetScalarRange());
  pm->SetScalarVisibility(anActor->GetMapper()->GetScalarVisibility());
  pm->SetLookupTable(anActor->GetMapper()->GetLookupTable());

  points = pd->GetPoints();
  pntData = pd->GetPointData();
  normals = pntData->GetNormals();
  tcoords = pntData->GetTCoords();
319
  colors  = pm->MapScalars(1.0);
Ken Martin's avatar
Ken Martin committed
320 321
  
  fprintf(fp,"%sMaterial {\n", indent);
Bill Lorensen's avatar
Bill Lorensen committed
322
  VTK_INDENT_MORE;
Ken Martin's avatar
Ken Martin committed
323 324 325 326 327
  
  // write out the material properties to the mat file
  prop = anActor->GetProperty();
  // the following is based on a guess about how VTK's GetAmbient
  // property corresponds to SoMaterial's ambientColor
328
  tempf2 = prop->GetAmbient();
Ken Martin's avatar
Ken Martin committed
329
  tempd = prop->GetAmbientColor();
330
  fprintf(fp,"%sambientColor %g %g %g\n", indent,
Ken Martin's avatar
Ken Martin committed
331
          tempd[0]*tempf2, tempd[1]*tempf2, tempd[2]*tempf2);
Ken Martin's avatar
Ken Martin committed
332
  tempf2 = prop->GetDiffuse();
Ken Martin's avatar
Ken Martin committed
333
  tempd = prop->GetDiffuseColor();
Ken Martin's avatar
Ken Martin committed
334
  fprintf(fp,"%sdiffuseColor %g %g %g\n", indent,
Ken Martin's avatar
Ken Martin committed
335
          tempd[0]*tempf2, tempd[1]*tempf2, tempd[2]*tempf2);
Ken Martin's avatar
Ken Martin committed
336
  tempf2 = prop->GetSpecular();
Ken Martin's avatar
Ken Martin committed
337
  tempd = prop->GetSpecularColor();
Ken Martin's avatar
Ken Martin committed
338
  fprintf(fp,"%sspecularColor %g %g %g\n", indent,
Ken Martin's avatar
Ken Martin committed
339
          tempd[0]*tempf2, tempd[1]*tempf2, tempd[2]*tempf2);
Ken Martin's avatar
Ken Martin committed
340 341 342
  fprintf(fp,"%sshininess %g\n", indent,prop->GetSpecularPower()/128.0);
  fprintf(fp,"%stransparency %g\n", indent,1.0 - prop->GetOpacity());
  fprintf(fp,"%s}\n", indent); // close matrial
Bill Lorensen's avatar
Bill Lorensen committed
343
  VTK_INDENT_LESS;
Ken Martin's avatar
Ken Martin committed
344 345 346 347 348 349

  // is there a texture map
  if (anActor->GetTexture())
    {
    vtkTexture *aTexture = anActor->GetTexture();
    int *size, xsize, ysize, bpp;
350 351
    vtkDataArray *scalars;
    vtkUnsignedCharArray *mappedScalars;
Ken Martin's avatar
Ken Martin committed
352 353 354 355 356 357 358 359 360
    unsigned char *txtrData;
    int totalValues;
    
    // make sure it is updated and then get some info
    if (aTexture->GetInput() == NULL)
      {
      vtkErrorMacro(<< "texture has no input!\n");
      return;
      }
Berk Geveci's avatar
Berk Geveci committed
361
    aTexture->GetInputAlgorithm()->Update();
Ken Martin's avatar
Ken Martin committed
362
    size = aTexture->GetInput()->GetDimensions();
363
    scalars = aTexture->GetInput()->GetPointData()->GetScalars();
Ken Martin's avatar
Ken Martin committed
364 365 366 367 368 369 370 371 372

    // make sure scalars are non null
    if (!scalars) 
      {
      vtkErrorMacro(<< "No scalar values found for texture input!\n");
      return;
      }

    // make sure using unsigned char data of color scalars type
373
    if (aTexture->GetMapColorScalarsThroughLookupTable () ||
374
        (scalars->GetDataType() != VTK_UNSIGNED_CHAR) )
Ken Martin's avatar
Ken Martin committed
375 376 377 378 379
      {
      mappedScalars = aTexture->GetMappedScalars ();
      }
    else
      {
380
      mappedScalars = static_cast<vtkUnsignedCharArray*>(scalars);
Ken Martin's avatar
Ken Martin committed
381 382 383 384 385 386 387 388 389 390 391 392 393
      }

    // we only support 2d texture maps right now
    // so one of the three sizes must be 1, but it 
    // could be any of them, so lets find it
    if (size[0] == 1)
      {
      xsize = size[1]; ysize = size[2];
      }
    else
      {
      xsize = size[0];
      if (size[1] == 1)
394 395 396
        {
        ysize = size[2];
        }
Ken Martin's avatar
Ken Martin committed
397
      else
398 399 400 401 402 403 404 405
        {
        ysize = size[1];
        if (size[2] != 1)
          {
          vtkErrorMacro(<< "3D texture maps currently are not supported!\n");
          return;
          }
        }
Ken Martin's avatar
Ken Martin committed
406 407 408
      }

    fprintf(fp, "%sTexture2 {\n", indent);
Bill Lorensen's avatar
Bill Lorensen committed
409
    VTK_INDENT_MORE;
410
    bpp = mappedScalars->GetNumberOfComponents();
Ken Martin's avatar
Ken Martin committed
411
    fprintf(fp, "%simage %d %d %d\n", indent, xsize, ysize, bpp);
Bill Lorensen's avatar
Bill Lorensen committed
412
    VTK_INDENT_MORE;
413
    txtrData = static_cast<vtkUnsignedCharArray *>(mappedScalars)->GetPointer(0);
Ken Martin's avatar
Ken Martin committed
414 415 416 417 418 419 420
    totalValues = xsize*ysize;
    fprintf(fp,"%s",indent);
    for (i = 0; i < totalValues; i++)
      {
      fprintf(fp,"%.2x",*txtrData);
      txtrData++;
      if (bpp > 1)
421 422 423 424
        {
        fprintf(fp,"%.2x",*txtrData);
        txtrData++;
        }
Ken Martin's avatar
Ken Martin committed
425
      if (bpp > 2) 
426 427 428 429
        {
        fprintf(fp,"%.2x",*txtrData);
        txtrData++;
        }
Ken Martin's avatar
Ken Martin committed
430
      if (bpp > 3) 
431 432 433 434
        {
        fprintf(fp,"%.2x",*txtrData);
        txtrData++;
        }
Bill Lorensen's avatar
Bill Lorensen committed
435
      if (i%8 == 0)
436 437 438
        {
        fprintf(fp,"\n%s    ", indent);
        }
Bill Lorensen's avatar
Bill Lorensen committed
439
      else
440 441 442
        {
        fprintf(fp," ");
        }
Ken Martin's avatar
Ken Martin committed
443
      }
Bill Lorensen's avatar
Bill Lorensen committed
444
    VTK_INDENT_LESS;
Ken Martin's avatar
Ken Martin committed
445
    fprintf(fp, "%s}\n", indent);
Bill Lorensen's avatar
Bill Lorensen committed
446
    VTK_INDENT_LESS;
Ken Martin's avatar
Ken Martin committed
447 448 449
    }

  // write out point data if any
450
  this->WritePointData(points, normals, tcoords, colors, fp);
Ken Martin's avatar
Ken Martin committed
451 452 453 454 455

  // write out polys if any
  if (pd->GetNumberOfPolys() > 0)
    {
    fprintf(fp,"%sIndexedFaceSet {\n", indent);
456
        VTK_INDENT_MORE;
Ken Martin's avatar
Ken Martin committed
457
    fprintf(fp,"%scoordIndex  [\n", indent);
458
        VTK_INDENT_MORE;
Ken Martin's avatar
Ken Martin committed
459 460 461 462 463 464
    
    cells = pd->GetPolys();
    for (cells->InitTraversal(); cells->GetNextCell(npts,indx); )
      {
      fprintf(fp,"%s", indent);
      for (i = 0; i < npts; i++)
465
        {
Amy Squillacote's avatar
Amy Squillacote committed
466
        // treating vtkIdType as int
467
        fprintf(fp,"%i, ",static_cast<int>(indx[i]));
468 469 470 471 472
        if (((i+1)%10) == 0)
          {
          fprintf(fp, "\n%s    ", indent);
          }
        }
Ken Martin's avatar
Ken Martin committed
473 474 475
      fprintf(fp,"-1,\n");
      }
    fprintf(fp,"%s]\n", indent);
476
        VTK_INDENT_LESS;
Ken Martin's avatar
Ken Martin committed
477
    fprintf(fp,"%s}\n", indent);
478
        VTK_INDENT_LESS;
Ken Martin's avatar
Ken Martin committed
479 480 481 482 483 484
  }

  // write out tstrips if any
  if (pd->GetNumberOfStrips() > 0)
    {
    fprintf(fp,"%sIndexedTriangleStripSet {\n", indent);
Bill Lorensen's avatar
Bill Lorensen committed
485
    VTK_INDENT_MORE;
Ken Martin's avatar
Ken Martin committed
486
    fprintf(fp,"%scoordIndex  [\n", indent);
Bill Lorensen's avatar
Bill Lorensen committed
487
    VTK_INDENT_MORE;
Ken Martin's avatar
Ken Martin committed
488 489 490 491 492
    cells = pd->GetStrips();
    for (cells->InitTraversal(); cells->GetNextCell(npts,indx); )
      {
      fprintf(fp,"%s", indent);
      for (i = 0; i < npts; i++)
493
        {
Amy Squillacote's avatar
Amy Squillacote committed
494
        // treating vtkIdType as int
495
        fprintf(fp,"%i, ", static_cast<int>(indx[i]));
496 497 498 499 500
        if (((i+1)%10) == 0)
          {
          fprintf(fp, "\n%s    ", indent);
          }
        }
Ken Martin's avatar
Ken Martin committed
501 502 503
      fprintf(fp,"-1,\n");
      }
    fprintf(fp,"%s]\n", indent);
Bill Lorensen's avatar
Bill Lorensen committed
504
    VTK_INDENT_LESS;
Ken Martin's avatar
Ken Martin committed
505
    fprintf(fp,"%s}\n", indent);
Bill Lorensen's avatar
Bill Lorensen committed
506
    VTK_INDENT_LESS;
Ken Martin's avatar
Ken Martin committed
507 508 509 510 511 512
    }
  
  // write out lines if any
  if (pd->GetNumberOfLines() > 0)
    {
    fprintf(fp,"%sIndexedLineSet {\n", indent);
Bill Lorensen's avatar
Bill Lorensen committed
513
    VTK_INDENT_MORE;
Ken Martin's avatar
Ken Martin committed
514
    fprintf(fp,"%scoordIndex  [\n", indent);
Bill Lorensen's avatar
Bill Lorensen committed
515
    VTK_INDENT_MORE;
Ken Martin's avatar
Ken Martin committed
516 517 518 519 520
    cells = pd->GetLines();
    for (cells->InitTraversal(); cells->GetNextCell(npts,indx); )
      {
      fprintf(fp,"%s", indent);
      for (i = 0; i < npts; i++)
521
        {
Amy Squillacote's avatar
Amy Squillacote committed
522
        // treating vtkIdType as int
523
        fprintf(fp,"%i, ", static_cast<int>(indx[i]));
524 525 526 527 528
        if (((i+1)%10) == 0)
          {
          fprintf(fp, "\n%s    ", indent);
          }
        }
Ken Martin's avatar
Ken Martin committed
529 530 531
      fprintf(fp,"-1,\n");
      }
    fprintf(fp,"%s]\n", indent);
Bill Lorensen's avatar
Bill Lorensen committed
532
    VTK_INDENT_LESS;
Ken Martin's avatar
Ken Martin committed
533
    fprintf(fp,"%s}\n", indent);
Bill Lorensen's avatar
Bill Lorensen committed
534
    VTK_INDENT_LESS;
Ken Martin's avatar
Ken Martin committed
535 536 537 538 539 540 541
    }
  
  // write out verts if any
  // (more complex because there is no IndexedPointSet)
  if (pd->GetNumberOfVerts() > 0)
    {
    fprintf(fp, "%sSeparator {\n", indent);
Bill Lorensen's avatar
Bill Lorensen committed
542
    VTK_INDENT_MORE;
Ken Martin's avatar
Ken Martin committed
543
    fprintf(fp, "%sCoordinate3 {\n", indent);
Bill Lorensen's avatar
Bill Lorensen committed
544
    VTK_INDENT_MORE;
Ken Martin's avatar
Ken Martin committed
545
    fprintf(fp,"%spoint [", indent);
Bill Lorensen's avatar
Bill Lorensen committed
546
    VTK_INDENT_MORE;
Ken Martin's avatar
Ken Martin committed
547 548 549 550
    cells = pd->GetVerts();
    for (cells->InitTraversal(); cells->GetNextCell(npts,indx); )
      {
      for (i = 0; i < npts; i++)
551 552 553 554
        {
        p = points->GetPoint(indx[i]);
        fprintf (fp,"%s%g %g %g,\n", indent, p[0], p[1], p[2]);
        }
Ken Martin's avatar
Ken Martin committed
555 556
      }
    fprintf(fp,"%s]\n", indent);
Bill Lorensen's avatar
Bill Lorensen committed
557
    VTK_INDENT_LESS;
Ken Martin's avatar
Ken Martin committed
558
    fprintf(fp,"%s}\n", indent);
Bill Lorensen's avatar
Bill Lorensen committed
559
    VTK_INDENT_LESS;
Ken Martin's avatar
Ken Martin committed
560 561 562
    if (colors)
      {
      fprintf(fp,"%sPackedColor {", indent);
Bill Lorensen's avatar
Bill Lorensen committed
563
      VTK_INDENT_MORE;
Ken Martin's avatar
Ken Martin committed
564
      fprintf(fp,"%srgba [\n", indent);
Bill Lorensen's avatar
Bill Lorensen committed
565
      VTK_INDENT_MORE;
Ken Martin's avatar
Ken Martin committed
566
      for (cells->InitTraversal(); cells->GetNextCell(npts,indx); )
567 568 569 570 571 572
        {
        fprintf(fp,"%s", indent);
        for (i = 0; i < npts; i++)
          {
          c = colors->GetPointer(4*indx[i]);
          fprintf (fp,"%#lx, ", 
573 574 575 576
                   (static_cast<unsigned long>(c[3]) << 24) |
                   (static_cast<unsigned long>(c[2])<<16) |
                   (static_cast<unsigned long>(c[1])<<8) |
                   static_cast<unsigned long>(c[0]));
577 578 579 580 581 582 583

          if (((i+1)%5) == 0)
            {
            fprintf(fp, "\n%s", indent);
            }
          }
        }
Ken Martin's avatar
Ken Martin committed
584
      fprintf(fp,"\n%s]\n", indent);
Bill Lorensen's avatar
Bill Lorensen committed
585
      VTK_INDENT_LESS;
Ken Martin's avatar
Ken Martin committed
586
      fprintf(fp,"%s}\n", indent);
Bill Lorensen's avatar
Bill Lorensen committed
587
      VTK_INDENT_LESS;
Ken Martin's avatar
Ken Martin committed
588 589 590 591
      fprintf(fp,"%sMaterialBinding { value PER_VERTEX_INDEXED }\n", indent);
      }
    
    fprintf(fp, "%sPointSet {\n", indent);
Bill Lorensen's avatar
Bill Lorensen committed
592
    VTK_INDENT_MORE;
Amy Squillacote's avatar
Amy Squillacote committed
593
    // treating vtkIdType as int
594
    fprintf(fp, "%snumPoints %d\n", indent, static_cast<int>(npts));
Bill Lorensen's avatar
Bill Lorensen committed
595
    VTK_INDENT_MORE;
Ken Martin's avatar
Ken Martin committed
596
    fprintf(fp, "%s}\n", indent);
Bill Lorensen's avatar
Bill Lorensen committed
597
    VTK_INDENT_LESS;
Ken Martin's avatar
Ken Martin committed
598
    fprintf(fp,"%s}\n", indent); // close the Separator
Bill Lorensen's avatar
Bill Lorensen committed
599
    VTK_INDENT_LESS;
Ken Martin's avatar
Ken Martin committed
600 601
    }  
  fprintf(fp, "%s}\n", indent);
Bill Lorensen's avatar
Bill Lorensen committed
602 603 604 605 606
  VTK_INDENT_LESS;
  if (gf)
    {
    gf->Delete();
    }
Ken Martin's avatar
Ken Martin committed
607 608 609
  pm->Delete();
}

610 611
void vtkIVExporter::WritePointData(vtkPoints *points, vtkDataArray *normals,
                                   vtkDataArray *tcoords, 
612
                                   vtkUnsignedCharArray *colors, FILE *fp)
Ken Martin's avatar
Ken Martin committed
613
{
Ken Martin's avatar
Ken Martin committed
614
  double *p;
Ken Martin's avatar
Ken Martin committed
615 616 617 618 619
  int i;
  unsigned char *c;
  
  // write out the points
  fprintf(fp,"%sCoordinate3 {\n", indent);
Bill Lorensen's avatar
Bill Lorensen committed
620
  VTK_INDENT_MORE;
Ken Martin's avatar
Ken Martin committed
621
  fprintf(fp,"%spoint [\n", indent);
Bill Lorensen's avatar
Bill Lorensen committed
622
  VTK_INDENT_MORE;
Ken Martin's avatar
Ken Martin committed
623 624 625 626 627 628
  for (i = 0; i < points->GetNumberOfPoints(); i++)
    {
    p = points->GetPoint(i);
    fprintf (fp,"%s%g %g %g,\n", indent, p[0], p[1], p[2]);
    }
  fprintf(fp,"%s]\n", indent);
Bill Lorensen's avatar
Bill Lorensen committed
629
  VTK_INDENT_LESS;
Ken Martin's avatar
Ken Martin committed
630
  fprintf(fp,"%s}\n", indent);
Bill Lorensen's avatar
Bill Lorensen committed
631
  VTK_INDENT_LESS;
Ken Martin's avatar
Ken Martin committed
632 633 634 635 636
  
  // write out the point data
  if (normals)
    {
    fprintf(fp,"%sNormal {\n", indent);
637
        VTK_INDENT_MORE;
Ken Martin's avatar
Ken Martin committed
638
    fprintf(fp,"%svector [\n", indent);
639
        VTK_INDENT_MORE;
640
    for (i = 0; i < normals->GetNumberOfTuples(); i++)
Ken Martin's avatar
Ken Martin committed
641
      {
642
      p = normals->GetTuple(i);
Ken Martin's avatar
Ken Martin committed
643 644 645
      fprintf (fp,"%s%g %g %g,\n", indent, p[0], p[1], p[2]);
      }
    fprintf(fp,"%s]\n", indent);
646
        VTK_INDENT_LESS;
Ken Martin's avatar
Ken Martin committed
647
    fprintf(fp,"%s}\n", indent);
648
        VTK_INDENT_LESS;
Ken Martin's avatar
Ken Martin committed
649 650 651 652 653
  }

  // write out the point data
  if (tcoords)
    {
654 655 656 657 658 659
        fprintf(fp,"%sTextureCoordinateBinding  {\n",indent);
        VTK_INDENT_MORE;
        fprintf(fp,"%svalue PER_VERTEX_INDEXED\n",indent);
        VTK_INDENT_LESS;
        fprintf(fp,"%s}\n",indent);
        fprintf(fp,"%sTextureCoordinate2 {\n", indent);
660
    VTK_INDENT_MORE;
Ken Martin's avatar
Ken Martin committed
661
    fprintf(fp,"%spoint [\n", indent);
662
    VTK_INDENT_MORE;
663
    for (i = 0; i < tcoords->GetNumberOfTuples(); i++)
Ken Martin's avatar
Ken Martin committed
664
      {
665
      p = tcoords->GetTuple(i);
Ken Martin's avatar
Ken Martin committed
666 667 668
      fprintf (fp,"%s%g %g,\n", indent, p[0], p[1]);
      }
    fprintf(fp,"%s]\n", indent);
669
        VTK_INDENT_LESS;
Ken Martin's avatar
Ken Martin committed
670
    fprintf(fp,"%s}\n", indent);
671
        VTK_INDENT_LESS;
Ken Martin's avatar
Ken Martin committed
672 673 674 675 676 677
  }

  // write out the point data
  if (colors)
    {
    fprintf(fp,"%sPackedColor {\n", indent);
678
        VTK_INDENT_MORE;
Ken Martin's avatar
Ken Martin committed
679
    fprintf(fp,"%srgba [\n", indent);
680 681
        VTK_INDENT_MORE;
        fprintf(fp,"%s", indent);
682
    for (i = 0; i < colors->GetNumberOfTuples(); i++)
Ken Martin's avatar
Ken Martin committed
683
      {
684
      c = colors->GetPointer(4*i);
685
      fprintf (fp,"%#lx, ", 
686 687 688 689 690
               (static_cast<unsigned long>(c[3]) << 24) |
               (static_cast<unsigned long>(c[2])<<16) |
               (static_cast<unsigned long>(c[1])<<8) |
               static_cast<unsigned long>(c[0]));
      
691
      if (((i+1)%5)==0)
692 693 694
        {
        fprintf(fp, "\n%s", indent);
        }
Ken Martin's avatar
Ken Martin committed
695 696
      }
    fprintf(fp,"\n%s]\n", indent);
697
    VTK_INDENT_LESS;
Ken Martin's avatar
Ken Martin committed
698
    fprintf(fp,"%s}\n", indent);
699 700
    VTK_INDENT_LESS;
    fprintf(fp,"%sMaterialBinding { value PER_VERTEX_INDEXED }\n", indent);
Ken Martin's avatar
Ken Martin committed
701 702 703 704
    }
}


705
void vtkIVExporter::PrintSelf(ostream& os, vtkIndent ind)
Ken Martin's avatar
Ken Martin committed
706
{
Brad King's avatar
Brad King committed
707
  this->Superclass::PrintSelf(os,ind);
Ken Martin's avatar
Ken Martin committed
708
 
709 710 711 712 713 714 715 716
  if (this->FileName)
    {
    os << ind << "FileName: " << this->FileName << "\n";
    }
  else
    {
    os << ind << "FileName: (null)\n";
    }
Ken Martin's avatar
Ken Martin committed
717 718
}