G4DAWNFILEViewer.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
00028 //
00029 // Satoshi TANAKA
00030 // DAWNFILE view - opens window, hard copy, etc.
00031 
00032 
00033 #define __G_ANSI_C__
00034 #define G4DAWNFILE_STRUCTURE_PRIORITY  1.
00035 
00036 // #define DEBUG_FR_VIEW
00037 
00038 #include "G4ios.hh"
00039 #include <stdio.h>
00040 #include <string.h>
00041 #include <assert.h>
00042 
00043 #include "G4VisManager.hh"
00044 #include "G4Scene.hh"
00045 #include "G4Vector3D.hh"
00046 #include "G4VisExtent.hh"
00047 #include "G4LogicalVolume.hh"
00048 #include "G4VSolid.hh"
00049 #include "G4PhysicalConstants.hh"
00050 
00051 #include "G4FRConst.hh"
00052 #include "G4DAWNFILE.hh"
00053 #include "G4DAWNFILESceneHandler.hh"
00054 #include "G4DAWNFILEViewer.hh"
00055 
00056 
00057 
00058         //----- constants
00059 const char  FR_ENV_MULTI_WINDOW [] = "G4DAWN_MULTI_WINDOW" ;
00060 const char  FR_ENV_MULTI_WINDOW2[] = "G4DAWNFILE_MULTI_WINDOW" ;
00061 
00062         //----- G4DAWNFILEViewer, constructor
00063 G4DAWNFILEViewer::G4DAWNFILEViewer (G4DAWNFILESceneHandler& sceneHandler,
00064                                 const G4String& name): 
00065   G4VViewer (sceneHandler,
00066              sceneHandler.IncrementViewCount (),
00067              name),
00068   fSceneHandler (sceneHandler)
00069 {
00070         // Set a g4.prim-file viewer 
00071         strcpy( fG4PrimViewer, "dawn" ); 
00072         if( getenv( "G4DAWNFILE_VIEWER" ) != NULL ) {
00073                 strcpy( fG4PrimViewer, getenv( "G4DAWNFILE_VIEWER" ) ) ;                        
00074         } 
00075 
00076         // string for viewer invocation
00077         if ( !strcmp( fG4PrimViewer, "NONE" ) ) {
00078                 
00079                 strcpy( fG4PrimViewerInvocation, "" );
00080         } else {
00081 
00082                 strcpy( fG4PrimViewerInvocation, fG4PrimViewer );
00083                 strcat( fG4PrimViewerInvocation, " ");
00084                 strcat( fG4PrimViewerInvocation, fSceneHandler.GetG4PrimFileName() );
00085         }
00086 
00087         // Set a PostScript Viewer
00088 //      strcpy( fPSViewer, "ghostview" ); 
00089         strcpy( fPSViewer, "gv" ); 
00090         if( getenv( "G4DAWNFILE_PS_VIEWER" ) != NULL ) {
00091                 strcpy( fPSViewer, getenv( "G4DAWNFILE_PS_VIEWER" ) ) ;                 
00092         } 
00093 
00094 }
00095 
00096         //----- G4DAWNFILEViewer, destructor
00097 G4DAWNFILEViewer::~G4DAWNFILEViewer () 
00098 {}
00099 
00100         //----- G4DAWNFILEViewer::SetView () 
00101 void G4DAWNFILEViewer::SetView () 
00102 {
00103 #if defined DEBUG_FR_VIEW
00104   if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
00105     G4cout << "***** G4DAWNFILEViewer::SetView(): No effects" << G4endl;
00106 #endif 
00107 // Do nothing, since DAWN is running as a different process.
00108 // SendViewParameters () will do this job instead.
00109 }
00110 
00111 
00112         //----- G4DAWNFILEViewer::ClearView()
00113 void
00114 G4DAWNFILEViewer::ClearView( void )
00115 {
00116 #if defined DEBUG_FR_VIEW
00117   if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
00118         G4cout << "***** G4DAWNFILEViewer::ClearView (): No effects " << G4endl;
00119 #endif
00120         if (fSceneHandler.fPrimDest.IsOpen()) {
00121           fSceneHandler.fPrimDest.Close();
00122           // Re-open with same filename...
00123           fSceneHandler.fPrimDest.Open(fSceneHandler.fG4PrimFileName);
00124           fSceneHandler.SendStr( FR_G4_PRIM_HEADER );
00125           fSceneHandler.FRflag_in_modeling = false;
00126           fSceneHandler.FRBeginModeling();
00127         }
00128 }
00129 
00130 
00131         //----- G4DAWNFILEViewer::DrawView () 
00132 void G4DAWNFILEViewer::DrawView () 
00133 {
00134 #if defined DEBUG_FR_VIEW
00135   if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
00136         G4cout << "***** G4DAWNFILEViewer::DrawView () " << G4endl;
00137 #endif
00138                 //----- 
00139         fSceneHandler.FRBeginModeling() ;
00140 
00141                 //----- Always visit G4 kernel 
00142         NeedKernelVisit ();
00143                                    
00144                 //----- Draw
00145         ProcessView () ;
00146 
00147 } // G4DAWNFILEViewer::DrawView () 
00148 
00149 
00150 
00151         //----- G4DAWNFILEViewer::ShowView()
00152 void G4DAWNFILEViewer::ShowView( void )
00153 {
00154 #if defined DEBUG_FR_VIEW
00155   if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
00156         G4cout << "***** G4DAWNFILEViewer::ShowView () " << G4endl;
00157 #endif
00158 
00159         if( fSceneHandler.FRIsInModeling() ) 
00160         {
00161                         //----- End of modeling
00162                         // !EndModeling, !DrawAll, !CloseDevice,
00163                         // close g4.prim
00164                 fSceneHandler.FREndModeling();
00165 
00166                         //----- Output DAWN GUI file 
00167                 SendViewParameters(); 
00168 
00169                         //----- string for viewer invocation
00170                 if ( !strcmp( fG4PrimViewer, "NONE" ) ) {
00171                 
00172                         strcpy( fG4PrimViewerInvocation, "" );
00173                 } else {
00174 
00175                         strcpy( fG4PrimViewerInvocation, fG4PrimViewer );
00176                         strcat( fG4PrimViewerInvocation, " ");
00177                         strcat( fG4PrimViewerInvocation, fSceneHandler.GetG4PrimFileName() );
00178                 }
00179 
00180 
00181                 //----- Invoke DAWN
00182                 G4cout << G4endl ;
00183                 if( false == G4FRofstream::DoesFileExist( fSceneHandler.GetG4PrimFileName() ) )   
00184                 {
00185                         G4cout << "ERROR: Failed to generate file  ";
00186                         G4cout << fSceneHandler.GetG4PrimFileName() << G4endl;
00187 
00188                 } else  if( strcmp( GetG4PrimViewerInvocation(), "" ) )  
00189                 {
00190                         G4cout << "File  " << fSceneHandler.GetG4PrimFileName() ;
00191                         G4cout << "  is generated." << G4endl;
00192                         G4cout << GetG4PrimViewerInvocation() << G4endl;
00193                         (void) system( GetG4PrimViewerInvocation() );
00194 
00195                 } else { // no view, i.e., only file generation
00196                         G4cout << "File  " << fSceneHandler.GetG4PrimFileName() ; 
00197                         G4cout << "  is generated." << G4endl;
00198                         G4cout << "No viewer is invoked." << G4endl;
00199                 }
00200 
00201         }
00202 
00203 } // G4DAWNFILEViewer::ShowView()
00204 
00205 
00206         //----- G4DAWNFILEViewer::SendDrawingStyleToDAWNGUI( std::ostream& out ) 
00207 void  G4DAWNFILEViewer::SendDrawingStyleToDAWNGUI( std::ostream& out ) 
00208 {
00210 //#if defined DEBUG_FR_VIEW
00211 //  G4cout << "***** G4DAWNFILEViewer::SendDrawingStyleToDAWNGUI()" << G4endl;
00212 //#endif
00214 
00215         G4int  style = fVP.GetDrawingStyle();
00216 
00217         enum {  FR_WIREFRAME_STYLE = 1, FR_WF_STORED_STYLE = 2,
00218                 FR_HID_STYLE       = 3, FR_HID2_STYLE      = 4,
00219                 FR_HID3_STYLE      = 5, FR_DRAWING_MODE_END_STYLE = 6 };
00220 
00221         switch( style )
00222         {
00223           case G4ViewParameters::wireframe: 
00224                 out <<  FR_WIREFRAME_STYLE << G4endl;
00225                 break;
00226           case G4ViewParameters::hlr:
00227                 out <<  FR_HID2_STYLE      << G4endl; // LINE
00228                 break;
00229           case G4ViewParameters::hsr:
00230           case G4ViewParameters::hlhsr:
00231                 out <<  FR_HID_STYLE       << G4endl; // SURFACE
00232                 break;
00233           default:
00234                 out <<  FR_WIREFRAME_STYLE << G4endl;
00235                 break;
00236         }
00237 
00238 } // G4DAWNFILEViewer::SendDrawingStyle()  
00239 
00240 
00241 
00242 //----- 
00243 void G4DAWNFILEViewer::SendViewParameters () 
00244 {
00245   // Calculates view representation based on extent of object being
00246   // viewed and (initial) direction of camera.  (Note: it can change
00247   // later due to user interaction via visualization system's GUI.)
00248 
00249 #if defined DEBUG_FR_VIEW
00250   if (G4VisManager::GetVerbosity() >= G4VisManager::errors) {
00251     G4cout << "***** G4DAWNFILEViewer::SendViewParameters()  "; 
00252     G4cout << "(GUI parameters)" << G4endl;
00253   }
00254 #endif 
00255 
00256                 //----- Magic number to decide camera distance automatically
00257         const    G4double        HOW_FAR            = 1000.0       ; // to define "infinity"
00258         const    G4double        MIN_HALF_ANGLE     = 0.01         ;
00259         const    G4double        MAX_HALF_ANGLE     = 0.499 * pi ;
00260 
00261                 //----- CALC camera distance
00262                 //..... Note: Camera cannot enter inside object
00263         G4double  camera_distance ;
00264         G4double  radius = fSceneHandler.GetScene()->GetExtent().GetExtentRadius();
00265 
00266         G4double half_view_angle  = std::fabs ( fVP.GetFieldHalfAngle () ) ;
00267         if( half_view_angle > MAX_HALF_ANGLE ) { 
00268           half_view_angle = MAX_HALF_ANGLE ; 
00269         } 
00270 
00271         if( half_view_angle < MIN_HALF_ANGLE ) {
00272                         //----- infinity (or ortho projection)
00273                 camera_distance = radius * HOW_FAR ;  
00274         } else {
00275                         //----- Calc camera distance from half view angle
00276                 camera_distance = radius / std::sin ( half_view_angle );
00277                 camera_distance -= fVP.GetDolly();
00278         }
00279 
00280         if ( camera_distance < radius ) { 
00281           if (G4VisManager::GetVerbosity() >= G4VisManager::errors) {
00282                 G4cout << "WARNING from DAWNFILE driver:" << G4endl;
00283                 G4cout << "  Camera cannot enter inside objects"      << G4endl;
00284           }
00285                 camera_distance = radius ; 
00286         }
00287 
00288                 //----- CALC camera direction
00289         const G4Vector3D& camera_direction \
00290           = fVP.GetViewpointDirection().unit();
00291         const G4double v_angle =  (180.0 / pi) * camera_direction.theta() ;
00292         const G4double h_angle =  (180.0 / pi) * camera_direction.phi  () ;
00293 
00294 
00295         //########### Generation of the file .DAWN.history for DAWN GUI
00296                 //-----
00297         std::ofstream gui_out (".DAWN_1.history") ; 
00298 
00299         // ######### P1 
00300 
00301                 //----- camera position
00302         gui_out << camera_distance << G4endl;
00303         gui_out << v_angle  << G4endl ;
00304         gui_out << h_angle  << G4endl ;
00305         gui_out << "0"  << G4endl     ; // auto target
00306 
00307                 //----- target point 
00308         const G4Point3D&  target_point
00309           = fSceneHandler.GetScene()->GetStandardTargetPoint()
00310           + fVP.GetCurrentTargetPoint();
00311         gui_out << target_point.x()          << G4endl ;
00312         gui_out << target_point.y()          << G4endl ;
00313         gui_out << target_point.z()          << G4endl ;
00314 
00315                 //----- Magnification
00316         const G4double   zoom_factor  = fVP.GetZoomFactor();
00317         if( half_view_angle < MIN_HALF_ANGLE ) {
00318 
00319                 gui_out << zoom_factor << G4endl;
00320 
00321         } else {
00322                 const G4double FR_HALF_SCREEN_SIZE = 0.5 ;
00323                 G4double  focal_distance \
00324                   = FR_HALF_SCREEN_SIZE / std::tan( half_view_angle ); 
00325                 focal_distance *= zoom_factor ;
00326 
00327                 gui_out << "fd" << focal_distance << G4endl;
00328 
00329         }
00330         SendDrawingStyleToDAWNGUI( gui_out ) ; // gui_out, viewing mode
00331         gui_out << "0.001" << G4endl           ; // 3D Tolerance 
00332         gui_out << "0"     << G4endl           ; // not display parameters
00333 
00334 
00335         // ######### P2
00336         gui_out << 1 << G4endl;   // Source light 
00337         gui_out << 1 << G4endl;   
00338         gui_out << 1 << G4endl;   
00339         gui_out << 0.5 << G4endl; // Ambient light
00340         gui_out << 0.5 << G4endl;
00341         gui_out << 0.5 << G4endl;
00342         gui_out << 19.0 << G4endl; // Light direction (Polar)
00343         gui_out << 71.0 << G4endl; // Light direction (Azimuthal)
00344 
00345         // ######### P3
00346         gui_out << 0.1 << G4endl;    // Real edge width
00347         gui_out << 0.1 << G4endl;    // outline   width
00348         gui_out << 0.1 << G4endl;    // aux edge  width
00349         gui_out << 3   << G4endl;      // aux edge  style
00350         gui_out << 70.0<< G4endl;   // aux-edge threshold angle
00351         gui_out << 0.1 << G4endl;       // line width
00352         gui_out << 0   << G4endl;        // haloing
00353         gui_out << 1   << G4endl;        // Dashed edged for back faces
00354 
00355         //######### P4
00356                //----- drawing device
00357   //    enum {PS=1, XWIN=2, PS2=3, XWIN2=4, OPEN_GL=5, DEVICE_END=6};
00358         if( ( ( getenv( FR_ENV_MULTI_WINDOW ) != NULL        ) && \
00359               ( strcmp( getenv( FR_ENV_MULTI_WINDOW ),"0"  ) )       ) || \
00360             ( ( getenv( FR_ENV_MULTI_WINDOW2 ) != NULL        ) && \
00361               ( strcmp( getenv( FR_ENV_MULTI_WINDOW2 ),"0"  ) )      )     )
00362         {
00363                 gui_out << 2 << G4endl; // OpenWindow
00364         } else {
00365                 gui_out << 1 << G4endl; // Invoke PS viewer
00366         }
00367 
00368         gui_out << GetPSViewer() << G4endl; // PS viewer
00369         gui_out << 1 << G4endl            ; // Do not add showpage 
00370         gui_out << 0 << G4endl            ; // Non-append mode
00371 
00372         gui_out.close();
00373         //########### end of generating file .DAWN.history 
00374 
00375 
00376 } 

Generated on Mon May 27 17:47:59 2013 for Geant4 by  doxygen 1.4.7