CMS 3D CMS Logo

FWGeometryTableManager.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: Core
4 // Class : FWGeometryTableManager
5 //
6 // Implementation:
7 // [Notes on implementation]
8 //
9 // Original Author:
10 // Created: Wed Jan 4 20:31:25 CET 2012
11 //
12 
13 // system include files
14 
15 // user include files
20 
21 #include "TEveUtil.h"
22 #include "TEveVector.h"
23 #include "TGeoShape.h"
24 #include "TGeoMatrix.h"
25 #include "TGeoBBox.h"
26 #include "TPRegexp.h"
27 
28 
31  m_browser(v),
32  m_filterOff(true)
33 {}
34 
36 {}
37 
39 {
40  if (m_browser->getVolumeMode())
41  return Form("%s [%d]", data.m_node->GetVolume()->GetName(), data.m_node->GetNdaughters());
42  else
43  return Form("%s [%d]", data.m_node->GetName(), data.m_node->GetNdaughters());
44 }
45 
46 //------------------------------------------------------------------------------
47 
48 FWTableCellRendererBase* FWGeometryTableManager::cellRenderer(int iSortedRowNumber, int iCol) const
49 {
51  if (m_row_to_index.empty()) return renderer;
52 
53  int unsortedRow = m_row_to_index[iSortedRowNumber];
54  if (unsortedRow < 0) printf("!!!!!!!!!!!!!!!! error %d %d \n",unsortedRow, iSortedRowNumber);
55 
56  // editor state
57  //
58  m_renderer.showEditor(unsortedRow == m_editTransparencyIdx && iCol == kTranspColumn);
59 
60  // selection state
61  //
62  const NodeInfo& data = m_entries[unsortedRow];
63  TGeoNode& gn = *data.m_node;
64  bool isSelected = data.testBit(kHighlighted) || data.testBit(kSelected);
65  // printf("cell render %s \n", data.name());
66  if (data.testBit(kSelected))
67  {
68  m_highlightContext->SetBackground(0xc86464);
69  }
70  else if (data.testBit(kHighlighted) )
71  {
72  m_highlightContext->SetBackground(0x6464c8);
73  }
74  else if (iCol == kMaterialColumn && data.testBit(kMatches) )
75  {
76  m_highlightContext->SetBackground(0xdddddd);
77  }
78 
79  // set column content
80  //
81  if (iCol == kNameColumn)
82  {
83  renderer->setData(cellName(data), isSelected);
84 
85  renderer->setIsParent(nodeIsParent(data));
86 
88 
89  int level = data.m_level - m_levelOffset;
90  if (nodeIsParent(data))
91  renderer->setIndentation(20*level);
92  else
93  renderer->setIndentation(20*level + FWTextTreeCellRenderer::iconWidth());
94 
95  return renderer;
96  }
97  else
98  {
99  // printf("title %s \n",data.m_node->GetTitle());
100  renderer->setIsParent(false);
101  renderer->setIndentation(0);
102  if (iCol == kColorColumn)
103  {
104  // m_colorBoxRenderer.setData(data.m_node->GetVolume()->GetLineColor(), isSelected);
105  m_colorBoxRenderer.setData(data.m_color, isSelected);
106  return &m_colorBoxRenderer;
107  }
108  else if (iCol == kTranspColumn)
109  {
110  renderer->setData(Form("%d", 100 -data.m_transparency), isSelected);
111  return renderer;
112  }
113  else if (iCol == kVisSelfColumn)
114  {
115  renderer->setData(getVisibility(data) ? "On" : "-", isSelected);
116  return renderer;
117  }
118  else if (iCol == kVisChildColumn)
119  {
120  renderer->setData( getVisibilityChld(data) ? "On" : "-", isSelected);
121  return renderer;
122  }
123  else if (iCol == kMaterialColumn)
124  {
125  renderer->setData( gn.GetVolume()->GetMaterial()->GetName(), isSelected);
126  return renderer;
127  }
128  else
129  { renderer->setData("ERROR", false);
130  return renderer;
131  }
132  }
133 }
134 
135 //------------------------------------------------------------------------------
136 
138 {
139  NodeInfo& parent = m_entries[parent_idx];
140  TGeoNode* parentGeoNode = parent.m_node;
141  int parentLevel = parent.m_level;
142 
143  int nV = parentGeoNode->GetNdaughters();
144  int dOff = 0;
145  for (int n = 0; n != nV; ++n)
146  {
147  NodeInfo& data = m_entries[parent_idx + n + 1 + dOff];
148  data.m_node = parentGeoNode->GetDaughter(n);
149  data.m_level = parentLevel + 1;
150  data.m_parent = parent_idx;
151  data.m_color = data.m_node->GetVolume()->GetLineColor();
152  data.m_transparency = data.m_node->GetVolume()->GetTransparency();
153  if (data.m_level <= m_browser->getAutoExpand()) data.setBit(kExpanded);
154 
155  importChildren(parent_idx + n + 1 + dOff);
156  getNNodesTotal(parentGeoNode->GetDaughter(n), dOff);
157  }
158 }
159 
160 //==============================================================================
161 
163 {
164  // Used for debug: in a NodeInfo entry look TGeoNode children from parent index and check
165  // if child is found.
166 
167  for (size_t i = 0, e = m_entries.size(); i != e; ++i)
168  {
169  if (m_entries[i].m_level > 0)
170  {
171  TGeoNode* pn = m_entries[m_entries[i].m_parent].m_node;
172  bool ok = false;
173  for (int d = 0; d < pn->GetNdaughters(); ++d)
174  {
175  if (m_entries[i].m_node == pn->GetDaughter(d))
176  {
177  ok = true;
178  break;
179  }
180  }
181  if (! ok) printf("!!!!!! node %s has false parent %s \n", m_entries[i].name(), pn->GetName());
182  }
183  }
184 }
185 
186 void FWGeometryTableManager::checkChildMatches(TGeoVolume* vol, std::vector<TGeoVolume*>& pstack)
187 {
188  if (m_volumes[vol].m_matches)
189  {
190  for (std::vector<TGeoVolume*>::iterator i = pstack.begin(); i != pstack.end(); ++i)
191  {
192  Match& pm = m_volumes[*i];
193  pm.m_childMatches = true;
194  }
195  }
196 
197  pstack.push_back(vol);
198 
199  int nD = vol->GetNdaughters(); //TMath::Min(m_browser->getMaxDaughters(), vol->GetNdaughters());
200  for (int i = 0; i < nD; ++i)
201  checkChildMatches(vol->GetNode(i)->GetVolume(), pstack);
202 
203  pstack.pop_back();
204 }
205 
206 
207 //------------------------------------------------------------------------------
208 // Callbacks
209 //------------------------------------------------------------------------------
210 namespace {
211 int matchTPME(const char* w, TPMERegexp& regexp) {
212  TString s(w);
213  return regexp.Match(s);
214 }
215 }
217 {
218  std::string filterExp = m_browser->getFilter();
219  m_filterOff = filterExp.empty();
220  printf("update filter %s OFF %d volumes size %d\n",filterExp.c_str(), m_filterOff , (int)m_volumes.size());
221 
222  if (m_filterOff || m_entries.empty()) return;
223 
224  // update volume-match entries
225  int numMatched = 0;
226 
227  TPMERegexp regexp(TString(filterExp.c_str()), "o");
228  for (Volumes_i i = m_volumes.begin(); i != m_volumes.end(); ++i)
229  {
230  int res = 0;
231 
233  {
234  res = matchTPME( i->first->GetMaterial()->GetName() , regexp);
235  }
237  {
238  res = matchTPME( i->first->GetMaterial()->GetTitle() , regexp);
239  }
240  else if (iType == FWGeometryTableView::kFilterShapeName)
241  {
242  res = matchTPME( i->first->GetShape()->GetName() , regexp);
243  }
245  {
246  res = matchTPME( i->first->GetShape()->ClassName() , regexp);
247  }
248 
249  i->second.m_matches = (res > 0);
250  i->second.m_childMatches = false;
251  if (res) numMatched++;
252  }
253 
254  printf("update filter [%d] volumes matched\n", numMatched);
255  std::vector<TGeoVolume*> pstack;
256  checkChildMatches(m_entries[0].m_node->GetVolume(), pstack);
257 
258  for (Entries_i ni = m_entries.begin(); ni != m_entries.end(); ++ni)
259  {
260  ni->resetBit(kFilterCached);
262  }
263 
264 }
265 
266 //==============================================================================
267 
268 void FWGeometryTableManager::loadGeometry(TGeoNode* iGeoTopNode, TObjArray* iVolumes)
269 {
270 #ifdef PERFTOOL_GEO_TABLE
271  ProfilerStart("loadGeo");
272 #endif
273 
274  // Prepare data for cell render.
275 
276  // clear entries
277  m_entries.clear();
278  m_row_to_index.clear();
279  m_volumes.clear();
280  m_levelOffset = 0;
281 
282  // set volume table for filters
283  Volumes_t pipi(iVolumes->GetSize());
284  m_volumes.swap(pipi);
285  TIter next( iVolumes);
286  TGeoVolume* v;
287  while ((v = (TGeoVolume*) next()) != nullptr)
288  m_volumes.insert(std::make_pair(v, Match()));
289 
290  if (!m_filterOff)
292 
293  // add top node to init
294 
295  int nTotal = 0;
296  NodeInfo topNodeInfo;
297  topNodeInfo.m_node = iGeoTopNode;
298  topNodeInfo.m_level = 0;
299  topNodeInfo.m_parent = -1;
300  topNodeInfo.m_color = iGeoTopNode->GetVolume()->GetLineColor();
301  topNodeInfo.m_transparency = iGeoTopNode->GetVolume()->GetTransparency();
302  topNodeInfo.setBitVal(kExpanded, m_browser->getAutoExpand());
303  topNodeInfo.setBitVal(kVisNodeSelf, m_browser->drawTopNode());
304 
305  getNNodesTotal(topNodeInfo.m_node , nTotal);
306  m_entries.resize(nTotal+1);
307  m_entries[0] = topNodeInfo;
308 
309  importChildren(0);
310 
311  // checkHierarchy();
312 
313 #ifdef PERFTOOL_GEO_TABLE
314  ProfilerStop();
315 #endif
316 }
317 
318 //------------------------------------------------------------------------------
319 
321 {
322  std::cerr << "not implemented \n";
323 }
324 
325 //------------------------------------------------------------------------------
326 
328 {
329  m_row_to_index.clear();
330 
331  int i = TMath::Max(0, m_browser->getTopNodeIdx());
332  m_row_to_index.push_back(i);
333 
334  NodeInfo& data = m_entries[i];
335 
336  if (!m_filterOff)
337  assertNodeFilterCache(data);
338 
339  if ((m_filterOff && data.testBit(kExpanded) == false) ||
340  (m_filterOff == false && data.testBit(kChildMatches) == false))
341  return;
342 
343  if (m_browser->getVolumeMode())
345  else
347 
348  // printf (" child [%d] FWGeometryTableManagerBase::recalculateVisibility table size %d \n", (int)m_row_to_index.size());
349 }
350 
351 //------------------------------------------------------------------------------
352 
354 {
355  TGeoNode* parentNode = m_entries[pIdx].m_node;
356  int nD = parentNode->GetNdaughters();
357  int dOff=0;
358 
359  // printf("----------- parent %s\n", parentNode->GetName() );
360 
361  std::vector<int> vi;
362  vi.reserve(nD);
363 
364  for (int n = 0; n != nD; ++n)
365  {
366  int idx = pIdx + 1 + n + dOff;
368 
369  bool toAdd = true;
370  for (std::vector<int>::iterator u = vi.begin(); u != vi.end(); ++u )
371  {
372  TGeoVolume* neighbourVolume = parentNode->GetDaughter(*u)->GetVolume();
373  if (neighbourVolume == data.m_node->GetVolume())
374  {
375  toAdd = false;
376  // printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n");
377  break;
378  }
379  }
380 
381  if (toAdd)
382  {
383  vi.push_back(n);
384  if (m_filterOff)
385  {
386  // std::cout << data.nameIndent() << std::endl;
387  m_row_to_index.push_back(idx);
389  }
390  else
391  {
392  assertNodeFilterCache(data);
393  if (data.testBitAny(kMatches | kChildMatches)) m_row_to_index.push_back(idx);
395  }
396  }
397  FWGeometryTableManagerBase::getNNodesTotal(parentNode->GetDaughter(n), dOff);
398  }
399 }
400 
401 //------------------------------------------------------------------------------
402 
404 {
405  TGeoNode* parentNode = m_entries[pIdx].m_node;
406  int nD = parentNode->GetNdaughters();
407  int dOff = 0;
408  for (int n = 0; n != nD; ++n)
409  {
410  int idx = pIdx + 1 + n + dOff;
412 
413  if (m_filterOff)
414  {
415  m_row_to_index.push_back(idx);
417  }
418  else
419  {
420  assertNodeFilterCache(data);
421  if (data.testBitAny(kMatches | kChildMatches)) m_row_to_index.push_back(idx);
423  }
424 
425  FWGeometryTableManagerBase::getNNodesTotal(parentNode->GetDaughter(n), dOff);
426  }
427 }
428 
429 //------------------------------------------------------------------------------
430 
432 {
433  if (! data.testBit(kFilterCached))
434  {
435  bool matches = m_volumes[data.m_node->GetVolume()].m_matches;
436  // if (matches) printf("%s matches filter \n", data.name());
437  data.setBitVal(kMatches, matches);
438  setVisibility(data, matches);
439 
440  bool childMatches = m_volumes[data.m_node->GetVolume()].m_childMatches;
441  data.setBitVal(kChildMatches, childMatches);
442  data.setBitVal(kExpanded, childMatches);
443  setVisibilityChld(data, childMatches);
444 
445  data.setBit(kFilterCached);
446  // printf("%s matches [%d] childMatches [%d] ................ %d %d \n", data.name(), data.testBit(kMatches), data.testBit(kChildMatches), matches , childMatches);
447  }
448 }
449 
450 //------------------------------------------------------------------------------
451 
453 {
454  if (m_browser->getVolumeMode())
455  {
456  if (data.m_node->GetVolume()->IsVisible() != x)
457  {
459  data.m_node->GetVolume()->SetVisibility(x);
460  }
461  }
462  else
463  {
464  data.setBitVal(kVisNodeSelf, x);
465  }
466 }
467 
468 //------------------------------------------------------------------------------
469 
471 {
472  if (m_browser->getVolumeMode())
473  {
474  if (data.m_node->GetVolume()->IsVisibleDaughters() != x)
475  {
476  TEveGeoManagerHolder gmgr( FWGeometryTableViewManager::getGeoMangeur());
477  data.m_node->GetVolume()->VisibleDaughters(x);
478  }
479  }
480  else
481  {
482  data.setBitVal(kVisNodeChld, x);
483  }
484 }
485 //______________________________________________________________________________
486 
488 {
489  TGeoNode *parentNode = m_entries[selectedIdx].m_node;
490  int nD = parentNode->GetNdaughters();
491  int dOff = 0;
492  for (int n = 0; n != nD; ++n)
493  {
494  int idx = selectedIdx + 1 + n + dOff;
496 
497  setVisibility(data, v);
498  setVisibilityChld(data, v);
499 
500  FWGeometryTableManager::getNNodesTotal(parentNode->GetDaughter(n), dOff);
501  }
502 }
503 
504 //------------------------------------------------------------------------------
505 
507 {
508  if (m_browser->getVolumeMode())
509  return data.m_node->GetVolume()->IsVisible();
510 
511  return data.testBit(kVisNodeSelf);
512 }
513 
515 {
516  if (m_browser->getVolumeMode())
517  return data.m_node->GetVolume()->IsVisibleDaughters();
518 
519  return data.testBit(kVisNodeChld);
520 }
521 
522 //------------------------------------------------------------------------------
523 
525 {
526  return (data.m_node->GetNdaughters() != 0) && (m_filterOff || data.testBit(kChildMatches));
527 }
528 
529 //------------------------------------------------------------------------------
530 
532 {
533  double sqr_r = radius * radius;
534 
535  for (Entries_i ni = m_entries.begin(); ni != m_entries.end(); ++ni)
536  ni->resetBit(kVisNodeChld);
537 
538  int cnt = 0;
539  TEveGeoManagerHolder mangeur( FWGeometryTableViewManager::getGeoMangeur());
540  printf("FWGeometryTableManagerBase::checkRegionOfInterest BEGIN r=%d center= (%.1f, %.1f, %.1f)\n ", (int)radius, center[0], center[1], center[2]);
541  TGeoIterator git(m_entries[0].m_node->GetVolume());
542  Entries_i eit(m_entries.begin());
543  while (git())
544  {
545  const TGeoMatrix *gm = git.GetCurrentMatrix();
546  const TGeoBBox *bb = static_cast<TGeoBBox*>(eit->m_node->GetVolume()->GetShape());
547  const Double_t *bo = bb->GetOrigin();
548  const Double_t bd[] = { bb->GetDX(), bb->GetDY(), bb->GetDZ() };
549  const Double_t *cc = center;
550 
551  bool visible = false;
552 
553  switch (algo)
554  {
556  {
557  const Double_t *t = gm->GetTranslation();
558  TEveVectorD d(cc[0] - (t[0] + bo[0]), cc[1] - (t[1] + bo[1]), cc[2] - (t[2] + bo[2]));
559  Double_t sqr_d = d.Mag2();;
560  visible = (sqr_d <= sqr_r);
561  break;
562  }
564  {
565  assert (gm->IsScale() == false);
566 
567  const Double_t *t = gm->GetTranslation();
568  const Double_t *r = gm->GetRotationMatrix();
569  TEveVectorD d(cc[0] - (t[0] + bo[0]), cc[1] - (t[1] + bo[1]), cc[2] - (t[2] + bo[2]));
570  Double_t sqr_d = 0;
571  for (Int_t i = 0; i < 3; ++i)
572  {
573  Double_t dp = d[0]*r[i] + d[1]*r[i+3] + d[2]*r[i+6];
574  if (dp < -bd[i])
575  {
576  Double_t delta = dp + bd[i];
577  sqr_d += delta * delta;
578  }
579  else if (dp > bd[i])
580  {
581  Double_t delta = dp - bd[i];
582  sqr_d += delta * delta;
583  }
584  }
585  visible = (sqr_d <= sqr_r);
586  }
587  }
588 
589  if (visible)
590  {
591  eit->setBit(kVisNodeSelf);
592  int pidx = eit->m_parent;
593  while (pidx >= 0)
594  {
595  m_entries[pidx].setBit(kVisNodeChld);
596  pidx = m_entries[pidx].m_parent;
597  ++cnt;
598  }
599  }
600  else
601  {
602  eit->resetBit(kVisNodeSelf);
603  }
604  eit++;
605  }
606 
607  printf("FWGeometryTableManager::checkRegionOfInterest END [%d]\n ", cnt);
608 }
609 
611 {
612  for (Entries_i ni = m_entries.begin(); ni != m_entries.end(); ++ni)
613  {
614  ni->setBit(kVisNodeSelf);
615  ni->setBit(kVisNodeChld);
616  }
617  // ni->setMatchRegion(true);
618 }
dbl * delta
Definition: mlp_gen.cc:36
FWGeometryTableView * m_browser
const double w
Definition: UKUtility.cc:23
bool getVisibility(const NodeInfo &nodeInfo) const override
FWGeometryTableManager(FWGeometryTableView *)
void checkRegionOfInterest(double *center, double radius, long algo)
std::string getFilter() const
std::unordered_map< TGeoVolume *, Match > Volumes_t
const char * cellName(const NodeInfo &data) const override
void setDaughtersSelfVisibility(int i, bool v) override
FWTableCellRendererBase * cellRenderer(int iSortedRowNumber, int iCol) const override
Definition: Electron.h:6
bool getVisibilityChld(const NodeInfo &nodeInfo) const override
void assertNodeFilterCache(NodeInfo &data)
virtual void setIndentation(int indentation=0)
T Max(T a, T b)
Definition: MathUtil.h:44
static void getNNodesTotal(TGeoNode *geoNode, int &off)
void setData(const std::string &, bool isSelected)
void loadGeometry(TGeoNode *iGeoTopNode, TObjArray *iVolumes)
void checkChildMatches(TGeoVolume *v, std::vector< TGeoVolume * > &)
def Match(required, got)
bool getVolumeMode() const
void setVisibility(NodeInfo &nodeInfo, bool) override
char data[epos_bytes_allocation]
Definition: EPOS_Wrapper.h:82
Volumes_t::iterator Volumes_i
void importChildren(int parent_idx)
void setVisibilityChld(NodeInfo &nodeInfo, bool) override
bool nodeIsParent(const NodeInfo &) const override
virtual void showEditor(bool value)