forked from cms-sw/cmssw
/
DDExpandedView.cc
545 lines (449 loc) · 12.7 KB
/
DDExpandedView.cc
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
#include "DetectorDescription/Core/interface/DDExpandedView.h"
#include "DetectorDescription/Core/interface/DDComparator.h"
/**
After construction the instance corresponds to the root of the geometrical tree.
*/
DDExpandedView::DDExpandedView( const DDCompactView & cpv )
: walker_(0),
w2_(cpv.graph(),cpv.root()),
trans_( DDTranslation()),
rot_( DDRotationMatrix()),
depth_( 0 ),
worldpos_( cpv.worldPosition())
{
walker_ = &w2_;
DDPosData * pd((*walker_).current().second);
if (!pd)
pd = worldpos_;
DDExpandedNode expn((*walker_).current().first,
pd,
trans_,
rot_,
0);
// starting point for position calculations, == root of expanded view
history_.push_back(expn);
}
DDExpandedView::~DDExpandedView() { }
const DDLogicalPart & DDExpandedView::logicalPart() const
{
return history_.back().logp_;
}
const DDTranslation & DDExpandedView::translation() const
{
return history_.back().trans_;
}
const DDRotationMatrix & DDExpandedView::rotation() const
{
return history_.back().rot_;
}
const DDGeoHistory & DDExpandedView::geoHistory() const
{
return history_;
}
int DDExpandedView::depth() const
{
return depth_;
}
int DDExpandedView::copyno() const
{
return history_.back().copyno();
}
namespace {
struct Counter {
int same;
int diff;
~Counter() {
}
};
inline Counter & counter() {
static Counter local;
return local;
}
}
/**
returns true, if a next sibling exists and updates \c this
otherwise returns false.
If a scope was set, the nextSibling of the root of the scope is not selected.
*/
bool DDExpandedView::nextSibling()
{
bool result(false);
if (scope_.size() && history_.back() == scope_.back()) {
; // no-next-sibling, if current node is the root of the scope!
}
else {
if ((*walker_).nextSibling()) {
DDExpandedNode & expn(history_.back()); // back of history_ is always current node
DDCompactView::walker_type::value_type curr = (*walker_).current();
DDPosData const * posdOld = expn.posd_;
expn.logp_=curr.first;
expn.posd_=curr.second;
DDGeoHistory::size_type hsize = history_.size();
if (hsize>1) {
const DDExpandedNode & expnBefore(history_[hsize-2]);
// T = T1 + INV[R1] * T2
expn.trans_ = expnBefore.trans_ + (expnBefore.rot_ * expn.posd_->trans_);
// R = R1*INV[R2]
// VI in principle we can do this
if ( !(expn.posd_->rot()==posdOld->rot()) ) {
expn.rot_ = expnBefore.rot_ * expn.posd_->rot();//.inverse();
++counter().diff;
}else ++counter().same;
}
else {
expn.trans_ = expn.posd_->trans_;
expn.rot_ = expn.posd_->rot();//.inverse();
}
++expn.siblingno_;
result = true;
}
}
return result;
}
/**
returns true, if a child of the current node exists and updates \c this
otherwise returns false
*/
bool DDExpandedView::firstChild()
{
bool result(false);
bool depthNotReached(true);
// Check for the depth within the scope ...
if (depth_) {
if ( (history_.size()-scope_.size())==depth_ ) {
depthNotReached=false;
}
}
if (depthNotReached) {
if ((*walker_).firstChild()) {
DDExpandedNode & expnBefore(history_.back());
DDCompactView::walker_type::value_type curr = (*walker_).current();
DDPosData * newPosd = curr.second;
// T = ... (see nextSiblinig())
DDTranslation newTrans = expnBefore.trans_ + expnBefore.rot_ * newPosd->trans_;
// R = ... (see nextSibling())
DDRotationMatrix newRot = expnBefore.rot_ * newPosd->rot();//.inverse();
// create a new Expanded node and push it to the history ...
DDExpandedNode expn(curr.first, curr.second,
newTrans, newRot, 0);
history_.push_back(expn);
result = true;
} // if firstChild
} // if depthNotReached
return result;
}
/**
returns ture, if a parent exists and updates \c this otherwise returns
false. When false is returned, the root node of the scope is reached.
*/
bool DDExpandedView::parent()
{
bool result(false);
bool scopeRoot(false);
// check for a scope
if (scope_.size()) {
if (scope_.back() == history_.back()) {
// the current node is the root of the scope
scopeRoot = true;
}
}
if (!scopeRoot) {
if ((*walker_).parent()) {
history_.pop_back();
result = true;
}
}
return result;
}
/*
bool DDExpandedView::hasChildren() const
{
bool result = false;
return result;
}
*/
// same implementation as in GraphWalker !
/**
Tree transversal:
- try to go to the first child
- else try to go to the next sibling
- else try to go to the next sibling of the parent
Currently the whole remaining subtree is transversed when next() is
subsequently called.
\todo stop, when subtree down a specified node has been transversed
*/
bool DDExpandedView::next()
{
bool res(false);
if(firstChild())
res=true;
else if(nextSibling())
res=true;
else {
while(parent()) {
//DCOUT('C', "pa=" << logicalPart() );
if(nextSibling()) {
//DCOUT('C', "ns=" << logicalPart() );
res=true;
break;
}
}
//DCOUT('C', current().first << " "<< current().second );
}
return res;
}
/** broad first */
bool DDExpandedView::nextB()
{
bool res(false);
return res;
}
void dump(const DDGeoHistory & h)
{
DDGeoHistory::const_iterator it = h.begin();
edm::LogInfo("DDExpandedView") << "--GeoHistory-Dump--[" << std::endl;
int i=0;
for (; it != h.end(); ++it) {
edm::LogInfo("DDExpandedView") << " " << i << it->logicalPart() << std::endl;
++i;
}
edm::LogInfo("DDExpandedView") << "]---------" << std::endl;
}
/**
User specific data can be attac
hed to single nodes or a selection of
nodes in the expanded view through the DDSpecifics interface.
The resulting std::vector is of size 0 if no specific data was attached.
*/
std::vector< const DDsvalues_type *> DDExpandedView::specifics() const
{
// backward compatible
std::vector<const DDsvalues_type * > result;
specificsV(result);
return result;
}
void
DDExpandedView::specificsV(std::vector<const DDsvalues_type * > & result) const
{
unsigned int i(0);
const std::vector<std::pair<DDPartSelection*, DDsvalues_type*> > & specs = logicalPart().attachedSpecifics();
if( specs.size())
{
result.reserve(specs.size());
for (; i<specs.size(); ++i) {
const std::pair<DDPartSelection*,DDsvalues_type*>& sp = specs[i];
// a part selection
const DDPartSelection & psel = *(sp.first);
const DDGeoHistory & hist = geoHistory();
if (DDCompareEqual(hist, psel)())
result.push_back( sp.second );
}
}
}
DDsvalues_type DDExpandedView::mergedSpecifics() const {
DDsvalues_type merged;
mergedSpecificsV(merged);
return merged;
}
void DDExpandedView::mergedSpecificsV(DDsvalues_type & merged) const
{
merged.clear();
const std::vector<std::pair<DDPartSelection*, DDsvalues_type*> > & specs = logicalPart().attachedSpecifics();
if (specs.empty()) return;
const DDGeoHistory & hist = geoHistory();
for (size_t i=0; i<specs.size(); ++i) {
const std::pair<DDPartSelection*,DDsvalues_type*>& sp = specs[i];
const DDPartSelection & psel = *(sp.first);
if (DDCompareEqual(hist, psel)())
merge(merged,*sp.second);
}
}
/**
All navigational commands only operate in the subtree rooted by the
node marked by the node of the DDGeoHistory returned by this method.
If the size() of the scope equals 0, the full scope covering the
whole expanded-view is set (default).
*/
const DDGeoHistory & DDExpandedView::scope() const
{
return scope_;
}
void DDExpandedView::clearScope()
{
scope_.clear();
depth_=0;
}
void DDExpandedView::reset()
{
clearScope();
while(parent())
;
}
/**
The scope of the expanded-view is set to the subtree rooted by the node
marked by the DDGeohistory hist.
The current not of the expanded view is set to the root of the subtree.
All navigational methods apply only on the subtree.
In case of hist not marking a valid node in the expanded-view, the
state of the expanded-view is unchanged and false is returned by setScope().
Otherwise true is returned.
*/
bool DDExpandedView::setScope(const DDGeoHistory & sc, int depth)
{
bool result(false);
DDGeoHistory buf = scope_; // save current scope
scope_.clear(); // sets scope to global (full) scope
while (parent()) ; // move up to the root of the expanded-view
if (descend(sc)) { // try to move down the given scope-history ...
scope_ = sc;
depth_ = depth;
result = true;
}
else {
scope_ = buf;
}
return result;
}
/**
goTo will reset the ExpandedView if pos is not a valid position.
Currently no checks are implemented to verify that pos is within the
current scope of the ExpandedView.
\todo check whether pos is in the current scope
*/
bool DDExpandedView::goToHistory(const DDGeoHistory & pos)
{
bool result = true;
int tempD = depth_;
//DCOUT('G', " goto- target= " << pos );
DDGeoHistory tempScope = scope_;
reset();
DDGeoHistory::size_type s = pos.size();
for( DDGeoHistory::size_type j=1; j<s; ++j) {
if (! firstChild()) {
result = false;
//edm::LogError("DDExpandedView") << " ERROR! , wrong usage of DDExpandedView::goTo! " << std::endl;
//exit(1);
break;
}
int i=0;
for (; i<pos[j].siblingno(); ++i) {
if (! nextSibling()) {
//edm::LogError("DDExpandedView") << " ERROR! , wrong usage of DDExpandedView::goTo! " << std::endl;
result = false;
}
}
}
if (!result) {
reset();
setScope(tempScope, tempD);
}
else {
scope_ = tempScope;
depth_ = tempD;
}
//DCOUT('G', " goto-result = " << history_ );
return result;
}
//! \todo implement it simpler using DDExpandedNode::siblingno()
bool DDExpandedView::descend(const DDGeoHistory & sc)
{
DDGeoHistory::size_type mxx = sc.size();
DDGeoHistory::size_type cur = 0;
bool result(false);
/* algo: compare currerent node in expanded-view with current-node in sc
if matching:
(A)go to first child in expanded-view, go one level deeper in sc
iterate over all children in expanded-view until one of them
matches the current node in sc.
if no one matches, return false
else continue at (A)
else return false
*/
const DDExpandedNode & curNode = history_.back();
if (sc.size()) {
//DCOUT('x', "curN=" << curNode.logicalPart() << " scope[0]=" << sc[cur].logicalPart() );
if (curNode==sc[cur]) {
bool res(false);
while(cur+1 < mxx && firstChild()) {
++cur;
//DCOUT('x', "fc-curN=" << history_.back().logicalPart() << " scope[x]=" << sc[cur].logicalPart() );
if (!(history_.back()==sc[cur])) {
while(nextSibling()) {
//DCOUT('x', "ns-curN=" << history_.back().logicalPart() << " scope[x]=" << sc[cur].logicalPart() );
if (history_.back()==sc[cur]) {
res=true;
break;
}
}
}
else {
res=true;
}
if (res==false)
break;
}
result = res;
}
}
return result;
}
bool DDExpandedView::goTo(const nav_type & newpos) {
return goTo(&newpos.front(),newpos.size());
}
bool DDExpandedView::goTo(NavRange newpos) {
return goTo(newpos.first,newpos.second);
}
bool DDExpandedView::goTo(int const * newpos, size_t sz)
{
bool result(false);
// save the current position
//nav_type savedPos = navPos();
DDGeoHistory savedPos = history_;
// reset to root node
//FIXME: reset to root of scope!!
reset();
// try to navigate down to the newpos
for (size_t i = 1; i < sz; ++i) {
result = firstChild();
if (result) {
int pos = newpos[i];
for(int k=0; k<pos; ++k) {
result = nextSibling();
}
}
else {
break;
}
}
if (!result) {
goToHistory(savedPos);
}
return result;
}
DDExpandedView::nav_type DDExpandedView::navPos() const
{
DDGeoHistory::size_type i=0;
DDGeoHistory::size_type j=history_.size();
nav_type pos(j);
for (;i<j;++i)
pos[i] = history_[i].siblingno();
return pos;
}
DDExpandedView::nav_type DDExpandedView::copyNumbers() const
{
DDGeoHistory::size_type it = 0;
DDGeoHistory::size_type sz = history_.size();
nav_type result(sz);
for (; it < sz; ++it) {
result[it] = history_[it].copyno();
}
return result;
}
std::ostream & printNavType(std::ostream & os, int const * n, size_t sz){
os << '(' ;
for (int const * it=n; it != n+sz; ++it) {
os << *it << ',';
}
os << ')';
return os;
}