Geant4 Cross Reference |
1 // 1 // 2 // ******************************************* 2 // ******************************************************************** 3 // * License and Disclaimer 3 // * License and Disclaimer * 4 // * 4 // * * 5 // * The Geant4 software is copyright of th 5 // * The Geant4 software is copyright of the Copyright Holders of * 6 // * the Geant4 Collaboration. It is provided 6 // * the Geant4 Collaboration. It is provided under the terms and * 7 // * conditions of the Geant4 Software License 7 // * conditions of the Geant4 Software License, included in the file * 8 // * LICENSE and available at http://cern.ch/ 8 // * LICENSE and available at http://cern.ch/geant4/license . These * 9 // * include a list of copyright holders. 9 // * include a list of copyright holders. * 10 // * 10 // * * 11 // * Neither the authors of this software syst 11 // * Neither the authors of this software system, nor their employing * 12 // * institutes,nor the agencies providing fin 12 // * institutes,nor the agencies providing financial support for this * 13 // * work make any representation or warran 13 // * work make any representation or warranty, express or implied, * 14 // * regarding this software system or assum 14 // * regarding this software system or assume any liability for its * 15 // * use. Please see the license in the file 15 // * use. Please see the license in the file LICENSE and URL above * 16 // * for the full disclaimer and the limitatio 16 // * for the full disclaimer and the limitation of liability. * 17 // * 17 // * * 18 // * This code implementation is the result 18 // * This code implementation is the result of the scientific and * 19 // * technical work of the GEANT4 collaboratio 19 // * technical work of the GEANT4 collaboration. * 20 // * By using, copying, modifying or distri 20 // * By using, copying, modifying or distributing the software (or * 21 // * any work based on the software) you ag 21 // * any work based on the software) you agree to acknowledge its * 22 // * use in resulting scientific publicati 22 // * use in resulting scientific publications, and indicate your * 23 // * acceptance of all terms of the Geant4 Sof 23 // * acceptance of all terms of the Geant4 Software license. * 24 // ******************************************* 24 // ******************************************************************** 25 // 25 // 26 // 26 // 27 // 27 // 28 // G4VRML2FileSceneHandler.cc 28 // G4VRML2FileSceneHandler.cc 29 // Satoshi Tanaka & Yasuhide Sawada 29 // Satoshi Tanaka & Yasuhide Sawada 30 30 >> 31 31 //#define DEBUG_FR_SCENE 32 //#define DEBUG_FR_SCENE 32 33 33 #include <fstream> 34 #include <fstream> 34 #include <stdio.h> 35 #include <stdio.h> 35 #include <string.h> 36 #include <string.h> 36 #include <stdlib.h> 37 #include <stdlib.h> 37 #include <cmath> 38 #include <cmath> 38 #include <sstream> 39 #include <sstream> 39 #include <iomanip> 40 #include <iomanip> 40 41 41 #include "globals.hh" 42 #include "globals.hh" 42 #include "G4VPhysicalVolume.hh" 43 #include "G4VPhysicalVolume.hh" 43 #include "G4LogicalVolume.hh" 44 #include "G4LogicalVolume.hh" 44 #include "G4VisManager.hh" 45 #include "G4VisManager.hh" >> 46 #include "G4Point3D.hh" 45 #include "G4VisAttributes.hh" 47 #include "G4VisAttributes.hh" 46 #include "G4VModel.hh" 48 #include "G4VModel.hh" 47 #include "G4Scene.hh" 49 #include "G4Scene.hh" 48 #include "G4Polyhedron.hh" 50 #include "G4Polyhedron.hh" 49 #include "G4Box.hh" 51 #include "G4Box.hh" 50 #include "G4Cons.hh" 52 #include "G4Cons.hh" 51 #include "G4Polyline.hh" 53 #include "G4Polyline.hh" 52 #include "G4Trd.hh" 54 #include "G4Trd.hh" 53 #include "G4Tubs.hh" 55 #include "G4Tubs.hh" 54 #include "G4Text.hh" 56 #include "G4Text.hh" 55 #include "G4Circle.hh" 57 #include "G4Circle.hh" 56 #include "G4Square.hh" 58 #include "G4Square.hh" 57 59 58 #include "G4VRML2FileSceneHandler.hh" 60 #include "G4VRML2FileSceneHandler.hh" 59 #include "G4VRML2FileViewer.hh" 61 #include "G4VRML2FileViewer.hh" 60 #include "G4VRML2File.hh" 62 #include "G4VRML2File.hh" 61 63 62 // CONST 64 // CONST 63 65 64 const char WRL_FILE_HEADER[] = "g4_"; << 66 const char WRL_FILE_HEADER [] = "g4_"; 65 const char ENV_WRL_FILE_HEADER[] = "G4VRMLFI << 67 const char DEFAULT_WRL_FILE_NAME[] = "g4.wrl"; 66 const char ENV_WRL_FILE_NAME[] = "G4VRMLFI << 68 const char ENV_VRML_VIEWER [] = "G4VRMLFILE_VIEWER"; 67 const char ENV_VRML_VIEWER[] = "G4VRMLFI << 69 const char NO_VRML_VIEWER [] = "NONE"; 68 const char NO_VRML_VIEWER[] = "NONE"; << 70 const char VRMLFILE_DEST_DIR [] = "G4VRMLFILE_DEST_DIR"; 69 const char VRMLFILE_DEST_DIR[] = "G4VRMLFI << 71 const int DEFAULT_MAX_WRL_FILE_NUM = 100 ; 70 const int DEFAULT_MAX_WRL_FILE_NUM = 100; << 72 71 << 73 72 G4VRML2FileSceneHandler::G4VRML2FileSceneHandl << 74 G4VRML2FileSceneHandler::G4VRML2FileSceneHandler(G4VRML2File& system, const G4String& name) : 73 << 75 G4VSceneHandler(system, fSceneIdCount++, name), 74 : G4VSceneHandler(system, fSceneIdCount++, n << 76 fSystem(system), 75 , fSystem(system) << 77 fFlagDestOpen( false ), 76 , fFlagDestOpen(false) << 78 fPVPickable ( false ), 77 , fPVPickable(false) << 79 fDest() 78 , fDest() << 79 { 80 { 80 // output file name << 81 // output file name 81 strcpy(fVRMLFileName, ""); << 82 strcpy(fVRMLFileName, ""); >> 83 >> 84 // destination directory >> 85 if ( std::getenv( VRMLFILE_DEST_DIR ) == NULL ) { >> 86 strcpy( fVRMLFileDestDir, "" ); >> 87 } else { >> 88 strcpy( fVRMLFileDestDir, std::getenv( VRMLFILE_DEST_DIR ) ); >> 89 } >> 90 >> 91 >> 92 // maximum number of g4.prim files in the dest directory >> 93 fMaxFileNum = DEFAULT_MAX_WRL_FILE_NUM ; // initialization >> 94 if ( std::getenv( "G4VRMLFILE_MAX_FILE_NUM" ) != NULL ) { >> 95 >> 96 sscanf( std::getenv("G4VRMLFILE_MAX_FILE_NUM"), "%d", &fMaxFileNum ) ; >> 97 >> 98 } else { >> 99 fMaxFileNum = DEFAULT_MAX_WRL_FILE_NUM ; >> 100 } >> 101 if( fMaxFileNum < 1 ) { fMaxFileNum = 1; } >> 102 >> 103 >> 104 // PV name pickability >> 105 if( std::getenv( "G4VRML_PV_PICKABLE" ) != NULL ) { 82 106 83 // destination directory << 107 int is_pickable ; 84 if(std::getenv(VRMLFILE_DEST_DIR) == NULL) << 108 sscanf( std::getenv("G4VRML_PV_PICKABLE"), "%d", &is_pickable ) ; 85 { << 109 86 strcpy(fVRMLFileDestDir, ""); << 110 if ( is_pickable ) { SetPVPickability ( true ) ; } 87 } << 111 } 88 else << 112 89 { << 113 // PV Transparency 90 strcpy(fVRMLFileDestDir, std::getenv(VRMLF << 114 SetPVTransparency (); 91 } << 92 << 93 // maximum number of g4.prim files in the de << 94 fMaxFileNum = DEFAULT_MAX_WRL_FILE_NUM; // << 95 if(std::getenv("G4VRMLFILE_MAX_FILE_NUM") != << 96 { << 97 sscanf(std::getenv("G4VRMLFILE_MAX_FILE_NU << 98 } << 99 else << 100 { << 101 fMaxFileNum = DEFAULT_MAX_WRL_FILE_NUM; << 102 } << 103 if(fMaxFileNum < 1) << 104 { << 105 fMaxFileNum = 1; << 106 } << 107 << 108 // PV name pickability << 109 if(std::getenv("G4VRML_PV_PICKABLE") != NULL << 110 { << 111 int is_pickable; << 112 sscanf(std::getenv("G4VRML_PV_PICKABLE"), << 113 << 114 if(is_pickable) << 115 { << 116 SetPVPickability(true); << 117 } << 118 } << 119 115 120 // PV Transparency << 121 SetPVTransparency(); << 122 } 116 } 123 117 >> 118 124 G4VRML2FileSceneHandler::~G4VRML2FileSceneHand 119 G4VRML2FileSceneHandler::~G4VRML2FileSceneHandler() 125 { 120 { 126 #if defined DEBUG_FR_SCENE 121 #if defined DEBUG_FR_SCENE 127 if(G4VisManager::GetVerbosity() >= G4VisMana << 122 if (G4VisManager::GetVerbosity() >= G4VisManager::errors) 128 G4cout << "***** ~G4VRML2FileSceneHandler" << 123 G4cout << "***** ~G4VRML2FileSceneHandler" << G4endl; 129 #endif << 124 #endif 130 VRMLEndModeling(); << 125 VRMLEndModeling(); 131 } 126 } 132 127 133 #define G4VRML2SCENEHANDLER G4VRML2FileSceneHa << 128 134 #define IS_CONNECTED this->isConnected() << 129 #define G4VRML2SCENEHANDLER G4VRML2FileSceneHandler >> 130 #define IS_CONNECTED this->isConnected() 135 #include "G4VRML2SceneHandlerFunc.icc" 131 #include "G4VRML2SceneHandlerFunc.icc" 136 #undef IS_CONNECTED << 132 #undef IS_CONNECTED 137 #undef G4VRML2SCENEHANDLER << 133 #undef G4VRML2SCENEHANDLER >> 134 138 135 139 void G4VRML2FileSceneHandler::connectPort() 136 void G4VRML2FileSceneHandler::connectPort() 140 { 137 { 141 // g4_00.wrl, g4_01.wrl, ..., g4_MAX_FILE_IN << 138 // g4_00.wrl, g4_01.wrl, ..., g4_MAX_FILE_INDEX.wrl 142 const int MAX_FILE_INDEX = fMaxFileNum - 1; << 139 const int MAX_FILE_INDEX = fMaxFileNum - 1 ; >> 140 >> 141 // dest directory (null if no environmental variables is set) >> 142 strcpy ( fVRMLFileName, fVRMLFileDestDir) ; 143 143 144 // dest directory (null if no environmental << 144 // create full path name (default) 145 strcpy(fVRMLFileName, fVRMLFileDestDir); << 145 strcat ( fVRMLFileName, DEFAULT_WRL_FILE_NAME ); 146 // add filename if environment variable supp << 146 147 if(std::getenv(ENV_WRL_FILE_NAME)) << 147 // Determine VRML file name 148 { << 148 for( int i = 0 ; i < fMaxFileNum ; i++) { 149 strcat(fVRMLFileName, std::getenv(ENV_WRL_ << 149 150 } << 150 // Message in the final execution 151 else << 151 if( i == MAX_FILE_INDEX ) 152 { << 152 { 153 // Determine VRML file name << 153 if (G4VisManager::GetVerbosity() >= G4VisManager::errors) { 154 for(int i = 0; i < fMaxFileNum; i++) << 154 G4cout << "===========================================" << G4endl; 155 { << 155 G4cout << "WARNING MESSAGE from VRML2FILE driver: " << G4endl; 156 // Message in the final execution << 156 G4cout << " This file name is the final one in the " << G4endl; 157 if(i == MAX_FILE_INDEX) << 157 G4cout << " automatic updation of the output file name." << G4endl; 158 { << 158 G4cout << " You may overwrite existing files, i.e. " << G4endl; 159 if(G4VisManager::GetVerbosity() >= G4V << 159 G4cout << " g4_XX.wrl. " << G4endl; 160 { << 160 G4cout << "===========================================" << G4endl; 161 G4cout << "========================= << 161 } 162 G4cout << "WARNING MESSAGE from VRML << 162 } 163 G4cout << " This file name is the f << 163 164 G4cout << " automatic updation of t << 164 // re-determine file name as G4VRMLFILE_DEST_DIR/g4_XX.wrl 165 G4cout << " You may overwrite exist << 165 std::ostringstream filename; 166 G4cout << " g4_XX.wrl. << 166 filename 167 G4cout << "========================= << 167 << fVRMLFileDestDir << WRL_FILE_HEADER 168 } << 168 << std::setw(2) << std::setfill('0') << i << ".wrl"; 169 } << 169 strncpy(fVRMLFileName,filename.str().c_str(),sizeof(fVRMLFileName)-1); 170 << 170 fVRMLFileName[sizeof(fVRMLFileName)-1] = '\0'; 171 // re-determine file name as G4VRMLFILE_ << 171 172 std::ostringstream filename; << 172 // check validity of the file name 173 filename << fVRMLFileDestDir; << 173 std::ifstream fin ; 174 if(std::getenv(ENV_WRL_FILE_HEADER) == N << 174 fin.open(fVRMLFileName) ; 175 { << 175 if(!fin) { 176 filename << WRL_FILE_HEADER; << 176 // new file 177 } << 177 fin.close(); 178 else << 178 break; 179 { << 179 } else { 180 filename << std::getenv(ENV_WRL_FILE_H << 180 // already exists (try next) 181 } << 181 fin.close(); 182 filename << std::setw(2) << std::setfill << 182 } 183 strncpy(fVRMLFileName, filename.str().c_ << 183 184 fVRMLFileName[sizeof(fVRMLFileName) - 1] << 184 } // for 185 << 185 186 // check validity of the file name << 186 // open a VRML 2.0 file with determined file name 187 std::ifstream fin; << 187 if (G4VisManager::GetVerbosity() >= G4VisManager::errors) { 188 fin.open(fVRMLFileName); << 188 G4cout << "===========================================" << G4endl; 189 if(!fin) << 189 G4cout << "Output VRML 2.0 file: " << fVRMLFileName << G4endl; 190 { << 190 G4cout << "Maximum number of files in the destination directory: " << fMaxFileNum << G4endl; 191 // new file << 191 G4cout << " (Customizable with the environment variable: G4VRMLFILE_MAX_FILE_NUM) " << G4endl; 192 fin.close(); << 192 G4cout << "===========================================" << G4endl; 193 break; << 193 } 194 } << 194 fDest.open(fVRMLFileName) ; fFlagDestOpen = true ; 195 else << 196 { << 197 // already exists (try next) << 198 fin.close(); << 199 } << 200 << 201 } // for << 202 } << 203 << 204 // open a VRML 2.0 file with determined file << 205 if(G4VisManager::GetVerbosity() >= G4VisMana << 206 { << 207 G4cout << "=============================== << 208 G4cout << "Output VRML 2.0 file: " << fVRM << 209 G4cout << "Maximum number of files in the << 210 << fMaxFileNum << G4endl; << 211 G4cout << " (Customizable with the enviro << 212 "G4VRMLFILE_MAX_FILE_NUM) " << 213 << G4endl; << 214 G4cout << "=============================== << 215 } << 216 fDest.open(fVRMLFileName); << 217 fFlagDestOpen = true; << 218 } 195 } 219 196 >> 197 220 void G4VRML2FileSceneHandler::closePort() 198 void G4VRML2FileSceneHandler::closePort() 221 { 199 { 222 char command[256]; << 200 char command[256] ; 223 char viewer[256]; << 201 char viewer [256] ; 224 strcpy(viewer, NO_VRML_VIEWER); // initiali << 202 strcpy( viewer, NO_VRML_VIEWER ); // initialization 225 if(std::getenv(ENV_VRML_VIEWER)) << 203 if( std::getenv( ENV_VRML_VIEWER ) ) { 226 { << 204 strcpy( viewer, std::getenv( ENV_VRML_VIEWER ) ) ; 227 strcpy(viewer, std::getenv(ENV_VRML_VIEWER << 205 } 228 } << 206 229 << 207 // close VRML file 230 // close VRML file << 208 fDest.close(); fFlagDestOpen = false ; 231 fDest.close(); << 209 if (G4VisManager::GetVerbosity() >= G4VisManager::errors) 232 fFlagDestOpen = false; << 210 G4cout << "*** VRML 2.0 File " << fVRMLFileName << " is generated." << G4endl; 233 if(G4VisManager::GetVerbosity() >= G4VisMana << 211 234 G4cout << "*** VRML 2.0 File " << fVRMLFi << 212 235 << G4endl; << 213 // Invoke viewer 236 << 214 237 // Invoke viewer << 215 if ( !strcmp(viewer, NO_VRML_VIEWER )) { 238 << 216 if (G4VisManager::GetVerbosity() >= G4VisManager::errors) { 239 if(!strcmp(viewer, NO_VRML_VIEWER)) << 217 G4cout << "MESSAGE from VRML2FILE driver:" << G4endl; 240 { << 218 G4cout << " Set an environmental variable " ; 241 if(G4VisManager::GetVerbosity() >= G4VisMa << 219 G4cout << ENV_VRML_VIEWER << G4endl; 242 { << 220 G4cout << " if you want to visualize the generated VRML file" << G4endl; 243 G4cout << "MESSAGE from VRML2FILE driver << 221 G4cout << " automatically. For example, " << G4endl; 244 G4cout << " Set an environmental vari << 222 G4cout << " setenv " << ENV_VRML_VIEWER << " vrwave " << G4endl; 245 G4cout << ENV_VRML_VIEWER << G4endl; << 223 } 246 G4cout << " if you want to visualize << 224 } else { 247 << G4endl; << 225 std::ostringstream ossCommand; 248 G4cout << " automatically. For examp << 226 ossCommand << viewer << ' ' << fVRMLFileName; 249 G4cout << " setenv " << ENV_VRML_VIE << 227 strncpy(command,ossCommand.str().c_str(),sizeof(command)-1); 250 G4cout << "ALSO you may change the file << 228 command[sizeof(command)-1] = '\0'; 251 << ENV_WRL_FILE_HEADER << G4endl; << 229 int iErr = system( command ); 252 G4cout << " or the whole filename wit << 230 if ( iErr != 0 ) { 253 << G4endl; << 231 G4ExceptionDescription ed; 254 } << 232 ed << "Error " << iErr 255 } << 233 << " when calling system with \"" << command 256 else << 234 << "\"."; 257 { << 235 G4Exception("G4VRML2FileSceneHandler::closePort()", 258 std::ostringstream ossCommand; << 236 "VRML-2006", JustWarning, ed); 259 ossCommand << viewer << ' ' << fVRMLFileNa << 237 } 260 strncpy(command, ossCommand.str().c_str(), << 238 } 261 command[sizeof(command) - 1] = '\0'; << 262 int iErr = system(comm << 263 if(iErr != 0) << 264 { << 265 G4ExceptionDescription ed; << 266 ed << "Error " << iErr << " when calling << 267 << "\"."; << 268 G4Exception("G4VRML2FileSceneHandler::cl << 269 JustWarning, ed); << 270 } << 271 } << 272 } 239 } 273 240 274 G4int G4VRML2FileSceneHandler::fSceneIdCount = 241 G4int G4VRML2FileSceneHandler::fSceneIdCount = 0; 275 242