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

Last change on this file since 1202 was 987, checked in by garnier, 15 years ago

fichiers manquants

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