G4DAWNFILESceneHandler.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
00028 //
00029 // Satoshi TANAKA
00030 // DAWNFILE scene.
00031 
00032 
00033 #define __G_ANSI_C__
00034 
00035 // #define DEBUG_FR_SCENE
00036 
00037      //----- header files
00038 #include <fstream>
00039 #include <stdlib.h>
00040 #include <string.h>
00041 #include "globals.hh"
00042 #include "G4VisManager.hh"
00043 #include "G4FRConst.hh"
00044 #include "G4DAWNFILE.hh"
00045 #include "G4DAWNFILESceneHandler.hh"
00046 #include "G4DAWNFILEViewer.hh"
00047 #include "G4Point3D.hh"
00048 #include "G4VisAttributes.hh"
00049 #include "G4Scene.hh"
00050 #include "G4Transform3D.hh"
00051 #include "G4Polyhedron.hh"
00052 #include "G4Box.hh"
00053 #include "G4Cons.hh"
00054 #include "G4Polyline.hh"
00055 #include "G4Trd.hh"
00056 #include "G4Tubs.hh"
00057 #include "G4Trap.hh"
00058 #include "G4Torus.hh"
00059 #include "G4Sphere.hh"
00060 #include "G4Para.hh"
00061 #include "G4Text.hh"
00062 #include "G4Circle.hh"
00063 #include "G4Square.hh"
00064 #include "G4VPhysicalVolume.hh"
00065 
00066 //----- constants
00067 const char  FR_ENV_CULL_INVISIBLE_OBJECTS [] = "G4DAWN_CULL_INVISIBLE_OBJECTS";
00068 const char  G4PRIM_FILE_HEADER      [] = "g4_";
00069 const char  DEFAULT_G4PRIM_FILE_NAME[] = "g4_00.prim";
00070 
00071 // const int   FR_MAX_FILE_NUM = 1 ;
00072 // const int   FR_MAX_FILE_NUM = 5 ;
00073 // const int   FR_MAX_FILE_NUM = 10 ;
00074 // const int   FR_MAX_FILE_NUM = 15 ;
00075 // const int   FR_MAX_FILE_NUM = 20 ;
00076    const int   FR_MAX_FILE_NUM = 100 ;
00077 
00078 
00080 // Driver-dependent part //
00082 
00083 
00084         //----- G4DAWNFILESceneHandler, constructor
00085 G4DAWNFILESceneHandler::G4DAWNFILESceneHandler (G4DAWNFILE& system, const G4String& name):
00086 G4VSceneHandler  (system, fSceneIdCount++, name) ,
00087 fSystem   (system)                        ,
00088 fPrimDest ()                              ,
00089 FRflag_in_modeling     (false)            ,
00090 flag_saving_g4_prim    (false)            ,
00091 COMMAND_BUF_SIZE       (G4FRofstream::SEND_BUFMAX),
00092 fPrec (9), fPrec2 (16)
00093 {
00094         // g4.prim filename and its directory
00095         if ( getenv( "G4DAWNFILE_DEST_DIR" ) == NULL ) {
00096                 strcpy( fG4PrimDestDir , "" )                      ;  // output dir
00097                 strcpy( fG4PrimFileName, DEFAULT_G4PRIM_FILE_NAME );  // filename
00098         } else {
00099                 strcpy( fG4PrimDestDir , getenv( "G4DAWNFILE_DEST_DIR" ) ); // output dir
00100                 strcpy( fG4PrimFileName, DEFAULT_G4PRIM_FILE_NAME        ); // filename 
00101         }
00102                 
00103         // maximum number of g4.prim files in the dest directory
00104         fMaxFileNum = FR_MAX_FILE_NUM ; // initialization
00105         if ( getenv( "G4DAWNFILE_MAX_FILE_NUM" ) != NULL ) {    
00106                 
00107                 sscanf( getenv("G4DAWNFILE_MAX_FILE_NUM"), "%d", &fMaxFileNum ) ;
00108 
00109         } else {
00110                 fMaxFileNum = FR_MAX_FILE_NUM ;
00111         }
00112         if( fMaxFileNum < 1 ) { fMaxFileNum = 1 ; }
00113 
00114 
00115                 //----- precision control
00116         if( getenv( "G4DAWNFILE_PRECISION" ) != NULL ) {
00117                 sscanf( getenv("G4DAWNFILE_PRECISION"), "%d", &fPrec ) ;
00118         } else {
00119                 fPrec = 9 ;
00120         }
00121         fPrec2 = fPrec + 7 ;
00122 
00123 } 
00124 
00125 
00126         //----- G4DAWNFILESceneHandler, destructor
00127 G4DAWNFILESceneHandler::~G4DAWNFILESceneHandler () 
00128 {
00129 #if defined DEBUG_FR_SCENE
00130   if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
00131         G4cout << "***** ~G4DAWNFILESceneHandler" << G4endl;
00132 #endif 
00133         if (fPrimDest.IsOpen()) 
00134           {
00135                         //----- End of modeling
00136                         // !EndModeling, !DrawAll, !CloseDevice,
00137                         // close g4.prim
00138                 FREndModeling();
00139         }
00140 }
00141 
00142 //-----
00143 void    G4DAWNFILESceneHandler::SetG4PrimFileName() 
00144 {
00145         // g4_00.prim, g4_01.prim, ..., g4_MAX_FILE_INDEX.prim
00146         const int MAX_FILE_INDEX = fMaxFileNum - 1 ;
00147 
00148         // dest directory (null if no environmental variables is set)
00149         strcpy ( fG4PrimFileName, fG4PrimDestDir) ; 
00150 
00151         // create full path name (default)
00152         strcat ( fG4PrimFileName, DEFAULT_G4PRIM_FILE_NAME );
00153 
00154         // Automatic updation of file names
00155         for( int i = 0 ; i < fMaxFileNum ; i++) { 
00156 
00157                 // Message in the final execution
00158                 if( i == MAX_FILE_INDEX ) 
00159                 {
00160                   if (G4VisManager::GetVerbosity() >= G4VisManager::errors) {
00161                     G4cout << "==========================================="   << G4endl; 
00162                     G4cout << "WARNING MESSAGE from DAWNFILE driver:      "   << G4endl;
00163                     G4cout << "  This file name is the final one in the   "   << G4endl;
00164                     G4cout << "  automatic updation of the output file name." << G4endl; 
00165                     G4cout << "  You may overwrite existing files, i.e.   "   << G4endl; 
00166                     G4cout << "  g4_XX.prim and g4_XX.eps                 "   << G4endl;
00167                     G4cout << "==========================================="   << G4endl; 
00168                   }
00169                 }
00170 
00171                 // re-determine file name as G4DAWNFILE_DEST_DIR/g4_XX.prim 
00172                 if( i >=  0 && i <= 9 ) { 
00173                         sprintf( fG4PrimFileName, "%s%s%s%d.prim" , fG4PrimDestDir,  G4PRIM_FILE_HEADER, "0", i );
00174                 } else {
00175                         sprintf( fG4PrimFileName, "%s%s%d.prim" , fG4PrimDestDir,  G4PRIM_FILE_HEADER, i );
00176                 }
00177 
00178                 // check validity of the file name
00179                 std::ifstream  fin ; 
00180                 fin.open(fG4PrimFileName) ;
00181                 if(!fin) { 
00182                         // new file     
00183                         fin.close();  
00184                         break; 
00185                 } else { 
00186                         // already exists (try next) 
00187                         fin.close(); 
00188                 } 
00189 
00190         } // for 
00191 
00192         G4cout << "===========================================    " << G4endl; 
00193         G4cout << "Output file: " <<    fG4PrimFileName             << G4endl; 
00194         G4cout << "Destination directory (current dir if NULL): "       << fG4PrimDestDir    << G4endl; 
00195         G4cout << "Maximal number of files in the destination directory: " << fMaxFileNum << G4endl; 
00196         G4cout << "Note:                                                " << G4endl; 
00197         G4cout << "  * The maximal number is customizable as:           " << G4endl;
00198         G4cout << "       % setenv  G4DAWNFILE_MAX_FILE_NUM  number " << G4endl;        
00199         G4cout << "  * The destination directory is customizable as:" << G4endl;
00200         G4cout << "       % setenv  G4DAWNFILE_DEST_DIR  dir_name/  " << G4endl;        
00201         G4cout << "     ** Do not forget \"/\" at the end of the    " << G4endl;              
00202         G4cout << "        dir_name, e.g. \"./tmp/\".  " << G4endl;              
00203         G4cout << "===========================================      " << G4endl; 
00204 
00205 } // G4DAWNFILESceneHandler::SetG4PrimFileName()
00206 
00207 
00208 //-----
00209 void    G4DAWNFILESceneHandler::BeginSavingG4Prim( void ) 
00210 {
00211 #if defined DEBUG_FR_SCENE
00212   if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
00213         G4cout << "***** BeginSavingG4Prim (called)\n";
00214 #endif
00215 
00216         if( !IsSavingG4Prim() ) 
00217         { 
00218 #if defined DEBUG_FR_SCENE
00219           if (G4VisManager::GetVerbosity() >= G4VisManager::errors) {
00220                 G4cout << "*****                   (started) " ;
00221                 G4cout << "(open g4.prim, ##)"  << G4endl;
00222           }
00223 #endif
00224                 SetG4PrimFileName() ; // result set to fG4PrimFileName
00225                 fPrimDest.Open(fG4PrimFileName)   ;
00226 
00227                 SendStr( FR_G4_PRIM_HEADER   )    ; 
00228                 flag_saving_g4_prim = true        ; 
00229         } 
00230 }
00231 
00232 void    G4DAWNFILESceneHandler::EndSavingG4Prim  ( void ) 
00233 {
00234 #if defined DEBUG_FR_SCENE
00235   if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
00236         G4cout << "***** EndSavingG4Prim (called)\n";
00237 #endif
00238 
00239         if(  IsSavingG4Prim() )
00240         {
00241 #if defined DEBUG_FR_SCENE
00242           if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
00243             G4cout << "*****                 (started) (close g4.prim)" << G4endl;
00244 #endif
00245                 fPrimDest.Close()               ;
00246                 flag_saving_g4_prim = false ; 
00247         } 
00248 }
00249 
00250 
00251 //----- 
00252 void G4DAWNFILESceneHandler::FRBeginModeling( void )
00253 {
00254         if( !FRIsInModeling() )         
00255         {
00256 #if defined DEBUG_FR_SCENE
00257           if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
00258             G4cout << "***** G4DAWNFILESceneHandler::FRBeginModeling (called & started)" << G4endl;
00259 #endif
00260 
00261                         //----- Send saving command and heading comment
00262                 BeginSavingG4Prim();
00263 
00264                         //----- Send bounding box command
00265                 SendBoundingBox();
00266 
00267                         //----- send SET_CAMERA command 
00268 #if defined DEBUG_FR_SCENE
00269                 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
00270                   G4cout << "*****   (!SetCamera in FRBeginModeling())" << G4endl;
00271 #endif
00272                 SendStr( FR_SET_CAMERA );
00273 
00274                 //----- open device
00275 #if defined DEBUG_FR_SCENE
00276                 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
00277                   G4cout << "*****   (!OpenDevice in FRBeginModeling())" << G4endl;
00278 #endif
00279                 SendStr( FR_OPEN_DEVICE      );
00280 
00281                 //----- begin sending primitives
00282 #if defined DEBUG_FR_SCENE
00283                 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
00284                   G4cout << "*****   (!BeginModeling in FRBeginModeling())" << G4endl;
00285 #endif
00286                 SendStr( FR_BEGIN_MODELING );  FRflag_in_modeling = true ;
00287 
00288         } // if
00289 
00290 } 
00291 
00292 
00294 // Common to DAWN and DAWNFILE drivers //
00296 
00297 #define  G4FRSCENEHANDLER  G4DAWNFILESceneHandler
00298 #include "G4FRSceneFunc.icc"
00299 #undef   G4FRSCENEHANDLER 
00300 
00302 // static variables //
00304 
00305         //----- static variables
00306 G4int G4DAWNFILESceneHandler::fSceneIdCount = 0; 

Generated on Mon May 27 17:47:59 2013 for Geant4 by  doxygen 1.4.7