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,v 1.18 2006/06/29 21:16:52 gunter Exp $ |
---|
28 | // GEANT4 tag $Name: $ |
---|
29 | // |
---|
30 | // Satoshi TANAKA |
---|
31 | // DAWNFILE scene. |
---|
32 | |
---|
33 | |
---|
34 | #define __G_ANSI_C__ |
---|
35 | |
---|
36 | // #define DEBUG_FR_SCENE |
---|
37 | |
---|
38 | //----- header files |
---|
39 | #include <fstream> |
---|
40 | #include <stdlib.h> |
---|
41 | #include <string.h> |
---|
42 | #include "globals.hh" |
---|
43 | #include "G4FRConst.hh" |
---|
44 | #include "G4DAWNFILE.hh" |
---|
45 | #include "G4DAWNFILESceneHandler.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 |
---|
85 | G4DAWNFILESceneHandler::G4DAWNFILESceneHandler (G4DAWNFILE& system, const G4String& name): |
---|
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 |
---|
127 | G4DAWNFILESceneHandler::~G4DAWNFILESceneHandler () |
---|
128 | { |
---|
129 | #if defined DEBUG_FR_SCENE |
---|
130 | G4cerr << "***** ~G4DAWNFILESceneHandler" << G4endl; |
---|
131 | #endif |
---|
132 | if (fPrimDest.IsOpen()) |
---|
133 | { |
---|
134 | //----- End of modeling |
---|
135 | // !EndModeling, !DrawAll, !CloseDevice, |
---|
136 | // close g4.prim |
---|
137 | FREndModeling(); |
---|
138 | } |
---|
139 | ClearStore (); // clear current scene |
---|
140 | } |
---|
141 | |
---|
142 | //----- |
---|
143 | void G4DAWNFILESceneHandler::SetG4PrimFileName() |
---|
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 | { |
---|
160 | G4cerr << "===========================================" << G4endl; |
---|
161 | G4cerr << "WARNING MESSAGE from DAWNFILE driver: " << G4endl; |
---|
162 | G4cerr << " This file name is the final one in the " << G4endl; |
---|
163 | G4cerr << " automatic updation of the output file name." << G4endl; |
---|
164 | G4cerr << " You may overwrite existing files, i.e. " << G4endl; |
---|
165 | G4cerr << " g4_XX.prim and g4_XX.eps " << G4endl; |
---|
166 | G4cerr << "===========================================" << G4endl; |
---|
167 | } |
---|
168 | |
---|
169 | // re-determine file name as G4DAWNFILE_DEST_DIR/g4_XX.prim |
---|
170 | if( i >= 0 && i <= 9 ) { |
---|
171 | sprintf( fG4PrimFileName, "%s%s%s%d.prim" , fG4PrimDestDir, G4PRIM_FILE_HEADER, "0", i ); |
---|
172 | } else { |
---|
173 | sprintf( fG4PrimFileName, "%s%s%d.prim" , fG4PrimDestDir, G4PRIM_FILE_HEADER, i ); |
---|
174 | } |
---|
175 | |
---|
176 | // check validity of the file name |
---|
177 | std::ifstream fin ; |
---|
178 | fin.open(fG4PrimFileName) ; |
---|
179 | if(!fin) { |
---|
180 | // new file |
---|
181 | fin.close(); |
---|
182 | break; |
---|
183 | } else { |
---|
184 | // already exists (try next) |
---|
185 | fin.close(); |
---|
186 | } |
---|
187 | |
---|
188 | } // for |
---|
189 | |
---|
190 | G4cerr << "=========================================== " << G4endl; |
---|
191 | G4cerr << "Output file: " << fG4PrimFileName << G4endl; |
---|
192 | G4cerr << "Destination directory (current dir if NULL): " << fG4PrimDestDir << G4endl; |
---|
193 | G4cerr << "Maximal number of files in the destination directory: " << fMaxFileNum << G4endl; |
---|
194 | G4cerr << "Note: " << G4endl; |
---|
195 | G4cerr << " * The maximal number is customizable as: " << G4endl; |
---|
196 | G4cerr << " % setenv G4DAWNFILE_MAX_FILE_NUM number " << G4endl; |
---|
197 | G4cerr << " * The destination directory is customizable as:" << G4endl; |
---|
198 | G4cerr << " % setenv G4DAWNFILE_DEST_DIR dir_name/ " << G4endl; |
---|
199 | G4cerr << " ** Do not forget \"/\" at the end of the " << G4endl; |
---|
200 | G4cerr << " dir_name, e.g. \"./tmp/\". " << G4endl; |
---|
201 | G4cerr << "=========================================== " << G4endl; |
---|
202 | |
---|
203 | } // G4DAWNFILESceneHandler::SetG4PrimFileName() |
---|
204 | |
---|
205 | |
---|
206 | //----- |
---|
207 | void G4DAWNFILESceneHandler::BeginSavingG4Prim( void ) |
---|
208 | { |
---|
209 | #if defined DEBUG_FR_SCENE |
---|
210 | G4cerr << "***** BeginSavingG4Prim (called)\n"; |
---|
211 | #endif |
---|
212 | |
---|
213 | if( !IsSavingG4Prim() ) |
---|
214 | { |
---|
215 | #if defined DEBUG_FR_SCENE |
---|
216 | G4cerr << "***** (started) " ; |
---|
217 | G4cerr << "(open g4.prim, ##)" << G4endl; |
---|
218 | #endif |
---|
219 | SetG4PrimFileName() ; // result set to fG4PrimFileName |
---|
220 | fPrimDest.Open(fG4PrimFileName) ; |
---|
221 | |
---|
222 | SendStr( FR_G4_PRIM_HEADER ) ; |
---|
223 | flag_saving_g4_prim = true ; |
---|
224 | } |
---|
225 | } |
---|
226 | |
---|
227 | void G4DAWNFILESceneHandler::EndSavingG4Prim ( void ) |
---|
228 | { |
---|
229 | #if defined DEBUG_FR_SCENE |
---|
230 | G4cerr << "***** EndSavingG4Prim (called)\n"; |
---|
231 | #endif |
---|
232 | |
---|
233 | if( IsSavingG4Prim() ) |
---|
234 | { |
---|
235 | #if defined DEBUG_FR_SCENE |
---|
236 | G4cerr << "***** (started) (close g4.prim)" << G4endl; |
---|
237 | #endif |
---|
238 | fPrimDest.Close() ; |
---|
239 | flag_saving_g4_prim = false ; |
---|
240 | } |
---|
241 | } |
---|
242 | |
---|
243 | |
---|
244 | //----- |
---|
245 | void G4DAWNFILESceneHandler::FRBeginModeling( void ) |
---|
246 | { |
---|
247 | if( !FRIsInModeling() ) |
---|
248 | { |
---|
249 | #if defined DEBUG_FR_SCENE |
---|
250 | G4cerr << "***** G4DAWNFILESceneHandler::FRBeginModeling (called & started)" << G4endl; |
---|
251 | #endif |
---|
252 | |
---|
253 | //----- Send saving command and heading comment |
---|
254 | BeginSavingG4Prim(); |
---|
255 | |
---|
256 | //----- Send bounding box command |
---|
257 | SendBoundingBox(); |
---|
258 | |
---|
259 | //----- send SET_CAMERA command |
---|
260 | #if defined DEBUG_FR_SCENE |
---|
261 | G4cerr << "***** (!SetCamera in FRBeginModeling())" << G4endl; |
---|
262 | #endif |
---|
263 | SendStr( FR_SET_CAMERA ); |
---|
264 | |
---|
265 | //----- open device |
---|
266 | #if defined DEBUG_FR_SCENE |
---|
267 | G4cerr << "***** (!OpenDevice in FRBeginModeling())" << G4endl; |
---|
268 | #endif |
---|
269 | SendStr( FR_OPEN_DEVICE ); |
---|
270 | |
---|
271 | //----- begin sending primitives |
---|
272 | #if defined DEBUG_FR_SCENE |
---|
273 | G4cerr << "***** (!BeginModeling in FRBeginModeling())" << G4endl; |
---|
274 | #endif |
---|
275 | SendStr( FR_BEGIN_MODELING ); FRflag_in_modeling = true ; |
---|
276 | |
---|
277 | } // if |
---|
278 | |
---|
279 | } |
---|
280 | |
---|
281 | |
---|
282 | ///////////////////////////////////////// |
---|
283 | // Common to DAWN and DAWNFILE drivers // |
---|
284 | ///////////////////////////////////////// |
---|
285 | |
---|
286 | #define G4FRSCENEHANDLER G4DAWNFILESceneHandler |
---|
287 | #include "G4FRSceneFunc.icc" |
---|
288 | #undef G4FRSCENEHANDLER |
---|
289 | |
---|
290 | ////////////////////// |
---|
291 | // static variables // |
---|
292 | ////////////////////// |
---|
293 | |
---|
294 | //----- static variables |
---|
295 | G4int G4DAWNFILESceneHandler::fSceneIdCount = 0; |
---|