vtkGL2PSExporter.cxx 11.8 KB
Newer Older
1 2 3 4 5
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkGL2PSExporter.cxx

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

10 11
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12 13 14 15 16 17 18
     PURPOSE.  See the above copyright notice for more information.

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

#include "vtkGL2PSExporter.h"
#include "vtkObjectFactory.h"
#include "vtkRenderWindow.h"
19 20 21 22 23 24 25 26 27
#include "vtkRendererCollection.h"
#include "vtkRenderer.h"
#include "vtkActorCollection.h"
#include "vtkActor.h"
#include "vtkActor2DCollection.h"
#include "vtkActor2D.h"
#include "vtkVolumeCollection.h"
#include "vtkVolume.h"
#include "vtkIntArray.h"
28
#include "vtk_gl2ps.h"
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

void _SavePropVisibility(vtkRendererCollection *renCol,
                         vtkIntArray *volVis, vtkIntArray *actVis,
                         vtkIntArray *act2dVis)
{
  int nRen = renCol->GetNumberOfItems();
  vtkRenderer *ren;
  vtkVolumeCollection *vCol;
  vtkActorCollection *aCol;
  vtkActor2DCollection *a2Col;
  vtkVolume *vol;
  vtkActor *act;
  vtkActor2D *act2d;
  int j;

  volVis->SetNumberOfComponents(nRen);
  actVis->SetNumberOfComponents(nRen);
  act2dVis->SetNumberOfComponents(nRen);
  
  renCol->InitTraversal();
  for (int i=0; i<nRen; ++i)
    {
    ren = renCol->GetNextItem();
    vCol = ren->GetVolumes();
    aCol = ren->GetActors();
    a2Col = ren->GetActors2D();
      
    volVis->SetNumberOfTuples(vCol->GetNumberOfItems());
    for (vCol->InitTraversal(), j=0; (vol = vCol->GetNextVolume()); ++j) 
      {
      volVis->SetComponent(i, j, vol->GetVisibility());
      }
    
    actVis->SetNumberOfTuples(aCol->GetNumberOfItems());
    for (aCol->InitTraversal(), j=0; (act = aCol->GetNextActor()); ++j)
      {
      actVis->SetComponent(i, j, act->GetVisibility());
      }

    act2dVis->SetNumberOfTuples(a2Col->GetNumberOfItems());
    for (a2Col->InitTraversal(), j=0; (act2d = a2Col->GetNextActor2D()); 
         ++j)
      {
      act2dVis->SetComponent(i, j, act2d->GetVisibility());
      }
    }
}

void _Turn3DPropsOff(vtkRendererCollection *renCol)
{
  vtkRenderer *ren;
  vtkVolumeCollection *vCol;
  vtkActorCollection *aCol;
  vtkVolume *vol;
  vtkActor *act;
  
  for (renCol->InitTraversal(); (ren = renCol->GetNextItem());)
    {
    vCol = ren->GetVolumes();
    aCol = ren->GetActors();
    for (vCol->InitTraversal(); (vol = vCol->GetNextVolume());) 
      {
      vol->SetVisibility(0);
      }

    for (aCol->InitTraversal(); (act = aCol->GetNextActor());)
      {
      act->SetVisibility(0);
      }
    }
}

void _Turn3DPropsOn(vtkRendererCollection *renCol, vtkIntArray *volVis, 
                    vtkIntArray *actVis)
{
  int nRen = renCol->GetNumberOfItems();
  vtkRenderer *ren;
  vtkVolumeCollection *vCol;
  vtkActorCollection *aCol;
  vtkVolume *vol;
  vtkActor *act;
  int j;
  
  renCol->InitTraversal();
  for (int i=0; i<nRen; ++i)
    {
    ren = renCol->GetNextItem();
    vCol = ren->GetVolumes();
    aCol = ren->GetActors();
      
    for (vCol->InitTraversal(), j=0; (vol = vCol->GetNextVolume()); ++j) 
      {
      vol->SetVisibility(static_cast<int>(volVis->GetComponent(i, j)));
      }

    for (aCol->InitTraversal(), j=0; (act = aCol->GetNextActor()); ++j)
      {
      act->SetVisibility(static_cast<int>(actVis->GetComponent(i, j)));
      }
    }  
}

void _Turn2DPropsOff(vtkRendererCollection *renCol)
{
  vtkRenderer *ren;
  vtkActor2DCollection *a2Col;
  vtkActor2D *act2d;
  
  for (renCol->InitTraversal(); (ren = renCol->GetNextItem());)
    {
    a2Col = ren->GetActors2D();
    for (a2Col->InitTraversal(); (act2d = a2Col->GetNextActor2D());) 
      {
      act2d->SetVisibility(0);
      }
    }
}

void _Turn2DPropsOn(vtkRendererCollection *renCol, vtkIntArray *act2dVis)
{
  int nRen = renCol->GetNumberOfItems();
  vtkRenderer *ren;
  vtkActor2DCollection *a2Col;
  vtkActor2D *act2d;
  int j;
  
  renCol->InitTraversal();
  for (int i=0; i<nRen; ++i)
    {
    ren = renCol->GetNextItem();
    a2Col = ren->GetActors2D();

    for (a2Col->InitTraversal(), j=0; (act2d = a2Col->GetNextActor2D()); 
         ++j)
      {
      act2d->SetVisibility(static_cast<int>(act2dVis->GetComponent(i, j)));
      }
    }
}

170 171
vtkStandardNewMacro(vtkGL2PSExporter);

172 173 174
static float vtkGL2PSExporterGlobalPointSizeFactor = 5.0/7.0;
static float vtkGL2PSExporterGlobalLineWidthFactor = 5.0/7.0;

175 176 177
vtkGL2PSExporter::vtkGL2PSExporter()
{
  this->FilePrefix = NULL;
178
  this->FileFormat = EPS_FILE;
179
  this->Sort = SIMPLE_SORT;
180
  this->Compress = 1;
181
  this->DrawBackground = 1;
182 183 184 185 186 187 188
  this->SimpleLineOffset = 1;
  this->Silent = 0;
  this->BestRoot = 1;
  this->Text = 1;
  this->Landscape = 0;
  this->PS3Shading = 1;
  this->OcclusionCull = 1;
189
  this->Write3DPropsAsRasterImage = 0;
190 191 192 193 194 195 196 197 198 199
}

vtkGL2PSExporter::~vtkGL2PSExporter()
{
  if ( this->FilePrefix )
    {
    delete [] this->FilePrefix;
    }
}

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
void vtkGL2PSExporter::SetGlobalPointSizeFactor(float val)
{
    vtkGL2PSExporterGlobalPointSizeFactor = fabs(val);
}

float vtkGL2PSExporter::GetGlobalPointSizeFactor()
{
    return vtkGL2PSExporterGlobalPointSizeFactor;
}

void vtkGL2PSExporter::SetGlobalLineWidthFactor(float val)
{
    vtkGL2PSExporterGlobalLineWidthFactor = fabs(val);
}

float vtkGL2PSExporter::GetGlobalLineWidthFactor()
{
    return vtkGL2PSExporterGlobalLineWidthFactor;
}

220 221 222 223
void vtkGL2PSExporter::WriteData()
{
  FILE *fpObj;
  char *fName;
224
  GLint format = GL2PS_EPS;
225
  GLint sort;
226
  GLint options = GL2PS_NONE;
227 228 229
  int buffsize = 0;
  int state = GL2PS_OVERFLOW;
  GLint viewport[4];
230 231 232 233 234 235 236
  int *winsize = this->RenderWindow->GetSize();

  vtkRendererCollection *renCol = this->RenderWindow->GetRenderers();
  // Stores visibility of actors/volumes.
  vtkIntArray *volVis = vtkIntArray::New();
  vtkIntArray *actVis = vtkIntArray::New();
  vtkIntArray *act2dVis = vtkIntArray::New();
237
  float *floatpixels = 0; // for writing 3d props as an image.
238 239 240 241

  // Setting this to the entire window size for now.
  viewport[0] = 0;
  viewport[1] = 0;
242 243
  viewport[2] = winsize[0];
  viewport[3] = winsize[1];
244

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
  // make sure the user specified a file prefix
  if (this->FilePrefix == NULL)
    {
    vtkErrorMacro(<< "Please specify a file prefix to use");
    return;
    }

  // Set the options based on user's choices.
  if (this->Sort == NO_SORT)
    {
    sort = GL2PS_NO_SORT;
    }
  else if (this->Sort == SIMPLE_SORT)
    {
    sort = GL2PS_SIMPLE_SORT;
    }
  else
    {
    sort = GL2PS_BSP_SORT;
264 265
    }

266 267 268 269
  if (this->Compress == 1)
    {
    options = options | GL2PS_COMPRESS;
    }  
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
  if (this->DrawBackground == 1)
    {
    options = options | GL2PS_DRAW_BACKGROUND;
    }
  if (this->SimpleLineOffset == 1)
    {
    options = options | GL2PS_SIMPLE_LINE_OFFSET;
    }
  if (this->Silent == 1)
    {
    options = options | GL2PS_SILENT;
    }
  if (this->BestRoot == 1)
    {
    options = options | GL2PS_BEST_ROOT;
    }
  if (this->Text == 0)
    {
    options = options | GL2PS_NO_TEXT;
    }
  if (this->Landscape == 1)
    {
    options = options | GL2PS_LANDSCAPE;
    }
  if (this->PS3Shading == 0)
    {
    options = options | GL2PS_NO_PS3_SHADING;
    }
  if (this->OcclusionCull == 1)
    {
    options = options | GL2PS_OCCLUSION_CULL;
301
    }  
302 303

  // Setup the file.
304
  fName = new char [strlen(this->FilePrefix) + 5] ;
305 306 307 308 309 310 311 312 313 314
  if (this->FileFormat == PS_FILE)
    {
    sprintf(fName, "%s.ps", this->FilePrefix);
    format = GL2PS_PS;
    }
  else if (this->FileFormat == EPS_FILE)
    {
    sprintf(fName, "%s.eps", this->FilePrefix);
    format = GL2PS_EPS;
    }
315 316 317 318 319
  else if (this->FileFormat == PDF_FILE)
    {
    sprintf(fName, "%s.pdf", this->FilePrefix);
    format = GL2PS_PDF;
    }
320 321 322 323 324
  else if (this->FileFormat == TEX_FILE)
    {
    sprintf(fName, "%s.tex", this->FilePrefix);
    format = GL2PS_TEX;
    }
325 326 327 328 329
  else if (this->FileFormat == SVG_FILE)
    {
    sprintf(fName, "%s.svg", this->FilePrefix);
    format = GL2PS_SVG;
    }
330
  
331
  fpObj = fopen(fName, "wb");
332 333 334 335 336 337
  if (!fpObj)
    {
    vtkErrorMacro(<< "unable to open file: " << fName);
    return;
    }

338 339
  // Ready to write file.

340 341 342 343 344
  // Need to render once before rendering into the feedback buffer to
  // prevent problems.
  this->RenderWindow->Render();  

  // Write out a raster image without the 2d actors
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
  if (this->Write3DPropsAsRasterImage)
    {
    _SavePropVisibility(renCol, volVis, actVis, act2dVis);
    _Turn2DPropsOff(renCol);

    int numpix= winsize[0]*winsize[1]*3;
    floatpixels = new float [numpix];
    unsigned char *charpixels;
    int offscreen = this->RenderWindow->GetOffScreenRendering();

    this->RenderWindow->OffScreenRenderingOn();
    this->RenderWindow->Render();
    charpixels = this->RenderWindow->GetPixelData(0, 0, winsize[0] - 1,
                                                  winsize[1] - 1, 1);
    
    for (int i=0; i<numpix; i++)
      {
      floatpixels[i] = (float)(charpixels[i]/255.0);
      }
    this->RenderWindow->SetOffScreenRendering(offscreen);
    delete [] charpixels;
    }
  
368 369 370
  // Writing the file using GL2PS.
  vtkDebugMacro(<<"Writing file using GL2PS");

371 372 373
  // Call gl2ps to generate the file.
  while(state == GL2PS_OVERFLOW)
    {
374
    buffsize += 2048*2048;
375
    gl2psBeginPage(this->RenderWindow->GetWindowName(), "VTK", viewport,
376 377
                   format, sort, options, GL_RGBA, 0, 
                   NULL, 0, 0, 0, buffsize, fpObj, fName);
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399

    if (this->Write3DPropsAsRasterImage)
      {
      // Dump the rendered image without 2d actors as a raster image.
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      glPushMatrix();
      glRasterPos3f(-1.0, -1.0, -1.0);
      gl2psDrawPixels(winsize[0], winsize[1], 0, 0, GL_RGB, 
                      GL_FLOAT, floatpixels);
      glPopMatrix();  

      // Render the 2d actors alone in a vector graphic format.
      _Turn3DPropsOff(renCol);
      _Turn2DPropsOn(renCol, act2dVis);
      this->RenderWindow->Render();
      }
    else
      {
      this->RenderWindow->Render();
      }

400 401 402
    state = gl2psEndPage();
    }
  fclose(fpObj);
403

404 405 406 407 408 409 410 411 412 413
  // Clean up.
  if (this->Write3DPropsAsRasterImage)
    {
    // Reset the visibility.
    _Turn3DPropsOn(renCol, volVis, actVis);
    // memory
    delete [] floatpixels;
    // Re-render the scene to show all actors.
    this->RenderWindow->Render();
    }
414
  delete[] fName;
415 416 417
  volVis->Delete();
  actVis->Delete();
  act2dVis->Delete();
418 419

  vtkDebugMacro(<<"Finished writing file using GL2PS");
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
}

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

435 436 437 438
  os << indent << "FileFormat: " 
     << this->GetFileFormatAsString() << "\n";
  os << indent << "Sort: "
     << this->GetSortAsString() << "\n";
439 440
  os << indent << "Compress: "
     << (this->Compress ? "On\n" : "Off\n");
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
  os << indent << "DrawBackground: "
     << (this->DrawBackground ? "On\n" : "Off\n");
  os << indent << "SimpleLineOffset: "
     << (this->SimpleLineOffset ? "On\n" : "Off\n");
  os << indent << "Silent: "
     << (this->Silent ? "On\n" : "Off\n");
  os << indent << "BestRoot: "
     << (this->BestRoot ? "On\n" : "Off\n");
  os << indent << "Text: "
     << (this->Text ? "On\n" : "Off\n");
  os << indent << "Landscape: "
     << (this->Landscape ? "On\n" : "Off\n");
  os << indent << "PS3Shading: "
     << (this->PS3Shading ? "On\n" : "Off\n");
  os << indent << "OcclusionCull: "
     << (this->OcclusionCull ? "On\n" : "Off\n");
457 458
  os << indent << "Write3DPropsAsRasterImage: "
     << (this->Write3DPropsAsRasterImage ? "On\n" : "Off\n");
459 460 461 462
  os << indent << "GlobalPointSizeFactor:" 
     << vtkGL2PSExporterGlobalPointSizeFactor << endl;
  os << indent << "GlobalLineWidthFactor:" 
     << vtkGL2PSExporterGlobalLineWidthFactor << endl;
463
}