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// ********************************************************************
28// G4VRML2SceneHandlerFunc.icc
29// Satoshi Tanaka & Yasuhide Sawada
31//#define DEBUG_SCENE_FUNC
33#include "G4VisManager.hh"
37void G4VRML2SCENEHANDLER::AddSolid(const G4Trd& trd)
39#if defined DEBUG_SCENE_FUNC
40 if(G4VisManager::GetVerbosity() >= G4VisManager::errors)
41 G4cout << "***** AddSolid trd"
45 G4VSceneHandler::AddSolid(trd);
48void G4VRML2SCENEHANDLER::AddSolid(const G4Trap& trap)
50#if defined DEBUG_SCENE_FUNC
51 if(G4VisManager::GetVerbosity() >= G4VisManager::errors)
52 G4cout << "***** AddSolid trap"
56 G4VSceneHandler::AddSolid(trap);
59void G4VRML2SCENEHANDLER::AddSolid(const G4Para& para)
61#if defined DEBUG_SCENE_FUNC
62 if(G4VisManager::GetVerbosity() >= G4VisManager::errors)
63 G4cout << "***** AddSolid para"
67 G4VSceneHandler::AddSolid(para);
70void G4VRML2SCENEHANDLER::AddSolid(const G4Torus& torus)
72#if defined DEBUG_SCENE_FUNC
73 if(G4VisManager::GetVerbosity() >= G4VisManager::errors)
74 G4cout << "***** AddSolid torus"
78 G4VSceneHandler::AddSolid(torus);
81void G4VRML2SCENEHANDLER::AddSolid(const G4VSolid& vsolid)
83#if defined DEBUG_SCENE_FUNC
84 if(G4VisManager::GetVerbosity() >= G4VisManager::errors)
85 G4cout << "***** AddSolid vsolid"
89 G4VSceneHandler::AddSolid(vsolid);
92void G4VRML2SCENEHANDLER::AddSolid(const G4Tubs& tubs)
94#if defined DEBUG_SCENE_FUNC
95 if(G4VisManager::GetVerbosity() >= G4VisManager::errors)
96 G4cout << "***** AddSolid tubs"
100 G4VSceneHandler::AddSolid(tubs);
103void G4VRML2SCENEHANDLER::AddSolid(const G4Cons& cons)
105#if defined DEBUG_SCENE_FUNC
106 if(G4VisManager::GetVerbosity() >= G4VisManager::errors)
107 G4cout << "***** AddSolid cons"
111 G4VSceneHandler::AddSolid(cons);
114void G4VRML2SCENEHANDLER::AddSolid(const G4Box& box)
116#if defined DEBUG_SCENE_FUNC
117 if(G4VisManager::GetVerbosity() >= G4VisManager::errors)
118 G4cout << "***** AddSolid box"
122 G4VSceneHandler::AddSolid(box);
125void G4VRML2SCENEHANDLER::AddSolid(const G4Sphere& sphere)
127#if defined DEBUG_SCENE_FUNC
128 if(G4VisManager::GetVerbosity() >= G4VisManager::errors)
129 G4cout << "***** AddSolid sphere"
133 G4VSceneHandler::AddSolid(sphere);
136void G4VRML2SCENEHANDLER::AddPrimitive(const G4Polyline& polyline)
138#if defined DEBUG_SCENE_FUNC
139 if(G4VisManager::GetVerbosity() >= G4VisManager::errors)
140 G4cout << "***** AddPrimitive polyline"
146 static G4bool warned = false;
150 G4Exception("G4VRML2SCENEHANDLER::AddPrimitive (const G4Polyline&)",
151 "VRML-2001", JustWarning,
152 "2D polylines not implemented. Ignored.");
159 // VRML codes are generated below
161 fDest << "#---------- POLYLINE"
166 SendLineColor(polyline.GetVisAttributes());
169 << "geometry IndexedLineSet {"
173 << "coord Coordinate {"
179 for(i = 0, e = polyline.size(); e; i++, e--)
181 G4Point3D point = polyline[i];
182 point.transform(fObjectTransformation);
185 fDest << point.x() << " ";
186 fDest << point.y() << " ";
187 fDest << point.z() << ","
201 for(i = 0, e = polyline.size(); e; i++, e--)
212 << "\n"; // coordIndex
215 << "\n"; // geometry IndexedLineSet
221void G4VRML2SCENEHANDLER::AddPrimitive(const G4Polyhedron& polyhedron)
223#if defined DEBUG_SCENE_FUNC
224 if(G4VisManager::GetVerbosity() >= G4VisManager::errors)
225 G4cout << "***** AddPrimitive(G4Polyhedron)"
229 if(polyhedron.GetNoFacets() == 0)
234 static G4bool warned = false;
238 G4Exception("G4VRML2SCENEHANDLER::AddPrimitive (const G4Polyhedron&)",
239 "VRML-2002", JustWarning,
240 "2D polyhedra not implemented. Ignored.");
247 // Transparency checking: If completely transparent, skip drawing
248 if(GetPVTransparency() > 0.99)
254 const G4VModel* pv_model = GetModel();
255 G4String pv_name = "No model";
257 pv_name = pv_model->GetCurrentTag();
259 // VRML codes are generated below
261 fDest << "#---------- SOLID: " << pv_name << "\n";
267 fDest << " description "
268 << "\"" << pv_name << "\""
270 fDest << " url \"\" "
272 fDest << " children ["
283 << "geometry IndexedFaceSet {"
287 << "coord Coordinate {"
293 for(i = 1, j = polyhedron.GetNoVertices(); j; j--, i++)
295 G4Point3D point = polyhedron.GetVertex(i);
297 point.transform(fObjectTransformation);
299 fDest << "\t\t\t\t\t";
300 fDest << point.x() << " ";
301 fDest << point.y() << " ";
302 fDest << point.z() << ","
318 for(f = polyhedron.GetNoFacets(); f; f--)
322 G4int index = -1, edgeFlag = 1;
326 notLastEdge = polyhedron.GetNextVertexIndex(index, edgeFlag);
327 fDest << index - 1 << ", ";
328 } while(notLastEdge);
334 << "\n"; // coordIndex
338 << "\n"; // draw backfaces
342 << "\n"; // IndexFaceSet
356void G4VRML2SCENEHANDLER::AddPrimitive(const G4Text&)
358#if defined DEBUG_SCENE_FUNC
359 if(G4VisManager::GetVerbosity() >= G4VisManager::errors)
360 G4cout << "***** AddPrimitive text"
363 /*** You might need this
365 static G4bool warned = false;
369 ("G4VRML2SCENEHANDLER::AddPrimitive (const G4Text&)",
370 "VRML-2003", JustWarning,
371 "2D text not implemented. Ignored.");
377 if(G4VisManager::GetVerbosity() >= G4VisManager::errors)
379 << "***** void G4VRML2SCENEHANDLER::AddPrimitive( const G4Text& text )"
380 " not implemented yet."
386void G4VRML2SCENEHANDLER::AddPrimitive(const G4Circle& circle)
388#if defined DEBUG_SCENE_FUNC
389 if(G4VisManager::GetVerbosity() >= G4VisManager::errors)
390 G4cout << "***** AddPrimitive circle"
396 static G4bool warned = false;
400 G4Exception("G4VRML2SCENEHANDLER::AddPrimitive (const G4Circle&)",
401 "VRML-2004", JustWarning,
402 "2D circles not implemented. Ignored.");
410 // const char* info = NULL ;
411 const char* info = circle.GetInfo();
413 // Position and half size of circle
415 GetMarkerWorldPosition(circle, &X, &Y, &Z);
416 G4double radius = GetMarkerHalfSize(circle);
418 // VRML codes are generated below
419 fDest << "#---------- 3D MARKER (Circle)"
424 if(circle.GetInfo().empty())
426 fDest << " description "
427 << "\"(" << X << " ";
434 fDest << " description "
435 << "\"" << info << "\""
439 fDest << " url \"\" "
442 fDest << " children ["
445 fDest << " Transform {"
448 fDest << " translation ";
449 fDest << X << " " << Y << " " << Z << "\n";
451 fDest << " children ["
458 SendMarkerColor(circle);
460 // send shape with size
462 << "geometry Sphere {"
465 << "radius " << radius << "\n";
477 << "\n"; // Transform
485void G4VRML2SCENEHANDLER::AddPrimitive(const G4Square& square)
487#if defined DEBUG_SCENE_FUNC
488 if(G4VisManager::GetVerbosity() >= G4VisManager::errors)
489 G4cout << "***** AddPrimitive square"
495 static G4bool warned = false;
499 G4Exception("G4VRML2SCENEHANDLER::AddPrimitive (const G4Square&)",
500 "VRML-2005", JustWarning,
501 "2D squares not implemented. Ignored.");
509 // const char* info = NULL ;
510 const char* info = square.GetInfo();
512 // Position and size of square
514 GetMarkerWorldPosition(square, &X, &Y, &Z);
515 G4double radius = GetMarkerHalfSize(square);
516 G4double diameter = 2.0 * radius;
518 // VRML codes are generated below
519 fDest << "#---------- 3D MARKER (Square)"
524 if(square.GetInfo().empty())
526 fDest << " description "
527 << "\"(" << X << " ";
534 fDest << " description "
535 << "\"" << info << "\""
539 fDest << " url \"\" "
542 fDest << " children ["
545 fDest << " Transform {"
548 fDest << " translation ";
549 fDest << X << " " << Y << " " << Z << "\n";
551 fDest << " children ["
558 SendMarkerColor(square);
560 // send shape with size
565 << "size " << diameter << " ";
566 fDest << diameter << " ";
567 fDest << diameter << "\n";
579 << "\n"; // Transform
587void G4VRML2SCENEHANDLER::BeginModeling()
589#if defined DEBUG_SCENE_FUNC
590 if(G4VisManager::GetVerbosity() >= G4VisManager::errors)
591 G4cout << "***** BeginModeling"
594 G4VSceneHandler::BeginModeling();
597void G4VRML2SCENEHANDLER::EndModeling()
599#if defined DEBUG_SCENE_FUNC
600 if(G4VisManager::GetVerbosity() >= G4VisManager::errors)
601 G4cout << "***** EndModeling "
604 G4VSceneHandler::EndModeling();
607void G4VRML2SCENEHANDLER::BeginPrimitives(
608 const G4Transform3D& objectTransformation)
610 G4VSceneHandler::BeginPrimitives(objectTransformation);
611#if defined DEBUG_SCENE_FUNC
612 if(G4VisManager::GetVerbosity() >= G4VisManager::errors)
613 G4cout << "***** BeginPrimitives "
619void G4VRML2SCENEHANDLER::EndPrimitives()
621#if defined DEBUG_SCENE_FUNC
622 if(G4VisManager::GetVerbosity() >= G4VisManager::errors)
623 G4cout << "***** EndPrimitives "
626 G4VSceneHandler::EndPrimitives();
629void G4VRML2SCENEHANDLER::SendMaterialNode(const G4VisAttributes* pVA)
631 // Initialization of drawing parameters
632 G4bool flagWF = false;
633 G4double TRANSPARENCY = GetPVTransparency();
636 const G4ViewParameters& cur_view_param = fpViewer->GetViewParameters();
639 if(pVA->IsForceDrawingStyle() &&
640 (pVA->GetForcedDrawingStyle() == G4VisAttributes::wireframe))
642 // wireframe style is assigned to this object
645 else if(cur_view_param.GetDrawingStyle() == G4ViewParameters::wireframe)
647 // current viewing style is wireframe
652 // VRML codes are generated below
655 // Error recovery: Use default values of VRML browser
657 fDest << "appearance Appearance { material Material {} }";
662 const G4Color& color = pVA->GetColor();
665 << "appearance Appearance {"
668 << "material Material {"
674 fDest << color.GetRed() << " ";
675 fDest << color.GetGreen() << " ";
676 fDest << color.GetBlue() << "\n";
679 // fDest << "\t\t\t\t" << "emissiveColor ";
680 // fDest << color.GetRed() << " " ;
681 // fDest << color.GetGreen() << " " ;
682 // fDest << color.GetBlue() << "\n";
685 // wireframe is expressed as transparency
689 << "transparency " << TRANSPARENCY << "\n";
697 << "\n"; // appearance
701void G4VRML2SCENEHANDLER::SendMaterialNode()
703 SendMaterialNode(fpViewer->GetApplicableVisAttributes(fpVisAttribs));
706void G4VRML2SCENEHANDLER::VRMLBeginModeling()
710#if defined DEBUG_SCENE_FUNC
711 if(G4VisManager::GetVerbosity() >= G4VisManager::errors)
712 G4cout << "***** VRMLBeginModeling (started)"
716 fDest << "#VRML V2.0 utf8"
718 fDest << "# Generated by VRML 2.0 driver of GEANT4\n"
723void G4VRML2SCENEHANDLER::VRMLEndModeling()
727#if defined DEBUG_SCENE_FUNC
728 if(G4VisManager::GetVerbosity() >= G4VisManager::errors)
729 G4cout << "***** VRMLEndModeling (started)"
732 fDest << "#End of file."
738void G4VRML2SCENEHANDLER::SendMarkerColor(const G4VMarker& marker)
740 fpVisAttribs = marker.GetVisAttributes();
741 const G4Color& color = GetColor();
744 << "appearance Appearance {"
747 << "material Material {"
751 fDest << color.GetRed() << " ";
752 fDest << color.GetGreen() << " ";
753 fDest << color.GetBlue() << "\n";
756 fDest << color.GetRed() << " ";
757 fDest << color.GetGreen() << " ";
758 fDest << color.GetBlue() << "\n";
764 << "\n"; // appearance
767void G4VRML2SCENEHANDLER::SendMarkerWorldPosition(const G4VMarker& mark)
769 G4Point3D point = mark.GetPosition();
771 point.transform(fObjectTransformation);
773 fDest << " translation ";
774 fDest << point.x() << " " << point.y() << " " << point.z() << "\n";
777void G4VRML2SCENEHANDLER::GetMarkerWorldPosition(const G4VMarker& mark,
778 double* pX, double* pY,
781 G4Point3D point = mark.GetPosition();
783 point.transform(fObjectTransformation);
790G4double G4VRML2SCENEHANDLER::GetMarkerHalfSize(const G4VMarker& mark)
792 //----- return value ( marker radius in 3d units)
793 G4double size = 1.0; // initialization
795 //----- parameters to calculate 3d size from 2d size
796 const double HALF_SCREEN_SIZE_2D = 300.0; // pixels
797 double zoom_factor = fpViewer->GetViewParameters().GetZoomFactor();
798 if(zoom_factor <= 0.0)
802 double extent_radius_3d = GetScene()->GetExtent().GetExtentRadius();
803 if(extent_radius_3d <= 0.0)
805 extent_radius_3d = 1.0;
808 //----- get marker radius in 3D units
809 if(mark.GetWorldSize() > 0.0)
811 // get mark radius in 3D units
812 size = 0.5 * mark.GetWorldSize();
814 else if(mark.GetScreenSize() > 0.0)
817 double mark_radius_2d = 0.5 * mark.GetScreenSize();
819 // get mark radius in 3D units
820 size = extent_radius_3d * (mark_radius_2d / HALF_SCREEN_SIZE_2D);
826 double mark_radius_2d =
827 fpViewer->GetViewParameters().GetDefaultMarker().GetScreenSize();
828 mark_radius_2d *= 0.1; // Magic number?
830 // get mark radius in 3D units
831 size = extent_radius_3d * (mark_radius_2d / HALF_SCREEN_SIZE_2D);
835 //----- global rescaling
836 size *= fpViewer->GetViewParameters().GetGlobalMarkerScale();
842void G4VRML2SCENEHANDLER::SendLineColor(const G4VisAttributes* pVA)
844 const G4Color& color = pVA->GetColor();
847 << "appearance Appearance {"
850 << "material Material {"
854 fDest << color.GetRed() << " ";
855 fDest << color.GetGreen() << " ";
856 fDest << color.GetBlue() << "\n";
859 fDest << color.GetRed() << " ";
860 fDest << color.GetGreen() << " ";
861 fDest << color.GetBlue() << "\n";
867 << "\n"; // appearance
870G4double G4VRML2SCENEHANDLER::SetPVTransparency()
872 G4double TRANSPARENCY = 0.7; // initialization
874 if(std::getenv("G4VRML_TRANSPARENCY") != NULL)
876 // get a value from the env value
877 // sscanf( getenv("G4VRML_TRANSPARENCY"), "%lg", &TRANSPARENCY ) ;
878 std::istringstream iss(std::getenv("G4VRML_TRANSPARENCY"));
882 TRANSPARENCY = std::fabs(TRANSPARENCY);
883 if(TRANSPARENCY > 1.0)
889 fPVTransparency = TRANSPARENCY;
891 return fPVTransparency;
894void G4VRML2SCENEHANDLER::ClearTransientStore()
896 // This is typically called after an update and before drawing hits
897 // of the next event. To simulate the clearing of "transients"
898 // (hits, etc.) the detector is redrawn...
902 fpViewer->ClearView();
903 fpViewer->DrawView();