ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4DAWNFILEViewer.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4DAWNFILEViewer.cc
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 //
28 // Satoshi TANAKA
29 // DAWNFILE view - opens window, hard copy, etc.
30 
31 
32 #define __G_ANSI_C__
33 #define G4DAWNFILE_STRUCTURE_PRIORITY 1.
34 
35 // #define DEBUG_FR_VIEW
36 
37 #include "G4ios.hh"
38 #include <stdio.h>
39 #include <string.h>
40 #include <assert.h>
41 
42 #include "G4VisManager.hh"
43 #include "G4Scene.hh"
44 #include "G4Vector3D.hh"
45 #include "G4VisExtent.hh"
46 #include "G4LogicalVolume.hh"
47 #include "G4VSolid.hh"
48 #include "G4PhysicalConstants.hh"
49 
50 #include "G4FRConst.hh"
51 #include "G4DAWNFILE.hh"
53 #include "G4DAWNFILEViewer.hh"
54 
55 
56 
57  //----- constants
58 const char FR_ENV_MULTI_WINDOW [] = "G4DAWN_MULTI_WINDOW" ;
59 const char FR_ENV_MULTI_WINDOW2[] = "G4DAWNFILE_MULTI_WINDOW" ;
60 
61  //----- G4DAWNFILEViewer, constructor
63  const G4String& name):
64  G4VViewer (sceneHandler,
65  sceneHandler.IncrementViewCount (),
66  name),
67  fSceneHandler (sceneHandler)
68 {
69  // Set a g4.prim-file viewer
70  strcpy( fG4PrimViewer, "dawn" );
71  if( std::getenv( "G4DAWNFILE_VIEWER" ) != NULL ) {
72  strcpy( fG4PrimViewer, std::getenv( "G4DAWNFILE_VIEWER" ) ) ;
73  }
74 
75  // string for viewer invocation
76  if ( !strcmp( fG4PrimViewer, "NONE" ) ) {
77 
78  strcpy( fG4PrimViewerInvocation, "" );
79  } else {
80 
82  strcat( fG4PrimViewerInvocation, " ");
84  }
85 
86  // Set a PostScript Viewer
87 // strcpy( fPSViewer, "ghostview" );
88  strcpy( fPSViewer, "gv" );
89  if( std::getenv( "G4DAWNFILE_PS_VIEWER" ) != NULL ) {
90  strcpy( fPSViewer, std::getenv( "G4DAWNFILE_PS_VIEWER" ) ) ;
91  }
92 
93 }
94 
95  //----- G4DAWNFILEViewer, destructor
97 {}
98 
99  //----- G4DAWNFILEViewer::SetView ()
101 {
102 #if defined DEBUG_FR_VIEW
104  G4cout << "***** G4DAWNFILEViewer::SetView(): No effects" << G4endl;
105 #endif
106 // Do nothing, since DAWN is running as a different process.
107 // SendViewParameters () will do this job instead.
108 }
109 
110 
111  //----- G4DAWNFILEViewer::ClearView()
112 void
114 {
115 #if defined DEBUG_FR_VIEW
117  G4cout << "***** G4DAWNFILEViewer::ClearView (): No effects " << G4endl;
118 #endif
121  // Re-open with same filename...
126  }
127 }
128 
129 
130  //----- G4DAWNFILEViewer::DrawView ()
132 {
133 #if defined DEBUG_FR_VIEW
135  G4cout << "***** G4DAWNFILEViewer::DrawView () " << G4endl;
136 #endif
137  //-----
139 
140  //----- Always visit G4 kernel
141  NeedKernelVisit ();
142 
143  //----- Draw
144  ProcessView () ;
145 
146 } // G4DAWNFILEViewer::DrawView ()
147 
148 
149 
150  //----- G4DAWNFILEViewer::ShowView()
152 {
153 #if defined DEBUG_FR_VIEW
155  G4cout << "***** G4DAWNFILEViewer::ShowView () " << G4endl;
156 #endif
157 
159  {
160  //----- End of modeling
161  // !EndModeling, !DrawAll, !CloseDevice,
162  // close g4.prim
164 
165  //----- Output DAWN GUI file
167 
168  //----- string for viewer invocation
169  if ( !strcmp( fG4PrimViewer, "NONE" ) ) {
170 
171  strcpy( fG4PrimViewerInvocation, "" );
172  } else {
173 
175  strcat( fG4PrimViewerInvocation, " ");
177  }
178 
179 
180  //----- Invoke DAWN
181  G4cout << G4endl ;
183  {
184  G4cout << "ERROR: Failed to generate file ";
186 
187  } else if( strcmp( GetG4PrimViewerInvocation(), "" ) )
188  {
189  G4cout << "File " << fSceneHandler.GetG4PrimFileName() ;
190  G4cout << " is generated." << G4endl;
193 
194  } else { // no view, i.e., only file generation
195  G4cout << "File " << fSceneHandler.GetG4PrimFileName() ;
196  G4cout << " is generated." << G4endl;
197  G4cout << "No viewer is invoked." << G4endl;
198  }
199 
200  }
201 
202 } // G4DAWNFILEViewer::ShowView()
203 
204 
205  //----- G4DAWNFILEViewer::SendDrawingStyleToDAWNGUI( std::ostream& out )
207 {
209 //#if defined DEBUG_FR_VIEW
210 // G4cout << "***** G4DAWNFILEViewer::SendDrawingStyleToDAWNGUI()" << G4endl;
211 //#endif
213 
214  G4int style = fVP.GetDrawingStyle();
215 
216  enum { FR_WIREFRAME_STYLE = 1, FR_WF_STORED_STYLE = 2,
217  FR_HID_STYLE = 3, FR_HID2_STYLE = 4,
218  FR_HID3_STYLE = 5, FR_DRAWING_MODE_END_STYLE = 6 };
219 
220  switch( style )
221  {
223  out << FR_WIREFRAME_STYLE << G4endl;
224  break;
226  out << FR_HID2_STYLE << G4endl; // LINE
227  break;
230  out << FR_HID_STYLE << G4endl; // SURFACE
231  break;
232  default:
233  out << FR_WIREFRAME_STYLE << G4endl;
234  break;
235  }
236 
237 } // G4DAWNFILEViewer::SendDrawingStyle()
238 
239 
240 
241 //-----
243 {
244  // Calculates view representation based on extent of object being
245  // viewed and (initial) direction of camera. (Note: it can change
246  // later due to user interaction via visualization system's GUI.)
247 
248 #if defined DEBUG_FR_VIEW
250  G4cout << "***** G4DAWNFILEViewer::SendViewParameters() ";
251  G4cout << "(GUI parameters)" << G4endl;
252  }
253 #endif
254 
255  //----- Magic number to decide camera distance automatically
256  const G4double HOW_FAR = 1000.0 ; // to define "infinity"
257  const G4double MIN_HALF_ANGLE = 0.01 ;
258  const G4double MAX_HALF_ANGLE = 0.499 * pi ;
259 
260  //----- CALC camera distance
261  //..... Note: Camera cannot enter inside object
262  G4double camera_distance ;
264 
265  G4double half_view_angle = std::fabs ( fVP.GetFieldHalfAngle () ) ;
266  if( half_view_angle > MAX_HALF_ANGLE ) {
267  half_view_angle = MAX_HALF_ANGLE ;
268  }
269 
270  if( half_view_angle < MIN_HALF_ANGLE ) {
271  //----- infinity (or ortho projection)
272  camera_distance = radius * HOW_FAR ;
273  } else {
274  //----- Calc camera distance from half view angle
275  camera_distance = radius / std::sin ( half_view_angle );
276  camera_distance -= fVP.GetDolly();
277  }
278 
279  if ( camera_distance < radius ) {
281  G4cout << "WARNING from DAWNFILE driver:" << G4endl;
282  G4cout << " Camera cannot enter inside objects" << G4endl;
283  }
284  camera_distance = radius ;
285  }
286 
287  //----- CALC camera direction
288  const G4Vector3D& camera_direction \
290  const G4double v_angle = (180.0 / pi) * camera_direction.theta() ;
291  const G4double h_angle = (180.0 / pi) * camera_direction.phi () ;
292 
293 
294  //########### Generation of the file .DAWN.history for DAWN GUI
295  //-----
296  std::ofstream gui_out (".DAWN_1.history") ;
297 
298  // ######### P1
299 
300  //----- camera position
301  gui_out << camera_distance << G4endl;
302  gui_out << v_angle << G4endl ;
303  gui_out << h_angle << G4endl ;
304  gui_out << "0" << G4endl ; // auto target
305 
306  //----- target point
307  const G4Point3D& target_point
310  gui_out << target_point.x() << G4endl ;
311  gui_out << target_point.y() << G4endl ;
312  gui_out << target_point.z() << G4endl ;
313 
314  //----- Magnification
315  const G4double zoom_factor = fVP.GetZoomFactor();
316  if( half_view_angle < MIN_HALF_ANGLE ) {
317 
318  gui_out << zoom_factor << G4endl;
319 
320  } else {
321  const G4double FR_HALF_SCREEN_SIZE = 0.5 ;
322  G4double focal_distance \
323  = FR_HALF_SCREEN_SIZE / std::tan( half_view_angle );
324  focal_distance *= zoom_factor ;
325 
326  gui_out << "fd" << focal_distance << G4endl;
327 
328  }
329  SendDrawingStyleToDAWNGUI( gui_out ) ; // gui_out, viewing mode
330  gui_out << "0.001" << G4endl ; // 3D Tolerance
331  gui_out << "0" << G4endl ; // not display parameters
332 
333 
334  // ######### P2
335  gui_out << 1 << G4endl; // Source light
336  gui_out << 1 << G4endl;
337  gui_out << 1 << G4endl;
338  gui_out << 0.5 << G4endl; // Ambient light
339  gui_out << 0.5 << G4endl;
340  gui_out << 0.5 << G4endl;
341  gui_out << 19.0 << G4endl; // Light direction (Polar)
342  gui_out << 71.0 << G4endl; // Light direction (Azimuthal)
343 
344  // ######### P3
345  gui_out << 0.1 << G4endl; // Real edge width
346  gui_out << 0.1 << G4endl; // outline width
347  gui_out << 0.1 << G4endl; // aux edge width
348  gui_out << 3 << G4endl; // aux edge style
349  gui_out << 70.0<< G4endl; // aux-edge threshold angle
350  gui_out << 0.1 << G4endl; // line width
351  gui_out << 0 << G4endl; // haloing
352  gui_out << 1 << G4endl; // Dashed edged for back faces
353 
354  //######### P4
355  //----- drawing device
356  // enum {PS=1, XWIN=2, PS2=3, XWIN2=4, OPEN_GL=5, DEVICE_END=6};
357  if( ( ( std::getenv( FR_ENV_MULTI_WINDOW ) != NULL ) && \
358  ( strcmp( std::getenv( FR_ENV_MULTI_WINDOW ),"0" ) ) ) || \
359  ( ( std::getenv( FR_ENV_MULTI_WINDOW2 ) != NULL ) && \
360  ( strcmp( std::getenv( FR_ENV_MULTI_WINDOW2 ),"0" ) ) ) )
361  {
362  gui_out << 2 << G4endl; // OpenWindow
363  } else {
364  gui_out << 1 << G4endl; // Invoke PS viewer
365  }
366 
367  gui_out << GetPSViewer() << G4endl; // PS viewer
368  gui_out << 1 << G4endl ; // Do not add showpage
369  gui_out << 0 << G4endl ; // Non-append mode
370 
371  gui_out.close();
372  //########### end of generating file .DAWN.history
373 
374 
375 }