source: trunk/source/persistency/gdml/src/G4GDMLWriteStructure.cc@ 1326

Last change on this file since 1326 was 1315, checked in by garnier, 15 years ago

update geant4-09-04-beta-cand-01 interfaces-V09-03-09 vis-V09-03-08

File size: 18.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: G4GDMLWriteStructure.cc,v 1.81 2010/05/20 12:56:57 gcosmo Exp $
28// GEANT4 tag $Name: geant4-09-04-beta-cand-01 $
29//
30// class G4GDMLWriteStructure Implementation
31//
32// Original author: Zoltan Torzsok, November 2007
33//
34// --------------------------------------------------------------------
35
36#include "G4GDMLWriteStructure.hh"
37
38#include "G4Material.hh"
39#include "G4ReflectedSolid.hh"
40#include "G4DisplacedSolid.hh"
41#include "G4LogicalVolumeStore.hh"
42#include "G4PhysicalVolumeStore.hh"
43#include "G4PVDivision.hh"
44#include "G4PVReplica.hh"
45#include "G4OpticalSurface.hh"
46#include "G4LogicalSkinSurface.hh"
47#include "G4LogicalBorderSurface.hh"
48
49G4GDMLWriteStructure::G4GDMLWriteStructure()
50 : G4GDMLWriteParamvol()
51{
52}
53
54G4GDMLWriteStructure::~G4GDMLWriteStructure()
55{
56}
57
58void
59G4GDMLWriteStructure::DivisionvolWrite(xercesc::DOMElement* volumeElement,
60 const G4PVDivision* const divisionvol)
61{
62 EAxis axis = kUndefined;
63 G4int number = 0;
64 G4double width = 0.0;
65 G4double offset = 0.0;
66 G4bool consuming = false;
67
68 divisionvol->GetReplicationData(axis,number,width,offset,consuming);
69 axis = divisionvol->GetDivisionAxis();
70
71 G4String unitString("mm");
72 G4String axisString("kUndefined");
73 if (axis==kXAxis) { axisString = "kXAxis"; }
74 else if (axis==kYAxis) { axisString = "kYAxis"; }
75 else if (axis==kZAxis) { axisString = "kZAxis"; }
76 else if (axis==kRho) { axisString = "kRho"; }
77 else if (axis==kPhi) { axisString = "kPhi"; unitString = "degree"; }
78
79 const G4String name
80 = GenerateName(divisionvol->GetName(),divisionvol);
81 const G4String volumeref
82 = GenerateName(divisionvol->GetLogicalVolume()->GetName(),
83 divisionvol->GetLogicalVolume());
84
85 xercesc::DOMElement* divisionvolElement = NewElement("divisionvol");
86 divisionvolElement->setAttributeNode(NewAttribute("axis",axisString));
87 divisionvolElement->setAttributeNode(NewAttribute("number",number));
88 divisionvolElement->setAttributeNode(NewAttribute("width",width));
89 divisionvolElement->setAttributeNode(NewAttribute("offset",offset));
90 divisionvolElement->setAttributeNode(NewAttribute("unit",unitString));
91 xercesc::DOMElement* volumerefElement = NewElement("volumeref");
92 volumerefElement->setAttributeNode(NewAttribute("ref",volumeref));
93 divisionvolElement->appendChild(volumerefElement);
94 volumeElement->appendChild(divisionvolElement);
95}
96
97void G4GDMLWriteStructure::PhysvolWrite(xercesc::DOMElement* volumeElement,
98 const G4VPhysicalVolume* const physvol,
99 const G4Transform3D& T,
100 const G4String& ModuleName)
101{
102 HepGeom::Scale3D scale;
103 HepGeom::Rotate3D rotate;
104 HepGeom::Translate3D translate;
105
106 T.getDecomposition(scale,rotate,translate);
107
108 const G4ThreeVector scl(scale(0,0),scale(1,1),scale(2,2));
109 const G4ThreeVector rot = GetAngles(rotate.getRotation());
110 const G4ThreeVector pos = T.getTranslation();
111
112 const G4String name = GenerateName(physvol->GetName(),physvol);
113
114 xercesc::DOMElement* physvolElement = NewElement("physvol");
115 physvolElement->setAttributeNode(NewAttribute("name",name));
116 volumeElement->appendChild(physvolElement);
117
118 const G4String volumeref
119 = GenerateName(physvol->GetLogicalVolume()->GetName(),
120 physvol->GetLogicalVolume());
121
122 if (ModuleName.empty())
123 {
124 xercesc::DOMElement* volumerefElement = NewElement("volumeref");
125 volumerefElement->setAttributeNode(NewAttribute("ref",volumeref));
126 physvolElement->appendChild(volumerefElement);
127 }
128 else
129 {
130 xercesc::DOMElement* fileElement = NewElement("file");
131 fileElement->setAttributeNode(NewAttribute("name",ModuleName));
132 fileElement->setAttributeNode(NewAttribute("volname",volumeref));
133 physvolElement->appendChild(fileElement);
134 }
135
136 if (std::fabs(pos.x()) > kLinearPrecision
137 || std::fabs(pos.y()) > kLinearPrecision
138 || std::fabs(pos.z()) > kLinearPrecision)
139 {
140 PositionWrite(physvolElement,name+"_pos",pos);
141 }
142 if (std::fabs(rot.x()) > kAngularPrecision
143 || std::fabs(rot.y()) > kAngularPrecision
144 || std::fabs(rot.z()) > kAngularPrecision)
145 {
146 RotationWrite(physvolElement,name+"_rot",rot);
147 }
148 if (std::fabs(scl.x()-1.0) > kRelativePrecision
149 || std::fabs(scl.y()-1.0) > kRelativePrecision
150 || std::fabs(scl.z()-1.0) > kRelativePrecision)
151 {
152 ScaleWrite(physvolElement,name+"_scl",scl);
153 }
154}
155
156void G4GDMLWriteStructure::ReplicavolWrite(xercesc::DOMElement* volumeElement,
157 const G4VPhysicalVolume* const replicavol)
158{
159 EAxis axis = kUndefined;
160 G4int number = 0;
161 G4double width = 0.0;
162 G4double offset = 0.0;
163 G4bool consuming = false;
164 G4String unitString("mm");
165
166 replicavol->GetReplicationData(axis,number,width,offset,consuming);
167
168 const G4String volumeref
169 = GenerateName(replicavol->GetLogicalVolume()->GetName(),
170 replicavol->GetLogicalVolume());
171
172 xercesc::DOMElement* replicavolElement = NewElement("replicavol");
173 replicavolElement->setAttributeNode(NewAttribute("number",number));
174 xercesc::DOMElement* volumerefElement = NewElement("volumeref");
175 volumerefElement->setAttributeNode(NewAttribute("ref",volumeref));
176 replicavolElement->appendChild(volumerefElement);
177 xercesc::DOMElement* replicateElement = NewElement("replicate_along_axis");
178 replicavolElement->appendChild(replicateElement);
179
180 xercesc::DOMElement* dirElement = NewElement("direction");
181 if(axis==kXAxis)
182 { dirElement->setAttributeNode(NewAttribute("x","1")); }
183 else if(axis==kYAxis)
184 { dirElement->setAttributeNode(NewAttribute("y","1")); }
185 else if(axis==kZAxis)
186 { dirElement->setAttributeNode(NewAttribute("z","1")); }
187 else if(axis==kRho)
188 { dirElement->setAttributeNode(NewAttribute("rho","1")); }
189 else if(axis==kPhi)
190 { dirElement->setAttributeNode(NewAttribute("phi","1")); }
191 replicateElement->appendChild(dirElement);
192
193 xercesc::DOMElement* widthElement = NewElement("width");
194 widthElement->setAttributeNode(NewAttribute("value",width));
195 widthElement->setAttributeNode(NewAttribute("unit",unitString));
196 replicateElement->appendChild(widthElement);
197
198 xercesc::DOMElement* offsetElement = NewElement("offset");
199 offsetElement->setAttributeNode(NewAttribute("value",offset));
200 offsetElement->setAttributeNode(NewAttribute("unit",unitString));
201 replicateElement->appendChild(offsetElement);
202
203 volumeElement->appendChild(replicavolElement);
204}
205
206void G4GDMLWriteStructure::
207BorderSurfaceCache(const G4LogicalBorderSurface* const bsurf)
208{
209 if (!bsurf) { return; }
210
211 const G4SurfaceProperty* psurf = bsurf->GetSurfaceProperty();
212
213 // Generate the new element for border-surface
214 //
215 xercesc::DOMElement* borderElement = NewElement("bordersurface");
216 borderElement->setAttributeNode(NewAttribute("name", bsurf->GetName()));
217 borderElement->setAttributeNode(NewAttribute("surfaceproperty",
218 psurf->GetName()));
219
220 const G4String volumeref1 = GenerateName(bsurf->GetVolume1()->GetName(),
221 bsurf->GetVolume1());
222 const G4String volumeref2 = GenerateName(bsurf->GetVolume2()->GetName(),
223 bsurf->GetVolume2());
224 xercesc::DOMElement* volumerefElement1 = NewElement("physvolref");
225 xercesc::DOMElement* volumerefElement2 = NewElement("physvolref");
226 volumerefElement1->setAttributeNode(NewAttribute("ref",volumeref1));
227 volumerefElement2->setAttributeNode(NewAttribute("ref",volumeref2));
228 borderElement->appendChild(volumerefElement1);
229 borderElement->appendChild(volumerefElement2);
230
231 if (FindOpticalSurface(psurf))
232 {
233 OpticalSurfaceWrite(solidsElement,
234 dynamic_cast<const G4OpticalSurface*>(psurf));
235 }
236
237 borderElementVec.push_back(borderElement);
238}
239
240void G4GDMLWriteStructure::
241SkinSurfaceCache(const G4LogicalSkinSurface* const ssurf)
242{
243 if (!ssurf) { return; }
244
245 const G4SurfaceProperty* psurf = ssurf->GetSurfaceProperty();
246
247 // Generate the new element for border-surface
248 //
249 xercesc::DOMElement* skinElement = NewElement("skinsurface");
250 skinElement->setAttributeNode(NewAttribute("name", ssurf->GetName()));
251 skinElement->setAttributeNode(NewAttribute("surfaceproperty",
252 psurf->GetName()));
253
254 const G4String volumeref = GenerateName(ssurf->GetLogicalVolume()->GetName(),
255 ssurf->GetLogicalVolume());
256 xercesc::DOMElement* volumerefElement = NewElement("volumeref");
257 volumerefElement->setAttributeNode(NewAttribute("ref",volumeref));
258 skinElement->appendChild(volumerefElement);
259
260 if (FindOpticalSurface(psurf))
261 {
262 OpticalSurfaceWrite(solidsElement,
263 dynamic_cast<const G4OpticalSurface*>(psurf));
264 }
265
266 skinElementVec.push_back(skinElement);
267}
268
269G4bool G4GDMLWriteStructure::FindOpticalSurface(const G4SurfaceProperty* psurf)
270{
271 const G4OpticalSurface* osurf = dynamic_cast<const G4OpticalSurface*>(psurf);
272 std::vector<const G4OpticalSurface*>::const_iterator pos;
273 pos = std::find(opt_vec.begin(), opt_vec.end(), osurf);
274 if (pos != opt_vec.end()) { return false; } // item already created!
275
276 opt_vec.push_back(osurf); // cache it for future reference
277 return true;
278}
279
280const G4LogicalSkinSurface*
281G4GDMLWriteStructure::GetSkinSurface(const G4LogicalVolume* const lvol)
282{
283 G4LogicalSkinSurface* surf = 0;
284 G4int nsurf = G4LogicalSkinSurface::GetNumberOfSkinSurfaces();
285 if (nsurf)
286 {
287 const G4LogicalSkinSurfaceTable* stable =
288 G4LogicalSkinSurface::GetSurfaceTable();
289 std::vector<G4LogicalSkinSurface*>::const_iterator pos;
290 for (pos = stable->begin(); pos != stable->end(); pos++)
291 {
292 if (lvol == (*pos)->GetLogicalVolume())
293 {
294 surf = *pos; break;
295 }
296 }
297 }
298 return surf;
299}
300
301const G4LogicalBorderSurface*
302G4GDMLWriteStructure::GetBorderSurface(const G4VPhysicalVolume* const pvol)
303{
304 G4LogicalBorderSurface* surf = 0;
305 G4int nsurf = G4LogicalBorderSurface::GetNumberOfBorderSurfaces();
306 if (nsurf)
307 {
308 const G4LogicalBorderSurfaceTable* btable =
309 G4LogicalBorderSurface::GetSurfaceTable();
310 std::vector<G4LogicalBorderSurface*>::const_iterator pos;
311 for (pos = btable->begin(); pos != btable->end(); pos++)
312 {
313 if (pvol == (*pos)->GetVolume1()) // just the first in the couple
314 { // is enough
315 surf = *pos; break;
316 }
317 }
318 }
319 return surf;
320}
321
322void G4GDMLWriteStructure::SurfacesWrite()
323{
324 G4cout << "G4GDML: Writing surfaces..." << G4endl;
325
326 std::vector<xercesc::DOMElement*>::const_iterator pos;
327 for (pos = skinElementVec.begin(); pos != skinElementVec.end(); pos++)
328 {
329 structureElement->appendChild(*pos);
330 }
331 for (pos = borderElementVec.begin(); pos != borderElementVec.end(); pos++)
332 {
333 structureElement->appendChild(*pos);
334 }
335}
336
337void G4GDMLWriteStructure::StructureWrite(xercesc::DOMElement* gdmlElement)
338{
339 G4cout << "G4GDML: Writing structure..." << G4endl;
340
341 structureElement = NewElement("structure");
342 gdmlElement->appendChild(structureElement);
343}
344
345G4Transform3D G4GDMLWriteStructure::
346TraverseVolumeTree(const G4LogicalVolume* const volumePtr, const G4int depth)
347{
348 if (VolumeMap().find(volumePtr) != VolumeMap().end())
349 {
350 return VolumeMap()[volumePtr]; // Volume is already processed
351 }
352
353 G4VSolid* solidPtr = volumePtr->GetSolid();
354 G4Transform3D R,invR;
355 G4int trans=0;
356
357 while (true) // Solve possible displacement/reflection
358 { // of the referenced solid!
359 if (trans>maxTransforms)
360 {
361 G4String ErrorMessage = "Referenced solid in volume '"
362 + volumePtr->GetName()
363 + "' was displaced/reflected too many times!";
364 G4Exception("G4GDMLWriteStructure::TraverseVolumeTree()",
365 "InvalidSetup", FatalException, ErrorMessage);
366 }
367
368 if (G4ReflectedSolid* refl = dynamic_cast<G4ReflectedSolid*>(solidPtr))
369 {
370 R = R*refl->GetTransform3D();
371 solidPtr = refl->GetConstituentMovedSolid();
372 trans++;
373 continue;
374 }
375
376 if (G4DisplacedSolid* disp = dynamic_cast<G4DisplacedSolid*>(solidPtr))
377 {
378 R = R*G4Transform3D(disp->GetObjectRotation(),
379 disp->GetObjectTranslation());
380 solidPtr = disp->GetConstituentMovedSolid();
381 trans++;
382 continue;
383 }
384
385 break;
386 }
387
388 // Only compute the inverse when necessary!
389 //
390 if (trans>0) { invR = R.inverse(); }
391
392 const G4String name
393 = GenerateName(volumePtr->GetName(),volumePtr);
394 const G4String materialref
395 = GenerateName(volumePtr->GetMaterial()->GetName(),
396 volumePtr->GetMaterial());
397 const G4String solidref
398 = GenerateName(solidPtr->GetName(),solidPtr);
399
400 xercesc::DOMElement* volumeElement = NewElement("volume");
401 volumeElement->setAttributeNode(NewAttribute("name",name));
402 xercesc::DOMElement* materialrefElement = NewElement("materialref");
403 materialrefElement->setAttributeNode(NewAttribute("ref",materialref));
404 volumeElement->appendChild(materialrefElement);
405 xercesc::DOMElement* solidrefElement = NewElement("solidref");
406 solidrefElement->setAttributeNode(NewAttribute("ref",solidref));
407 volumeElement->appendChild(solidrefElement);
408
409 const G4int daughterCount = volumePtr->GetNoDaughters();
410
411 for (G4int i=0;i<daughterCount;i++) // Traverse all the children!
412 {
413 const G4VPhysicalVolume* const physvol = volumePtr->GetDaughter(i);
414 const G4String ModuleName = Modularize(physvol,depth);
415
416 G4Transform3D daughterR;
417
418 if (ModuleName.empty()) // Check if subtree requested to be
419 { // a separate module!
420 daughterR = TraverseVolumeTree(physvol->GetLogicalVolume(),depth+1);
421 }
422 else
423 {
424 G4GDMLWriteStructure writer;
425 daughterR = writer.Write(ModuleName,physvol->GetLogicalVolume(),
426 SchemaLocation,depth+1);
427 }
428
429 if (const G4PVDivision* const divisionvol
430 = dynamic_cast<const G4PVDivision*>(physvol)) // Is it division?
431 {
432 if (!G4Transform3D::Identity.isNear(invR*daughterR,kRelativePrecision))
433 {
434 G4String ErrorMessage = "Division volume in '"
435 + name
436 + "' can not be related to reflected solid!";
437 G4Exception("G4GDMLWriteStructure::TraverseVolumeTree()",
438 "InvalidSetup", FatalException, ErrorMessage);
439 }
440 DivisionvolWrite(volumeElement,divisionvol);
441 } else
442 if (physvol->IsParameterised()) // Is it a paramvol?
443 {
444 if (!G4Transform3D::Identity.isNear(invR*daughterR,kRelativePrecision))
445 {
446 G4String ErrorMessage = "Parameterised volume in '"
447 + name
448 + "' can not be related to reflected solid!";
449 G4Exception("G4GDMLWriteStructure::TraverseVolumeTree()",
450 "InvalidSetup", FatalException, ErrorMessage);
451 }
452 ParamvolWrite(volumeElement,physvol);
453 } else
454 if (physvol->IsReplicated()) // Is it a replicavol?
455 {
456 if (!G4Transform3D::Identity.isNear(invR*daughterR,kRelativePrecision))
457 {
458 G4String ErrorMessage = "Replica volume in '"
459 + name
460 + "' can not be related to reflected solid!";
461 G4Exception("G4GDMLWriteStructure::TraverseVolumeTree()",
462 "InvalidSetup", FatalException, ErrorMessage);
463 }
464 ReplicavolWrite(volumeElement,physvol);
465 }
466 else // Is it a physvol?
467 {
468 G4RotationMatrix rot;
469
470 if (physvol->GetFrameRotation() != 0)
471 {
472 rot = *(physvol->GetFrameRotation());
473 }
474 G4Transform3D P(rot,physvol->GetObjectTranslation());
475 PhysvolWrite(volumeElement,physvol,invR*P*daughterR,ModuleName);
476 }
477 BorderSurfaceCache(GetBorderSurface(physvol));
478 }
479
480 structureElement->appendChild(volumeElement);
481 // Append the volume AFTER traversing the children so that
482 // the order of volumes will be correct!
483
484 VolumeMap()[volumePtr] = R;
485
486 AddExtension(volumeElement, volumePtr);
487 // Add any possible user defined extension attached to a volume
488
489 AddMaterial(volumePtr->GetMaterial());
490 // Add the involved materials and solids!
491
492 AddSolid(solidPtr);
493
494 SkinSurfaceCache(GetSkinSurface(volumePtr));
495
496 return R;
497}
Note: See TracBrowser for help on using the repository browser.