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: G4VRML1SceneHandlerFunc.icc 76287 2013-11-08 13:03:49Z gcosmo $
29 // G4VRML1SceneHandlerFunc.icc
30 // Satoshi Tanaka & Yasuhide Sawada
32 #include <CLHEP/Units/SystemOfUnits.h>
34 #include "G4VisManager.hh"
36 //#define DEBUG_SCENE_FUNC
39 #define ADDTHIS_WITH_NAME(Solid) \
40 fCurrentDEF = #Solid "_" + Solid.GetName(); \
41 RequestPrimitives(Solid); \
46 void G4VRML1SCENEHANDLER::AddSolid(const G4Trd& trd)
48 #if defined DEBUG_SCENE_FUNC
49 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
50 G4cout << "***** AddSolid trd" << "\n" ;
52 VRMLBeginModeling () ;
53 ADDTHIS_WITH_NAME(trd)
56 void G4VRML1SCENEHANDLER::AddSolid(const G4Trap& trap)
58 #if defined DEBUG_SCENE_FUNC
59 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
60 G4cout << "***** AddSolid trap" << "\n" ;
62 VRMLBeginModeling () ;
63 ADDTHIS_WITH_NAME(trap)
66 void G4VRML1SCENEHANDLER::AddSolid(const G4Para& para)
68 #if defined DEBUG_SCENE_FUNC
69 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
70 G4cout << "***** AddSolid para" << "\n" ;
72 VRMLBeginModeling () ;
73 ADDTHIS_WITH_NAME(para)
76 void G4VRML1SCENEHANDLER::AddSolid(const G4Torus& torus )
78 #if defined DEBUG_SCENE_FUNC
79 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
80 G4cout << "***** AddSolid torus" << "\n" ;
82 VRMLBeginModeling () ;
83 ADDTHIS_WITH_NAME(torus)
87 void G4VRML1SCENEHANDLER::AddSolid(const G4VSolid& vsolid)
89 #if defined DEBUG_SCENE_FUNC
90 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
91 G4cout << "***** AddSolid vsolid" << "\n" ;
93 VRMLBeginModeling () ;
94 ADDTHIS_WITH_NAME(vsolid)
97 void G4VRML1SCENEHANDLER::AddSolid(const G4Tubs& tubs)
99 #if defined DEBUG_SCENE_FUNC
100 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
101 G4cout << "***** AddSolid tubs" << "\n" ;
103 VRMLBeginModeling () ;
106 fCurrentDEF = "tubs_" + tubs.GetName();
109 const G4double R = tubs.GetOuterRadius(); // outside radius
110 const G4double r = tubs.GetInnerRadius(); // inside radius
111 const G4double dz = tubs.GetZHalfLength(); // half length in z
112 const G4double dp = tubs.GetDeltaPhiAngle(); // angle interval
115 if ( r == 0.0 && dp >= 360. * CLHEP::deg ) {
116 // Send a built-in VRML node (Cylinder)
117 fDest << "Separator {" << "\n";
118 SendMatrixTransformNode( fObjectTransformation );
120 fDest << "\t" << "DEF " << fCurrentDEF << " Separator {" << "\n";
122 SendCylinderNode(R, dz * 2);
123 fDest << "\t" << "}" << "\n"; // DEF Separator
124 fDest << "}" << "\n"; // Separator
126 // call AddPrimitive(Polyhedron)
127 RequestPrimitives(tubs);
130 // reset current name to null
135 void G4VRML1SCENEHANDLER::AddSolid(const G4Cons& cons)
137 #if defined DEBUG_SCENE_FUNC
138 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
139 G4cout << "***** AddSolid cons" << "\n" ;
141 VRMLBeginModeling () ;
144 fCurrentDEF = "cons_" + cons.GetName();
147 const G4double r1 = cons.GetInnerRadiusMinusZ(); // inside radius at -dz
148 const G4double R1 = cons.GetOuterRadiusMinusZ(); // outside radius at -dz
149 const G4double r2 = cons.GetInnerRadiusPlusZ(); // inside radius at +dz
150 const G4double R2 = cons.GetOuterRadiusPlusZ(); // outside radius at +dz
151 const G4double dz = cons.GetZHalfLength(); // half length in z
152 //const G4double sp = cons.GetStartPhiAngle(); // starting angle
153 const G4double dp = cons.GetDeltaPhiAngle(); // angle width
156 if ( r1 == 0.0 && r2 == 0.0 && R1 == R2 && dp >= 360. * CLHEP::deg) {
157 // Send a built-in VRML node (Cylinder)
158 fDest << "Separator {" << "\n";
159 SendMatrixTransformNode( fObjectTransformation );
161 fDest << "\t" << "DEF " << fCurrentDEF << " Separator {" << "\n";
163 SendCylinderNode(R1, dz * 2);
164 fDest << "\t" << "}" << "\n"; // DEF Separator
165 fDest << "}" << "\n"; // Separator
167 // call AddPrimitive(Polyhedron)
168 RequestPrimitives(cons);
171 // reset current name to null
175 void G4VRML1SCENEHANDLER::AddSolid(const G4Box& box)
177 #if defined DEBUG_SCENE_FUNC
178 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
179 G4cout << "***** AddSolid box" << "\n" ;
181 VRMLBeginModeling () ;
184 fCurrentDEF = "box_" + box.GetName();
186 // Send a built-in VRML node (Cube)
187 fDest << "Separator {" << "\n";
189 //fDest << "\t" << "renderCulling ON" << "\n";
190 SendMatrixTransformNode( fObjectTransformation );
192 fDest << "\t" << "DEF " << fCurrentDEF << " Separator {" << "\n";
194 SendCubeNode(box.GetXHalfLength() * 2, box.GetYHalfLength() * 2, box.GetZHalfLength() * 2);
195 fDest << "\t" << "}" << "\n"; // DEF Separator
197 fDest << "}" << "\n"; // Separator
199 // reset current name to null
204 void G4VRML1SCENEHANDLER::AddSolid(const G4Sphere& sphere)
206 #if defined DEBUG_SCENE_FUNC
207 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
208 G4cout << "***** AddSolid sphere" << "\n" ;
210 VRMLBeginModeling () ;
213 fCurrentDEF = "sphere_" + sphere.GetName();
216 G4double dphi = sphere.GetDeltaPhiAngle () ;
217 G4double dtheta = sphere.GetDeltaThetaAngle() ;
218 G4double rmax = sphere.GetOuterRadius () ;
219 G4double rmin = sphere.GetInnerRadius () ;
222 if ( (dphi >= 360. * CLHEP::deg) && (dtheta >= 180. * CLHEP::deg) && (rmin == 0.0) ) {
223 // Send a built-in VRML node (Sphere)
224 fDest << "Separator {" << "\n";
225 SendMatrixTransformNode( fObjectTransformation );
227 fDest << "\t" << "DEF " << fCurrentDEF << " Separator {" << "\n";
229 SendSphereNode( rmax );
230 fDest << "\t" << "}" << "\n"; // DEF Separator
231 fDest << "}" << "\n"; // Separator
233 // call AddPrimitive(Polyhedron)
234 RequestPrimitives( sphere );
237 // reset current name to null
241 void G4VRML1SCENEHANDLER::AddPrimitive(const G4Polyline& polyline)
243 #if defined DEBUG_SCENE_FUNC
244 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
245 G4cout << "***** AddPrimitive polyline" << "\n" ;
248 static G4bool warned = false;
252 ("G4VRML1SCENEHANDLER::AddPrimitive (const G4Polyline&)",
253 "VRML-1001", JustWarning,
254 "2D polylines not implemented. Ignored.");
259 VRMLBeginModeling () ;
261 fDest << "Separator {" << "\n";
263 SendMatrixTransformNode (fObjectTransformation );
265 SendMaterialNode( polyline.GetVisAttributes() );
267 fDest << "\t" << "Coordinate3 {" << "\n";
268 fDest << "\t\t" << "point [" << "\n";
270 for (i = 0, e = polyline.size(); e; i++, e--) {
272 fDest << polyline[i].x() << " ";
273 fDest << polyline[i].y() << " ";
274 fDest << polyline[i].z() << "," << "\n";
276 fDest << "\t\t" << "]" << "\n";
277 fDest << "\t" << "}" << "\n"; // Coordinate3
279 fDest << "\t" << "IndexedLineSet {" << "\n";
280 fDest << "\t\t" << "coordIndex [";
281 for (i = 0, e = polyline.size(); e; i++, e--) {
283 fDest << "\n" << "\t\t\t";
286 fDest << "-1" << "\n";
287 fDest << "\t\t" << "]" << "\n";
288 fDest << "\t" << "}" << "\n"; // IndexedLineSet
290 fDest << "}" << "\n"; // Separator
293 void G4VRML1SCENEHANDLER::AddPrimitive(const G4Polyhedron& polyhedron)
295 #if defined DEBUG_SCENE_FUNC
296 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
297 G4cout << "***** AddPrimitive(G4Polyhedron)" << "\n";
300 if (polyhedron.GetNoFacets() == 0) return;
303 static G4bool warned = false;
307 ("G4VRML1SCENEHANDLER::AddPrimitive (const G4Polyhedron&)",
308 "VRML-1002", JustWarning,
309 "2D polyhedra not implemented. Ignored.");
314 VRMLBeginModeling () ;
316 fDest << "Separator {" << "\n";
318 SendMatrixTransformNode( fObjectTransformation );
321 if (fCurrentDEF != "")
322 fDest << "DEF " << fCurrentDEF << " ";
324 fDest << "Separator {" << "\n";
326 //fDest << "\t\t" << "renderCulling ON" << "\n";
328 fDest << "\t\t" << "ShapeHints {" << "\n";
329 fDest << "\t\t\t" << "vertexOrdering COUNTERCLOCKWISE" << "\n";
330 fDest << "\t\t\t" << "shapeType SOLID" << "\n";
331 fDest << "\t\t\t" << "faceType CONVEX" << "\n";
332 fDest << "\t\t" << "}" << "\n";
336 fDest << "\t\t" << "Coordinate3 {" << "\n";
337 fDest << "\t\t\t" << "point [" << "\n";
339 for (i = 1, j = polyhedron.GetNoVertices(); j; j--, i++) {
340 G4Point3D point = polyhedron.GetVertex(i);
342 fDest << point.x() << " ";
343 fDest << point.y() << " ";
344 fDest << point.z() << "," << "\n";
346 fDest << "\t\t\t" << "]" << "\n";
347 fDest << "\t\t" << "}" << "\n"; // Coordinate3
349 fDest << "\t\t" << "IndexedFaceSet {" << "\n";
350 fDest << "\t\t\t" << "coordIndex [" << "\n";
354 for (f = polyhedron.GetNoFacets(); f; f--) {
358 G4int index = -1, edgeFlag = 1;
361 notLastEdge = polyhedron.GetNextVertexIndex(index, edgeFlag);
362 fDest << index - 1 << ", ";
363 } while (notLastEdge);
364 fDest << "-1," << "\n";
366 fDest << "\t\t\t" << "]" << "\n";
367 fDest << "\t\t" << "}" << "\n"; // IndexFaceSet
369 fDest << "\t" << "}" << "\n"; // (DEF) Separator
370 fDest << "}" << "\n"; // Separator
374 void G4VRML1SCENEHANDLER::AddPrimitive( const G4Text& )
376 #if defined DEBUG_SCENE_FUNC
377 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
378 G4cout << "***** AddPrimitive text" << "\n" ;
380 /*** You might need this
382 static G4bool warned = false;
386 ("G4VRML1SCENEHANDLER::AddPrimitive (const G4Text&)",
387 "VRML-1003", JustWarning,
388 "2D text not implemented. Ignored.");
394 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
396 "***** void G4VRML1SCENEHANDLER::AddPrimitive( const G4Text& text )"
397 " not implemented yet."
400 VRMLBeginModeling () ;
403 void G4VRML1SCENEHANDLER::AddPrimitive( const G4Circle& circle )
405 #if defined DEBUG_SCENE_FUNC
406 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
407 G4cout << "***** AddPrimitive circle" << "\n" ;
411 static G4bool warned = false;
415 ("G4VRML1SCENEHANDLER::AddPrimitive (const G4Circle&)",
416 "VRML-1004", JustWarning,
417 "2D circles not implemented. Ignored.");
422 VRMLBeginModeling () ;
424 // begin sending a mark
425 fDest << "Separator {" << "\n";
428 SendMarkerColor ( circle ) ;
431 SendMarkerWorldPosition ( circle ) ;
434 G4double size = GetMarkerHalfSize ( circle );
436 // send shape with size
437 fDest << "\t" << "Sphere {" << "\n";
438 fDest << "\t\t" << "radius " << size << "\n";
439 fDest << "\t" << "}" << "\n";
441 // end sending a mark
442 fDest << "}" << "\n"; // Separator
446 void G4VRML1SCENEHANDLER::AddPrimitive(const G4Square& square)
448 #if defined DEBUG_SCENE_FUNC
449 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
450 G4cout << "***** AddPrimitive square" << "\n" ;
454 static G4bool warned = false;
458 ("G4VRML1SCENEHANDLER::AddPrimitive (const G4Square&)",
459 "VRML-1005", JustWarning,
460 "2D squares not implemented. Ignored.");
465 VRMLBeginModeling () ;
467 // begin sending a mark
468 fDest << "Separator {" << "\n";
471 SendMarkerColor ( square );
474 SendMarkerWorldPosition ( square );
477 G4double size = GetMarkerHalfSize ( square );
480 // send shape with size
481 fDest << "\t" << "Cube {" << "\n";
482 fDest << "\t\t" << "width " << size << "\n";
483 fDest << "\t\t" << "height " << size << "\n";
484 fDest << "\t\t" << "depth " << size << "\n";
485 fDest << "\t" << "}" << "\n";
487 // end sending a mark
488 fDest << "}" << "\n"; // Separator
491 void G4VRML1SCENEHANDLER::BeginModeling()
493 #if defined DEBUG_SCENE_FUNC
494 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
495 G4cout << "***** BeginModeling " << "\n" ;
497 G4VSceneHandler::BeginModeling();
500 void G4VRML1SCENEHANDLER::EndModeling()
502 #if defined DEBUG_SCENE_FUNC
503 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
504 G4cout << "***** EndModeling " << "\n" ;
506 G4VSceneHandler::EndModeling();
509 void G4VRML1SCENEHANDLER::BeginPrimitives(const G4Transform3D& objectTransformation)
511 G4VSceneHandler::BeginPrimitives (objectTransformation);
512 #if defined DEBUG_SCENE_FUNC
513 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
514 G4cout << "***** BeginPrimitives " << "\n" ;
519 void G4VRML1SCENEHANDLER::EndPrimitives()
521 #if defined DEBUG_SCENE_FUNC
522 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
523 G4cout << "***** EndPrimitives " << "\n" ;
525 G4VSceneHandler::EndPrimitives();
528 void G4VRML1SCENEHANDLER::SendMaterialNode( const G4VisAttributes* pVA )
530 // const double TRANSPARENCY = 0.9 ;
531 const double TRANSPARENCY = 0.5 ;
536 const G4Color& color = pVA->GetColor();
538 fDest << "\t\t" << "Material {" << "\n";
540 if (pVA->IsForceDrawingStyle() &&
541 (pVA->GetForcedDrawingStyle() == G4VisAttributes::wireframe)) {
542 fDest << "\t\t\t" << "transparency " << TRANSPARENCY << "\n";
545 fDest << "\t\t\t" << "diffuseColor";
546 fDest << " " << color.GetRed();
547 fDest << " " << color.GetGreen();
548 fDest << " " << color.GetBlue();
550 fDest << "\t\t" << "}" << "\n";
554 void G4VRML1SCENEHANDLER::SendMaterialNode()
557 ( fpViewer->GetApplicableVisAttributes (fpVisAttribs) );
561 void G4VRML1SCENEHANDLER::SendMatrixTransformNode(const G4Transform3D& trans)
563 G4Point3D B (0.0, 0.0, 0.0);
565 G4Point3D x1 (1.0, 0.0, 0.0);
566 G4Point3D y1 (0.0, 1.0, 0.0);
568 G4Vector3D e1 (1.0, 0.0, 0.0);
569 G4Vector3D e2 (0.0, 1.0, 0.0);
570 G4Vector3D e3 (0.0, 0.0, 1.0);
572 //----- transformed origin of body coord
575 //----- transformed base vectors of body coord
584 e1 = e1.unit(); // normalize again for accuracy
588 fDest << "\t" << "MatrixTransform {" << "\n";
589 fDest << "\t\t" << "matrix ";
590 fDest << e1.x() << " " << e1.y() << " " << e1.z() << " 0 ";
591 fDest << e2.x() << " " << e2.y() << " " << e2.z() << " 0 ";
592 fDest << e3.x() << " " << e3.y() << " " << e3.z() << " 0 ";
593 fDest << B.x() << " " << B.y() << " " << B.z() << " 1" << "\n";
594 fDest << "\t" << "}" << "\n";
597 void G4VRML1SCENEHANDLER::SendCubeNode(G4double w, G4double h, G4double d)
599 fDest << "\t\t" << "Cube {" << "\n";
600 fDest << "\t\t\t" << "width " << w << "\n";
601 fDest << "\t\t\t" << "height " << h << "\n";
602 fDest << "\t\t\t" << "depth " << d << "\n";
603 fDest << "\t\t" << "}" << "\n";
606 void G4VRML1SCENEHANDLER::SendCylinderNode(G4double R, G4double h)
608 fDest << "\t\t" << "Transform {" << "\n";
609 fDest << "\t\t\t" << "rotation 1 0 0 " << (90. * CLHEP::deg) << "\n";
610 fDest << "\t\t" << "}" << "\n";
612 fDest << "\t\t" << "Cylinder {" << "\n";
613 fDest << "\t\t\t" << "radius " << R << "\n";
614 fDest << "\t\t\t" << "height " << h << "\n";
615 fDest << "\t\t" << "}" << "\n";
618 void G4VRML1SCENEHANDLER::SendSphereNode(G4double R)
620 fDest << "\t\t" << "Sphere {" << "\n";
621 fDest << "\t\t\t" << "radius " << R << "\n";
622 fDest << "\t\t" << "}" << "\n";
625 void G4VRML1SCENEHANDLER::VRMLBeginModeling()
627 if (!IS_CONNECTED ) {
628 #if defined DEBUG_SCENE_FUNC
629 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
630 G4cout << "***** VRMLBeginModeling() (started)" << "\n" ;
633 fDest << "#VRML V1.0 ascii" << "\n";
634 fDest << "# Generated by VRML 1.0 driver of GEANT4\n" << "\n";
638 void G4VRML1SCENEHANDLER::VRMLEndModeling()
640 if ( IS_CONNECTED ) {
641 #if defined DEBUG_SCENE_FUNC
642 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
643 G4cout << "***** VRMLEndModeling() (started)" << "\n" ;
646 fDest << "#End of file." << "\n";
651 void G4VRML1SCENEHANDLER::SendMarkerColor ( const G4VMarker& mark )
653 const G4Color& color = GetColor( mark );
655 fDest << "\t" << "Material {" << "\n";
657 fDest << "ambientColor [] ";
658 fDest << "specularColor [] ";
659 fDest << "\t\t" << "diffuseColor";
660 fDest << " " << color.GetRed();
661 fDest << " " << color.GetGreen();
662 fDest << " " << color.GetBlue();
664 fDest << "\t\t" << "emissiveColor";
665 fDest << " " << color.GetRed();
666 fDest << " " << color.GetGreen();
667 fDest << " " << color.GetBlue();
669 fDest << "\t" << "}" << "\n";
673 G4VRML1SCENEHANDLER::SendMarkerWorldPosition ( const G4VMarker& mark )
675 G4Point3D point = mark.GetPosition();
677 SendMatrixTransformNode( fObjectTransformation );
679 fDest << "\t\t" << "Transform {" << "\n";
680 fDest << "\t\t\t" << "translation ";
681 fDest << point.x() << " " << point.y() << " " << point.z() << "\n";
682 fDest << "\t\t" << "}" << "\n" ;
686 G4double G4VRML1SCENEHANDLER::GetMarkerHalfSize ( const G4VMarker& mark )
688 //----- return value ( marker radius in 3d units)
689 G4double size = 1.0 ; // initialization
691 //----- parameters to calculate 3d size from 2d size
692 const double HALF_SCREEN_SIZE_2D = 300.0 ; // pixels
693 double zoom_factor = fpViewer->GetViewParameters().GetZoomFactor() ;
694 if ( zoom_factor <= 0.0 ) { zoom_factor = 1.0 ; }
695 double extent_radius_3d = GetScene()->GetExtent().GetExtentRadius() ;
696 if ( extent_radius_3d <= 0.0 ) { extent_radius_3d = 1.0 ; }
698 //----- get marker radius in 3D units
699 if ( mark.GetWorldSize() > 0.0 ) {
700 // get mark radius in 3D units
701 size = 0.5 * mark.GetWorldSize() ;
703 } else if ( mark.GetScreenSize() > 0.0 ) {
705 double mark_radius_2d = 0.5 * mark.GetScreenSize() ;
707 // get mark radius in 3D units
709 = extent_radius_3d * ( mark_radius_2d / HALF_SCREEN_SIZE_2D );
710 size *= zoom_factor ;
714 double mark_radius_2d \
715 = fpViewer->GetViewParameters().GetDefaultMarker().GetScreenSize();
716 mark_radius_2d *= 0.1 ; // Magic number?
718 // get mark radius in 3D units
720 = extent_radius_3d * ( mark_radius_2d / HALF_SCREEN_SIZE_2D );
721 size *= zoom_factor ;
724 //----- global rescaling
725 size *= fpViewer->GetViewParameters().GetGlobalMarkerScale();
732 void G4VRML1SCENEHANDLER::ClearTransientStore()
734 // This is typically called after an update and before drawing hits
735 // of the next event. To simulate the clearing of "transients"
736 // (hits, etc.) the detector is redrawn...
738 fpViewer -> SetView ();
739 fpViewer -> ClearView ();
740 fpViewer -> DrawView ();
745 // #undef ADDTHIS_WITH_NAME(Solid)
746 // #undef MAKE_NAME(Solid)