[834] | 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: G4VRML2FileSceneHandler.cc,v 1.14 2006/06/29 21:26:11 gunter Exp $ |
---|
[944] | 28 | // GEANT4 tag $Name: $ |
---|
[834] | 29 | // |
---|
| 30 | // G4VRML2FileSceneHandler.cc |
---|
| 31 | // Satoshi Tanaka & Yasuhide Sawada |
---|
| 32 | |
---|
| 33 | |
---|
| 34 | //#define DEBUG_FR_SCENE |
---|
| 35 | |
---|
| 36 | #include <fstream> |
---|
| 37 | #include <stdio.h> |
---|
| 38 | #include <string.h> |
---|
| 39 | #include <stdlib.h> |
---|
| 40 | #include <cmath> |
---|
| 41 | |
---|
| 42 | #include "globals.hh" |
---|
| 43 | #include "G4VPhysicalVolume.hh" |
---|
| 44 | #include "G4LogicalVolume.hh" |
---|
| 45 | #include "G4Point3D.hh" |
---|
| 46 | #include "G4VisAttributes.hh" |
---|
| 47 | #include "G4VModel.hh" |
---|
| 48 | #include "G4Scene.hh" |
---|
| 49 | #include "G4Polyhedron.hh" |
---|
| 50 | #include "G4Box.hh" |
---|
| 51 | #include "G4Cons.hh" |
---|
| 52 | #include "G4Polyline.hh" |
---|
| 53 | #include "G4Trd.hh" |
---|
| 54 | #include "G4Tubs.hh" |
---|
| 55 | #include "G4Text.hh" |
---|
| 56 | #include "G4Circle.hh" |
---|
| 57 | #include "G4Square.hh" |
---|
| 58 | |
---|
| 59 | #include "G4VRML2FileSceneHandler.hh" |
---|
| 60 | #include "G4VRML2FileViewer.hh" |
---|
| 61 | #include "G4VRML2File.hh" |
---|
| 62 | |
---|
| 63 | // CONST |
---|
| 64 | |
---|
| 65 | const char WRL_FILE_HEADER [] = "g4_"; |
---|
| 66 | const char DEFAULT_WRL_FILE_NAME[] = "g4.wrl"; |
---|
| 67 | const char ENV_VRML_VIEWER [] = "G4VRMLFILE_VIEWER"; |
---|
| 68 | const char NO_VRML_VIEWER [] = "NONE"; |
---|
| 69 | const char VRMLFILE_DEST_DIR [] = "G4VRMLFILE_DEST_DIR"; |
---|
| 70 | const int DEFAULT_MAX_WRL_FILE_NUM = 100 ; |
---|
| 71 | |
---|
| 72 | |
---|
| 73 | G4VRML2FileSceneHandler::G4VRML2FileSceneHandler(G4VRML2File& system, const G4String& name) : |
---|
| 74 | G4VSceneHandler(system, fSceneIdCount++, name), |
---|
| 75 | fSystem(system), |
---|
| 76 | fFlagDestOpen( false ), |
---|
| 77 | fPVPickable ( false ), |
---|
| 78 | fDest() |
---|
| 79 | { |
---|
| 80 | // output file name |
---|
| 81 | strcpy(fVRMLFileName, ""); |
---|
| 82 | |
---|
| 83 | // destination directory |
---|
| 84 | if ( getenv( VRMLFILE_DEST_DIR ) == NULL ) { |
---|
| 85 | strcpy( fVRMLFileDestDir, "" ); |
---|
| 86 | } else { |
---|
| 87 | strcpy( fVRMLFileDestDir, getenv( VRMLFILE_DEST_DIR ) ); |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | |
---|
| 91 | // maximum number of g4.prim files in the dest directory |
---|
| 92 | fMaxFileNum = DEFAULT_MAX_WRL_FILE_NUM ; // initialization |
---|
| 93 | if ( getenv( "G4VRMLFILE_MAX_FILE_NUM" ) != NULL ) { |
---|
| 94 | |
---|
| 95 | sscanf( getenv("G4VRMLFILE_MAX_FILE_NUM"), "%d", &fMaxFileNum ) ; |
---|
| 96 | |
---|
| 97 | } else { |
---|
| 98 | fMaxFileNum = DEFAULT_MAX_WRL_FILE_NUM ; |
---|
| 99 | } |
---|
| 100 | if( fMaxFileNum < 1 ) { fMaxFileNum = 1; } |
---|
| 101 | |
---|
| 102 | |
---|
| 103 | // PV name pickability |
---|
| 104 | if( getenv( "G4VRML_PV_PICKABLE" ) != NULL ) { |
---|
| 105 | |
---|
| 106 | int is_pickable ; |
---|
| 107 | sscanf( getenv("G4VRML_PV_PICKABLE"), "%d", &is_pickable ) ; |
---|
| 108 | |
---|
| 109 | if ( is_pickable ) { SetPVPickability ( true ) ; } |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | // PV Transparency |
---|
| 113 | SetPVTransparency (); |
---|
| 114 | |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | |
---|
| 118 | G4VRML2FileSceneHandler::~G4VRML2FileSceneHandler() |
---|
| 119 | { |
---|
| 120 | #if defined DEBUG_FR_SCENE |
---|
| 121 | G4cerr << "***** ~G4VRML2FileSceneHandler" << G4endl; |
---|
| 122 | #endif |
---|
| 123 | VRMLEndModeling(); |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | |
---|
| 127 | #define G4VRML2SCENEHANDLER G4VRML2FileSceneHandler |
---|
| 128 | #define IS_CONNECTED this->isConnected() |
---|
| 129 | #include "G4VRML2SceneHandlerFunc.icc" |
---|
| 130 | #undef IS_CONNECTED |
---|
| 131 | #undef G4VRML2SCENEHANDLER |
---|
| 132 | |
---|
| 133 | |
---|
| 134 | void G4VRML2FileSceneHandler::connectPort() |
---|
| 135 | { |
---|
| 136 | // g4_00.wrl, g4_01.wrl, ..., g4_MAX_FILE_INDEX.wrl |
---|
| 137 | const int MAX_FILE_INDEX = fMaxFileNum - 1 ; |
---|
| 138 | |
---|
| 139 | // dest directory (null if no environmental variables is set) |
---|
| 140 | strcpy ( fVRMLFileName, fVRMLFileDestDir) ; |
---|
| 141 | |
---|
| 142 | // create full path name (default) |
---|
| 143 | strcat ( fVRMLFileName, DEFAULT_WRL_FILE_NAME ); |
---|
| 144 | |
---|
| 145 | // Determine VRML file name |
---|
| 146 | for( int i = 0 ; i < fMaxFileNum ; i++) { |
---|
| 147 | |
---|
| 148 | // Message in the final execution |
---|
| 149 | if( i == MAX_FILE_INDEX ) |
---|
| 150 | { |
---|
| 151 | G4cerr << "===========================================" << G4endl; |
---|
| 152 | G4cerr << "WARNING MESSAGE from VRML2FILE driver: " << G4endl; |
---|
| 153 | G4cerr << " This file name is the final one in the " << G4endl; |
---|
| 154 | G4cerr << " automatic updation of the output file name." << G4endl; |
---|
| 155 | G4cerr << " You may overwrite existing files, i.e. " << G4endl; |
---|
| 156 | G4cerr << " g4_XX.wrl. " << G4endl; |
---|
| 157 | G4cerr << "===========================================" << G4endl; |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | // re-determine file name as G4VRMLFILE_DEST_DIR/g4_XX.wrl |
---|
| 161 | if( i >= 0 && i <= 9 ) { |
---|
| 162 | sprintf( fVRMLFileName, "%s%s%s%d.wrl" , fVRMLFileDestDir, WRL_FILE_HEADER, "0", i ); |
---|
| 163 | } else { |
---|
| 164 | sprintf( fVRMLFileName, "%s%s%d.wrl" , fVRMLFileDestDir, WRL_FILE_HEADER, i ); |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | // check validity of the file name |
---|
| 168 | std::ifstream fin ; |
---|
| 169 | fin.open(fVRMLFileName) ; |
---|
| 170 | if(!fin) { |
---|
| 171 | // new file |
---|
| 172 | fin.close(); |
---|
| 173 | break; |
---|
| 174 | } else { |
---|
| 175 | // already exists (try next) |
---|
| 176 | fin.close(); |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | } // for |
---|
| 180 | |
---|
| 181 | // open a VRML 2.0 file with determined file name |
---|
| 182 | G4cerr << "===========================================" << G4endl; |
---|
| 183 | G4cerr << "Output VRML 2.0 file: " << fVRMLFileName << G4endl; |
---|
| 184 | G4cerr << "Maximum number of files in the destination directory: " << fMaxFileNum << G4endl; |
---|
| 185 | G4cerr << " (Customizable with the environment variable: G4VRMLFILE_MAX_FILE_NUM) " << G4endl; |
---|
| 186 | G4cerr << "===========================================" << G4endl; |
---|
| 187 | |
---|
| 188 | fDest.open(fVRMLFileName) ; fFlagDestOpen = true ; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | |
---|
| 192 | void G4VRML2FileSceneHandler::closePort() |
---|
| 193 | { |
---|
| 194 | char command[256] ; |
---|
| 195 | char viewer [256] ; |
---|
| 196 | strcpy( viewer, NO_VRML_VIEWER ); // initialization |
---|
| 197 | if( getenv( ENV_VRML_VIEWER ) ) { |
---|
| 198 | strcpy( viewer, getenv( ENV_VRML_VIEWER ) ) ; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | // close VRML file |
---|
| 202 | fDest.close(); fFlagDestOpen = false ; |
---|
| 203 | G4cerr << "*** VRML 2.0 File " << fVRMLFileName << " is generated." << G4endl; |
---|
| 204 | |
---|
| 205 | |
---|
| 206 | // Invoke viewer |
---|
| 207 | |
---|
| 208 | if ( !strcmp(viewer, NO_VRML_VIEWER )) { |
---|
| 209 | G4cerr << "MESSAGE from VRML2FILE driver:" << G4endl; |
---|
| 210 | G4cerr << " Set an environmental variable " ; |
---|
| 211 | G4cerr << ENV_VRML_VIEWER << G4endl; |
---|
| 212 | G4cerr << " if you want to visualize the generated VRML file" << G4endl; |
---|
| 213 | G4cerr << " automatically. For example, " << G4endl; |
---|
| 214 | G4cerr << " setenv " << ENV_VRML_VIEWER << " vrwave " << G4endl; |
---|
| 215 | } else { |
---|
| 216 | sprintf( command, "%s %s", viewer, fVRMLFileName ); |
---|
| 217 | system( command ); |
---|
| 218 | } |
---|
| 219 | } |
---|
| 220 | |
---|
| 221 | G4int G4VRML2FileSceneHandler::fSceneIdCount = 0; |
---|