TGLOutput.cxx

Go to the documentation of this file.
00001 // @(#)root/gl:$Id: TGLOutput.cxx 28515 2009-05-11 12:08:22Z couet $
00002 // Author:  Richard Maunder, Olivier Couet  02/07/2005
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2005, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #include "Riostream.h"
00013 #include "TVirtualPad.h"
00014 #include "TVirtualPS.h"
00015 #include "TGLOutput.h"
00016 #include "TGLViewer.h"
00017 #include "TSystem.h" // For gSystem
00018 #include "gl2ps.h"
00019 #include "TError.h"
00020 #include <assert.h>
00021 
00022 //////////////////////////////////////////////////////////////////////////
00023 //                                                                      //
00024 // TGLOutput                                                            //
00025 //                                                                      //
00026 // Wrapper class for GL capture & output routines                       //
00027 //////////////////////////////////////////////////////////////////////////
00028 
00029 ClassImp(TGLOutput)
00030 
00031 //______________________________________________________________________________
00032 Bool_t TGLOutput::Capture(TGLViewer & viewer, EFormat format, const char * filePath)
00033 {
00034    // Capture viewer to file. Arguments are:
00035    // 'viewer' - viewer object to capture from
00036    // 'format' - output format - only postscript types presently.
00037    //            One of kEPS_SIMPLE, kEPS_BSP, kPDF_SIMPLE or kPDF_BSP
00038    //             See TGLOutput::CapturePostscript() for meanings
00039    // 'filePath' - file output name. If null defaults to './viewer.eps' or './viewer.pdf'
00040    // depending on format requested
00041    //
00042    // Note : Output files can be large and take considerable time (up to mins)
00043    // to generate
00044    switch(format) {
00045       case(kEPS_SIMPLE):
00046       case(kEPS_BSP):
00047       case(kPDF_SIMPLE):
00048       case(kPDF_BSP): {
00049          return CapturePostscript(viewer, format, filePath);
00050       }
00051    }
00052 
00053    assert(kFALSE);
00054    return kFALSE;
00055 }
00056 
00057 //______________________________________________________________________________
00058 Bool_t TGLOutput::CapturePostscript(TGLViewer & viewer, EFormat format, const char * filePath)
00059 {
00060    // Capture viewer to postscript file. Arguments are:
00061    // 'viewer' - viewer object to capture from
00062    // 'format' - output format
00063    //                kEPS_SIMPLE - lower quality EPS
00064    //                kEPS_BSP    - higher quality EPS
00065    //                kPDF_SIMPLE - lower quality PDF
00066    //                kPDF_BSP    - higher quality PDF
00067    // 'filePath' - file output name. If null defaults to './viewer.eps' or './viewer.pdf'
00068    // depending on format requested
00069    if (!filePath || strlen(filePath) == 0) {
00070       if (format == kEPS_SIMPLE || format == kEPS_BSP) {
00071          filePath = "viewer.eps";
00072       } else if (format == kPDF_SIMPLE || format == kPDF_BSP) {
00073          filePath = "viewer.pdf";
00074       }
00075    }
00076    Info("TGLOutput::Postscript()", "Start creating %s.", filePath);
00077    std::cout << "Please wait.";
00078 
00079    if (FILE *output = fopen (filePath, "w+b"))
00080    {
00081       Int_t gl2psFormat;
00082       Int_t gl2psSort;
00083 
00084       switch(format) {
00085          case kEPS_SIMPLE:
00086             gl2psFormat = GL2PS_EPS;
00087             gl2psSort = GL2PS_SIMPLE_SORT;
00088             break;
00089          case kEPS_BSP:
00090             gl2psFormat = GL2PS_EPS;
00091             gl2psSort = GL2PS_BSP_SORT;
00092             break;
00093          case kPDF_SIMPLE:
00094             gl2psFormat = GL2PS_PDF;
00095             gl2psSort = GL2PS_SIMPLE_SORT;
00096             break;
00097          case kPDF_BSP:
00098             gl2psFormat = GL2PS_PDF;
00099             gl2psSort = GL2PS_BSP_SORT;
00100             break;
00101          default:
00102             assert(kFALSE);
00103             return kFALSE;
00104       }
00105       Int_t buffsize = 0, state = GL2PS_OVERFLOW;
00106       viewer.DoDraw();
00107       viewer.fIsPrinting = kTRUE;
00108       while (state == GL2PS_OVERFLOW) {
00109          buffsize += 1024*1024;
00110          gl2psBeginPage ("ROOT Scene Graph", "ROOT", NULL,
00111          gl2psFormat, gl2psSort, GL2PS_USE_CURRENT_VIEWPORT
00112          | GL2PS_SILENT | GL2PS_BEST_ROOT | GL2PS_OCCLUSION_CULL | 0,
00113          GL_RGBA, 0, NULL,0, 0, 0,
00114          buffsize, output, NULL);
00115          viewer.DoDraw();
00116          state = gl2psEndPage();
00117          std::cout << ".";
00118       }
00119       std::cout << std::endl;
00120       fclose (output);
00121       viewer.fIsPrinting = kFALSE;
00122       if (!gSystem->AccessPathName(filePath)) {
00123          Info("TGLOutput::Postscript", "Finished creating %s.", filePath);
00124          return kTRUE;
00125       }
00126    } else {
00127       Error("TGLOutput::Postscript", "Failed to create %s. ", filePath);
00128    }
00129 
00130    return kFALSE;
00131 }
00132 
00133 //______________________________________________________________________________
00134 void TGLOutput::StartEmbeddedPS()
00135 {
00136    //this function used by gl-in-pad
00137    Info("TGLOutput::StartEmbeddedPS", "PS output started ...");
00138 
00139    gVirtualPS->PrintStr("@");
00140    gVirtualPS->PrintStr("% Start gl2ps EPS@");
00141    gVirtualPS->PrintStr("newpath gsave save@");
00142    Double_t xx[2] = {0.}, yy[2] = {0.};
00143    xx[0] = gPad->GetUxmin();
00144    yy[0] = gPad->GetUymin();
00145    xx[1] = gPad->GetUxmax();
00146    yy[1] = gPad->GetUymax();
00147    gVirtualPS->PrintStr("@");
00148    
00149 ///gVirtualPS->DrawPS(0, xx, yy);
00150 ///gVirtualPS->WriteInteger(4*gPad->GetBorderSize());
00151 ///gVirtualPS->PrintStr(" add exch");
00152 ///gVirtualPS->WriteInteger(4*gPad->GetBorderSize());
00153 ///gVirtualPS->PrintStr(" add exch translate");
00154 ///gVirtualPS->PrintStr("@");
00155 
00156    GLint vp[4];
00157    glGetIntegerv(GL_VIEWPORT,vp);
00158    gVirtualPS->DrawPS(0, xx, yy);
00159    gVirtualPS->PrintStr(" exch");
00160    xx[0] = xx[1];
00161    yy[0] = yy[1];
00162    gVirtualPS->DrawPS(0, xx, yy);
00163    gVirtualPS->PrintStr(" 4 1 roll exch sub 3 1 roll sub");
00164    gVirtualPS->WriteInteger(2*4*gPad->GetBorderSize());
00165    gVirtualPS->PrintStr(" sub exch");
00166    gVirtualPS->WriteInteger(2*4*gPad->GetBorderSize());
00167    gVirtualPS->PrintStr(" sub exch");
00168    gVirtualPS->WriteInteger((Int_t)(vp[3]));
00169    gVirtualPS->WriteInteger((Int_t)(vp[2]));
00170    gVirtualPS->PrintStr(" 4 1 roll div 3 1 roll exch div exch scale@");
00171    gVirtualPS->PrintStr("@");
00172    gVirtualPS->PrintStr("countdictstack@");
00173    gVirtualPS->PrintStr("mark@");
00174    gVirtualPS->PrintStr("/showpage {} def@");
00175 
00176    // Close the gVirtualPS output stream
00177    ofstream *fs = (ofstream*)gVirtualPS->GetStream();
00178    fs->close();
00179 
00180 }
00181 
00182 //______________________________________________________________________________
00183 void TGLOutput::CloseEmbeddedPS()
00184 {
00185    //this function used by gl-in-pad
00186    // Restore the gVirtualPS output stream
00187    ofstream *fs = new ofstream(gVirtualPS->GetName(),ios::app);
00188    gVirtualPS->SetStream(fs);
00189    gVirtualPS->PrintStr("@");
00190    gVirtualPS->PrintStr("cleartomark@");
00191    gVirtualPS->PrintStr("countdictstack exch sub { end } repeat@");
00192    gVirtualPS->PrintStr("restore grestore@");
00193    gVirtualPS->PrintStr("% End gl2ps EPS@");
00194 
00195    Info("TGLOutput::CloseEmbeddedPS", "PS output finished");
00196 }
00197 
00198 //______________________________________________________________________________
00199 void TGLOutput::Capture(TGLViewer & viewer)
00200 {
00201    //this function used by gl-viewer, embedded into pad
00202    StartEmbeddedPS();
00203 
00204    FILE *output = fopen (gVirtualPS->GetName(), "a");
00205    Int_t gl2psFormat = GL2PS_EPS;
00206    Int_t gl2psSort = GL2PS_BSP_SORT;
00207    Int_t buffsize = 0, state = GL2PS_OVERFLOW;
00208    viewer.DoDraw();
00209    viewer.fIsPrinting = kTRUE;
00210 
00211    while (state == GL2PS_OVERFLOW) {
00212       buffsize += 1024*1024;
00213       gl2psBeginPage ("ROOT Scene Graph", "ROOT", NULL,
00214       gl2psFormat, gl2psSort, GL2PS_USE_CURRENT_VIEWPORT
00215       | GL2PS_SILENT | GL2PS_BEST_ROOT | GL2PS_OCCLUSION_CULL | 0,
00216       GL_RGBA, 0, NULL,0, 0, 0,
00217       buffsize, output, NULL);
00218       viewer.DoDraw();
00219       state = gl2psEndPage();
00220       std::cout << ".";
00221    }
00222 
00223    std::cout << std::endl;
00224    fclose (output);
00225    viewer.fIsPrinting = kFALSE;
00226 
00227    CloseEmbeddedPS();
00228 }

Generated on Tue Jul 5 14:18:07 2011 for ROOT_528-00b_version by  doxygen 1.5.1