source: trunk/source/persistency/gdml/src/G4GDMLWriteSolids.cc @ 1348

Last change on this file since 1348 was 1347, checked in by garnier, 14 years ago

geant4 tag 9.4

File size: 40.1 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: G4GDMLWriteSolids.cc,v 1.69 2010/10/14 16:19:40 gcosmo Exp $
28// GEANT4 tag $Name: gdml-V09-03-09 $
29//
30// class G4GDMLWriteSolids Implementation
31//
32// Original author: Zoltan Torzsok, November 2007
33//
34// --------------------------------------------------------------------
35
36#include "G4GDMLWriteSolids.hh"
37
38#include "G4BooleanSolid.hh"
39#include "G4Box.hh"
40#include "G4Cons.hh"
41#include "G4Ellipsoid.hh"
42#include "G4EllipticalCone.hh"
43#include "G4EllipticalTube.hh"
44#include "G4ExtrudedSolid.hh"
45#include "G4Hype.hh"
46#include "G4Orb.hh"
47#include "G4Para.hh"
48#include "G4Paraboloid.hh"
49#include "G4IntersectionSolid.hh"
50#include "G4Polycone.hh"
51#include "G4Polyhedra.hh"
52#include "G4ReflectedSolid.hh"
53#include "G4Sphere.hh"
54#include "G4SubtractionSolid.hh"
55#include "G4GenericTrap.hh"
56#include "G4TessellatedSolid.hh"
57#include "G4Tet.hh"
58#include "G4Torus.hh"
59#include "G4Trap.hh"
60#include "G4Trd.hh"
61#include "G4Tubs.hh"
62#include "G4TwistedBox.hh"
63#include "G4TwistedTrap.hh"
64#include "G4TwistedTrd.hh"
65#include "G4TwistedTubs.hh"
66#include "G4UnionSolid.hh"
67#include "G4OpticalSurface.hh"
68#include "G4SurfaceProperty.hh"
69
70G4GDMLWriteSolids::G4GDMLWriteSolids()
71  : G4GDMLWriteMaterials(), solidsElement(0)
72{
73}
74
75G4GDMLWriteSolids::~G4GDMLWriteSolids()
76{
77}
78
79void G4GDMLWriteSolids::
80BooleanWrite(xercesc::DOMElement* solidsElement,
81             const G4BooleanSolid* const boolean)
82{
83   G4int displaced=0;
84
85   G4String tag("undefined");
86   if (dynamic_cast<const G4IntersectionSolid*>(boolean))
87     { tag = "intersection"; } else
88   if (dynamic_cast<const G4SubtractionSolid*>(boolean))
89     { tag = "subtraction"; } else
90   if (dynamic_cast<const G4UnionSolid*>(boolean))
91     { tag = "union"; }
92   
93   G4VSolid* firstPtr = const_cast<G4VSolid*>(boolean->GetConstituentSolid(0));
94   G4VSolid* secondPtr = const_cast<G4VSolid*>(boolean->GetConstituentSolid(1));
95   
96   G4ThreeVector firstpos,firstrot,pos,rot;
97
98   // Solve possible displacement of referenced solids!
99   //
100   while (true)
101   {
102      if ( displaced>8 )
103      {
104        G4String ErrorMessage = "The referenced solid '"
105                              + firstPtr->GetName() +
106                              + "in the Boolean shape '" +
107                              + boolean->GetName() +
108                              + "' was displaced too many times!";
109        G4Exception("G4GDMLWriteSolids::BooleanWrite()",
110                    "InvalidSetup", FatalException, ErrorMessage);
111      }
112
113      if (G4DisplacedSolid* disp = dynamic_cast<G4DisplacedSolid*>(firstPtr))
114      {
115         firstpos += disp->GetObjectTranslation();
116         firstrot += firstrot + GetAngles(disp->GetObjectRotation());
117         firstPtr = disp->GetConstituentMovedSolid();
118         displaced++;
119         continue;
120      }
121      break;
122   }
123   displaced = 0;
124   while (true)
125   {
126      if ( displaced>maxTransforms )
127      {
128        G4String ErrorMessage = "The referenced solid '"
129                              + secondPtr->GetName() +
130                              + "in the Boolean shape '" +
131                              + boolean->GetName() +
132                              + "' was displaced too many times!";
133        G4Exception("G4GDMLWriteSolids::BooleanWrite()",
134                    "InvalidSetup", FatalException, ErrorMessage);
135      }
136
137      if (G4DisplacedSolid* disp = dynamic_cast<G4DisplacedSolid*>(secondPtr))
138      {
139         pos += disp->GetObjectTranslation();
140         rot += GetAngles(disp->GetObjectRotation());
141         secondPtr = disp->GetConstituentMovedSolid();
142         displaced++;
143         continue;
144      }
145      break;
146   }
147
148   AddSolid(firstPtr);   // At first add the constituent solids!
149   AddSolid(secondPtr);
150
151   const G4String& name = GenerateName(boolean->GetName(),boolean);
152   const G4String& firstref = GenerateName(firstPtr->GetName(),firstPtr);
153   const G4String& secondref = GenerateName(secondPtr->GetName(),secondPtr);
154
155   xercesc::DOMElement* booleanElement = NewElement(tag);
156   booleanElement->setAttributeNode(NewAttribute("name",name));
157   xercesc::DOMElement* firstElement = NewElement("first");
158   firstElement->setAttributeNode(NewAttribute("ref",firstref));
159   booleanElement->appendChild(firstElement);
160   xercesc::DOMElement* secondElement = NewElement("second");
161   secondElement->setAttributeNode(NewAttribute("ref",secondref));
162   booleanElement->appendChild(secondElement);
163   solidsElement->appendChild(booleanElement);
164     // Add the boolean solid AFTER the constituent solids!
165
166   if ( (std::fabs(pos.x()) > kLinearPrecision)
167     || (std::fabs(pos.y()) > kLinearPrecision)
168     || (std::fabs(pos.z()) > kLinearPrecision) )
169   {
170     PositionWrite(booleanElement,name+"_pos",pos);
171   }
172
173   if ( (std::fabs(rot.x()) > kAngularPrecision)
174     || (std::fabs(rot.y()) > kAngularPrecision)
175     || (std::fabs(rot.z()) > kAngularPrecision) )
176   {
177     RotationWrite(booleanElement,name+"_rot",rot);
178   }
179
180   if ( (std::fabs(firstpos.x()) > kLinearPrecision)
181     || (std::fabs(firstpos.y()) > kLinearPrecision)
182     || (std::fabs(firstpos.z()) > kLinearPrecision) )
183   {
184     FirstpositionWrite(booleanElement,name+"_fpos",firstpos);
185   }
186
187   if ( (std::fabs(firstrot.x()) > kAngularPrecision)
188     || (std::fabs(firstrot.y()) > kAngularPrecision)
189     || (std::fabs(firstrot.z()) > kAngularPrecision) )
190   {
191     FirstrotationWrite(booleanElement,name+"_frot",firstrot);
192   }
193}
194
195void G4GDMLWriteSolids::
196BoxWrite(xercesc::DOMElement* solidsElement, const G4Box* const box)
197{
198   const G4String& name = GenerateName(box->GetName(),box);
199
200   xercesc::DOMElement* boxElement = NewElement("box");
201   boxElement->setAttributeNode(NewAttribute("name",name));
202   boxElement->setAttributeNode(NewAttribute("x",2.0*box->GetXHalfLength()/mm));
203   boxElement->setAttributeNode(NewAttribute("y",2.0*box->GetYHalfLength()/mm));
204   boxElement->setAttributeNode(NewAttribute("z",2.0*box->GetZHalfLength()/mm));
205   boxElement->setAttributeNode(NewAttribute("lunit","mm"));
206   solidsElement->appendChild(boxElement);
207}
208
209void G4GDMLWriteSolids::
210ConeWrite(xercesc::DOMElement* solidsElement, const G4Cons* const cone)
211{
212   const G4String& name = GenerateName(cone->GetName(),cone);
213
214   xercesc::DOMElement* coneElement = NewElement("cone");
215   coneElement->setAttributeNode(NewAttribute("name",name));
216   coneElement->
217     setAttributeNode(NewAttribute("rmin1",cone->GetInnerRadiusMinusZ()/mm));
218   coneElement->
219     setAttributeNode(NewAttribute("rmax1",cone->GetOuterRadiusMinusZ()/mm));
220   coneElement->
221     setAttributeNode(NewAttribute("rmin2",cone->GetInnerRadiusPlusZ()/mm));
222   coneElement->
223     setAttributeNode(NewAttribute("rmax2",cone->GetOuterRadiusPlusZ()/mm));
224   coneElement->
225     setAttributeNode(NewAttribute("z",2.0*cone->GetZHalfLength()/mm));
226   coneElement->
227     setAttributeNode(NewAttribute("startphi",cone->GetStartPhiAngle()/degree));
228   coneElement->
229     setAttributeNode(NewAttribute("deltaphi",cone->GetDeltaPhiAngle()/degree));
230   coneElement->setAttributeNode(NewAttribute("aunit","deg"));
231   coneElement->setAttributeNode(NewAttribute("lunit","mm"));
232   solidsElement->appendChild(coneElement);
233}
234
235void G4GDMLWriteSolids::
236ElconeWrite(xercesc::DOMElement* solidsElement,
237            const G4EllipticalCone* const elcone)
238{
239   const G4String& name = GenerateName(elcone->GetName(),elcone);
240
241   xercesc::DOMElement* elconeElement = NewElement("elcone");
242   elconeElement->setAttributeNode(NewAttribute("name",name));
243   elconeElement->setAttributeNode(NewAttribute("dx",elcone->GetSemiAxisX()/mm));
244   elconeElement->setAttributeNode(NewAttribute("dy",elcone->GetSemiAxisY()/mm));
245   elconeElement->setAttributeNode(NewAttribute("zmax",elcone->GetZMax()/mm));
246   elconeElement->setAttributeNode(NewAttribute("zcut",elcone->GetZTopCut()/mm));
247   elconeElement->setAttributeNode(NewAttribute("lunit","mm"));
248   solidsElement->appendChild(elconeElement);
249}
250
251void G4GDMLWriteSolids::
252EllipsoidWrite(xercesc::DOMElement* solidsElement,
253               const G4Ellipsoid* const ellipsoid)
254{
255   const G4String& name = GenerateName(ellipsoid->GetName(),ellipsoid);
256
257   xercesc::DOMElement* ellipsoidElement = NewElement("ellipsoid");
258   ellipsoidElement->setAttributeNode(NewAttribute("name",name));
259   ellipsoidElement->
260     setAttributeNode(NewAttribute("ax",ellipsoid->GetSemiAxisMax(0)/mm));
261   ellipsoidElement->
262     setAttributeNode(NewAttribute("by",ellipsoid->GetSemiAxisMax(1)/mm));
263   ellipsoidElement->
264     setAttributeNode(NewAttribute("cz",ellipsoid->GetSemiAxisMax(2)/mm));
265   ellipsoidElement->
266     setAttributeNode(NewAttribute("zcut1",ellipsoid->GetZBottomCut()/mm));
267   ellipsoidElement->
268     setAttributeNode(NewAttribute("zcut2",ellipsoid->GetZTopCut()/mm));
269   ellipsoidElement->
270     setAttributeNode(NewAttribute("lunit","mm"));
271   solidsElement->appendChild(ellipsoidElement);
272}
273
274void G4GDMLWriteSolids::
275EltubeWrite(xercesc::DOMElement* solidsElement,
276            const G4EllipticalTube* const eltube)
277{
278   const G4String& name = GenerateName(eltube->GetName(),eltube);
279
280   xercesc::DOMElement* eltubeElement = NewElement("eltube");
281   eltubeElement->setAttributeNode(NewAttribute("name",name));
282   eltubeElement->setAttributeNode(NewAttribute("dx",eltube->GetDx()/mm));
283   eltubeElement->setAttributeNode(NewAttribute("dy",eltube->GetDy()/mm));
284   eltubeElement->setAttributeNode(NewAttribute("dz",eltube->GetDz()/mm));
285   eltubeElement->setAttributeNode(NewAttribute("lunit","mm"));
286   solidsElement->appendChild(eltubeElement);
287}
288
289void G4GDMLWriteSolids::
290XtruWrite(xercesc::DOMElement* solidsElement,
291          const G4ExtrudedSolid* const xtru)
292{
293   const G4String& name = GenerateName(xtru->GetName(),xtru);
294
295   xercesc::DOMElement* xtruElement = NewElement("xtru");
296   xtruElement->setAttributeNode(NewAttribute("name",name));
297   xtruElement->setAttributeNode(NewAttribute("lunit","mm"));
298   solidsElement->appendChild(xtruElement);
299
300   const G4int NumVertex = xtru->GetNofVertices();
301   
302   for (G4int i=0;i<NumVertex;i++)
303   {
304      xercesc::DOMElement* twoDimVertexElement = NewElement("twoDimVertex");
305      xtruElement->appendChild(twoDimVertexElement);
306
307      const G4TwoVector& vertex = xtru->GetVertex(i);
308
309      twoDimVertexElement->setAttributeNode(NewAttribute("x",vertex.x()/mm));
310      twoDimVertexElement->setAttributeNode(NewAttribute("y",vertex.y()/mm));
311   }
312
313   const G4int NumSection = xtru->GetNofZSections();
314
315   for (G4int i=0;i<NumSection;i++)
316   {
317      xercesc::DOMElement* sectionElement = NewElement("section");
318      xtruElement->appendChild(sectionElement);
319
320      const G4ExtrudedSolid::ZSection section = xtru->GetZSection(i);
321
322      sectionElement->setAttributeNode(NewAttribute("zOrder",i));
323      sectionElement->setAttributeNode(NewAttribute("zPosition",section.fZ/mm));
324      sectionElement->
325        setAttributeNode(NewAttribute("xOffset",section.fOffset.x()/mm));
326      sectionElement->
327        setAttributeNode(NewAttribute("yOffset",section.fOffset.y()/mm));
328      sectionElement->
329        setAttributeNode(NewAttribute("scalingFactor",section.fScale));
330   }
331}
332
333void G4GDMLWriteSolids::
334HypeWrite(xercesc::DOMElement* solidsElement, const G4Hype* const hype)
335{
336   const G4String& name = GenerateName(hype->GetName(),hype);
337
338   xercesc::DOMElement* hypeElement = NewElement("hype");
339   hypeElement->setAttributeNode(NewAttribute("name",name));
340   hypeElement->setAttributeNode(NewAttribute("rmin",
341                hype->GetInnerRadius()/mm));
342   hypeElement->setAttributeNode(NewAttribute("rmax",
343                hype->GetOuterRadius()/mm));
344   hypeElement->setAttributeNode(NewAttribute("inst",
345                hype->GetInnerStereo()/degree));
346   hypeElement->setAttributeNode(NewAttribute("outst",
347                hype->GetOuterStereo()/degree));
348   hypeElement->setAttributeNode(NewAttribute("z",
349                2.0*hype->GetZHalfLength()/mm));
350   hypeElement->setAttributeNode(NewAttribute("aunit","deg"));
351   hypeElement->setAttributeNode(NewAttribute("lunit","mm"));
352   solidsElement->appendChild(hypeElement);
353}
354
355void G4GDMLWriteSolids::
356OrbWrite(xercesc::DOMElement* solidsElement, const G4Orb* const orb)
357{
358   const G4String& name = GenerateName(orb->GetName(),orb);
359
360   xercesc::DOMElement* orbElement = NewElement("orb");
361   orbElement->setAttributeNode(NewAttribute("name",name));
362   orbElement->setAttributeNode(NewAttribute("r",orb->GetRadius()/mm));
363   orbElement->setAttributeNode(NewAttribute("lunit","mm"));
364   solidsElement->appendChild(orbElement);
365}
366
367void G4GDMLWriteSolids::
368ParaWrite(xercesc::DOMElement* solidsElement, const G4Para* const para)
369{
370   const G4String& name = GenerateName(para->GetName(),para);
371
372   const G4ThreeVector simaxis = para->GetSymAxis();
373   const G4double alpha = std::atan(para->GetTanAlpha());
374   const G4double theta = std::acos(simaxis.z());
375   const G4double phi = (simaxis.z() != 1.0)
376                      ? (std::atan(simaxis.y()/simaxis.x())) : (0.0);
377
378   xercesc::DOMElement* paraElement = NewElement("para");
379   paraElement->setAttributeNode(NewAttribute("name",name));
380   paraElement->setAttributeNode(NewAttribute("x",
381                2.0*para->GetXHalfLength()/mm));
382   paraElement->setAttributeNode(NewAttribute("y",
383                2.0*para->GetYHalfLength()/mm));
384   paraElement->setAttributeNode(NewAttribute("z",
385                2.0*para->GetZHalfLength()/mm));
386   paraElement->setAttributeNode(NewAttribute("alpha",alpha/degree));
387   paraElement->setAttributeNode(NewAttribute("theta",theta/degree));
388   paraElement->setAttributeNode(NewAttribute("phi",phi/degree));
389   paraElement->setAttributeNode(NewAttribute("aunit","deg"));
390   paraElement->setAttributeNode(NewAttribute("lunit","mm"));
391   solidsElement->appendChild(paraElement);
392}
393
394void G4GDMLWriteSolids::
395ParaboloidWrite(xercesc::DOMElement* solidsElement,
396                const G4Paraboloid* const paraboloid)
397{
398   const G4String& name = GenerateName(paraboloid->GetName(),paraboloid);
399
400   xercesc::DOMElement* paraboloidElement = NewElement("paraboloid");
401   paraboloidElement->setAttributeNode(NewAttribute("name",name));
402   paraboloidElement->setAttributeNode(NewAttribute("rlo",
403                      paraboloid->GetRadiusMinusZ()/mm));
404   paraboloidElement->setAttributeNode(NewAttribute("rhi",
405                      paraboloid->GetRadiusPlusZ()/mm));
406   paraboloidElement->setAttributeNode(NewAttribute("dz",
407                      paraboloid->GetZHalfLength()/mm));
408   paraboloidElement->setAttributeNode(NewAttribute("lunit","mm"));
409   solidsElement->appendChild(paraboloidElement);
410}
411
412void G4GDMLWriteSolids::
413PolyconeWrite(xercesc::DOMElement* solidsElement,
414              const G4Polycone* const polycone)
415{
416   const G4String& name = GenerateName(polycone->GetName(),polycone);
417
418   xercesc::DOMElement* polyconeElement = NewElement("polycone");
419   polyconeElement->setAttributeNode(NewAttribute("name",name));
420   polyconeElement->setAttributeNode(NewAttribute("startphi",
421                    polycone->GetOriginalParameters()->Start_angle/degree));
422   polyconeElement->setAttributeNode(NewAttribute("deltaphi",
423                    polycone->GetOriginalParameters()->Opening_angle/degree));
424   polyconeElement->setAttributeNode(NewAttribute("aunit","deg"));
425   polyconeElement->setAttributeNode(NewAttribute("lunit","mm"));
426   solidsElement->appendChild(polyconeElement);
427
428   const size_t num_zplanes = polycone->GetOriginalParameters()->Num_z_planes;
429   const G4double* z_array = polycone->GetOriginalParameters()->Z_values;
430   const G4double* rmin_array = polycone->GetOriginalParameters()->Rmin;
431   const G4double* rmax_array = polycone->GetOriginalParameters()->Rmax;
432
433   for (size_t i=0; i<num_zplanes; i++)
434   {
435      ZplaneWrite(polyconeElement,z_array[i],rmin_array[i],rmax_array[i]);
436   }
437}
438
439void G4GDMLWriteSolids::
440PolyhedraWrite(xercesc::DOMElement* solidsElement,
441               const G4Polyhedra* const polyhedra)
442{
443   const G4String& name = GenerateName(polyhedra->GetName(),polyhedra);
444
445   xercesc::DOMElement* polyhedraElement = NewElement("polyhedra");
446   polyhedraElement->setAttributeNode(NewAttribute("name",name));
447   polyhedraElement->setAttributeNode(NewAttribute("startphi",
448                     polyhedra->GetOriginalParameters()->Start_angle/degree));
449   polyhedraElement->setAttributeNode(NewAttribute("deltaphi",
450                     polyhedra->GetOriginalParameters()->Opening_angle/degree));
451   polyhedraElement->setAttributeNode(NewAttribute("numsides",
452                     polyhedra->GetOriginalParameters()->numSide));
453   polyhedraElement->setAttributeNode(NewAttribute("aunit","deg"));
454   polyhedraElement->setAttributeNode(NewAttribute("lunit","mm"));
455   solidsElement->appendChild(polyhedraElement);
456
457   const size_t num_zplanes = polyhedra->GetOriginalParameters()->Num_z_planes;
458   const G4double* z_array = polyhedra->GetOriginalParameters()->Z_values;
459   const G4double* rmin_array = polyhedra->GetOriginalParameters()->Rmin;
460   const G4double* rmax_array = polyhedra->GetOriginalParameters()->Rmax;
461
462   const G4double convertRad =
463         std::cos(0.5*polyhedra->GetOriginalParameters()->Opening_angle
464       / polyhedra->GetOriginalParameters()->numSide);
465
466   for (size_t i=0;i<num_zplanes;i++)
467   {
468      ZplaneWrite(polyhedraElement,z_array[i],
469                  rmin_array[i]*convertRad, rmax_array[i]*convertRad);
470   }
471}
472
473void G4GDMLWriteSolids::
474SphereWrite(xercesc::DOMElement* solidsElement, const G4Sphere* const sphere)
475{
476   const G4String& name = GenerateName(sphere->GetName(),sphere);
477
478   xercesc::DOMElement* sphereElement = NewElement("sphere");
479   sphereElement->setAttributeNode(NewAttribute("name",name));
480   sphereElement->setAttributeNode(NewAttribute("rmin",
481                  sphere->GetInsideRadius()/mm));
482   sphereElement->setAttributeNode(NewAttribute("rmax",
483                  sphere->GetOuterRadius()/mm));
484   sphereElement->setAttributeNode(NewAttribute("startphi",
485                  sphere->GetStartPhiAngle()/degree));
486   sphereElement->setAttributeNode(NewAttribute("deltaphi",
487                  sphere->GetDeltaPhiAngle()/degree));
488   sphereElement->setAttributeNode(NewAttribute("starttheta",
489                  sphere->GetStartThetaAngle()/degree));
490   sphereElement->setAttributeNode(NewAttribute("deltatheta",
491                  sphere->GetDeltaThetaAngle()/degree));
492   sphereElement->setAttributeNode(NewAttribute("aunit","deg"));
493   sphereElement->setAttributeNode(NewAttribute("lunit","mm"));
494   solidsElement->appendChild(sphereElement);
495}
496
497void G4GDMLWriteSolids::
498TessellatedWrite(xercesc::DOMElement* solidsElement,
499                 const G4TessellatedSolid* const tessellated)
500{
501   const G4String& solid_name = tessellated->GetName();
502   const G4String& name = GenerateName(solid_name, tessellated);
503
504   xercesc::DOMElement* tessellatedElement = NewElement("tessellated");
505   tessellatedElement->setAttributeNode(NewAttribute("name",name));
506   tessellatedElement->setAttributeNode(NewAttribute("aunit","deg"));
507   tessellatedElement->setAttributeNode(NewAttribute("lunit","mm"));
508   solidsElement->appendChild(tessellatedElement);
509
510   std::map<G4ThreeVector, G4String> vertexMap;
511
512   const size_t NumFacets = tessellated->GetNumberOfFacets();
513   size_t NumVertex = 0;
514   
515   for (size_t i=0;i<NumFacets;i++)
516   {
517      const G4VFacet* facet = tessellated->GetFacet(i);
518      const size_t NumVertexPerFacet = facet->GetNumberOfVertices();
519
520      G4String FacetTag;
521     
522      if (NumVertexPerFacet==3) { FacetTag="triangular"; } else
523      if (NumVertexPerFacet==4) { FacetTag="quadrangular"; }
524      else
525      {
526        G4Exception("G4GDMLWriteSolids::TessellatedWrite()", "InvalidSetup",
527                    FatalException, "Facet should contain 3 or 4 vertices!");
528      }
529
530      xercesc::DOMElement* facetElement = NewElement(FacetTag);
531      tessellatedElement->appendChild(facetElement);
532
533      for (size_t j=0; j<NumVertexPerFacet; j++)
534      {
535         std::stringstream name_stream;
536         std::stringstream ref_stream;
537
538         name_stream << "vertex" << (j+1);
539         ref_stream << solid_name << "_v" << NumVertex;
540
541         const G4String& name = name_stream.str();  // facet's tag variable
542         G4String ref = ref_stream.str();     // vertex tag to be associated
543
544         // Now search for the existance of the current vertex in the
545         // map of cached vertices. If existing, do NOT store it as
546         // position in the GDML file, so avoiding duplication; otherwise
547         // cache it in the local map and add it as position in the
548         // "define" section of the GDML file.
549
550         const G4ThreeVector& vertex = facet->GetVertex(j);
551
552         if(vertexMap.find(vertex) != vertexMap.end())  // Vertex is cached
553         {
554           ref = vertexMap[vertex];     // Set the proper tag for it
555         }
556         else                                           // Vertex not found
557         {
558           vertexMap.insert(std::make_pair(vertex,ref)); // Cache vertex and ...
559           AddPosition(ref, vertex);    // ... add it to define section!
560           NumVertex++;
561         }
562
563         // Now create association of the vertex with its facet
564         //
565         facetElement->setAttributeNode(NewAttribute(name,ref));
566      }
567   }
568}
569
570void G4GDMLWriteSolids::
571TetWrite(xercesc::DOMElement* solidsElement, const G4Tet* const tet)
572{
573   const G4String& solid_name = tet->GetName();
574   const G4String& name = GenerateName(solid_name, tet);
575
576   std::vector<G4ThreeVector> vertexList = tet->GetVertices();
577
578   xercesc::DOMElement* tetElement = NewElement("tet");
579   tetElement->setAttributeNode(NewAttribute("name",name));
580   tetElement->setAttributeNode(NewAttribute("vertex1",solid_name+"_v1"));
581   tetElement->setAttributeNode(NewAttribute("vertex2",solid_name+"_v2"));
582   tetElement->setAttributeNode(NewAttribute("vertex3",solid_name+"_v3"));
583   tetElement->setAttributeNode(NewAttribute("vertex4",solid_name+"_v4"));
584   tetElement->setAttributeNode(NewAttribute("lunit","mm"));
585   solidsElement->appendChild(tetElement);
586
587   AddPosition(solid_name+"_v1",vertexList[0]);
588   AddPosition(solid_name+"_v2",vertexList[1]);
589   AddPosition(solid_name+"_v3",vertexList[2]);
590   AddPosition(solid_name+"_v4",vertexList[3]);
591}
592
593void G4GDMLWriteSolids::
594TorusWrite(xercesc::DOMElement* solidsElement, const G4Torus* const torus)
595{
596   const G4String& name = GenerateName(torus->GetName(),torus);
597
598   xercesc::DOMElement* torusElement = NewElement("torus");
599   torusElement->setAttributeNode(NewAttribute("name",name));
600   torusElement->setAttributeNode(NewAttribute("rmin",torus->GetRmin()/mm));
601   torusElement->setAttributeNode(NewAttribute("rmax",torus->GetRmax()/mm));
602   torusElement->setAttributeNode(NewAttribute("rtor",torus->GetRtor()/mm));
603   torusElement->
604     setAttributeNode(NewAttribute("startphi",torus->GetSPhi()/degree));
605   torusElement->
606     setAttributeNode(NewAttribute("deltaphi",torus->GetDPhi()/degree));
607   torusElement->setAttributeNode(NewAttribute("aunit","deg"));
608   torusElement->setAttributeNode(NewAttribute("lunit","mm"));
609   solidsElement->appendChild(torusElement);
610}
611
612void G4GDMLWriteSolids::
613GenTrapWrite(xercesc::DOMElement* solidsElement,
614             const G4GenericTrap* const gtrap)
615{
616   const G4String& name = GenerateName(gtrap->GetName(),gtrap);
617
618   std::vector<G4TwoVector> vertices = gtrap->GetVertices();
619
620   xercesc::DOMElement* gtrapElement = NewElement("arb8");
621   gtrapElement->setAttributeNode(NewAttribute("name",name));
622   gtrapElement->setAttributeNode(NewAttribute("dz",
623                                           gtrap->GetZHalfLength()/mm));
624   gtrapElement->setAttributeNode(NewAttribute("v1x", vertices[0].x()));
625   gtrapElement->setAttributeNode(NewAttribute("v1y", vertices[0].y()));
626   gtrapElement->setAttributeNode(NewAttribute("v2x", vertices[1].x()));
627   gtrapElement->setAttributeNode(NewAttribute("v2y", vertices[1].y()));
628   gtrapElement->setAttributeNode(NewAttribute("v3x", vertices[2].x()));
629   gtrapElement->setAttributeNode(NewAttribute("v3y", vertices[2].y()));
630   gtrapElement->setAttributeNode(NewAttribute("v4x", vertices[3].x()));
631   gtrapElement->setAttributeNode(NewAttribute("v4y", vertices[3].y()));
632   gtrapElement->setAttributeNode(NewAttribute("v5x", vertices[4].x()));
633   gtrapElement->setAttributeNode(NewAttribute("v5y", vertices[4].y()));
634   gtrapElement->setAttributeNode(NewAttribute("v6x", vertices[5].x()));
635   gtrapElement->setAttributeNode(NewAttribute("v6y", vertices[5].y()));
636   gtrapElement->setAttributeNode(NewAttribute("v7x", vertices[6].x()));
637   gtrapElement->setAttributeNode(NewAttribute("v7y", vertices[6].y()));
638   gtrapElement->setAttributeNode(NewAttribute("v8x", vertices[7].x()));
639   gtrapElement->setAttributeNode(NewAttribute("v8y", vertices[7].y()));
640   gtrapElement->setAttributeNode(NewAttribute("lunit","mm"));
641   solidsElement->appendChild(gtrapElement);
642}
643
644void G4GDMLWriteSolids::
645TrapWrite(xercesc::DOMElement* solidsElement, const G4Trap* const trap)
646{
647   const G4String& name = GenerateName(trap->GetName(),trap);
648
649   const G4ThreeVector& simaxis = trap->GetSymAxis();
650   const G4double phi = (simaxis.z() != 1.0)
651                      ? (std::atan(simaxis.y()/simaxis.x())) : (0.0);
652   const G4double theta = std::acos(simaxis.z());
653   const G4double alpha1 = std::atan(trap->GetTanAlpha1());
654   const G4double alpha2 = std::atan(trap->GetTanAlpha2());
655
656   xercesc::DOMElement* trapElement = NewElement("trap");
657   trapElement->setAttributeNode(NewAttribute("name",name));
658   trapElement->setAttributeNode(NewAttribute("z",
659                2.0*trap->GetZHalfLength()/mm));
660   trapElement->setAttributeNode(NewAttribute("theta",theta/degree));
661   trapElement->setAttributeNode(NewAttribute("phi",phi/degree));
662   trapElement->setAttributeNode(NewAttribute("y1",
663                2.0*trap->GetYHalfLength1()/mm));
664   trapElement->setAttributeNode(NewAttribute("x1",
665                2.0*trap->GetXHalfLength1()/mm));
666   trapElement->setAttributeNode(NewAttribute("x2",
667                2.0*trap->GetXHalfLength2()/mm));
668   trapElement->setAttributeNode(NewAttribute("alpha1",alpha1/degree));
669   trapElement->setAttributeNode(NewAttribute("y2",
670                2.0*trap->GetYHalfLength2()/mm));
671   trapElement->setAttributeNode(NewAttribute("x3",
672                2.0*trap->GetXHalfLength3()/mm));
673   trapElement->setAttributeNode(NewAttribute("x4",
674                2.0*trap->GetXHalfLength4()/mm));
675   trapElement->setAttributeNode(NewAttribute("alpha2",alpha2/degree));
676   trapElement->setAttributeNode(NewAttribute("aunit","deg"));
677   trapElement->setAttributeNode(NewAttribute("lunit","mm"));
678   solidsElement->appendChild(trapElement);
679}
680
681void G4GDMLWriteSolids::
682TrdWrite(xercesc::DOMElement* solidsElement, const G4Trd* const trd)
683{
684   const G4String& name = GenerateName(trd->GetName(),trd);
685
686   xercesc::DOMElement* trdElement = NewElement("trd");
687   trdElement->setAttributeNode(NewAttribute("name",name));
688   trdElement->setAttributeNode(NewAttribute("x1",
689               2.0*trd->GetXHalfLength1()/mm));
690   trdElement->setAttributeNode(NewAttribute("x2",
691               2.0*trd->GetXHalfLength2()/mm));
692   trdElement->setAttributeNode(NewAttribute("y1",
693               2.0*trd->GetYHalfLength1()/mm));
694   trdElement->setAttributeNode(NewAttribute("y2",
695               2.0*trd->GetYHalfLength2()/mm));
696   trdElement->setAttributeNode(NewAttribute("z",
697               2.0*trd->GetZHalfLength()/mm));
698   trdElement->setAttributeNode(NewAttribute("lunit","mm"));
699   solidsElement->appendChild(trdElement);
700}
701
702void G4GDMLWriteSolids::
703TubeWrite(xercesc::DOMElement* solidsElement, const G4Tubs* const tube)
704{
705   const G4String& name = GenerateName(tube->GetName(),tube);
706
707   xercesc::DOMElement* tubeElement = NewElement("tube");
708   tubeElement->setAttributeNode(NewAttribute("name",name));
709   tubeElement->setAttributeNode(NewAttribute("rmin",
710                tube->GetInnerRadius()/mm));
711   tubeElement->setAttributeNode(NewAttribute("rmax",
712                tube->GetOuterRadius()/mm));
713   tubeElement->setAttributeNode(NewAttribute("z",
714                2.0*tube->GetZHalfLength()/mm));
715   tubeElement->setAttributeNode(NewAttribute("startphi",
716                tube->GetStartPhiAngle()/degree));
717   tubeElement->setAttributeNode(NewAttribute("deltaphi",
718                tube->GetDeltaPhiAngle()/degree));
719   tubeElement->setAttributeNode(NewAttribute("aunit","deg"));
720   tubeElement->setAttributeNode(NewAttribute("lunit","mm"));
721   solidsElement->appendChild(tubeElement);
722}
723
724void G4GDMLWriteSolids::
725TwistedboxWrite(xercesc::DOMElement* solidsElement,
726                const G4TwistedBox* const twistedbox)
727{
728   const G4String& name = GenerateName(twistedbox->GetName(),twistedbox);
729
730   xercesc::DOMElement* twistedboxElement = NewElement("twistedbox");
731   twistedboxElement->setAttributeNode(NewAttribute("name",name));
732   twistedboxElement->setAttributeNode(NewAttribute("x",
733                      2.0*twistedbox->GetXHalfLength()/mm));
734   twistedboxElement->setAttributeNode(NewAttribute("y",
735                      2.0*twistedbox->GetYHalfLength()/mm));
736   twistedboxElement->setAttributeNode(NewAttribute("z",
737                      2.0*twistedbox->GetZHalfLength()/mm));
738   twistedboxElement->setAttributeNode(NewAttribute("PhiTwist",
739                      twistedbox->GetPhiTwist()/degree));
740   twistedboxElement->setAttributeNode(NewAttribute("aunit","deg"));
741   twistedboxElement->setAttributeNode(NewAttribute("lunit","mm"));
742   solidsElement->appendChild(twistedboxElement);
743}
744
745void G4GDMLWriteSolids::
746TwistedtrapWrite(xercesc::DOMElement* solidsElement,
747                 const G4TwistedTrap* const twistedtrap)
748{
749   const G4String& name = GenerateName(twistedtrap->GetName(),twistedtrap);
750
751   xercesc::DOMElement* twistedtrapElement = NewElement("twistedtrap");
752   twistedtrapElement->setAttributeNode(NewAttribute("name",name));
753   twistedtrapElement->setAttributeNode(NewAttribute("y1",
754                       2.0*twistedtrap->GetY1HalfLength()/mm));
755   twistedtrapElement->setAttributeNode(NewAttribute("x1",
756                       2.0*twistedtrap->GetX1HalfLength()/mm));
757   twistedtrapElement->setAttributeNode(NewAttribute("x2",
758                       2.0*twistedtrap->GetX2HalfLength()/mm));
759   twistedtrapElement->setAttributeNode(NewAttribute("y2",
760                       2.0*twistedtrap->GetY2HalfLength()/mm));
761   twistedtrapElement->setAttributeNode(NewAttribute("x3",
762                       2.0*twistedtrap->GetX3HalfLength()/mm));
763   twistedtrapElement->setAttributeNode(NewAttribute("x4",
764                       2.0*twistedtrap->GetX4HalfLength()/mm));
765   twistedtrapElement->setAttributeNode(NewAttribute("z",
766                       2.0*twistedtrap->GetZHalfLength()/mm));
767   twistedtrapElement->setAttributeNode(NewAttribute("Alph",
768                       twistedtrap->GetTiltAngleAlpha()/degree));
769   twistedtrapElement->setAttributeNode(NewAttribute("Theta",
770                       twistedtrap->GetPolarAngleTheta()/degree));
771   twistedtrapElement->setAttributeNode(NewAttribute("Phi",
772                       twistedtrap->GetAzimuthalAnglePhi()/degree));
773   twistedtrapElement->setAttributeNode(NewAttribute("PhiTwist",
774                       twistedtrap->GetPhiTwist()/degree));
775   twistedtrapElement->setAttributeNode(NewAttribute("aunit","deg"));
776   twistedtrapElement->setAttributeNode(NewAttribute("lunit","mm"));
777   
778   solidsElement->appendChild(twistedtrapElement);
779}
780
781void G4GDMLWriteSolids::
782TwistedtrdWrite(xercesc::DOMElement* solidsElement,
783                const G4TwistedTrd* const twistedtrd)
784{
785   const G4String& name = GenerateName(twistedtrd->GetName(),twistedtrd);
786
787   xercesc::DOMElement* twistedtrdElement = NewElement("twistedtrd");
788   twistedtrdElement->setAttributeNode(NewAttribute("name",name));
789   twistedtrdElement->setAttributeNode(NewAttribute("x1",
790                      2.0*twistedtrd->GetX1HalfLength()/mm));
791   twistedtrdElement->setAttributeNode(NewAttribute("x2",
792                      2.0*twistedtrd->GetX2HalfLength()/mm));
793   twistedtrdElement->setAttributeNode(NewAttribute("y1",
794                      2.0*twistedtrd->GetY1HalfLength()/mm));
795   twistedtrdElement->setAttributeNode(NewAttribute("y2",
796                      2.0*twistedtrd->GetY2HalfLength()/mm));
797   twistedtrdElement->setAttributeNode(NewAttribute("z",
798                      2.0*twistedtrd->GetZHalfLength()/mm));
799   twistedtrdElement->setAttributeNode(NewAttribute("PhiTwist",
800                      twistedtrd->GetPhiTwist()/degree));
801   twistedtrdElement->setAttributeNode(NewAttribute("aunit","deg"));
802   twistedtrdElement->setAttributeNode(NewAttribute("lunit","mm"));
803   solidsElement->appendChild(twistedtrdElement);
804}
805
806void G4GDMLWriteSolids::
807TwistedtubsWrite(xercesc::DOMElement* solidsElement,
808                 const G4TwistedTubs* const twistedtubs)
809{
810   const G4String& name = GenerateName(twistedtubs->GetName(),twistedtubs);
811
812   xercesc::DOMElement* twistedtubsElement = NewElement("twistedtubs");
813   twistedtubsElement->setAttributeNode(NewAttribute("name",name));
814   twistedtubsElement->setAttributeNode(NewAttribute("twistedangle",
815                       twistedtubs->GetPhiTwist()/degree));
816   twistedtubsElement->setAttributeNode(NewAttribute("endinnerrad",
817                       twistedtubs->GetInnerRadius()/mm));
818   twistedtubsElement->setAttributeNode(NewAttribute("endouterrad",
819                       twistedtubs->GetOuterRadius()/mm));
820   twistedtubsElement->setAttributeNode(NewAttribute("zlen",
821                       2.0*twistedtubs->GetZHalfLength()/mm));
822   twistedtubsElement->setAttributeNode(NewAttribute("phi",
823                       twistedtubs->GetDPhi()/degree));
824   twistedtubsElement->setAttributeNode(NewAttribute("aunit","deg"));
825   twistedtubsElement->setAttributeNode(NewAttribute("lunit","mm"));
826   solidsElement->appendChild(twistedtubsElement);
827}
828
829void G4GDMLWriteSolids::
830ZplaneWrite(xercesc::DOMElement* element, const G4double& z,
831            const G4double& rmin, const G4double& rmax)
832{
833   xercesc::DOMElement* zplaneElement = NewElement("zplane");
834   zplaneElement->setAttributeNode(NewAttribute("z",z/mm));
835   zplaneElement->setAttributeNode(NewAttribute("rmin",rmin/mm));
836   zplaneElement->setAttributeNode(NewAttribute("rmax",rmax/mm));
837   element->appendChild(zplaneElement);
838}
839
840void G4GDMLWriteSolids::
841OpticalSurfaceWrite(xercesc::DOMElement* solidsElement,
842                    const G4OpticalSurface* const surf)
843{
844   xercesc::DOMElement* optElement = NewElement("opticalsurface");
845   G4OpticalSurfaceModel smodel = surf->GetModel();
846   G4double sval = (smodel==glisur) ? surf->GetPolish() : surf->GetSigmaAlpha();
847
848   optElement->setAttributeNode(NewAttribute("name", surf->GetName()));
849   optElement->setAttributeNode(NewAttribute("model", smodel));
850   optElement->setAttributeNode(NewAttribute("finish", surf->GetFinish()));
851   optElement->setAttributeNode(NewAttribute("type", surf->GetType()));
852   optElement->setAttributeNode(NewAttribute("value", sval));
853
854   solidsElement->appendChild(optElement);
855}
856
857void G4GDMLWriteSolids::SolidsWrite(xercesc::DOMElement* gdmlElement)
858{
859   G4cout << "G4GDML: Writing solids..." << G4endl;
860
861   solidsElement = NewElement("solids");
862   gdmlElement->appendChild(solidsElement);
863
864   solidList.clear();
865}
866
867void G4GDMLWriteSolids::AddSolid(const G4VSolid* const solidPtr)
868{
869   for (size_t i=0; i<solidList.size(); i++)   // Check if solid is
870   {                                           // already in the list!
871      if (solidList[i] == solidPtr)  { return; }
872   }
873
874   solidList.push_back(solidPtr);
875
876   if (const G4BooleanSolid* const booleanPtr
877     = dynamic_cast<const G4BooleanSolid*>(solidPtr))
878     { BooleanWrite(solidsElement,booleanPtr); } else
879   if (const G4Box* const boxPtr
880     = dynamic_cast<const G4Box*>(solidPtr))
881     { BoxWrite(solidsElement,boxPtr); } else
882   if (const G4Cons* const conePtr
883     = dynamic_cast<const G4Cons*>(solidPtr))
884     { ConeWrite(solidsElement,conePtr); } else
885   if (const G4EllipticalCone* const elconePtr
886     = dynamic_cast<const G4EllipticalCone*>(solidPtr))
887     { ElconeWrite(solidsElement,elconePtr); } else
888   if (const G4Ellipsoid* const ellipsoidPtr
889     = dynamic_cast<const G4Ellipsoid*>(solidPtr))
890     { EllipsoidWrite(solidsElement,ellipsoidPtr); } else
891   if (const G4EllipticalTube* const eltubePtr
892     = dynamic_cast<const G4EllipticalTube*>(solidPtr))
893     { EltubeWrite(solidsElement,eltubePtr); } else
894   if (const G4ExtrudedSolid* const xtruPtr
895     = dynamic_cast<const G4ExtrudedSolid*>(solidPtr))
896     { XtruWrite(solidsElement,xtruPtr); } else
897   if (const G4Hype* const hypePtr
898     = dynamic_cast<const G4Hype*>(solidPtr))
899     { HypeWrite(solidsElement,hypePtr); } else
900   if (const G4Orb* const orbPtr
901     = dynamic_cast<const G4Orb*>(solidPtr))
902     { OrbWrite(solidsElement,orbPtr); } else
903   if (const G4Para* const paraPtr
904     = dynamic_cast<const G4Para*>(solidPtr))
905     { ParaWrite(solidsElement,paraPtr); } else
906   if (const G4Paraboloid* const paraboloidPtr
907     = dynamic_cast<const G4Paraboloid*>(solidPtr))
908     { ParaboloidWrite(solidsElement,paraboloidPtr); } else
909   if (const G4Polycone* const polyconePtr
910     = dynamic_cast<const G4Polycone*>(solidPtr))
911     { PolyconeWrite(solidsElement,polyconePtr); } else
912   if (const G4Polyhedra* const polyhedraPtr
913     = dynamic_cast<const G4Polyhedra*>(solidPtr))
914     { PolyhedraWrite(solidsElement,polyhedraPtr); } else
915   if (const G4Sphere* const spherePtr
916     = dynamic_cast<const G4Sphere*>(solidPtr))
917     { SphereWrite(solidsElement,spherePtr); } else
918   if (const G4TessellatedSolid* const tessellatedPtr
919     = dynamic_cast<const G4TessellatedSolid*>(solidPtr))
920     { TessellatedWrite(solidsElement,tessellatedPtr); } else
921   if (const G4Tet* const tetPtr
922     = dynamic_cast<const G4Tet*>(solidPtr))
923     { TetWrite(solidsElement,tetPtr); } else
924   if (const G4Torus* const torusPtr
925     = dynamic_cast<const G4Torus*>(solidPtr))
926     { TorusWrite(solidsElement,torusPtr); } else
927   if (const G4GenericTrap* const gtrapPtr
928     = dynamic_cast<const G4GenericTrap*>(solidPtr))
929     { GenTrapWrite(solidsElement,gtrapPtr); } else
930   if (const G4Trap* const trapPtr
931     = dynamic_cast<const G4Trap*>(solidPtr))
932     { TrapWrite(solidsElement,trapPtr); } else
933   if (const G4Trd* const trdPtr
934     = dynamic_cast<const G4Trd*>(solidPtr))
935     { TrdWrite(solidsElement,trdPtr); } else
936   if (const G4Tubs* const tubePtr
937     = dynamic_cast<const G4Tubs*>(solidPtr))
938     { TubeWrite(solidsElement,tubePtr); } else
939   if (const G4TwistedBox* const twistedboxPtr
940     = dynamic_cast<const G4TwistedBox*>(solidPtr))
941     { TwistedboxWrite(solidsElement,twistedboxPtr); } else
942   if (const G4TwistedTrap* const twistedtrapPtr
943     = dynamic_cast<const G4TwistedTrap*>(solidPtr))
944     { TwistedtrapWrite(solidsElement,twistedtrapPtr); } else
945   if (const G4TwistedTrd* const twistedtrdPtr
946     = dynamic_cast<const G4TwistedTrd*>(solidPtr))
947     { TwistedtrdWrite(solidsElement,twistedtrdPtr); } else
948   if (const G4TwistedTubs* const twistedtubsPtr
949     = dynamic_cast<const G4TwistedTubs*>(solidPtr))
950     { TwistedtubsWrite(solidsElement,twistedtubsPtr); }
951   else
952   {
953     G4String error_msg = "Unknown solid: " + solidPtr->GetName()
954                        + "; Type: " + solidPtr->GetEntityType();
955     G4Exception("G4GDMLWriteSolids::AddSolid()", "WriteError",
956                 FatalException, error_msg);
957   }
958}
Note: See TracBrowser for help on using the repository browser.