Geant4.10
 All Data Structures 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 66870 2013-01-14 23:38:59Z adotti $
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 "globals.hh"
42 #include "G4VisManager.hh"
43 #include "G4FRConst.hh"
44 #include "G4DAWNFILE.hh"
46 #include "G4DAWNFILEViewer.hh"
47 #include "G4Point3D.hh"
48 #include "G4VisAttributes.hh"
49 #include "G4Scene.hh"
50 #include "G4Transform3D.hh"
51 #include "G4Polyhedron.hh"
52 #include "G4Box.hh"
53 #include "G4Cons.hh"
54 #include "G4Polyline.hh"
55 #include "G4Trd.hh"
56 #include "G4Tubs.hh"
57 #include "G4Trap.hh"
58 #include "G4Torus.hh"
59 #include "G4Sphere.hh"
60 #include "G4Para.hh"
61 #include "G4Text.hh"
62 #include "G4Circle.hh"
63 #include "G4Square.hh"
64 #include "G4VPhysicalVolume.hh"
65 
66 //----- constants
67 const char FR_ENV_CULL_INVISIBLE_OBJECTS [] = "G4DAWN_CULL_INVISIBLE_OBJECTS";
68 const char G4PRIM_FILE_HEADER [] = "g4_";
69 const char DEFAULT_G4PRIM_FILE_NAME[] = "g4_00.prim";
70 
71 // const int FR_MAX_FILE_NUM = 1 ;
72 // const int FR_MAX_FILE_NUM = 5 ;
73 // const int FR_MAX_FILE_NUM = 10 ;
74 // const int FR_MAX_FILE_NUM = 15 ;
75 // const int FR_MAX_FILE_NUM = 20 ;
76  const int FR_MAX_FILE_NUM = 100 ;
77 
78 
79 ///////////////////////////
80 // Driver-dependent part //
81 ///////////////////////////
82 
83 
84  //----- G4DAWNFILESceneHandler, constructor
86 G4VSceneHandler (system, fSceneIdCount++, name) ,
87 fSystem (system) ,
88 fPrimDest () ,
89 FRflag_in_modeling (false) ,
90 flag_saving_g4_prim (false) ,
91 COMMAND_BUF_SIZE (G4FRofstream::SEND_BUFMAX),
92 fPrec (9), fPrec2 (16)
93 {
94  // g4.prim filename and its directory
95  if ( getenv( "G4DAWNFILE_DEST_DIR" ) == NULL ) {
96  strcpy( fG4PrimDestDir , "" ) ; // output dir
97  strcpy( fG4PrimFileName, DEFAULT_G4PRIM_FILE_NAME ); // filename
98  } else {
99  strcpy( fG4PrimDestDir , getenv( "G4DAWNFILE_DEST_DIR" ) ); // output dir
100  strcpy( fG4PrimFileName, DEFAULT_G4PRIM_FILE_NAME ); // filename
101  }
102 
103  // maximum number of g4.prim files in the dest directory
104  fMaxFileNum = FR_MAX_FILE_NUM ; // initialization
105  if ( getenv( "G4DAWNFILE_MAX_FILE_NUM" ) != NULL ) {
106 
107  sscanf( getenv("G4DAWNFILE_MAX_FILE_NUM"), "%d", &fMaxFileNum ) ;
108 
109  } else {
110  fMaxFileNum = FR_MAX_FILE_NUM ;
111  }
112  if( fMaxFileNum < 1 ) { fMaxFileNum = 1 ; }
113 
114 
115  //----- precision control
116  if( getenv( "G4DAWNFILE_PRECISION" ) != NULL ) {
117  sscanf( getenv("G4DAWNFILE_PRECISION"), "%d", &fPrec ) ;
118  } else {
119  fPrec = 9 ;
120  }
121  fPrec2 = fPrec + 7 ;
122 
123 }
124 
125 
126  //----- G4DAWNFILESceneHandler, destructor
128 {
129 #if defined DEBUG_FR_SCENE
131  G4cout << "***** ~G4DAWNFILESceneHandler" << G4endl;
132 #endif
133  if (fPrimDest.IsOpen())
134  {
135  //----- End of modeling
136  // !EndModeling, !DrawAll, !CloseDevice,
137  // close g4.prim
138  FREndModeling();
139  }
140 }
141 
142 //-----
144 {
145  // g4_00.prim, g4_01.prim, ..., g4_MAX_FILE_INDEX.prim
146  const int MAX_FILE_INDEX = fMaxFileNum - 1 ;
147 
148  // dest directory (null if no environmental variables is set)
149  strcpy ( fG4PrimFileName, fG4PrimDestDir) ;
150 
151  // create full path name (default)
152  strcat ( fG4PrimFileName, DEFAULT_G4PRIM_FILE_NAME );
153 
154  // Automatic updation of file names
155  for( int i = 0 ; i < fMaxFileNum ; i++) {
156 
157  // Message in the final execution
158  if( i == MAX_FILE_INDEX )
159  {
161  G4cout << "===========================================" << G4endl;
162  G4cout << "WARNING MESSAGE from DAWNFILE driver: " << G4endl;
163  G4cout << " This file name is the final one in the " << G4endl;
164  G4cout << " automatic updation of the output file name." << G4endl;
165  G4cout << " You may overwrite existing files, i.e. " << G4endl;
166  G4cout << " g4_XX.prim and g4_XX.eps " << G4endl;
167  G4cout << "===========================================" << G4endl;
168  }
169  }
170 
171  // re-determine file name as G4DAWNFILE_DEST_DIR/g4_XX.prim
172  if( i >= 0 && i <= 9 ) {
173  sprintf( fG4PrimFileName, "%s%s%s%d.prim" , fG4PrimDestDir, G4PRIM_FILE_HEADER, "0", i );
174  } else {
175  sprintf( fG4PrimFileName, "%s%s%d.prim" , fG4PrimDestDir, G4PRIM_FILE_HEADER, i );
176  }
177 
178  // check validity of the file name
179  std::ifstream fin ;
180  fin.open(fG4PrimFileName) ;
181  if(!fin) {
182  // new file
183  fin.close();
184  break;
185  } else {
186  // already exists (try next)
187  fin.close();
188  }
189 
190  } // for
191 
192  G4cout << "=========================================== " << G4endl;
193  G4cout << "Output file: " << fG4PrimFileName << G4endl;
194  G4cout << "Destination directory (current dir if NULL): " << fG4PrimDestDir << G4endl;
195  G4cout << "Maximal number of files in the destination directory: " << fMaxFileNum << G4endl;
196  G4cout << "Note: " << G4endl;
197  G4cout << " * The maximal number is customizable as: " << G4endl;
198  G4cout << " % setenv G4DAWNFILE_MAX_FILE_NUM number " << G4endl;
199  G4cout << " * The destination directory is customizable as:" << G4endl;
200  G4cout << " % setenv G4DAWNFILE_DEST_DIR dir_name/ " << G4endl;
201  G4cout << " ** Do not forget \"/\" at the end of the " << G4endl;
202  G4cout << " dir_name, e.g. \"./tmp/\". " << G4endl;
203  G4cout << "=========================================== " << G4endl;
204 
205 } // G4DAWNFILESceneHandler::SetG4PrimFileName()
206 
207 
208 //-----
210 {
211 #if defined DEBUG_FR_SCENE
213  G4cout << "***** BeginSavingG4Prim (called)\n";
214 #endif
215 
216  if( !IsSavingG4Prim() )
217  {
218 #if defined DEBUG_FR_SCENE
220  G4cout << "***** (started) " ;
221  G4cout << "(open g4.prim, ##)" << G4endl;
222  }
223 #endif
224  SetG4PrimFileName() ; // result set to fG4PrimFileName
225  fPrimDest.Open(fG4PrimFileName) ;
226 
228  flag_saving_g4_prim = true ;
229  }
230 }
231 
233 {
234 #if defined DEBUG_FR_SCENE
236  G4cout << "***** EndSavingG4Prim (called)\n";
237 #endif
238 
239  if( IsSavingG4Prim() )
240  {
241 #if defined DEBUG_FR_SCENE
243  G4cout << "***** (started) (close g4.prim)" << G4endl;
244 #endif
245  fPrimDest.Close() ;
246  flag_saving_g4_prim = false ;
247  }
248 }
249 
250 
251 //-----
253 {
254  if( !FRIsInModeling() )
255  {
256 #if defined DEBUG_FR_SCENE
258  G4cout << "***** G4DAWNFILESceneHandler::FRBeginModeling (called & started)" << G4endl;
259 #endif
260 
261  //----- Send saving command and heading comment
263 
264  //----- Send bounding box command
265  SendBoundingBox();
266 
267  //----- send SET_CAMERA command
268 #if defined DEBUG_FR_SCENE
270  G4cout << "***** (!SetCamera in FRBeginModeling())" << G4endl;
271 #endif
273 
274  //----- open device
275 #if defined DEBUG_FR_SCENE
277  G4cout << "***** (!OpenDevice in FRBeginModeling())" << G4endl;
278 #endif
280 
281  //----- begin sending primitives
282 #if defined DEBUG_FR_SCENE
284  G4cout << "***** (!BeginModeling in FRBeginModeling())" << G4endl;
285 #endif
286  SendStr( FR_BEGIN_MODELING ); FRflag_in_modeling = true ;
287 
288  } // if
289 
290 }
291 
292 
293 /////////////////////////////////////////
294 // Common to DAWN and DAWNFILE drivers //
295 /////////////////////////////////////////
296 
297 #define G4FRSCENEHANDLER G4DAWNFILESceneHandler
298 #include "G4FRSceneFunc.icc"
299 #undef G4FRSCENEHANDLER
300 
301 //////////////////////
302 // static variables //
303 //////////////////////
304 
305  //----- static variables
306 G4int G4DAWNFILESceneHandler::fSceneIdCount = 0;
void SendStr(const char *char_string)
G4DAWNFILESceneHandler(G4DAWNFILE &system, const G4String &name="")
const char FR_BEGIN_MODELING[]
Definition: G4FRConst.hh:64
const char G4PRIM_FILE_HEADER[]
const XML_Char * name
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)