SLIC  6.1.1
Simulation for the Linear Collider (SLIC) - Geant4 simulation application
VRML2Writer.cc
Go to the documentation of this file.
1 #include "VRML2Writer.hh"
2 
3 // stl
4 #include <sstream>
5 #include <iostream>
6 
7 // geant4
8 #include "G4TransportationManager.hh"
9 #include "G4VPhysicalVolume.hh"
10 #include "G4LogicalVolume.hh"
11 #include "G4VisAttributes.hh"
12 #include "G4Box.hh"
13 #include "G4Polyhedron.hh"
14 #include "G4RotationMatrix.hh"
15 #include "G4ThreeVector.hh"
16 #include "G4RotationMatrix.hh"
17 #include "G4Point3D.hh"
18 #include "G4SystemOfUnits.hh"
19 
20 #include "math.h"
21 
22 namespace slic {
23 
24 VRML2Writer::VRML2Writer(const ::std::string& filename) :
25  _indentLevel(0) {
26  _out.open(filename.c_str());
27 }
28 
30  if (_out.is_open())
31  _out.close();
32 }
33 
35  writeLine("#VRML V2.0 utf8");
36  processTopVolume(G4TransportationManager::GetTransportationManager()->GetNavigatorForTracking()->GetWorldVolume());
37 }
38 
39 void VRML2Writer::processTopVolume(G4VPhysicalVolume* wv) {
40  writeLine("Group {");
41  indent();
42  writeLine("children [");
43  indent();
45  unindent();
46  writeLine("]");
47  unindent();
48  writeLine("}"); // Group
49 }
50 
51 void VRML2Writer::processPhysicalVolume(G4VPhysicalVolume* pv) {
52  writeLine("DEF " + pv->GetName() + " Transform {");
53  indent();
54  writeLine("children [");
55  indent();
56 
57  // shape node for each physical volume
58  const G4VisAttributes* vis = pv->GetLogicalVolume()->GetVisAttributes();
59  bool show = true;
60  if (vis && !vis->IsVisible())
61  show = false;
62  if (show) {
63  writeLine("DEF " + pv->GetName() + " Shape {");
64  indent();
66  processSolid(pv->GetLogicalVolume()->GetSolid());
67  unindent();
68  writeLine("}");
69  }
70 
71  G4LogicalVolume* lv = pv->GetLogicalVolume();
72 
73  // Recurse through daughter volumes
74  bool showDaughters = true;
75  if (vis && vis->IsDaughtersInvisible())
76  showDaughters = false;
77  if (showDaughters) {
78  for (int i = 0, ndau = lv->GetNoDaughters(); i < ndau; i++) {
79  processPhysicalVolume(lv->GetDaughter(i));
80  }
81  }
82 
83  unindent();
84  writeLine("]"); // children
85 
86  // translation
87  const G4ThreeVector& pos = pv->GetTranslation();
88  std::stringstream ss;
89  ss << "translation " << pos.x() / m << " " << pos.y() / m << " " << pos.z() / m;
90  writeLine(ss.str());
91 
92  // rotation
93  const G4RotationMatrix* rot = pv->GetRotation();
94  CLHEP::Hep3Vector axis(0, 0, 0);
95  double angle = 0;
96  if (rot != 0)
97  rot->getAngleAxis(angle, axis);
98  std::stringstream ss2;
99  ss2 << "rotation " << axis.x() << " " << axis.y() << " " << axis.z() << " " << -angle;
100  rot = 0;
101  writeLine(ss2.str());
102 
103  unindent();
104  writeLine("}"); // Transform
105 }
106 
107 void VRML2Writer::processVisualization(G4VPhysicalVolume* pv) {
108  const G4VisAttributes* vis = pv->GetLogicalVolume()->GetVisAttributes();
109  double r, g, b;
110  r = g = b = 1.0;
111  if (vis != 0) {
112  const G4Colour& col = vis->GetColour();
113  r = col.GetRed();
114  g = col.GetGreen();
115  b = col.GetBlue();
116  }
117  writeLine("appearance Appearance {");
118  indent();
119  writeLine("material Material {");
120  indent();
121  std::stringstream ss;
122  ss << r << " " << g << " " << b;
123  writeLine("diffuseColor " + ss.str());
124  writeLine("transparency 0.0"); // use viewer like Deep View to get a wireframe or transparent view
125  unindent();
126  writeLine("}");
127  unindent();
128  writeLine("}");
129 }
130 
131 void VRML2Writer::processSolid(G4VSolid* solid) {
132  if (solid->GetEntityType() == "G4Box") {
133  processBox((G4Box*) solid);
134  } else {
135  processPolyhedron(solid->GetPolyhedron());
136  }
137 }
138 
139 void VRML2Writer::processBox(G4Box* box) {
140  writeLine("geometry Box {");
141  indent();
142  std::stringstream ss;
143  ss << "size " << box->GetXHalfLength() * 2 / m << " " << box->GetYHalfLength() * 2 / m << " " << box->GetZHalfLength() * 2 / m;
144  writeLine(ss.str());
145  unindent();
146  writeLine("}");
147 }
148 
149 // code taken from Geant4....
150 // G4MVRL2SceneHandlerFunc.icc - G4VRML2SCENEHANDLER::AddPrimitive(const G4Polyhedron& polyhedron)
151 void VRML2Writer::processPolyhedron(G4Polyhedron* polyhedron) {
152  writeLine("geometry IndexedFaceSet {");
153  indent();
154  writeLine("coord Coordinate {");
155  indent();
156  writeLine("point [");
157  indent();
158 
159  int i, j;
160  for (i = 1, j = polyhedron->GetNoVertices(); j; j--, i++) {
161  G4Point3D point = polyhedron->GetVertex(i);
162  std::stringstream ss;
163  ss << point.x() / m << " " << point.y() / m << " " << point.z() / m;
164  writeLine(ss.str());
165  }
166  unindent();
167  writeLine("]"); // point
168  unindent();
169  writeLine("}"); // coord
170 
171  writeLine("coordIndex [");
172  indent();
173 
174  // facet loop
175  int f;
176  for (f = polyhedron->GetNoFacets(); f; f--) {
177  // edge loop
178  bool notLastEdge;
179  int index = -1, edgeFlag = 1;
180  std::stringstream ss;
181  do {
182  notLastEdge = polyhedron->GetNextVertexIndex(index, edgeFlag);
183  ss << index - 1 << ", ";
184  } while (notLastEdge);
185  ss << "-1, ";
186  writeLine(ss.str());
187  }
188 
189  unindent();
190  writeLine("]");
191  writeLine("solid FALSE");
192  unindent();
193  writeLine("}"); // IndexFaceSet
194 }
195 
196 void VRML2Writer::writeLine(const std::string& text) {
197  for (int i = 0; i < _indentLevel; i++) {
198  _out << " ";
199  }
200  _out << text << std::endl;
201 }
202 
204  ++_indentLevel;
205 }
206 
208  --_indentLevel;
209 }
210 }
void processBox(G4Box *box)
Definition: VRML2Writer.cc:139
void processSolid(G4VSolid *solid)
Definition: VRML2Writer.cc:131
std::ofstream _out
Definition: VRML2Writer.hh:96
VRML2Writer(const std::string &filename)
Definition: VRML2Writer.cc:24
void processPolyhedron(G4Polyhedron *poly)
Definition: VRML2Writer.cc:151
void processVisualization(G4VPhysicalVolume *volume)
Definition: VRML2Writer.cc:107
void writeLine(const std::string &line)
Definition: VRML2Writer.cc:196
void processTopVolume(G4VPhysicalVolume *volume)
Definition: VRML2Writer.cc:39
void processPhysicalVolume(G4VPhysicalVolume *volume)
Definition: VRML2Writer.cc:51
@ endl
Definition: LogStream.hh:26