2 // ********************************************************************
3 // * License and Disclaimer *
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. *
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. *
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 // ********************************************************************
27 // $Id: G4VRML2SceneHandlerFunc.icc 68043 2013-03-13 14:27:49Z gcosmo $
29 // G4VRML2SceneHandlerFunc.icc
30 // Satoshi Tanaka & Yasuhide Sawada
32 //#define DEBUG_SCENE_FUNC
34 #include "G4VisManager.hh"
38 void G4VRML2SCENEHANDLER::AddSolid(const G4Trd& trd)
40 #if defined DEBUG_SCENE_FUNC
41 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
42 G4cout << "***** AddSolid trd" << "\n" ;
44 VRMLBeginModeling () ;
45 G4VSceneHandler::AddSolid(trd);
48 void G4VRML2SCENEHANDLER::AddSolid(const G4Trap& trap)
50 #if defined DEBUG_SCENE_FUNC
51 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
52 G4cout << "***** AddSolid trap" << "\n" ;
54 VRMLBeginModeling () ;
55 G4VSceneHandler::AddSolid(trap);
58 void G4VRML2SCENEHANDLER::AddSolid(const G4Para& para)
60 #if defined DEBUG_SCENE_FUNC
61 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
62 G4cout << "***** AddSolid para" << "\n" ;
64 VRMLBeginModeling () ;
65 G4VSceneHandler::AddSolid(para);
68 void G4VRML2SCENEHANDLER::AddSolid(const G4Torus& torus )
70 #if defined DEBUG_SCENE_FUNC
71 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
72 G4cout << "***** AddSolid torus" << "\n" ;
74 VRMLBeginModeling () ;
75 G4VSceneHandler::AddSolid(torus);
79 void G4VRML2SCENEHANDLER::AddSolid(const G4VSolid& vsolid)
81 #if defined DEBUG_SCENE_FUNC
82 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
83 G4cout << "***** AddSolid vsolid" << "\n" ;
85 VRMLBeginModeling () ;
86 G4VSceneHandler::AddSolid(vsolid);
89 void G4VRML2SCENEHANDLER::AddSolid(const G4Tubs& tubs)
91 #if defined DEBUG_SCENE_FUNC
92 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
93 G4cout << "***** AddSolid tubs" << "\n" ;
95 VRMLBeginModeling () ;
96 G4VSceneHandler::AddSolid(tubs) ;
101 void G4VRML2SCENEHANDLER::AddSolid(const G4Cons& cons)
103 #if defined DEBUG_SCENE_FUNC
104 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
105 G4cout << "***** AddSolid cons" << "\n" ;
107 VRMLBeginModeling () ;
108 G4VSceneHandler::AddSolid(cons) ;
111 void G4VRML2SCENEHANDLER::AddSolid(const G4Box& box)
113 #if defined DEBUG_SCENE_FUNC
114 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
115 G4cout << "***** AddSolid box" << "\n" ;
117 VRMLBeginModeling () ;
118 G4VSceneHandler::AddSolid(box) ;
122 void G4VRML2SCENEHANDLER::AddSolid(const G4Sphere& sphere)
124 #if defined DEBUG_SCENE_FUNC
125 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
126 G4cout << "***** AddSolid sphere" << "\n" ;
128 VRMLBeginModeling () ;
129 G4VSceneHandler::AddSolid(sphere) ;
132 void G4VRML2SCENEHANDLER::AddPrimitive(const G4Polyline& polyline)
134 #if defined DEBUG_SCENE_FUNC
135 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
136 G4cout << "***** AddPrimitive polyline" << "\n" ;
140 static G4bool warned = false;
144 ("G4VRML2SCENEHANDLER::AddPrimitive (const G4Polyline&)",
145 "VRML-2001", JustWarning,
146 "2D polylines not implemented. Ignored.");
151 VRMLBeginModeling () ;
153 // VRML codes are generated below
155 fDest << "#---------- POLYLINE" << "\n";
156 fDest << "Shape {" << "\n";
158 SendLineColor ( polyline.GetVisAttributes() );
160 fDest << "\t" << "geometry IndexedLineSet {" << "\n";
162 fDest << "\t\t" << "coord Coordinate {" << "\n";
163 fDest << "\t\t\t" << "point [" << "\n";
165 for (i = 0, e = polyline.size(); e; i++, e--) {
167 G4Point3D point = polyline[i];
168 point.transform( fObjectTransformation );
171 fDest << point.x() << " ";
172 fDest << point.y() << " ";
173 fDest << point.z() << "," << "\n";
177 fDest << "\t\t\t" << "]" << "\n"; // point
178 fDest << "\t\t" << "}" << "\n"; // coord
180 fDest << "\t\t" << "coordIndex [";
181 for (i = 0, e = polyline.size(); e; i++, e--) {
183 fDest << "\n" << "\t\t\t";
186 fDest << "-1" << "\n";
187 fDest << "\t\t" << "]" << "\n"; // coordIndex
188 fDest << "\t" << "}" << "\n"; // geometry IndexedLineSet
190 fDest << "}" << "\n"; // Shape
194 void G4VRML2SCENEHANDLER::AddPrimitive(const G4Polyhedron& polyhedron)
196 #if defined DEBUG_SCENE_FUNC
197 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
198 G4cout << "***** AddPrimitive(G4Polyhedron)" << "\n";
201 if (polyhedron.GetNoFacets() == 0) return;
204 static G4bool warned = false;
208 ("G4VRML2SCENEHANDLER::AddPrimitive (const G4Polyhedron&)",
209 "VRML-2002", JustWarning,
210 "2D polyhedra not implemented. Ignored.");
215 VRMLBeginModeling () ;
217 // Transparency checking: If completely transparent, skip drawing
218 if ( GetPVTransparency() > 0.99 ) { return ; }
221 const G4VModel* pv_model = GetModel();
222 G4String pv_name = "No model";
223 if (pv_model) pv_name = pv_model->GetCurrentTag() ;
225 // VRML codes are generated below
227 fDest << "#---------- SOLID: " << pv_name << "\n";
229 if ( IsPVPickable() ) {
231 fDest << "Anchor {" << "\n";
232 fDest << " description " << "\"" << pv_name << "\"" << "\n";
233 fDest << " url \"\" " << "\n";
234 fDest << " children [" << "\n";
237 fDest << "\t"; fDest << "Shape {" << "\n";
241 fDest << "\t\t" << "geometry IndexedFaceSet {" << "\n";
243 fDest << "\t\t\t" << "coord Coordinate {" << "\n";
244 fDest << "\t\t\t\t" << "point [" << "\n";
246 for (i = 1, j = polyhedron.GetNoVertices(); j; j--, i++) {
247 G4Point3D point = polyhedron.GetVertex(i);
249 point.transform( fObjectTransformation );
251 fDest << "\t\t\t\t\t";
252 fDest << point.x() << " ";
253 fDest << point.y() << " ";
254 fDest << point.z() << "," << "\n";
256 fDest << "\t\t\t\t" << "]" << "\n"; // point
257 fDest << "\t\t\t" << "}" << "\n"; // coord
259 fDest << "\t\t\t" << "coordIndex [" << "\n";
263 for (f = polyhedron.GetNoFacets(); f; f--) {
267 G4int index = -1, edgeFlag = 1;
270 notLastEdge = polyhedron.GetNextVertexIndex(index, edgeFlag);
271 fDest << index - 1 << ", ";
272 } while (notLastEdge);
273 fDest << "-1," << "\n";
275 fDest << "\t\t\t" << "]" << "\n"; // coordIndex
277 fDest << "\t\t\t" << "solid FALSE" << "\n"; // draw backfaces
279 fDest << "\t\t" << "}" << "\n"; // IndexFaceSet
280 fDest << "\t" << "}" << "\n"; // Shape
282 if ( IsPVPickable() ) {
283 fDest << " ]" << "\n"; // children
284 fDest << "}" << "\n"; // Anchor
289 void G4VRML2SCENEHANDLER::AddPrimitive( const G4Text& )
291 #if defined DEBUG_SCENE_FUNC
292 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
293 G4cout << "***** AddPrimitive text" << "\n" ;
295 /*** You might need this
297 static G4bool warned = false;
301 ("G4VRML2SCENEHANDLER::AddPrimitive (const G4Text&)",
302 "VRML-2003", JustWarning,
303 "2D text not implemented. Ignored.");
309 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
311 "***** void G4VRML2SCENEHANDLER::AddPrimitive( const G4Text& text )"
312 " not implemented yet."
315 VRMLBeginModeling () ;
319 void G4VRML2SCENEHANDLER::AddPrimitive( const G4Circle& circle )
321 #if defined DEBUG_SCENE_FUNC
322 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
323 G4cout << "***** AddPrimitive circle" << "\n" ;
327 static G4bool warned = false;
331 ("G4VRML2SCENEHANDLER::AddPrimitive (const G4Circle&)",
332 "VRML-2004", JustWarning,
333 "2D circles not implemented. Ignored.");
338 VRMLBeginModeling () ;
341 // const char* info = NULL ;
342 const char* info = circle.GetInfo() ;
344 // Position and half size of circle
345 G4double X, Y, Z ; GetMarkerWorldPosition( circle, &X, &Y, &Z ) ;
346 G4double radius = GetMarkerHalfSize ( circle );
348 // VRML codes are generated below
349 fDest << "#---------- 3D MARKER (Circle)" << "\n";
350 fDest << "Anchor {" << "\n";
352 if( circle.GetInfo().isNull() ) {
353 fDest << " description " << "\"(" << X << " " ;
355 fDest << Z << ")\"" << "\n";
357 fDest << " description " << "\"" << info << "\"" << "\n";
361 fDest << " url \"\" " << "\n";
363 fDest << " children [" << "\n";
365 fDest << " Transform {" << "\n";
367 fDest << " translation ";
368 fDest << X << " " << Y << " " << Z << "\n";
370 fDest << " children [" << "\n";
372 fDest << "\t" << "Shape {" << "\n";
374 SendMarkerColor( circle );
376 // send shape with size
377 fDest << "\t\t" << "geometry Sphere {" << "\n";
378 fDest << "\t\t\t" << "radius " << radius << "\n";
379 fDest << "\t\t" << "}" << "\n";
381 fDest << "\t" << "}" << "\n"; // Shape
383 fDest << " ]" << "\n"; // children
384 fDest << " }" << "\n"; // Transform
386 fDest << " ]" << "\n"; // children
387 fDest << "}" << "\n"; // Anchor
391 void G4VRML2SCENEHANDLER::AddPrimitive(const G4Square& square)
393 #if defined DEBUG_SCENE_FUNC
394 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
395 G4cout << "***** AddPrimitive square" << "\n" ;
399 static G4bool warned = false;
403 ("G4VRML2SCENEHANDLER::AddPrimitive (const G4Square&)",
404 "VRML-2005", JustWarning,
405 "2D squares not implemented. Ignored.");
410 VRMLBeginModeling () ;
413 // const char* info = NULL ;
414 const char* info = square.GetInfo() ;
416 // Position and size of square
417 G4double X, Y, Z ; GetMarkerWorldPosition ( square, &X, &Y, &Z ) ;
418 G4double radius = GetMarkerHalfSize ( square );
419 G4double diameter = 2.0 * radius ;
421 // VRML codes are generated below
422 fDest << "#---------- 3D MARKER (Square)" << "\n";
423 fDest << "Anchor {" << "\n";
425 if( square.GetInfo().isNull() ) {
426 fDest << " description " << "\"(" << X << " " ;
428 fDest << Z << ")\"" << "\n";
430 fDest << " description " << "\"" << info << "\"" << "\n";
433 fDest << " url \"\" " << "\n";
435 fDest << " children [" << "\n";
437 fDest << " Transform {" << "\n";
439 fDest << " translation ";
440 fDest << X << " " << Y << " " << Z << "\n";
442 fDest << " children [" << "\n";
444 fDest << "\t" << "Shape {" << "\n";
446 SendMarkerColor( square );
448 // send shape with size
449 fDest << "\t\t" << "geometry Box {" << "\n";
450 fDest << "\t\t\t" << "size " << diameter << " " ;
451 fDest << diameter << " " ;
452 fDest << diameter << "\n" ;
453 fDest << "\t\t" << "}" << "\n";
455 fDest << "\t" << "}" << "\n"; // Shape
457 fDest << " ]" << "\n"; // children
458 fDest << " }" << "\n"; // Transform
460 fDest << " ]" << "\n"; // children
461 fDest << "}" << "\n"; // Anchor
465 void G4VRML2SCENEHANDLER::BeginModeling()
467 #if defined DEBUG_SCENE_FUNC
468 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
469 G4cout << "***** BeginModeling" << "\n" ;
471 G4VSceneHandler::BeginModeling();
475 void G4VRML2SCENEHANDLER::EndModeling()
477 #if defined DEBUG_SCENE_FUNC
478 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
479 G4cout << "***** EndModeling " << "\n" ;
481 G4VSceneHandler::EndModeling();
484 void G4VRML2SCENEHANDLER::BeginPrimitives(const G4Transform3D& objectTransformation)
486 G4VSceneHandler::BeginPrimitives (objectTransformation);
487 #if defined DEBUG_SCENE_FUNC
488 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
489 G4cout << "***** BeginPrimitives " << "\n" ;
494 void G4VRML2SCENEHANDLER::EndPrimitives()
496 #if defined DEBUG_SCENE_FUNC
497 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
498 G4cout << "***** EndPrimitives " << "\n" ;
500 G4VSceneHandler::EndPrimitives();
503 void G4VRML2SCENEHANDLER::SendMaterialNode( const G4VisAttributes* pVA )
505 // Initialization of drawing parameters
506 G4bool flagWF = false ;
507 G4double TRANSPARENCY = GetPVTransparency() ;
510 const G4ViewParameters& cur_view_param = fpViewer -> GetViewParameters ();
512 if (pVA->IsForceDrawingStyle() &&
513 (pVA->GetForcedDrawingStyle() == G4VisAttributes::wireframe))
515 // wireframe style is assigned to this object
518 } else if ( cur_view_param.GetDrawingStyle() == G4ViewParameters::wireframe )
520 // current viewing style is wireframe
525 // VRML codes are generated below
528 // Error recovery: Use default values of VRML browser
530 fDest << "appearance Appearance { material Material {} }";
535 const G4Color& color = pVA->GetColor();
537 fDest << "\t\t" << "appearance Appearance {" << "\n";
538 fDest << "\t\t\t" << "material Material {" << "\n";
541 fDest << "\t\t\t\t" << "diffuseColor ";
542 fDest << color.GetRed() << " " ;
543 fDest << color.GetGreen() << " " ;
544 fDest << color.GetBlue() << "\n";
547 // fDest << "\t\t\t\t" << "emissiveColor ";
548 // fDest << color.GetRed() << " " ;
549 // fDest << color.GetGreen() << " " ;
550 //fDest << color.GetBlue() << "\n";
553 // wireframe is expressed as transparency
556 fDest << "\t\t\t\t" << "transparency " << TRANSPARENCY << "\n";
559 fDest << "\t\t\t" << "}" << "\n"; // material
560 fDest << "\t\t" << "}" << "\n"; // appearance
566 void G4VRML2SCENEHANDLER::SendMaterialNode()
569 ( fpViewer->GetApplicableVisAttributes (fpVisAttribs) );
573 void G4VRML2SCENEHANDLER::VRMLBeginModeling()
575 if (!IS_CONNECTED ) {
576 #if defined DEBUG_SCENE_FUNC
577 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
578 G4cout << "***** VRMLBeginModeling (started)" << "\n" ;
581 fDest << "#VRML V2.0 utf8" << "\n";
582 fDest << "# Generated by VRML 2.0 driver of GEANT4\n" << "\n";
586 void G4VRML2SCENEHANDLER::VRMLEndModeling()
588 if ( IS_CONNECTED ) {
589 #if defined DEBUG_SCENE_FUNC
590 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
591 G4cout << "***** VRMLEndModeling (started)" << "\n" ;
593 fDest << "#End of file." << "\n";
598 void G4VRML2SCENEHANDLER::SendMarkerColor ( const G4VMarker& mark )
600 const G4Color& color = GetColor( mark );
602 fDest << "\t\t" << "appearance Appearance {" << "\n";
603 fDest << "\t\t\t" << "material Material {" << "\n";
604 fDest << "\t\t\t\t" << "diffuseColor ";
605 fDest << color.GetRed() << " " ;
606 fDest << color.GetGreen() << " " ;
607 fDest << color.GetBlue() << "\n";
608 fDest << "\t\t\t\t" << "emissiveColor ";
609 fDest << color.GetRed() << " " ;
610 fDest << color.GetGreen() << " " ;
611 fDest << color.GetBlue() << "\n";
612 fDest << "\t\t\t" << "}" << "\n"; // material
613 fDest << "\t\t" << "}" << "\n"; // appearance
618 G4VRML2SCENEHANDLER::SendMarkerWorldPosition ( const G4VMarker& mark )
620 G4Point3D point = mark.GetPosition();
622 point.transform( fObjectTransformation );
624 fDest << " translation ";
625 fDest << point.x() << " " << point.y() << " " << point.z() << "\n";
629 void G4VRML2SCENEHANDLER::GetMarkerWorldPosition ( const G4VMarker& mark ,
634 G4Point3D point = mark.GetPosition();
636 point.transform( fObjectTransformation );
643 G4double G4VRML2SCENEHANDLER::GetMarkerHalfSize ( const G4VMarker& mark )
645 //----- return value ( marker radius in 3d units)
646 G4double size = 1.0 ; // initialization
648 //----- parameters to calculate 3d size from 2d size
649 const double HALF_SCREEN_SIZE_2D = 300.0 ; // pixels
650 double zoom_factor = fpViewer->GetViewParameters().GetZoomFactor() ;
651 if ( zoom_factor <= 0.0 ) { zoom_factor = 1.0 ; }
652 double extent_radius_3d = GetScene()->GetExtent().GetExtentRadius() ;
653 if ( extent_radius_3d <= 0.0 ) { extent_radius_3d = 1.0 ; }
655 //----- get marker radius in 3D units
656 if ( mark.GetWorldSize() > 0.0 ) {
658 // get mark radius in 3D units
659 size = 0.5 * mark.GetWorldSize() ;
661 } else if ( mark.GetScreenSize() > 0.0 ) {
664 double mark_radius_2d = 0.5 * mark.GetScreenSize() ;
666 // get mark radius in 3D units
668 = extent_radius_3d * ( mark_radius_2d / HALF_SCREEN_SIZE_2D );
669 size *= zoom_factor ;
673 double mark_radius_2d \
674 = fpViewer->GetViewParameters().GetDefaultMarker().GetScreenSize();
675 mark_radius_2d *= 0.1 ; // Magic number?
677 // get mark radius in 3D units
679 = extent_radius_3d * ( mark_radius_2d / HALF_SCREEN_SIZE_2D );
680 size *= zoom_factor ;
683 //----- global rescaling
684 size *= fpViewer->GetViewParameters().GetGlobalMarkerScale();
691 void G4VRML2SCENEHANDLER::SendLineColor ( const G4VisAttributes* pVA )
693 const G4Color& color = pVA->GetColor();
696 fDest << "\t" << "appearance Appearance {" << "\n";
697 fDest << "\t\t" << "material Material {" << "\n";
698 fDest << "\t\t\t" << "diffuseColor ";
699 fDest << color.GetRed() << " " ;
700 fDest << color.GetGreen() << " " ;
701 fDest << color.GetBlue() << "\n";
702 fDest << "\t\t\t" << "emissiveColor ";
703 fDest << color.GetRed() << " " ;
704 fDest << color.GetGreen() << " " ;
705 fDest << color.GetBlue() << "\n";
706 fDest << "\t\t" << "}" << "\n"; // material
707 fDest << "\t" << "}" << "\n"; // appearance
710 G4double G4VRML2SCENEHANDLER::SetPVTransparency ()
713 G4double TRANSPARENCY = 0.7 ; // initialization
715 if( getenv( "G4VRML_TRANSPARENCY" ) != NULL ) {
717 // get a value from the env value
718 // sscanf( getenv("G4VRML_TRANSPARENCY"), "%lg", &TRANSPARENCY ) ;
719 std::istringstream iss( getenv("G4VRML_TRANSPARENCY") );
723 TRANSPARENCY = std::fabs ( TRANSPARENCY ) ;
724 if ( TRANSPARENCY > 1.0 ) { TRANSPARENCY = 1.0 ; }
727 fPVTransparency = TRANSPARENCY ;
729 return fPVTransparency ;
733 void G4VRML2SCENEHANDLER::ClearTransientStore()
735 // This is typically called after an update and before drawing hits
736 // of the next event. To simulate the clearing of "transients"
737 // (hits, etc.) the detector is redrawn...
739 fpViewer -> SetView ();
740 fpViewer -> ClearView ();
741 fpViewer -> DrawView ();