Geant4  10.03.p03
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4DAWNFILESceneHandler.cc
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 //
27 // $Id: G4DAWNFILESceneHandler.cc 105238 2017-07-14 13:50:46Z gcosmo $
28 //
29 // Satoshi TANAKA
30 // DAWNFILE scene.
31 
32 
33 #define __G_ANSI_C__
34 
35 // #define DEBUG_FR_SCENE
36 
37  //----- header files
38 #include <fstream>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sstream>
42 #include <iomanip>
43 #include "globals.hh"
44 #include "G4VisManager.hh"
45 #include "G4FRConst.hh"
46 #include "G4DAWNFILE.hh"
48 #include "G4DAWNFILEViewer.hh"
49 #include "G4Point3D.hh"
50 #include "G4VisAttributes.hh"
51 #include "G4Scene.hh"
52 #include "G4Transform3D.hh"
53 #include "G4Polyhedron.hh"
54 #include "G4Box.hh"
55 #include "G4Cons.hh"
56 #include "G4Polyline.hh"
57 #include "G4Trd.hh"
58 #include "G4Tubs.hh"
59 #include "G4Trap.hh"
60 #include "G4Torus.hh"
61 #include "G4Sphere.hh"
62 #include "G4Para.hh"
63 #include "G4Text.hh"
64 #include "G4Circle.hh"
65 #include "G4Square.hh"
66 #include "G4VPhysicalVolume.hh"
67 
68 //----- constants
69 const char FR_ENV_CULL_INVISIBLE_OBJECTS [] = "G4DAWN_CULL_INVISIBLE_OBJECTS";
70 const char G4PRIM_FILE_HEADER [] = "g4_";
71 const char DEFAULT_G4PRIM_FILE_NAME[] = "g4_0000.prim";
72 
73 // const int FR_MAX_FILE_NUM = 1 ;
74 // const int FR_MAX_FILE_NUM = 5 ;
75 // const int FR_MAX_FILE_NUM = 10 ;
76 // const int FR_MAX_FILE_NUM = 15 ;
77 // const int FR_MAX_FILE_NUM = 20 ;
78  const int FR_MAX_FILE_NUM = 100 ;
79 
80 
82 // Driver-dependent part //
84 
85 
86  //----- G4DAWNFILESceneHandler, constructor
88 G4VSceneHandler (system, fSceneIdCount++, name) ,
89 fSystem (system) ,
90 fPrimDest () ,
91 FRflag_in_modeling (false) ,
92 flag_saving_g4_prim (false) ,
93 COMMAND_BUF_SIZE (G4FRofstream::SEND_BUFMAX),
94 fPrec (9), fPrec2 (16)
95 {
96  // g4.prim filename and its directory
97  if ( getenv( "G4DAWNFILE_DEST_DIR" ) == NULL ) {
98  strcpy( fG4PrimDestDir , "" ) ; // output dir
99  strcpy( fG4PrimFileName, DEFAULT_G4PRIM_FILE_NAME ); // filename
100  } else {
101  strcpy( fG4PrimDestDir , getenv( "G4DAWNFILE_DEST_DIR" ) ); // output dir
102  strcpy( fG4PrimFileName, DEFAULT_G4PRIM_FILE_NAME ); // filename
103  }
104 
105  // maximum number of g4.prim files in the dest directory
106  fMaxFileNum = FR_MAX_FILE_NUM ; // initialization
107  if ( getenv( "G4DAWNFILE_MAX_FILE_NUM" ) != NULL ) {
108 
109  sscanf( getenv("G4DAWNFILE_MAX_FILE_NUM"), "%d", &fMaxFileNum ) ;
110 
111  } else {
112  fMaxFileNum = FR_MAX_FILE_NUM ;
113  }
114  if( fMaxFileNum < 1 ) { fMaxFileNum = 1 ; }
115 
116 
117  //----- precision control
118  if( getenv( "G4DAWNFILE_PRECISION" ) != NULL ) {
119  sscanf( getenv("G4DAWNFILE_PRECISION"), "%d", &fPrec ) ;
120  } else {
121  fPrec = 9 ;
122  }
123  fPrec2 = fPrec + 7 ;
124 
125 }
126 
127 
128  //----- G4DAWNFILESceneHandler, destructor
130 {
131 #if defined DEBUG_FR_SCENE
133  G4cout << "***** ~G4DAWNFILESceneHandler" << G4endl;
134 #endif
135  if (fPrimDest.IsOpen())
136  {
137  //----- End of modeling
138  // !EndModeling, !DrawAll, !CloseDevice,
139  // close g4.prim
140  FREndModeling();
141  }
142 }
143 
144 //-----
146 {
147  // g4_0000.prim, g4_0001.prim, ..., g4_MAX_FILE_INDEX.prim
148  const int MAX_FILE_INDEX = fMaxFileNum - 1 ;
149 
150  // dest directory (null if no environmental variables is set)
151  strcpy ( fG4PrimFileName, fG4PrimDestDir) ;
152 
153  // create full path name (default)
154  strcat ( fG4PrimFileName, DEFAULT_G4PRIM_FILE_NAME );
155 
156  // Automatic updation of file names
157  for( int i = 0 ; i < fMaxFileNum ; i++) {
158 
159  // Message in the final execution
160  if( i == MAX_FILE_INDEX )
161  {
163  G4cout << "===========================================" << G4endl;
164  G4cout << "WARNING MESSAGE from DAWNFILE driver: " << G4endl;
165  G4cout << " This file name is the final one in the " << G4endl;
166  G4cout << " automatic updation of the output file name." << G4endl;
167  G4cout << " You may overwrite existing files, i.e. " << G4endl;
168  G4cout << " g4_XXXX.prim and g4_XXXX.eps " << G4endl;
169  G4cout << "===========================================" << G4endl;
170  }
171  }
172 
173  // re-determine file name as G4DAWNFILE_DEST_DIR/g4_XXXX.prim
174  std::ostringstream filename; filename
175  << fG4PrimDestDir << G4PRIM_FILE_HEADER
176  << std::setw(4) << std::setfill('0') << i << ".prim";
177  strncpy(fG4PrimFileName,filename.str().c_str(),sizeof(fG4PrimFileName));
178 
179  // check validity of the file name
180  std::ifstream fin ;
181  fin.open(fG4PrimFileName) ;
182  if(!fin) {
183  // new file
184  fin.close();
185  break;
186  } else {
187  // already exists (try next)
188  fin.close();
189  }
190 
191  } // for
192 
193  G4cout << "=========================================== " << G4endl;
194  G4cout << "Output file: " << fG4PrimFileName << G4endl;
195  G4cout << "Destination directory (current dir if NULL): " << fG4PrimDestDir << G4endl;
196  G4cout << "Maximal number of files in the destination directory: " << fMaxFileNum << G4endl;
197  G4cout << "Note: " << G4endl;
198  G4cout << " * The maximal number is customizable as: " << G4endl;
199  G4cout << " % setenv G4DAWNFILE_MAX_FILE_NUM number " << G4endl;
200  G4cout << " * The destination directory is customizable as:" << G4endl;
201  G4cout << " % setenv G4DAWNFILE_DEST_DIR dir_name/ " << G4endl;
202  G4cout << " ** Do not forget \"/\" at the end of the " << G4endl;
203  G4cout << " dir_name, e.g. \"./tmp/\". " << G4endl;
204  G4cout << "=========================================== " << G4endl;
205 
206 } // G4DAWNFILESceneHandler::SetG4PrimFileName()
207 
208 
209 //-----
211 {
212 #if defined DEBUG_FR_SCENE
214  G4cout << "***** BeginSavingG4Prim (called)\n";
215 #endif
216 
217  if( !IsSavingG4Prim() )
218  {
219 #if defined DEBUG_FR_SCENE
221  G4cout << "***** (started) " ;
222  G4cout << "(open g4.prim, ##)" << G4endl;
223  }
224 #endif
225  SetG4PrimFileName() ; // result set to fG4PrimFileName
226  fPrimDest.Open(fG4PrimFileName) ;
227 
229  flag_saving_g4_prim = true ;
230  }
231 }
232 
234 {
235 #if defined DEBUG_FR_SCENE
237  G4cout << "***** EndSavingG4Prim (called)\n";
238 #endif
239 
240  if( IsSavingG4Prim() )
241  {
242 #if defined DEBUG_FR_SCENE
244  G4cout << "***** (started) (close g4.prim)" << G4endl;
245 #endif
246  fPrimDest.Close() ;
247  flag_saving_g4_prim = false ;
248  }
249 }
250 
251 
252 //-----
254 {
255  if( !FRIsInModeling() )
256  {
257 #if defined DEBUG_FR_SCENE
259  G4cout << "***** G4DAWNFILESceneHandler::FRBeginModeling (called & started)" << G4endl;
260 #endif
261 
262  //----- Send saving command and heading comment
264 
265  //----- Send bounding box command
266  SendBoundingBox();
267 
268  //----- send SET_CAMERA command
269 #if defined DEBUG_FR_SCENE
271  G4cout << "***** (!SetCamera in FRBeginModeling())" << G4endl;
272 #endif
274 
275  //----- open device
276 #if defined DEBUG_FR_SCENE
278  G4cout << "***** (!OpenDevice in FRBeginModeling())" << G4endl;
279 #endif
281 
282  //----- begin sending primitives
283 #if defined DEBUG_FR_SCENE
285  G4cout << "***** (!BeginModeling in FRBeginModeling())" << G4endl;
286 #endif
287  SendStr( FR_BEGIN_MODELING ); FRflag_in_modeling = true ;
288 
289  } // if
290 
291 }
292 
293 
295 // Common to DAWN and DAWNFILE drivers //
297 
298 #define G4FRSCENEHANDLER G4DAWNFILESceneHandler
299 #include "G4FRSceneFunc.icc"
300 #undef G4FRSCENEHANDLER
301 
303 // static variables //
305 
306  //----- static variables
307 G4int G4DAWNFILESceneHandler::fSceneIdCount = 0;
void SendStr(const char *char_string)
G4DAWNFILESceneHandler(G4DAWNFILE &system, const G4String &name="")
const XML_Char * name
Definition: expat.h:151
const char FR_BEGIN_MODELING[]
Definition: G4FRConst.hh:64
const char G4PRIM_FILE_HEADER[]
void Open(const char *filename)
Definition: G4FRofstream.hh:74
G4bool IsOpen()
Definition: G4FRofstream.hh:60
const char FR_G4_PRIM_HEADER[]
Definition: G4FRConst.hh:38
int G4int
Definition: G4Types.hh:78
G4GLOB_DLL std::ostream G4cout
const int FR_MAX_FILE_NUM
const char FR_OPEN_DEVICE[]
Definition: G4FRConst.hh:46
const char DEFAULT_G4PRIM_FILE_NAME[]
const char FR_SET_CAMERA[]
Definition: G4FRConst.hh:45
static Verbosity GetVerbosity()
#define G4endl
Definition: G4ios.hh:61
const char FR_ENV_CULL_INVISIBLE_OBJECTS[]
void SendBoundingBox(void)