source: trunk/source/visualization/VRML/src/G4VRML2SceneHandlerFunc.icc @ 850

Last change on this file since 850 was 850, checked in by garnier, 16 years ago

geant4.8.2 beta

File size: 18.0 KB
Line 
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// $Id: G4VRML2SceneHandlerFunc.icc,v 1.20 2006/06/29 21:26:17 gunter Exp $
28// GEANT4 tag $Name: HEAD $
29//
30// G4VRML2SceneHandlerFunc.icc
31// Satoshi Tanaka & Yasuhide Sawada
32
33//#define DEBUG_SCENE_FUNC
34
35void G4VRML2SCENEHANDLER::AddSolid(const G4Trd& trd)
36{
37#if defined DEBUG_SCENE_FUNC
38        G4cerr << "***** AddSolid trd" << "\n" ;
39#endif
40        VRMLBeginModeling () ;
41        G4VSceneHandler::AddSolid(trd);
42}
43
44void G4VRML2SCENEHANDLER::AddSolid(const G4Trap& trap)
45{
46#if defined DEBUG_SCENE_FUNC
47        G4cerr << "***** AddSolid trap" << "\n" ;
48#endif
49        VRMLBeginModeling () ;
50        G4VSceneHandler::AddSolid(trap);
51}
52
53void G4VRML2SCENEHANDLER::AddSolid(const G4Para& para)
54{
55#if defined DEBUG_SCENE_FUNC
56        G4cerr << "***** AddSolid para" << "\n" ;
57#endif
58        VRMLBeginModeling () ;
59        G4VSceneHandler::AddSolid(para);
60}
61
62void G4VRML2SCENEHANDLER::AddSolid(const G4Torus& torus )
63{
64#if defined DEBUG_SCENE_FUNC
65        G4cerr << "***** AddSolid torus" << "\n" ;
66#endif
67        VRMLBeginModeling () ;
68        G4VSceneHandler::AddSolid(torus);
69}
70
71
72void G4VRML2SCENEHANDLER::AddSolid(const G4VSolid& vsolid)
73{
74#if defined DEBUG_SCENE_FUNC
75        G4cerr << "***** AddSolid vsolid" << "\n" ;
76#endif
77        VRMLBeginModeling () ;
78        G4VSceneHandler::AddSolid(vsolid);
79}
80
81void G4VRML2SCENEHANDLER::AddSolid(const G4Tubs& tubs)
82{
83#if defined DEBUG_SCENE_FUNC
84        G4cerr << "***** AddSolid tubs" << "\n" ;
85#endif
86        VRMLBeginModeling () ;
87        G4VSceneHandler::AddSolid(tubs) ;
88
89}
90
91
92void G4VRML2SCENEHANDLER::AddSolid(const G4Cons& cons)
93{
94#if defined DEBUG_SCENE_FUNC
95    G4cerr << "***** AddSolid cons" << "\n" ;
96#endif
97        VRMLBeginModeling () ;
98        G4VSceneHandler::AddSolid(cons) ;
99}
100
101void G4VRML2SCENEHANDLER::AddSolid(const G4Box& box)
102{
103#if defined DEBUG_SCENE_FUNC
104        G4cerr << "***** AddSolid box" << "\n" ;
105#endif
106        VRMLBeginModeling () ;
107        G4VSceneHandler::AddSolid(box) ;
108}
109
110
111void G4VRML2SCENEHANDLER::AddSolid(const G4Sphere& sphere)
112{
113#if defined DEBUG_SCENE_FUNC
114        G4cerr << "***** AddSolid sphere" << "\n" ;
115#endif
116        VRMLBeginModeling () ;
117        G4VSceneHandler::AddSolid(sphere) ;
118}
119
120void G4VRML2SCENEHANDLER::AddPrimitive(const G4Polyline& polyline)
121{
122#if defined DEBUG_SCENE_FUNC
123        G4cerr << "***** AddPrimitive polyline" << "\n" ;
124#endif
125
126        VRMLBeginModeling () ;
127
128        // VRML codes are generated below
129
130        fDest << "#---------- POLYLINE" << "\n";
131        fDest << "Shape {" << "\n";
132
133        SendLineColor   ( polyline.GetVisAttributes() );
134
135        fDest << "\t" << "geometry IndexedLineSet {" << "\n";
136
137        fDest << "\t\t"   << "coord Coordinate {" << "\n";
138        fDest << "\t\t\t" <<      "point [" << "\n";
139        G4int e, i;
140        for (i = 0, e = polyline.size(); e; i++, e--) {
141
142                G4Point3D point = polyline[i];
143                point.transform( *fpObjectTransformation );
144
145                fDest << "\t\t\t\t";
146                fDest <<                  point.x() << " ";
147                fDest <<                  point.y() << " ";
148                fDest <<                  point.z() << "," << "\n";
149
150        } // for
151
152        fDest << "\t\t\t" <<      "]" << "\n"; // point
153        fDest << "\t\t"   << "}"      << "\n"; // coord
154
155        fDest << "\t\t" << "coordIndex [";
156        for (i = 0, e = polyline.size(); e; i++, e--) {
157                if (i % 10 == 0)
158                        fDest << "\n" << "\t\t\t";
159                fDest << i << ", ";
160        }
161        fDest << "-1" << "\n";
162        fDest << "\t\t" << "]"        << "\n"; // coordIndex
163        fDest << "\t" << "}" << "\n"; // geometry IndexedLineSet
164
165        fDest << "}" << "\n"; // Shape
166}
167
168
169void G4VRML2SCENEHANDLER::AddPrimitive(const G4Polyhedron& polyhedron)
170{
171#if defined DEBUG_SCENE_FUNC
172        G4cerr << "***** AddPrimitive(G4Polyhedron)" << "\n";
173#endif
174
175        if (polyhedron.GetNoFacets() == 0) return;
176
177        VRMLBeginModeling () ;
178
179        // Transparency checking: If completely transparent, skip drawing
180        if ( GetPVTransparency() > 0.99 ) { return ; }
181
182        // Current Model
183        const G4VModel* pv_model  = GetModel();
184        G4String pv_name = "No model";
185        if (pv_model) pv_name = pv_model->GetCurrentTag() ;
186
187        // VRML codes are generated below
188
189        fDest << "#---------- SOLID: " << pv_name << "\n";
190
191        if ( IsPVPickable() ) {
192
193         fDest << "Anchor {" << "\n";
194         fDest << " description " << "\"" << pv_name << "\"" << "\n";
195         fDest << " url \"\" " << "\n";
196         fDest << " children [" << "\n";
197        }
198
199        fDest << "\t"; fDest << "Shape {" << "\n";
200
201        SendMaterialNode();
202
203        fDest << "\t\t" << "geometry IndexedFaceSet {" << "\n";
204
205        fDest << "\t\t\t"   << "coord Coordinate {" << "\n";
206        fDest << "\t\t\t\t" <<      "point [" << "\n";
207        G4int i, j;
208        for (i = 1, j = polyhedron.GetNoVertices(); j; j--, i++) {
209                G4Point3D point = polyhedron.GetVertex(i);
210
211                point.transform( *fpObjectTransformation );
212
213                fDest << "\t\t\t\t\t";
214                fDest <<                   point.x() << " ";
215                fDest <<                   point.y() << " ";
216                fDest <<                   point.z() << "," << "\n";
217        }
218        fDest << "\t\t\t\t" <<      "]" << "\n"; // point
219        fDest << "\t\t\t"   << "}"      << "\n"; // coord
220
221        fDest << "\t\t\t"   << "coordIndex [" << "\n";
222
223        // facet loop
224        G4int f;
225        for (f = polyhedron.GetNoFacets(); f; f--) {
226
227                // edge loop 
228                G4bool notLastEdge;
229                G4int index = -1, edgeFlag = 1;
230                fDest << "\t\t\t\t";
231                do {
232                        notLastEdge = polyhedron.GetNextVertexIndex(index, edgeFlag);
233                        fDest << index - 1 << ", ";
234                } while (notLastEdge);
235                fDest << "-1," << "\n";
236        }
237        fDest << "\t\t\t"   << "]" << "\n"; // coordIndex
238
239        fDest << "\t\t\t"   << "solid FALSE" << "\n"; // draw backfaces
240
241        fDest << "\t\t" << "}"     << "\n"; // IndexFaceSet
242        fDest << "\t" << "}"       << "\n"; // Shape
243
244        if ( IsPVPickable() ) {
245         fDest << " ]"              << "\n"; // children
246         fDest << "}"               << "\n"; // Anchor
247        }
248
249}
250
251
252void G4VRML2SCENEHANDLER::AddPrimitive(const G4NURBS&)
253{
254#if defined DEBUG_SCENE_FUNC
255        G4cerr << "***** AddPrimitive nurbs" << "\n" ;
256#endif
257        G4cerr << "G4VRML2File::AddPrimitive(G4NURBS&): not implemented. " << "\n" ;
258
259        VRMLBeginModeling () ;
260}
261
262void G4VRML2SCENEHANDLER::AddPrimitive( const G4Text& )
263{
264#if defined DEBUG_SCENE_FUNC
265        G4cerr << "***** AddPrimitive text" << "\n" ;
266#endif
267        G4cerr <<
268          "***** void G4VRML2SCENEHANDLER::AddPrimitive( const G4Text& text )"
269          " not implemented yet."
270             << "\n";
271
272        VRMLBeginModeling () ;
273
274}
275
276void G4VRML2SCENEHANDLER::AddPrimitive( const G4Circle& circle )
277{
278#if defined DEBUG_SCENE_FUNC
279        G4cerr << "***** AddPrimitive circle" << "\n" ;
280#endif
281        VRMLBeginModeling () ;
282
283        // Information
284//      const char* info = NULL ;
285        const char* info = circle.GetInfo() ;
286
287        // Position and half size of circle
288        G4double X, Y, Z ; GetMarkerWorldPosition( circle, &X, &Y, &Z ) ;
289        G4double radius = GetMarkerHalfSize ( circle );
290
291        // VRML codes are generated below
292        fDest << "#---------- 3D MARKER (Circle)" << "\n";
293        fDest << "Anchor {" << "\n";
294
295        if( circle.GetInfo().isNull() ) {
296                fDest << " description " << "\"(" <<  X <<  "  " ;
297                fDest <<                              Y <<  "  " ;
298                fDest <<                              Z <<  ")\"" << "\n";
299        } else {
300                fDest << " description " << "\"" <<  info << "\"" << "\n";
301        }
302
303
304        fDest << " url \"\" " << "\n";
305
306        fDest << " children [" << "\n";
307
308        fDest << "  Transform {" << "\n";
309
310        fDest << "   translation ";
311        fDest <<      X << " " << Y << " " << Z << "\n";
312
313        fDest << "   children ["   << "\n";
314
315        fDest << "\t" << "Shape {" << "\n";
316
317        SendMarkerColor( circle );
318
319        // send shape with size
320        fDest << "\t\t"   << "geometry Sphere {" << "\n";
321        fDest << "\t\t\t" <<      "radius " << radius << "\n";
322        fDest << "\t\t"   << "}" << "\n";
323
324        fDest << "\t" << "}"       << "\n"; // Shape
325
326        fDest << "   ]"            << "\n"; //  children
327        fDest << "  }"             << "\n"; //  Transform
328       
329        fDest << " ]"              << "\n"; //  children
330        fDest << "}"               << "\n"; //  Anchor
331
332}
333
334void G4VRML2SCENEHANDLER::AddPrimitive(const G4Square& square)
335{
336#if defined DEBUG_SCENE_FUNC
337        G4cerr << "***** AddPrimitive square" << "\n" ;
338#endif
339
340        VRMLBeginModeling () ;
341
342        // Information
343//      const char* info = NULL ;
344        const char* info = square.GetInfo() ;
345
346        // Position and size of square
347        G4double X, Y, Z ; GetMarkerWorldPosition  ( square, &X, &Y, &Z ) ;
348        G4double radius   = GetMarkerHalfSize ( square );
349        G4double diameter = 2.0 * radius ;
350
351        // VRML codes are generated below
352        fDest << "#---------- 3D MARKER (Square)" << "\n";
353        fDest << "Anchor {" << "\n";
354
355        if( square.GetInfo().isNull() ) {
356                fDest << " description " << "\"(" <<  X <<  "  " ;
357                fDest <<                              Y <<  "  " ;
358                fDest <<                              Z <<  ")\"" << "\n";
359        } else {
360                fDest << " description " << "\"" <<  info << "\"" << "\n";
361        }
362
363        fDest << " url \"\" " << "\n";
364
365        fDest << " children [" << "\n";
366
367        fDest << "  Transform {" << "\n";
368
369        fDest << "   translation ";
370        fDest <<      X << " " << Y << " " << Z << "\n";
371
372        fDest << "   children ["   << "\n";
373
374        fDest << "\t" << "Shape {" << "\n";
375
376        SendMarkerColor( square );
377
378        // send shape with size
379        fDest << "\t\t"   << "geometry Box {" << "\n";
380        fDest << "\t\t\t" <<      "size "   << diameter << " "  ;
381        fDest                               << diameter << " "  ;
382        fDest                               << diameter << "\n" ;
383        fDest << "\t\t"   << "}"                        << "\n";
384
385        fDest << "\t" << "}"       << "\n"; //  Shape
386
387        fDest << "   ]"            << "\n"; //  children
388        fDest << "  }"             << "\n"; //  Transform
389       
390        fDest << " ]"              << "\n"; //  children
391        fDest << "}"               << "\n"; //  Anchor
392
393}
394
395void G4VRML2SCENEHANDLER::BeginModeling()
396{
397#if defined DEBUG_SCENE_FUNC
398        G4cerr << "***** BeginModeling" << "\n" ;
399#endif
400        G4VSceneHandler::BeginModeling();
401
402}
403
404void G4VRML2SCENEHANDLER::EndModeling()
405{
406#if defined DEBUG_SCENE_FUNC
407        G4cerr << "***** EndModeling " << "\n" ;
408#endif
409        G4VSceneHandler::EndModeling();
410}
411
412void G4VRML2SCENEHANDLER::BeginPrimitives(const G4Transform3D& objectTransformation)
413{
414  G4VSceneHandler::BeginPrimitives (objectTransformation);
415  fpObjectTransformation = &objectTransformation;
416#if defined DEBUG_SCENE_FUNC
417        G4cerr << "***** BeginPrimitives " << "\n" ;
418#endif
419        VRMLBeginModeling();
420}
421
422void G4VRML2SCENEHANDLER::EndPrimitives()
423{
424#if defined DEBUG_SCENE_FUNC
425        G4cerr << "***** EndPrimitives " << "\n" ;
426#endif
427  G4VSceneHandler::EndPrimitives();
428}
429
430void G4VRML2SCENEHANDLER::SendMaterialNode( const G4VisAttributes*  pVA )
431{
432        // Initialization of drawing parameters         
433        G4bool          flagWF       = false ;
434        G4double        TRANSPARENCY = GetPVTransparency() ;
435
436        //  Wireframe or not
437        const   G4ViewParameters& cur_view_param = fpViewer -> GetViewParameters ();
438        if (pVA->IsForceDrawingStyle() &&
439           (pVA->GetForcedDrawingStyle() == G4VisAttributes::wireframe))
440        {
441                // wireframe style is assigned to this object
442                flagWF = true ;
443
444        } else if ( cur_view_param.GetDrawingStyle() == G4ViewParameters::wireframe )   
445        {
446                // current viewing style is  wireframe
447                flagWF = true  ;
448        }
449
450        // VRML codes are generated below
451        if (!pVA) {
452               
453                // Error recovery: Use default values of VRML browser
454                fDest << "\t\t" ;
455                fDest << "appearance Appearance { material Material {} }";
456                fDest << "\n";
457               
458        } else {
459
460                const G4Color& color = pVA->GetColor();
461
462                fDest << "\t\t"   << "appearance Appearance {" << "\n";
463                fDest << "\t\t\t" <<      "material Material {" << "\n";
464
465                // diffuse color       
466                fDest << "\t\t\t\t" << "diffuseColor ";
467                fDest <<                   color.GetRed()   << " " ;
468                fDest <<                   color.GetGreen() << " " ;
469                fDest <<                   color.GetBlue()  << "\n";
470
471                // emmisive color       
472                // fDest << "\t\t\t\t" << "emissiveColor ";
473                // fDest <<                   color.GetRed()   << " " ;
474                // fDest <<                   color.GetGreen() << " " ;
475                //fDest <<                   color.GetBlue()  << "\n";
476                //
477
478                // wireframe is expressed as transparency
479                if ( flagWF )
480                {
481                        fDest << "\t\t\t\t" << "transparency " << TRANSPARENCY << "\n";
482                }
483
484                fDest << "\t\t\t" <<      "}" << "\n"; // material
485                fDest << "\t\t"   << "}"      << "\n"; // appearance
486        }
487
488}
489
490
491void G4VRML2SCENEHANDLER::SendMaterialNode()
492{
493        SendMaterialNode
494        ( fpViewer->GetApplicableVisAttributes (fpVisAttribs) );
495}
496
497
498void G4VRML2SCENEHANDLER::VRMLBeginModeling()
499{
500        if (!IS_CONNECTED ) {
501#if defined DEBUG_SCENE_FUNC
502        G4cerr << "***** VRMLBeginModeling (started)" << "\n" ;
503#endif
504                this->connectPort();
505                fDest << "#VRML V2.0 utf8" << "\n";
506                fDest << "# Generated by VRML 2.0 driver of GEANT4\n" << "\n";
507        }
508}
509
510void G4VRML2SCENEHANDLER::VRMLEndModeling()
511{
512        if ( IS_CONNECTED ) {
513#if defined DEBUG_SCENE_FUNC
514        G4cerr << "***** VRMLEndModeling (started)" << "\n" ;
515#endif
516                fDest << "#End of file." << "\n";
517                this->closePort();
518        }
519}
520
521void      G4VRML2SCENEHANDLER::SendMarkerColor ( const G4VMarker& mark )
522{
523        const G4Color& color = GetColor( mark );
524
525        fDest << "\t\t"     << "appearance Appearance {" << "\n";
526        fDest << "\t\t\t"   <<      "material Material {" << "\n";
527        fDest << "\t\t\t\t" <<      "diffuseColor ";
528        fDest <<                   color.GetRed()    << " " ;
529        fDest <<                   color.GetGreen()  << " " ;
530        fDest <<                   color.GetBlue()   << "\n";
531        fDest << "\t\t\t\t" <<    "emissiveColor ";
532        fDest <<                   color.GetRed()    << " " ;
533        fDest <<                   color.GetGreen()  << " " ;
534        fDest <<                   color.GetBlue()   << "\n";
535        fDest << "\t\t\t"   <<      "}"              << "\n"; // material
536        fDest << "\t\t"     << "}"                   << "\n"; // appearance
537
538}
539
540void     
541G4VRML2SCENEHANDLER::SendMarkerWorldPosition ( const G4VMarker& mark )
542{
543        G4Point3D point = mark.GetPosition();
544
545        point.transform( *fpObjectTransformation );
546
547        fDest << "   translation ";
548        fDest <<      point.x() << " " << point.y() << " " << point.z() << "\n";
549
550}
551
552void  G4VRML2SCENEHANDLER::GetMarkerWorldPosition    (  const G4VMarker&  mark ,
553                                                double* pX             ,
554                                                double* pY             ,
555                                                double* pZ              )
556{
557        G4Point3D point = mark.GetPosition();
558
559        point.transform( *fpObjectTransformation );
560
561        *pX = point.x() ;
562        *pY = point.y() ;
563        *pZ = point.z() ;
564}
565
566G4double  G4VRML2SCENEHANDLER::GetMarkerHalfSize ( const G4VMarker& mark )
567{
568        //----- return value ( marker radius in 3d units)
569        G4double size       = 1.0 ; // initialization
570
571        //----- parameters to calculate 3d size from 2d size
572        const double HALF_SCREEN_SIZE_2D = 300.0 ; // pixels
573        double zoom_factor  = fpViewer->GetViewParameters().GetZoomFactor() ;
574        if ( zoom_factor <=  0.0 ) { zoom_factor = 1.0 ; }
575        double extent_radius_3d = GetScene()->GetExtent().GetExtentRadius() ;
576        if ( extent_radius_3d <= 0.0 ) { extent_radius_3d = 1.0 ; }
577
578        //----- get marker radius in 3D units
579        if        ( mark.GetWorldSize() > 0.0 ) {
580
581                // get mark radius in 3D units
582                size = 0.5 * mark.GetWorldSize()  ;
583
584        } else if ( mark.GetScreenSize() > 0.0 ) {
585
586                // local
587                double mark_radius_2d   = 0.5 * mark.GetScreenSize() ;
588
589                // get mark radius in 3D units
590                size \
591                 = extent_radius_3d * ( mark_radius_2d / HALF_SCREEN_SIZE_2D );
592                size *= zoom_factor ;
593
594        } else {
595                // local
596                double mark_radius_2d \
597                 = fpViewer->GetViewParameters().GetDefaultMarker().GetScreenSize();
598                mark_radius_2d *= 0.1 ; // Magic number?
599
600                // get mark radius in 3D units
601                size \
602                 = extent_radius_3d * ( mark_radius_2d / HALF_SCREEN_SIZE_2D );
603                size *= zoom_factor ;
604        }
605
606                //----- global rescaling
607        size *= fpViewer->GetViewParameters().GetGlobalMarkerScale();
608
609                //----- return size
610        return size ;
611
612}
613
614void G4VRML2SCENEHANDLER::SendLineColor ( const G4VisAttributes*  pVA )
615{
616        const G4Color& color = pVA->GetColor();
617
618
619        fDest << "\t"   << "appearance Appearance {" << "\n";
620        fDest << "\t\t" <<      "material Material {" << "\n";
621        fDest << "\t\t\t" <<      "diffuseColor ";
622        fDest <<                   color.GetRed()    << " " ;
623        fDest <<                   color.GetGreen()  << " " ;
624        fDest <<                   color.GetBlue()   << "\n";
625        fDest << "\t\t\t" <<    "emissiveColor ";
626        fDest <<                   color.GetRed()    << " " ;
627        fDest <<                   color.GetGreen()  << " " ;
628        fDest <<                   color.GetBlue()   << "\n";
629        fDest << "\t\t" <<      "}"                  << "\n"; // material
630        fDest << "\t"   << "}"                       << "\n"; // appearance
631}
632
633G4double      G4VRML2SCENEHANDLER::SetPVTransparency ()
634{
635
636        G4double        TRANSPARENCY = 0.7   ;  // initialization
637                                               
638        if( getenv( "G4VRML_TRANSPARENCY" ) != NULL ) {
639
640                // get a value from the env value
641                sscanf( getenv("G4VRML_TRANSPARENCY"), "%lg", &TRANSPARENCY ) ;
642
643                // truncation
644                TRANSPARENCY = std::fabs ( TRANSPARENCY ) ;
645                if ( TRANSPARENCY > 1.0 ) { TRANSPARENCY = 1.0 ; }
646        }
647
648        fPVTransparency = TRANSPARENCY ;
649
650        return fPVTransparency ;
651}
652
653
654void G4VRML2SCENEHANDLER::ClearTransientStore()
655{
656  G4VSceneHandler::ClearTransientStore ();
657  // This is typically called after an update and before drawing hits
658  // of the next event.  To simulate the clearing of "transients"
659  // (hits, etc.) the detector is redrawn...
660  if (fpViewer) {
661    fpViewer -> SetView ();
662    fpViewer -> ClearView ();
663    fpViewer -> DrawView ();
664  }
665}
666
667
668// End of file.
Note: See TracBrowser for help on using the repository browser.