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

Last change on this file since 1257 was 1228, checked in by garnier, 15 years ago

update geant4.9.3 tag

File size: 38.2 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.65 2009/04/24 15:34:20 gcosmo Exp $
28// GEANT4 tag $Name: geant4-09-03 $
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 "G4TessellatedSolid.hh"
56#include "G4Tet.hh"
57#include "G4Torus.hh"
58#include "G4Trap.hh"
59#include "G4Trd.hh"
60#include "G4Tubs.hh"
61#include "G4TwistedBox.hh"
62#include "G4TwistedTrap.hh"
63#include "G4TwistedTrd.hh"
64#include "G4TwistedTubs.hh"
65#include "G4UnionSolid.hh"
66#include "G4OpticalSurface.hh"
67#include "G4SurfaceProperty.hh"
68
69G4GDMLWriteSolids::
70G4GDMLWriteSolids() : G4GDMLWriteMaterials()
71{
72}
73
74G4GDMLWriteSolids::
75~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::
613TrapWrite(xercesc::DOMElement* solidsElement, const G4Trap* const trap)
614{
615   const G4String& name = GenerateName(trap->GetName(),trap);
616
617   const G4ThreeVector& simaxis = trap->GetSymAxis();
618   const G4double phi = (simaxis.z() != 1.0)
619                      ? (std::atan(simaxis.y()/simaxis.x())) : (0.0);
620   const G4double theta = std::acos(simaxis.z());
621   const G4double alpha1 = std::atan(trap->GetTanAlpha1());
622   const G4double alpha2 = std::atan(trap->GetTanAlpha2());
623
624   xercesc::DOMElement* trapElement = NewElement("trap");
625   trapElement->setAttributeNode(NewAttribute("name",name));
626   trapElement->setAttributeNode(NewAttribute("z",
627                2.0*trap->GetZHalfLength()/mm));
628   trapElement->setAttributeNode(NewAttribute("theta",theta/degree));
629   trapElement->setAttributeNode(NewAttribute("phi",phi/degree));
630   trapElement->setAttributeNode(NewAttribute("y1",
631                2.0*trap->GetYHalfLength1()/mm));
632   trapElement->setAttributeNode(NewAttribute("x1",
633                2.0*trap->GetXHalfLength1()/mm));
634   trapElement->setAttributeNode(NewAttribute("x2",
635                2.0*trap->GetXHalfLength2()/mm));
636   trapElement->setAttributeNode(NewAttribute("alpha1",alpha1/degree));
637   trapElement->setAttributeNode(NewAttribute("y2",
638                2.0*trap->GetYHalfLength2()/mm));
639   trapElement->setAttributeNode(NewAttribute("x3",
640                2.0*trap->GetXHalfLength3()/mm));
641   trapElement->setAttributeNode(NewAttribute("x4",
642                2.0*trap->GetXHalfLength4()/mm));
643   trapElement->setAttributeNode(NewAttribute("alpha2",alpha2/degree));
644   trapElement->setAttributeNode(NewAttribute("aunit","deg"));
645   trapElement->setAttributeNode(NewAttribute("lunit","mm"));
646   solidsElement->appendChild(trapElement);
647}
648
649void G4GDMLWriteSolids::
650TrdWrite(xercesc::DOMElement* solidsElement, const G4Trd* const trd)
651{
652   const G4String& name = GenerateName(trd->GetName(),trd);
653
654   xercesc::DOMElement* trdElement = NewElement("trd");
655   trdElement->setAttributeNode(NewAttribute("name",name));
656   trdElement->setAttributeNode(NewAttribute("x1",
657               2.0*trd->GetXHalfLength1()/mm));
658   trdElement->setAttributeNode(NewAttribute("x2",
659               2.0*trd->GetXHalfLength2()/mm));
660   trdElement->setAttributeNode(NewAttribute("y1",
661               2.0*trd->GetYHalfLength1()/mm));
662   trdElement->setAttributeNode(NewAttribute("y2",
663               2.0*trd->GetYHalfLength2()/mm));
664   trdElement->setAttributeNode(NewAttribute("z",
665               2.0*trd->GetZHalfLength()/mm));
666   trdElement->setAttributeNode(NewAttribute("lunit","mm"));
667   solidsElement->appendChild(trdElement);
668}
669
670void G4GDMLWriteSolids::
671TubeWrite(xercesc::DOMElement* solidsElement, const G4Tubs* const tube)
672{
673   const G4String& name = GenerateName(tube->GetName(),tube);
674
675   xercesc::DOMElement* tubeElement = NewElement("tube");
676   tubeElement->setAttributeNode(NewAttribute("name",name));
677   tubeElement->setAttributeNode(NewAttribute("rmin",
678                tube->GetInnerRadius()/mm));
679   tubeElement->setAttributeNode(NewAttribute("rmax",
680                tube->GetOuterRadius()/mm));
681   tubeElement->setAttributeNode(NewAttribute("z",
682                2.0*tube->GetZHalfLength()/mm));
683   tubeElement->setAttributeNode(NewAttribute("startphi",
684                tube->GetStartPhiAngle()/degree));
685   tubeElement->setAttributeNode(NewAttribute("deltaphi",
686                tube->GetDeltaPhiAngle()/degree));
687   tubeElement->setAttributeNode(NewAttribute("aunit","deg"));
688   tubeElement->setAttributeNode(NewAttribute("lunit","mm"));
689   solidsElement->appendChild(tubeElement);
690}
691
692void G4GDMLWriteSolids::
693TwistedboxWrite(xercesc::DOMElement* solidsElement,
694                const G4TwistedBox* const twistedbox)
695{
696   const G4String& name = GenerateName(twistedbox->GetName(),twistedbox);
697
698   xercesc::DOMElement* twistedboxElement = NewElement("twistedbox");
699   twistedboxElement->setAttributeNode(NewAttribute("name",name));
700   twistedboxElement->setAttributeNode(NewAttribute("x",
701                      2.0*twistedbox->GetXHalfLength()/mm));
702   twistedboxElement->setAttributeNode(NewAttribute("y",
703                      2.0*twistedbox->GetYHalfLength()/mm));
704   twistedboxElement->setAttributeNode(NewAttribute("z",
705                      2.0*twistedbox->GetZHalfLength()/mm));
706   twistedboxElement->setAttributeNode(NewAttribute("PhiTwist",
707                      twistedbox->GetPhiTwist()/degree));
708   twistedboxElement->setAttributeNode(NewAttribute("aunit","deg"));
709   twistedboxElement->setAttributeNode(NewAttribute("lunit","mm"));
710   solidsElement->appendChild(twistedboxElement);
711}
712
713void G4GDMLWriteSolids::
714TwistedtrapWrite(xercesc::DOMElement* solidsElement,
715                 const G4TwistedTrap* const twistedtrap)
716{
717   const G4String& name = GenerateName(twistedtrap->GetName(),twistedtrap);
718
719   xercesc::DOMElement* twistedtrapElement = NewElement("twistedtrap");
720   twistedtrapElement->setAttributeNode(NewAttribute("name",name));
721   twistedtrapElement->setAttributeNode(NewAttribute("y1",
722                       2.0*twistedtrap->GetY1HalfLength()/mm));
723   twistedtrapElement->setAttributeNode(NewAttribute("x1",
724                       2.0*twistedtrap->GetX1HalfLength()/mm));
725   twistedtrapElement->setAttributeNode(NewAttribute("x2",
726                       2.0*twistedtrap->GetX2HalfLength()/mm));
727   twistedtrapElement->setAttributeNode(NewAttribute("y2",
728                       2.0*twistedtrap->GetY2HalfLength()/mm));
729   twistedtrapElement->setAttributeNode(NewAttribute("x3",
730                       2.0*twistedtrap->GetX3HalfLength()/mm));
731   twistedtrapElement->setAttributeNode(NewAttribute("x4",
732                       2.0*twistedtrap->GetX4HalfLength()/mm));
733   twistedtrapElement->setAttributeNode(NewAttribute("z",
734                       2.0*twistedtrap->GetZHalfLength()/mm));
735   twistedtrapElement->setAttributeNode(NewAttribute("Alph",
736                       twistedtrap->GetTiltAngleAlpha()/degree));
737   twistedtrapElement->setAttributeNode(NewAttribute("Theta",
738                       twistedtrap->GetPolarAngleTheta()/degree));
739   twistedtrapElement->setAttributeNode(NewAttribute("Phi",
740                       twistedtrap->GetAzimuthalAnglePhi()/degree));
741   twistedtrapElement->setAttributeNode(NewAttribute("PhiTwist",
742                       twistedtrap->GetPhiTwist()/degree));
743   twistedtrapElement->setAttributeNode(NewAttribute("aunit","deg"));
744   twistedtrapElement->setAttributeNode(NewAttribute("lunit","mm"));
745   
746   solidsElement->appendChild(twistedtrapElement);
747}
748
749void G4GDMLWriteSolids::
750TwistedtrdWrite(xercesc::DOMElement* solidsElement,
751                const G4TwistedTrd* const twistedtrd)
752{
753   const G4String& name = GenerateName(twistedtrd->GetName(),twistedtrd);
754
755   xercesc::DOMElement* twistedtrdElement = NewElement("twistedtrd");
756   twistedtrdElement->setAttributeNode(NewAttribute("name",name));
757   twistedtrdElement->setAttributeNode(NewAttribute("x1",
758                      2.0*twistedtrd->GetX1HalfLength()/mm));
759   twistedtrdElement->setAttributeNode(NewAttribute("x2",
760                      2.0*twistedtrd->GetX2HalfLength()/mm));
761   twistedtrdElement->setAttributeNode(NewAttribute("y1",
762                      2.0*twistedtrd->GetY1HalfLength()/mm));
763   twistedtrdElement->setAttributeNode(NewAttribute("y2",
764                      2.0*twistedtrd->GetY2HalfLength()/mm));
765   twistedtrdElement->setAttributeNode(NewAttribute("z",
766                      2.0*twistedtrd->GetZHalfLength()/mm));
767   twistedtrdElement->setAttributeNode(NewAttribute("PhiTwist",
768                      twistedtrd->GetPhiTwist()/degree));
769   twistedtrdElement->setAttributeNode(NewAttribute("aunit","deg"));
770   twistedtrdElement->setAttributeNode(NewAttribute("lunit","mm"));
771   solidsElement->appendChild(twistedtrdElement);
772}
773
774void G4GDMLWriteSolids::
775TwistedtubsWrite(xercesc::DOMElement* solidsElement,
776                 const G4TwistedTubs* const twistedtubs)
777{
778   const G4String& name = GenerateName(twistedtubs->GetName(),twistedtubs);
779
780   xercesc::DOMElement* twistedtubsElement = NewElement("twistedtubs");
781   twistedtubsElement->setAttributeNode(NewAttribute("name",name));
782   twistedtubsElement->setAttributeNode(NewAttribute("twistedangle",
783                       twistedtubs->GetPhiTwist()/degree));
784   twistedtubsElement->setAttributeNode(NewAttribute("endinnerrad",
785                       twistedtubs->GetInnerRadius()/mm));
786   twistedtubsElement->setAttributeNode(NewAttribute("endouterrad",
787                       twistedtubs->GetOuterRadius()/mm));
788   twistedtubsElement->setAttributeNode(NewAttribute("zlen",
789                       2.0*twistedtubs->GetZHalfLength()/mm));
790   twistedtubsElement->setAttributeNode(NewAttribute("phi",
791                       twistedtubs->GetDPhi()/degree));
792   twistedtubsElement->setAttributeNode(NewAttribute("aunit","deg"));
793   twistedtubsElement->setAttributeNode(NewAttribute("lunit","mm"));
794   solidsElement->appendChild(twistedtubsElement);
795}
796
797void G4GDMLWriteSolids::
798ZplaneWrite(xercesc::DOMElement* element, const G4double& z,
799            const G4double& rmin, const G4double& rmax)
800{
801   xercesc::DOMElement* zplaneElement = NewElement("zplane");
802   zplaneElement->setAttributeNode(NewAttribute("z",z/mm));
803   zplaneElement->setAttributeNode(NewAttribute("rmin",rmin/mm));
804   zplaneElement->setAttributeNode(NewAttribute("rmax",rmax/mm));
805   element->appendChild(zplaneElement);
806}
807
808void G4GDMLWriteSolids::
809OpticalSurfaceWrite(xercesc::DOMElement* solidsElement,
810                    const G4OpticalSurface* const surf)
811{
812   xercesc::DOMElement* optElement = NewElement("opticalsurface");
813   G4OpticalSurfaceModel smodel = surf->GetModel();
814   G4double sval = (smodel==glisur) ? surf->GetPolish() : surf->GetSigmaAlpha();
815
816   optElement->setAttributeNode(NewAttribute("name", surf->GetName()));
817   optElement->setAttributeNode(NewAttribute("model", smodel));
818   optElement->setAttributeNode(NewAttribute("finish", surf->GetFinish()));
819   optElement->setAttributeNode(NewAttribute("type", surf->GetType()));
820   optElement->setAttributeNode(NewAttribute("value", sval));
821
822   solidsElement->appendChild(optElement);
823}
824
825void G4GDMLWriteSolids::SolidsWrite(xercesc::DOMElement* gdmlElement)
826{
827   G4cout << "G4GDML: Writing solids..." << G4endl;
828
829   solidsElement = NewElement("solids");
830   gdmlElement->appendChild(solidsElement);
831
832   solidList.clear();
833}
834
835void G4GDMLWriteSolids::AddSolid(const G4VSolid* const solidPtr)
836{
837   for (size_t i=0; i<solidList.size(); i++)   // Check if solid is
838   {                                           // already in the list!
839      if (solidList[i] == solidPtr)  { return; }
840   }
841
842   solidList.push_back(solidPtr);
843
844   if (const G4BooleanSolid* const booleanPtr
845     = dynamic_cast<const G4BooleanSolid*>(solidPtr))
846     { BooleanWrite(solidsElement,booleanPtr); } else
847   if (const G4Box* const boxPtr
848     = dynamic_cast<const G4Box*>(solidPtr))
849     { BoxWrite(solidsElement,boxPtr); } else
850   if (const G4Cons* const conePtr
851     = dynamic_cast<const G4Cons*>(solidPtr))
852     { ConeWrite(solidsElement,conePtr); } else
853   if (const G4EllipticalCone* const elconePtr
854     = dynamic_cast<const G4EllipticalCone*>(solidPtr))
855     { ElconeWrite(solidsElement,elconePtr); } else
856   if (const G4Ellipsoid* const ellipsoidPtr
857     = dynamic_cast<const G4Ellipsoid*>(solidPtr))
858     { EllipsoidWrite(solidsElement,ellipsoidPtr); } else
859   if (const G4EllipticalTube* const eltubePtr
860     = dynamic_cast<const G4EllipticalTube*>(solidPtr))
861     { EltubeWrite(solidsElement,eltubePtr); } else
862   if (const G4ExtrudedSolid* const xtruPtr
863     = dynamic_cast<const G4ExtrudedSolid*>(solidPtr))
864     { XtruWrite(solidsElement,xtruPtr); } else
865   if (const G4Hype* const hypePtr
866     = dynamic_cast<const G4Hype*>(solidPtr))
867     { HypeWrite(solidsElement,hypePtr); } else
868   if (const G4Orb* const orbPtr
869     = dynamic_cast<const G4Orb*>(solidPtr))
870     { OrbWrite(solidsElement,orbPtr); } else
871   if (const G4Para* const paraPtr
872     = dynamic_cast<const G4Para*>(solidPtr))
873     { ParaWrite(solidsElement,paraPtr); } else
874   if (const G4Paraboloid* const paraboloidPtr
875     = dynamic_cast<const G4Paraboloid*>(solidPtr))
876     { ParaboloidWrite(solidsElement,paraboloidPtr); } else
877   if (const G4Polycone* const polyconePtr
878     = dynamic_cast<const G4Polycone*>(solidPtr))
879     { PolyconeWrite(solidsElement,polyconePtr); } else
880   if (const G4Polyhedra* const polyhedraPtr
881     = dynamic_cast<const G4Polyhedra*>(solidPtr))
882     { PolyhedraWrite(solidsElement,polyhedraPtr); } else
883   if (const G4Sphere* const spherePtr
884     = dynamic_cast<const G4Sphere*>(solidPtr))
885     { SphereWrite(solidsElement,spherePtr); } else
886   if (const G4TessellatedSolid* const tessellatedPtr
887     = dynamic_cast<const G4TessellatedSolid*>(solidPtr))
888     { TessellatedWrite(solidsElement,tessellatedPtr); } else
889   if (const G4Tet* const tetPtr
890     = dynamic_cast<const G4Tet*>(solidPtr))
891     { TetWrite(solidsElement,tetPtr); } else
892   if (const G4Torus* const torusPtr
893     = dynamic_cast<const G4Torus*>(solidPtr))
894     { TorusWrite(solidsElement,torusPtr); } else
895   if (const G4Trap* const trapPtr
896     = dynamic_cast<const G4Trap*>(solidPtr))
897     { TrapWrite(solidsElement,trapPtr); } else
898   if (const G4Trd* const trdPtr
899     = dynamic_cast<const G4Trd*>(solidPtr))
900     { TrdWrite(solidsElement,trdPtr); } else
901   if (const G4Tubs* const tubePtr
902     = dynamic_cast<const G4Tubs*>(solidPtr))
903     { TubeWrite(solidsElement,tubePtr); } else
904   if (const G4TwistedBox* const twistedboxPtr
905     = dynamic_cast<const G4TwistedBox*>(solidPtr))
906     { TwistedboxWrite(solidsElement,twistedboxPtr); } else
907   if (const G4TwistedTrap* const twistedtrapPtr
908     = dynamic_cast<const G4TwistedTrap*>(solidPtr))
909     { TwistedtrapWrite(solidsElement,twistedtrapPtr); } else
910   if (const G4TwistedTrd* const twistedtrdPtr
911     = dynamic_cast<const G4TwistedTrd*>(solidPtr))
912     { TwistedtrdWrite(solidsElement,twistedtrdPtr); } else
913   if (const G4TwistedTubs* const twistedtubsPtr
914     = dynamic_cast<const G4TwistedTubs*>(solidPtr))
915     { TwistedtubsWrite(solidsElement,twistedtubsPtr); }
916   else
917   {
918     G4String error_msg = "Unknown solid: " + solidPtr->GetName()
919                        + "; Type: " + solidPtr->GetEntityType();
920     G4Exception("G4GDMLWriteSolids::AddSolid()", "WriteError",
921                 FatalException, error_msg);
922   }
923}
Note: See TracBrowser for help on using the repository browser.