VGM Version 5.3
Loading...
Searching...
No Matches
Maps.cxx
Go to the documentation of this file.
1// $Id$
2
3// -----------------------------------------------------------------------
4// The XmlVGM package of the Virtual Geometry Model
5// Copyright (C) 2007, Ivana Hrivnacova
6// All rights reserved.
7//
8// For the licensing terms see vgm/LICENSE.
9// Contact: ivana@ipno.in2p3.fr
10// -----------------------------------------------------------------------
11
12//
13// Class Maps
14// --------------------
15// Class for generation of geometry data files in XML,
16// the XML format is independent from the geometry object model.
17//
18// Author: I. Hrivnacova, 19.1.2004
19
20#include "VGM/common/Math.h"
25
26#include "ClhepVGM/transform.h"
27
28#include "XmlVGM/IWriter.h"
29#include "XmlVGM/Maps.h"
30
31#include <iomanip>
32#include <sstream>
33#include <stdlib.h>
34#include <vector>
35
36//_____________________________________________________________________________
37XmlVGM::Maps::Maps(double numPrecision, double angleUnit, double lengthUnit)
38 : fNumPrecision(numPrecision),
39 fAngleUnit(angleUnit),
40 fLengthUnit(lengthUnit),
41 fPositions(),
42 fRotations(),
43 fIsotopes(),
44 fElements(),
45 fMaterials(),
46 fMedia(),
47 fNofBoolPositions(0),
48 fNofBoolRotations(0)
49{
54}
55
56//_____________________________________________________________________________
58 : fPositions(),
59 fRotations(),
60 fIsotopes(),
61 fElements(),
62 fMaterials(),
63 fMedia(),
64 fNofBoolPositions(0),
65 fNofBoolRotations(0)
66{
68
69 std::cerr << " XmlVGM::Maps::Maps:" << std::endl;
70 std::cerr << " Dummy constructor protected." << std::endl;
71 std::cerr << "** Exception: Aborting execution **" << std::endl;
72 exit(1);
73}
74
75//_____________________________________________________________________________
76XmlVGM::Maps::Maps(const Maps& /*right*/)
77 : fPositions(),
78 fRotations(),
79 fIsotopes(),
80 fElements(),
81 fMaterials(),
82 fNofBoolPositions(0),
83 fNofBoolRotations(0)
84{
86
87 std::cerr << " XmlVGM::Maps::Maps:" << std::endl;
88 std::cerr << " Copy constructor not implemented." << std::endl;
89 std::cerr << "** Exception: Aborting execution **" << std::endl;
90 exit(1);
91}
92
93//_____________________________________________________________________________
95{
96 //
97}
98
99//
100// operators
101//
102
103//_____________________________________________________________________________
105{
107
108 // check assignement to self
109 if (this == &right) return *this;
110
111 std::cerr << " XmlVGM::Maps::operator=:" << std::endl;
112 std::cerr << " Assignement operator not implemented." << std::endl;
113 std::cerr << "** Exception: Aborting execution **" << std::endl;
114 exit(1);
115
116 return *this;
117}
118
119//
120// private methods
121//
122
123//_____________________________________________________________________________
124void XmlVGM::Maps::CutName(std::string& name) const
125{
127
128 int i = name.length();
129 while (name[--i] == ' ') name = std::string(name, 0, i);
130}
131
132//_____________________________________________________________________________
133double XmlVGM::Maps::Round2(double number) const
134{
136
137 double precision = fNumPrecision;
138 return ClhepVGM::Round(number * pow(10., precision)) / pow(10., precision);
139}
140
141//_____________________________________________________________________________
142VGM::ThreeVector XmlVGM::Maps::PurifyAngles(
143 const VGM::ThreeVector& rotation) const
144{
147
148 double roundedPI = Round2(M_PI / fAngleUnit);
149
150 VGM::ThreeVector roundedRotation(3);
151 roundedRotation[0] = Round2(rotation[0] / fAngleUnit);
152 roundedRotation[1] = Round2(rotation[1] / fAngleUnit);
153 roundedRotation[2] = Round2(rotation[2] / fAngleUnit);
154
155 VGM::ThreeVector rotation2(3);
156 rotation2[0] = rotation[0];
157 rotation2[1] = rotation[1];
158 rotation2[2] = rotation[2];
159
160 if (roundedRotation[0] == -roundedPI) rotation2[0] = -rotation2[0];
161 if (roundedRotation[1] == -roundedPI) rotation2[1] = -rotation2[1];
162 if (roundedRotation[2] == -roundedPI) rotation2[2] = -rotation2[2];
163
164 return rotation2;
165}
166
167//
168// public methods
169//
170
171//_____________________________________________________________________________
172std::string XmlVGM::Maps::AddPosition(const VGM::Transform& transform)
173{
176
177 VGM::ThreeVector position(3);
178 position[0] = transform[VGM::kDx];
179 position[1] = transform[VGM::kDy];
180 position[2] = transform[VGM::kDz];
181
182 VGM::ThreeVector roundedPosition(3);
183 roundedPosition[0] = Round2(position[0] / fLengthUnit);
184 roundedPosition[1] = Round2(position[1] / fLengthUnit);
185 roundedPosition[2] = Round2(position[2] / fLengthUnit);
186
187 if (fPositions.find(roundedPosition) != fPositions.end()) return "";
188
189 // Generate position name
190 //
191 std::string name("pos_");
192 std::ostringstream tmpStream;
193 tmpStream << fPositions.size();
194 name.append(tmpStream.str());
195
196 // Add position to the map
197 fPositions[roundedPosition] = name;
198
199 return name;
200}
201
202//_____________________________________________________________________________
203std::string XmlVGM::Maps::AddRotation(const VGM::Transform& transform)
204{
207
208 // Get rotation
209 VGM::ThreeVector rotation(3);
210 rotation[0] = transform[VGM::kAngleX];
211 rotation[1] = transform[VGM::kAngleY];
212 rotation[2] = transform[VGM::kAngleZ];
213 VGM::ThreeVector rotation2 = PurifyAngles(rotation);
214
215 VGM::ThreeVector roundedRotation(3);
216 roundedRotation[0] = Round2(rotation2[0] / fAngleUnit);
217 roundedRotation[1] = Round2(rotation2[1] / fAngleUnit);
218 roundedRotation[2] = Round2(rotation2[2] / fAngleUnit);
219
220 if (fRotations.find(roundedRotation) != fRotations.end()) return "";
221
222 // Generate rotation name
223 //
224 std::string name("rot_");
225 std::ostringstream tmpStream;
226 tmpStream << fRotations.size();
227 name.append(tmpStream.str());
228
229 // Add position to the map
230 fRotations[roundedRotation] = name;
231
232 return name;
233}
234
235//_____________________________________________________________________________
237{
239
240 std::string name("posB_");
241 std::ostringstream tmpStream;
242 tmpStream << fNofBoolPositions++;
243 name.append(tmpStream.str());
244
245 return name;
246}
247
248//_____________________________________________________________________________
250{
252
253 std::string name("rotB_");
254 std::ostringstream tmpStream;
255 tmpStream << fNofBoolRotations++;
256 name.append(tmpStream.str());
257
258 return name;
259}
260
261//_____________________________________________________________________________
263{
267
268 VGM::ThreeVector roundedValues(3);
269 roundedValues[0] = Round2(isotope->Z());
270 roundedValues[1] = Round2(isotope->N());
271 roundedValues[2] = Round2(isotope->A());
272
273 // if (fIsotopes.find(roundedValues) != fIsotopes.end()) return 0;
274 IsotopeMap::iterator it;
275 for (it = fIsotopes.begin(); it != fIsotopes.end(); it++) {
276 if ((*it).first == roundedValues && (*it).second->Name() == isotope->Name())
277 return 0;
278 }
279
280 // Add isotope to the map
281 // fIsotopes[roundedValues] = isotope;
282 fIsotopes.insert(std::make_pair(roundedValues, isotope));
283
284 return isotope;
285}
286
287//_____________________________________________________________________________
289{
293
294 VGM::ThreeVector roundedValues(3);
295 roundedValues[0] = Round2(element->Z());
296 roundedValues[1] = Round2(element->N());
297 roundedValues[2] = Round2(element->A());
298
299 // if (fElements.find(roundedValues) != fElements.end()) return 0;
300 ElementMap::iterator it;
301 for (it = fElements.begin(); it != fElements.end(); it++) {
302 if ((*it).first == roundedValues && (*it).second->Name() == element->Name())
303 return 0;
304 }
305
306 // Add element to the map
307 // fElements[roundedValues] = element;
308 fElements.insert(std::make_pair(roundedValues, element));
309
310 return element;
311}
312
313//_____________________________________________________________________________
315{
318
319 std::string name = material->Name();
320 CutName(name);
321
322 if (fMaterials.find(name) != fMaterials.end()) return 0;
323
324 // Add material to the map
325 fMaterials[name] = material;
326
327 return material;
328}
329
330//_____________________________________________________________________________
332{
335
336 std::string name = medium->Name();
337 CutName(name);
338
339 if (fMedia.find(name) != fMedia.end()) return 0;
340
341 // Add material to the map
342 fMedia[name] = medium;
343
344 return medium;
345}
346
347//_____________________________________________________________________________
349 const VGM::Transform& transform) const
350{
354
355 VGM::ThreeVector position(3);
356 position[0] = transform[VGM::kDx];
357 position[1] = transform[VGM::kDy];
358 position[2] = transform[VGM::kDz];
359
360 VGM::ThreeVector roundedPosition(3);
361 roundedPosition[0] = Round2(position[0] / fLengthUnit);
362 roundedPosition[1] = Round2(position[1] / fLengthUnit);
363 roundedPosition[2] = Round2(position[2] / fLengthUnit);
364
365 ThreeVectorMap::const_iterator it = fPositions.find(roundedPosition);
366 if (it != fPositions.end())
367 return (*it).second;
368 else
369 return std::string();
370}
371
372//_____________________________________________________________________________
374 const VGM::Transform& transform) const
375{
379
380 // rotation
381 VGM::ThreeVector rotation(3);
382 rotation[0] = transform[VGM::kAngleX];
383 rotation[1] = transform[VGM::kAngleY];
384 rotation[2] = transform[VGM::kAngleZ];
385
386 VGM::ThreeVector rotation2 = PurifyAngles(rotation);
387
388 VGM::ThreeVector roundedRotation(3);
389 roundedRotation[0] = Round2(rotation2[0] / fAngleUnit);
390 roundedRotation[1] = Round2(rotation2[1] / fAngleUnit);
391 roundedRotation[2] = Round2(rotation2[2] / fAngleUnit);
392
393 ThreeVectorMap::const_iterator it = fRotations.find(roundedRotation);
394 if (it != fRotations.end())
395 return (*it).second;
396 else
397 return std::string();
398}
399
400//_____________________________________________________________________________
402{
404
405 ThreeVectorMap::const_iterator it1;
406 for (it1 = fPositions.begin(); it1 != fPositions.end(); it1++)
407 writer->WritePosition((*it1).second, (*it1).first);
408}
409
410//_____________________________________________________________________________
412{
414
415 ThreeVectorMap::const_iterator it1;
416 for (it1 = fRotations.begin(); it1 != fRotations.end(); it1++)
417 writer->WriteRotation((*it1).second, (*it1).first);
418}
419
420//_____________________________________________________________________________
422{
424
425 IsotopeMap::const_iterator it1;
426 for (it1 = fIsotopes.begin(); it1 != fIsotopes.end(); it1++)
427 writer->WriteIsotope((*it1).second);
428}
429
430//_____________________________________________________________________________
432{
434
435 ElementMap::const_iterator it1;
436 for (it1 = fElements.begin(); it1 != fElements.end(); it1++)
437 writer->WriteElement((*it1).second);
438}
439
440//_____________________________________________________________________________
442{
444
445 MaterialMap::const_iterator it2;
446 for (it2 = fMaterials.begin(); it2 != fMaterials.end(); it2++)
447 writer->WriteMaterial((*it2).second);
448}
449
450//_____________________________________________________________________________
452{
454
455 MediumMap::const_iterator it;
456 for (it = fMedia.begin(); it != fMedia.end(); it++)
457 writer->WriteMedium((*it).second);
458}
459
460//_____________________________________________________________________________
462{
464
465 MaterialMap::const_iterator it;
466 for (it = fMaterials.begin(); it != fMaterials.end(); it++)
467 writer->WriteMedium((*it).second);
468}
469
470//_____________________________________________________________________________
472{
474
475 fPositions.erase(fPositions.begin(), fPositions.end());
476 fRotations.erase(fRotations.begin(), fRotations.end());
477 fIsotopes.erase(fIsotopes.begin(), fIsotopes.end());
478 fElements.erase(fElements.begin(), fElements.end());
479 fMaterials.erase(fMaterials.begin(), fMaterials.end());
480 fMedia.erase(fMedia.begin(), fMedia.end());
481}
The VGM interface to elements.
Definition IElement.h:34
virtual double Z() const =0
Return the effective atomic number.
virtual std::string Name() const =0
Return the name of this element.
virtual double A() const =0
Return the effective effective mass of a mole in g/mole.
virtual double N() const =0
Return the effective number of nucleons.
The VGM interface to elements.
Definition IIsotope.h:28
virtual int N() const =0
Return the effective number of nucleons.
virtual std::string Name() const =0
Return the name of this element.
virtual double A() const =0
Return the effective effective mass of a mole in g/mole.
virtual int Z() const =0
Return the effective atomic number.
The VGM interface to materials.
Definition IMaterial.h:44
virtual std::string Name() const =0
Return the name of this element.
The VGM interface to tracking medium.
Definition IMedium.h:31
virtual std::string Name() const =0
Return its name.
The interface for the XML writer that writes VGM geometry objects to XML:
Definition IWriter.h:43
virtual void WriteIsotope(const VGM::IIsotope *isotope)=0
Write VGM isotope.
virtual void WriteElement(const VGM::IElement *element)=0
Write VGM element.
virtual void WritePosition(const std::string &name, const VGM::Transform &position)=0
Write position (from VGM transformation)
virtual void WriteRotation(const std::string &name, const VGM::Transform &rotation)=0
Write rotation (from VGM transformation)
virtual void WriteMaterial(const VGM::IMaterial *material)=0
Write VGM material.
virtual void WriteMedium(const VGM::IMedium *medium)=0
Write VGM medium.
Utility class for mapping XML element values to their names within the XML exporter precision.
Definition Maps.h:42
void ClearAllMaps()
Definition Maps.cxx:471
const VGM::IElement * AddElement(const VGM::IElement *element)
Definition Maps.cxx:288
void WriteAllRotations(IWriter *writer)
Definition Maps.cxx:411
std::string AddBooleanPosition()
Definition Maps.cxx:236
const VGM::IMaterial * AddMaterial(const VGM::IMaterial *material)
Definition Maps.cxx:314
void WriteAllPositions(IWriter *writer)
Definition Maps.cxx:401
std::string FindPositionName(const VGM::Transform &transform) const
Definition Maps.cxx:348
void WriteAllMedia(IWriter *writer)
Definition Maps.cxx:451
Maps & operator=(const Maps &)
Definition Maps.cxx:104
void WriteAllMediaFromMaterials(IWriter *writer)
Definition Maps.cxx:461
std::string FindRotationName(const VGM::Transform &transform) const
Definition Maps.cxx:373
std::string AddPosition(const VGM::Transform &transform)
Definition Maps.cxx:172
const VGM::IIsotope * AddIsotope(const VGM::IIsotope *isotope)
Definition Maps.cxx:262
std::string AddRotation(const VGM::Transform &transform)
Definition Maps.cxx:203
void WriteAllIsotopes(IWriter *writer)
Definition Maps.cxx:421
std::string AddBooleanRotation()
Definition Maps.cxx:249
const VGM::IMedium * AddMedium(const VGM::IMedium *medium)
Definition Maps.cxx:331
void WriteAllMaterials(IWriter *writer)
Definition Maps.cxx:441
void WriteAllElements(IWriter *writer)
Definition Maps.cxx:431
virtual ~Maps()
Definition Maps.cxx:94
#define M_PI
Definition of math constant M_PI not available on Win32.
Definition Math.h:24
double Round(double x)
std::vector< double > Transform
Definition Transform.h:40
std::vector< double > ThreeVector
Definition ThreeVector.h:27
@ kAngleZ
Definition Transform.h:49
@ kDx
Definition Transform.h:44
@ kAngleY
Definition Transform.h:48
@ kDz
Definition Transform.h:46
@ kAngleX
Definition Transform.h:47
@ kDy
Definition Transform.h:45
void CutName(std::string &name)