-
Notifications
You must be signed in to change notification settings - Fork 4
/
hddsBrowser.cpp
427 lines (418 loc) · 14.2 KB
/
hddsBrowser.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/* HDDS Browser Classes
*
* Author: [email protected]
*
* Original version - Richard Jones, June 3, 2008.
*
*/
#include "XString.hpp"
#include "XParsers.hpp"
#include "hddsBrowser.hpp"
#include <stdio.h>
#include <math.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <iomanip>
#include <vector>
#include <list>
#define APP_NAME "hddsBrowser"
#define X(str) XString(str).unicode_str()
#define S(str) str.c_str()
// constructor
hddsBrowser::hddsBrowser(const XString xmlFile)
{
fGeomDoc = buildDOMDocument(xmlFile,false);
if (fGeomDoc == 0) {
std::cerr
<< APP_NAME << " - error parsing HDDS document, "
<< "cannot continue" << std::endl;
return;
}
}
// Look up a volume in the geometry and return a pointer to
// a vector of Refsys objects containing the origin and rotation
// parameters for the placement of the object in the global
// reference system. The user is responsible for deleting the
// returned vector when he is done with it.
std::vector<Refsys>* hddsBrowser::find(const XString volume,
const Refsys *ref,
const DOMElement *contEl)
{
std::vector<Refsys> *result = new std::vector<Refsys>;
if (contEl == 0) {
contEl = fGeomDoc->getElementById(X("everything"));
if (contEl == 0) {
std::cerr
<< APP_NAME << " - error scanning HDDS document, " << std::endl
<< " no element named \"everything\" found" << std::endl;
return result;
}
}
XString nameS = contEl->getAttribute(X("name"));
XString envelS = contEl->getAttribute(X("envelope"));
if (nameS == volume || envelS == volume) {
if (ref == 0) {
Refsys ref0;
result->push_back(ref0);
}
else {
result->push_back(*ref);
}
return result;
}
#if DEBUGGING_FIND
std::cout << "exploring volume " << nameS.c_str()
<< ", envelope " << envelS.c_str() << std::endl;
#endif
// At this point contEl is pointing to an element in the geometry document
// that is a container of positioning tags:
// * composition
// * union | intersection | subtraction
// * stackX | stackY | stackZ
// The task of find() is to go through the list of positioning tags:
// * posXYZ (composition or booleans)
// * posRPhiZ (composition or booleans)
// * mposX (composition only)
// * mposY (composition only)
// * mposZ (composition only)
// * mposR (composition only)
// * mposPhi (composition only)
// * axisPos (stacks only)
// * axisMPos (stacks only)
// and update the positioning parameters in ref, and look up the daughter
// volume in the geometry tree. There are three possibilities:
// 1. The daughter volume name matches the argument "volume":
// result: Form a vector of Refsys objects describing
// the placement of the named daughter in the MRS.
// 2. else the daughter volume is another container element:
// result: Update the placement information in ref and call the
// find() method recursively for each positioning step.
// 3. else the daughter volume is one of the basic shapes:
// result: Return an empty vector of Refsys objects.
for (DOMNode *node = contEl->getFirstChild();
node != 0;
node = node->getNextSibling())
{
if (node->getNodeType() != DOMNode::ELEMENT_NODE) {
continue;
}
DOMElement* el = (DOMElement*) node;
XString elS(el->getTagName());
XString volumeS(el->getAttribute(X("volume")));
double origin[] = {0, 0, 0};
double angle[] = {0, 0, 0};
int ncopy = 1;
Units unit;
unit.getConversions(el);
XString rotS(el->getAttribute(X("rot")));
if (rotS.size() > 0) {
std::stringstream listr1(rotS);
listr1 >> angle[0] >> angle[1] >> angle[2];
angle[0] /= unit.rad;
angle[1] /= unit.rad;
angle[2] /= unit.rad;
}
XString implrotS(contEl->getAttribute(X("impliedRot")));
XString sS(el->getAttribute(X("S")));
double s = 0;
if (sS.size() > 0) {
s = atof(S(sS));
s /= unit.cm;
}
XString ncopyS(el->getAttribute(X("ncopy")));
if (ncopyS.size() > 0) {
ncopy = atoi(S(ncopyS));
}
Refsys *dref = (ref == 0)? new Refsys : new Refsys(*ref);
if (elS == "posXYZ") {
XString xyzS(el->getAttribute(X("X_Y_Z")));
if (xyzS.size() > 0) {
std::stringstream listr1(xyzS);
listr1 >> origin[0] >> origin[1] >> origin[2];
origin[0] /= unit.cm;
origin[1] /= unit.cm;
origin[2] /= unit.cm;
dref->shift(origin);
dref->rotate(angle);
if (volumeS == volume) {
result->push_back(*dref);
}
else {
DOMElement* childEl = fGeomDoc->getElementById(X(volumeS));
std::vector<Refsys> *res = find(volume,dref,childEl);
if (res->size() > 0) {
std::vector<Refsys>::iterator it = result->end();
result->insert(it,res->begin(),res->end());
}
delete res;
}
}
}
else if (elS == "posRPhiZ") {
XString rphizS(el->getAttribute(X("R_Phi_Z")));
if (rphizS.size() > 0) {
double r=0, phi=0;
if (rphizS.size() > 0) {
std::stringstream listr1(rphizS);
listr1 >> r >> phi >> origin[2];
r /= unit.cm;
phi /= unit.rad;
}
origin[0] = r * cos(phi) - s * sin(phi);
origin[1] = r * sin(phi) + s * cos(phi);
origin[2] /= unit.cm;
if (implrotS == "true") {
angle[2] += phi;
}
dref->shift(origin);
dref->rotate(angle);
if (volumeS == volume) {
result->push_back(*dref);
}
else {
DOMElement* childEl = fGeomDoc->getElementById(X(volumeS));
std::vector<Refsys> *res = find(volume,dref,childEl);
if (res->size() > 0) {
std::vector<Refsys>::iterator it = result->end();
result->insert(it,res->begin(),res->end());
}
delete res;
}
}
}
else if (elS == "mposPhi") {
double phi0, dphi;
XString phi0S(contEl->getAttribute(X("Phi0")));
phi0 = atof(S(phi0S)) /unit.rad;
XString dphiS(contEl->getAttribute(X("dPhi")));
if (dphiS.size() != 0) {
dphi = atof(S(dphiS)) /unit.rad;
}
else {
dphi = 2 * M_PI / ncopy;
}
double r=0, z=0;
XString rzS(contEl->getAttribute(X("R_Z")));
if (rzS.size() > 0) {
std::stringstream listr(rzS);
listr >> r >> z;
r /= unit.cm;
z /= unit.cm;
}
dref->rotate(angle);
for (int inst = 0; inst < ncopy; inst++) {
double phi = phi0 + inst * dphi;
origin[0] = r * cos(phi) - s * sin(phi);
origin[1] = r * sin(phi) + s * cos(phi);
origin[2] = z;
Refsys *mref = new Refsys(*dref);
mref->shift(origin);
if (implrotS == "true") {
angle[2] += ((inst == 0) ? phi0 : dphi);
mref->rotate(angle);
}
if (volumeS == volume) {
result->push_back(*mref);
delete mref;
}
else {
DOMElement* childEl = fGeomDoc->getElementById(X(volumeS));
std::vector<Refsys> *res = find(volume,mref,childEl);
delete mref;
if (res->size() > 0) {
std::vector<Refsys>::iterator it = result->end();
result->insert(it,res->begin(),res->end());
delete res;
}
else {
delete res;
break;
}
}
}
}
else if (elS == "mposR") {
double r0, dr;
XString r0S(contEl->getAttribute(X("R0")));
r0 = atof(S(r0S)) /unit.cm;
XString drS(contEl->getAttribute(X("dR")));
dr = atof(S(drS)) /unit.cm;
double phi=0, z=0;
XString zphiS(contEl->getAttribute(X("Z_Phi")));
if (zphiS.size() > 0) {
std::stringstream listr(zphiS);
listr >> z >> phi;
phi /= unit.rad;
z /= unit.cm;
}
dref->rotate(angle);
for (int inst = 0; inst < ncopy; inst++) {
double r = r0 + inst * dr;
origin[0] = r * cos(phi) - s * sin(phi);
origin[1] = r * sin(phi) + s * cos(phi);
origin[2] = z;
Refsys *mref = new Refsys(*dref);
mref->shift(origin);
if (volumeS == volume) {
result->push_back(*mref);
delete mref;
}
else {
DOMElement* childEl = fGeomDoc->getElementById(X(volumeS));
std::vector<Refsys> *res = find(volume,mref,childEl);
delete mref;
if (res->size() > 0) {
std::vector<Refsys>::iterator it = result->end();
result->insert(it,res->begin(),res->end());
delete res;
}
else {
delete res;
break;
}
}
}
}
else if (elS == "mposX") {
double x0, dx;
XString x0S(contEl->getAttribute(X("X0")));
x0 = atof(S(x0S)) /unit.cm;
XString dxS(contEl->getAttribute(X("dX")));
dx = atof(S(dxS)) /unit.cm;
double y=0, z=0;
XString yzS(contEl->getAttribute(X("Y_Z")));
if (yzS.size() > 0) {
std::stringstream listr(yzS);
listr >> y >> z;
y /= unit.cm;
z /= unit.cm;
}
dref->rotate(angle);
for (int inst = 0; inst < ncopy; inst++) {
double x = x0 + inst * dx;
origin[0] = x;
origin[1] = y + s;
origin[2] = z;
Refsys *mref = new Refsys(*dref);
mref->shift(origin);
if (volumeS == volume) {
result->push_back(*mref);
delete mref;
}
else {
DOMElement* childEl = fGeomDoc->getElementById(X(volumeS));
std::vector<Refsys> *res = find(volume,mref,childEl);
delete mref;
if (res->size() > 0) {
std::vector<Refsys>::iterator it = result->end();
result->insert(it,res->begin(),res->end());
delete res;
}
else {
delete res;
break;
}
}
}
}
else if (elS == "mposY") {
double y0, dy;
XString y0S(contEl->getAttribute(X("Y0")));
y0 = atof(S(y0S)) /unit.cm;
XString dyS(contEl->getAttribute(X("dY")));
dy = atof(S(dyS)) /unit.cm;
double x=0, z=0;
XString zxS(contEl->getAttribute(X("Z_X")));
if (zxS.size() > 0) {
std::stringstream listr(zxS);
listr >> z >> x;
x /= unit.cm;
z /= unit.cm;
}
dref->rotate(angle);
for (int inst = 0; inst < ncopy; inst++) {
double y = y0 + inst * dy;
//double phi = atan2(y,x);
origin[0] = x;
origin[1] = y;
origin[2] = z;
Refsys *mref = new Refsys(*dref);
mref->shift(origin);
if (volumeS == volume) {
result->push_back(*mref);
delete mref;
}
else {
DOMElement* childEl = fGeomDoc->getElementById(X(volumeS));
std::vector<Refsys> *res = find(volume,mref,childEl);
delete mref;
if (res->size() > 0) {
std::vector<Refsys>::iterator it = result->end();
result->insert(it,res->begin(),res->end());
delete res;
}
else {
delete res;
break;
}
}
}
}
else if (elS == "mposZ") {
double z0, dz;
XString z0S(contEl->getAttribute(X("Z0")));
z0 = atof(S(z0S)) /unit.cm;
XString dzS(contEl->getAttribute(X("dZ")));
dz = atof(S(dzS)) /unit.cm;
double x=0, y=0;
XString xyS(contEl->getAttribute(X("X_Y")));
XString rphiS(contEl->getAttribute(X("R_Phi")));
if (xyS.size() > 0) {
std::stringstream listr(xyS);
listr >> x >> y;
}
else if (rphiS.size() > 0) {
double r, phi;
std::stringstream listr(rphiS);
listr >> r >> phi;
phi /= unit.rad;
x = r * cos(phi);
y = r * sin(phi);
}
x /= unit.cm;
y /= unit.cm;
dref->rotate(angle);
double phi = atan2(y,x);
origin[0] = x - s * sin(phi);
origin[1] = y + s * cos(phi);
for (int inst = 0; inst < ncopy; inst++) {
double z = z0 + inst * dz;
origin[2] = z;
Refsys *mref = new Refsys(*dref);
mref->shift(origin);
if (volumeS == volume) {
result->push_back(*mref);
delete mref;
}
else {
DOMElement* childEl = fGeomDoc->getElementById(X(volumeS));
std::vector<Refsys> *res = find(volume,mref,childEl);
delete mref;
if (res->size() > 0) {
std::vector<Refsys>::iterator it = result->end();
result->insert(it,res->begin(),res->end());
delete res;
}
else {
delete res;
break;
}
}
}
}
delete dref;
}
return result;
}