CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_5_3_13_patch3/src/Fireworks/Core/src/FWOverlapTableManager.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     Core
00004 // Class  :     FWOverlapTableManager
00005 // 
00006 // Implementation:
00007 //     [Notes on implementation]
00008 //
00009 // Original Author:  
00010 //         Created:  Wed Jan  4 20:31:32 CET 2012
00011 // $Id: FWOverlapTableManager.cc,v 1.8 2012/04/25 06:09:35 amraktad Exp $
00012 //
00013 
00014 // system include files
00015 
00016 // user include files
00017 #include "Fireworks/Core/src/FWOverlapTableManager.h"
00018 #include "Fireworks/Core/src/FWOverlapTableView.h"
00019 #include "Fireworks/Core/interface/FWGeometryTableViewManager.h"
00020 #include "Fireworks/Core/interface/fwLog.h"
00021 
00022 #include "TEvePointSet.h"
00023 #include "TGeoVolume.h"
00024 #include "TGeoMatrix.h"
00025 #include "TGeoShape.h"
00026 #include "TGeoBBox.h"
00027 #include "TGeoMatrix.h"
00028 #include "TEveUtil.h"
00029 #include "TObjString.h"
00030 #include "TGeoNode.h"
00031 #include "TGeoOverlap.h"
00032 #include "TGeoManager.h"
00033 #include "TPolyMarker3D.h"
00034 
00035 #include "TStopwatch.h"
00036 #include "TTimer.h"
00037 #include "TGeoPainter.h"
00038 #include "TPRegexp.h"
00039 
00040 FWOverlapTableManager::FWOverlapTableManager(FWOverlapTableView* v ):
00041    FWGeometryTableManagerBase(),
00042    m_browser(v)
00043 {
00044 }
00045 
00046 FWOverlapTableManager::~FWOverlapTableManager()
00047 {
00048 }
00049 
00050 
00051 
00052 std::vector<std::string> FWOverlapTableManager::getTitles() const 
00053 {
00054    std::vector<std::string> returnValue;
00055    returnValue.reserve(numberOfColumns());
00056 
00057    returnValue.push_back("Name");
00058    returnValue.push_back("Color");
00059    returnValue.push_back("Opcty");
00060    returnValue.push_back("RnrSelf");
00061    returnValue.push_back("RnrChildren");
00062    returnValue.push_back("Overlap");
00063    returnValue.push_back("RnrMarker");
00064    return returnValue;
00065 }
00066 
00067 
00068 
00069 
00070 //---------------------------------------------------------------------------------
00071 void FWOverlapTableManager::importOverlaps(std::string iPath, double iPrecision)
00072 {
00073    m_entries.clear();
00074    m_mapNodeOverlaps.clear();
00075    m_browser->m_markerVertices.clear();
00076    m_browser->m_markerIndices.clear();
00077 
00078    TEveGeoManagerHolder mangeur( FWGeometryTableViewManager::getGeoMangeur());
00079    // gGeoManager->cd();
00080    NodeInfo topNodeInfo;
00081    topNodeInfo.m_node   = gGeoManager->GetTopNode();
00082    topNodeInfo.m_level  = 0;
00083    topNodeInfo.m_color  = gGeoManager->GetTopNode()->GetVolume()->GetLineColor();
00084    topNodeInfo.m_transparency  = gGeoManager->GetTopNode()->GetVolume()->GetTransparency();
00085    topNodeInfo.m_parent = -1;
00086    topNodeInfo.resetBit(kVisNodeSelf);
00087 
00088    m_entries.resize(gGeoManager->GetNNodes());
00089    m_entries[0] = topNodeInfo;
00090 
00091    m_entries.resize( gGeoManager->GetNNodes());
00092   
00093    TGeoVolume* topVol =  topNodeInfo.m_node->GetVolume();
00094    Int_t icheck = 0;
00095    Int_t ncheck = 0;
00096    TStopwatch *timer;
00097    Int_t i;  
00098    bool checkingOverlaps = false;
00099    TGeoManager *geom = topVol->GetGeoManager();
00100    ncheck = topNodeInfo.m_node->CountDaughters(kFALSE);
00101    timer = new TStopwatch();
00102    geom->ClearOverlaps();
00103    geom->SetCheckingOverlaps(kTRUE);
00104 
00105    int oldS = 0;
00106    timer->Start();
00107    geom->GetGeomPainter()->OpProgress(topVol->GetName(),icheck,ncheck,timer,kFALSE);
00108 //   topVol->CheckOverlaps(iPrecision);
00109    icheck++;
00110    TGeoIterator git(topVol);
00111    Entries_i eit = m_entries.begin();
00112    /*
00113      if (gGeoManager->GetListOfOverlaps()->GetEntriesFast()) {
00114      int newCnt =  gGeoManager->GetListOfOverlaps()->GetEntriesFast();
00115      for (int i=0; i<newCnt; ++i) {
00116      addOverlapEntry((TGeoOverlap*)gGeoManager->GetListOfOverlaps()->At(i), new TGeoHMatrix(*geom->GetCurrentMatrix()), topNode, next); 
00117      }
00118      oldS= newCnt;
00119      }*/
00120    eit++;
00121    TGeoNode *node;
00122    icheck = 1;
00123   
00124    int topNodeIdx =  m_browser->getTopNodeIdx();
00125 
00126    while ((node=git())) {
00127       if (!eit->testBit(kOverlap)) eit->resetBit(kVisNodeSelf);
00128       eit->m_node = node;
00129       eit->m_color = node->GetVolume()->GetLineColor();
00130       eit->m_transparency = node->GetVolume()->GetTransparency();
00131       eit->m_level = git.GetLevel();
00132       eit->m_parent = icheck;
00133      
00134      if ((topNodeIdx )== icheck || !topNodeIdx  ) { 
00135      //  printf("start to check overlaps on topNodeIdx %s \n", eit->name());
00136        checkingOverlaps=true;
00137      }
00138      else if (checkingOverlaps && ( eit->m_level <= m_entries[m_browser->getTopNodeIdx()].m_level)) 
00139      {
00140        checkingOverlaps=false;
00141      }
00142       // parent index
00143       Entries_i pit = eit;
00144       do 
00145       {
00146          --pit;
00147          --(eit->m_parent);
00148          if (pit->m_level <  eit->m_level) 
00149             break;
00150       } while (pit !=  m_entries.begin());
00151 
00152       // overlap bits
00153       if ( checkingOverlaps) {
00154          if (!node->GetVolume()->IsSelected()) {
00155             geom->GetGeomPainter()->OpProgress(node->GetVolume()->GetName(),icheck+1,ncheck,timer,kFALSE);
00156             node->GetVolume()->SelectVolume(kFALSE);
00157 
00158             node->GetVolume()->CheckOverlaps(iPrecision);
00159 
00160             if (oldS !=  gGeoManager->GetListOfOverlaps()->GetEntriesFast()) {
00161               // printf("mother %s overlaps \n", node->GetName());
00162             
00163               eit->setBit(kOverlapChild);
00164               eit->setBit(kVisNodeChld);              
00165               eit->setBit(kVisMarker);
00166               
00167                TGeoHMatrix* motherm = new TGeoHMatrix(*geom->GetCurrentMatrix());        
00168                {
00169                   TGeoNode* ni = topNodeInfo.m_node;
00170                   for (Int_t i=1; i<=git.GetLevel(); i++) {
00171                      ni = ni->GetDaughter(git.GetIndex(i));
00172                      motherm->Multiply(ni->GetMatrix());
00173                   }
00174                }
00175         
00176                int newCnt =  gGeoManager->GetListOfOverlaps()->GetEntriesFast();       
00177           
00178                for (int i=oldS; i<newCnt; ++i)
00179                {
00180                   //                  printf("add %p %p \n", (void*)node->GetVolume(), (void*)m_entries[icheck].m_node->GetVolume());
00181                   addOverlapEntry((TGeoOverlap*)gGeoManager->GetListOfOverlaps()->At(i), i, icheck, motherm); 
00182                }
00183             
00184                oldS = newCnt;
00185             } 
00186          }   
00187       }
00188       eit++; 
00189       icheck ++;    
00190    } 
00191   
00192    topVol->SelectVolume(kTRUE);
00193    geom->SetCheckingOverlaps(kFALSE);
00194    //   geom->SortOverlaps();
00195    TObjArray *overlaps = geom->GetListOfOverlaps();
00196    Int_t novlps = overlaps->GetEntriesFast();     
00197    TNamed *obj;
00198    for (i=0; i<novlps; i++) {
00199       obj = (TNamed*)overlaps->At(i);
00200       obj->SetName(Form("ov%05d",i));
00201    }
00202    geom->GetGeomPainter()->OpProgress("Check overlaps:",icheck,ncheck,timer,kTRUE);
00203    Info("CheckOverlaps", "Number of illegal overlaps/extrusions : %d\n", novlps);
00204    delete timer;
00205 }
00206 
00207 
00208 //______________________________________________________________________________
00209 
00210 
00211 void FWOverlapTableManager::addOverlapEntry(TGeoOverlap* ovl, int ovlIdx,  Int_t parentIdx, TGeoHMatrix* motherm)
00212 {     
00213 
00214    // printf("add %s \n", ovl->GetTitle());
00215    // get doughter indices of overlaps
00216    /* 
00217       TPMERegexp re(" ", "o");
00218       re.Split(TString(ovl->GetTitle()));
00219       printf("add title %s \n", ovl->GetTitle());
00220    */
00221    int pcnt = parentIdx+1;
00222    int dOff =0;
00223    TGeoNode* mothern = m_entries[parentIdx].m_node;
00224 
00225    for (int i = 0; i < mothern->GetNdaughters(); ++i)
00226    {
00227       TGeoNode* n = mothern->GetDaughter(i);
00228 
00229       int  cnt = pcnt + i+dOff;
00230 
00231       if (ovl->IsOverlap()) { 
00232          if (n->GetVolume() == ovl->GetFirstVolume() && (*(ovl->GetFirstMatrix()) == *(n->GetMatrix())))
00233          {
00234             // std::string x = re[0].Data();
00235             //if (x.find(n->GetName()) == std::string::npos) printf("ERROT \n");
00236 
00237            m_entries[cnt].setBit(kOverlap);
00238            m_entries[cnt].setBit(kVisNodeSelf);
00239            m_mapNodeOverlaps.insert(std::pair<int, int>(cnt, ovlIdx));
00240            int nno; n->GetOverlaps(nno); 
00241            nno |= BIT(1); n->SetOverlaps(0, nno); 
00242          }
00243 
00244       }
00245 
00246       if (n->GetVolume() == ovl->GetSecondVolume() && (*(ovl->GetSecondMatrix()) == *(n->GetMatrix())))
00247       {
00248          //printf("-----------------------------------------------\n");
00249          // std::string x = re[2].Data();
00250          // if (x.find(n->GetName()) == std::string::npos) printf("ERROT \n");
00251 
00252         m_entries[cnt].setBit(kOverlap);
00253         m_entries[cnt].setBit(kVisNodeSelf);
00254         
00255         m_mapNodeOverlaps.insert(std::pair<int, int>(cnt, ovlIdx));        
00256         int nno; n->GetOverlaps(nno); 
00257         nno |= (ovl->IsOverlap()  ? BIT(1) : BIT(2)); 
00258         n->SetOverlaps(0, nno); 
00259         
00260       }
00261 
00262 
00263       FWGeometryTableManagerBase::getNNodesTotal(n, dOff);  
00264    }
00265 
00266    TPolyMarker3D* pm = ovl->GetPolyMarker();
00267    for (int j=0; j<pm->GetN(); ++j )
00268    {
00269       double pl[3];
00270       double pg[3];
00271       pm->GetPoint(j, pl[0], pl[1], pl[2]);
00272       motherm->LocalToMaster(pl, pg);
00273      m_browser->m_markerIndices.push_back(ovl->IsExtrusion() ? -parentIdx : parentIdx);
00274      
00275       m_browser->m_markerVertices.push_back( pg[0]);
00276       m_browser->m_markerVertices.push_back( pg[1]);
00277       m_browser->m_markerVertices.push_back( pg[2]);
00278    }
00279 
00280 
00281    int aIdx = parentIdx;
00282    int aLev = m_entries[aIdx].m_level;
00283    int topNodeIdx =  m_browser->getTopNodeIdx();
00284 
00285    while(aIdx > topNodeIdx)
00286    {
00287       aIdx--;
00288       if (m_entries[aIdx].m_level < aLev)
00289       {
00290         m_entries[aIdx].setBit(kOverlapChild);
00291         m_entries[aIdx].setBit(kVisNodeChld);
00292          //  printf("stamp %s \n", m_entries[aIdx].name());
00293          aLev--;
00294       }
00295    }
00296 }
00297 
00298 
00299 //_____________________________________________________________________________
00300 
00301 void FWOverlapTableManager::recalculateVisibility( )
00302 {
00303    // printf("overlap recalcuate vis \n");
00304    m_row_to_index.clear();
00305    int i = m_browser->getTopNodeIdx();
00306    m_row_to_index.push_back(i);
00307 
00308    if (m_entries[i].testBit(kExpanded)  )
00309       recalculateVisibilityNodeRec(i);
00310 }
00311 
00312 void FWOverlapTableManager::recalculateVisibilityNodeRec( int pIdx)
00313 {
00314    TGeoNode* parentNode = m_entries[pIdx].m_node;
00315    int nD = parentNode->GetNdaughters();
00316    int dOff=0;
00317    for (int n = 0; n != nD; ++n)
00318    {
00319       int idx = pIdx + 1 + n + dOff;
00320       NodeInfo& data = m_entries[idx];
00321 
00322 
00323       if (m_browser->listAllNodes() || data.testBitAny(kOverlap | kOverlapChild))
00324          m_row_to_index.push_back(idx);
00325  
00326       if ((m_browser->listAllNodes() || data.testBit(kOverlapChild)) && data.testBit(kExpanded) )
00327          recalculateVisibilityNodeRec(idx);
00328 
00329       FWGeometryTableManagerBase::getNNodesTotal(parentNode->GetDaughter(n), dOff);
00330    }
00331 } 
00332 
00333 
00334 //______________________________________________________________________________
00335 
00336 bool  FWOverlapTableManager::nodeIsParent(const NodeInfo& data) const
00337 {
00338    return  m_browser->listAllNodes() ? data.m_node->GetNdaughters() : data.testBit(kOverlapChild) ;
00339 }
00340 
00341 void FWOverlapTableManager::printOverlaps(int idx) const
00342 {
00343   
00344   TEveGeoManagerHolder gmgr( FWGeometryTableViewManager::getGeoMangeur());
00345   std::pair<std::multimap<int, int>::const_iterator, std::multimap<int, int>::const_iterator> ppp;
00346   ppp = m_mapNodeOverlaps.equal_range(idx);
00347   for (std::multimap<int, int>::const_iterator it2 = ppp.first;it2 != ppp.second;++it2) {
00348     const TGeoOverlap* ovl = (const TGeoOverlap*) gGeoManager->GetListOfOverlaps()->At((*it2).second);
00349     if (ovl) ovl->Print();
00350   }    
00351 }
00352 
00353 void FWOverlapTableManager::getOverlapTitles(int idx, TString& txt) const
00354 {
00355   
00356    TEveGeoManagerHolder gmgr( FWGeometryTableViewManager::getGeoMangeur());
00357    std::pair<std::multimap<int, int>::const_iterator, std::multimap<int, int>::const_iterator> ppp;
00358    ppp = m_mapNodeOverlaps.equal_range(idx);
00359    for (std::multimap<int, int>::const_iterator it2 = ppp.first;it2 != ppp.second;++it2) {
00360       const TGeoOverlap* ovl = (const TGeoOverlap*) gGeoManager->GetListOfOverlaps()->At((*it2).second);
00361       {
00362          txt += "\n";
00363 
00364          if (ovl) {
00365             txt += Form("%s: %g, ", ovl->IsOverlap() ? "Ovl" : "Extr",  ovl->GetOverlap());
00366             txt += ovl->GetTitle();    
00367          }
00368       }
00369    }    
00370 }
00371 //______________________________________________________________________________
00372 /*
00373   const char* FWOverlapTableManager::cellName(const NodeInfo& data) const
00374   {
00375   if (data.m_parent == -1)
00376   {
00377   int ne = 0;
00378   int no = 0;
00379   TGeoOverlap* ovl;
00380   TEveGeoManagerHolder gmgr( FWGeometryTableViewManager::getGeoMangeur());
00381   TIter next_ovl(gGeoManager->GetListOfOverlaps());
00382   while((ovl = (TGeoOverlap*)next_ovl())) 
00383   ovl->IsOverlap() ? no++ : ne++;
00384      
00385   return Form("%s Ovl[%d] Ext[%d]", data.m_node->GetName(), no, ne);
00386   }
00387   else
00388   {
00389   return data.name();
00390   }
00391   }*/
00392 
00393 //______________________________________________________________________________
00394 
00395 FWTableCellRendererBase* FWOverlapTableManager::cellRenderer(int iSortedRowNumber, int iCol) const
00396 {  
00397    if (m_row_to_index.empty()) return &m_renderer;
00398 
00399    int unsortedRow =  m_row_to_index[iSortedRowNumber];
00400 
00401    if (unsortedRow < 0) printf("!!!!!!!!!!!!!!!! error %d %d \n",unsortedRow,  iSortedRowNumber);
00402 
00403    // editor state
00404    //
00405    m_renderer.showEditor(unsortedRow == m_editTransparencyIdx && iCol == 2);
00406 
00407 
00408    // selection state
00409    //
00410    const NodeInfo& data = m_entries[unsortedRow];
00411 
00412    bool isSelected = data.testBit(kHighlighted) ||  data.testBit(kSelected);
00413    if (m_browser->listAllNodes()) isSelected = isSelected ||  data.testBit(kOverlap);
00414 
00415    if (data.testBit(kSelected))
00416    {
00417       m_highlightContext->SetBackground(0xc86464);
00418    }
00419    else if (data.testBit(kHighlighted) )
00420    {
00421       m_highlightContext->SetBackground(0x6464c8);
00422    }
00423    else if (m_browser->listAllNodes() && data.testBit(kOverlap) )
00424    {
00425       m_highlightContext->SetBackground(0xdddddd);
00426    }
00427   
00428 
00429    // set column content
00430    //
00431    if (iCol == 0)
00432    {
00433       if (unsortedRow == m_browser->getTopNodeIdx())
00434       {
00435          int no = 0, ne =0;
00436          TEveGeoManagerHolder gmgr( FWGeometryTableViewManager::getGeoMangeur());
00437          TIter next_ovl(gGeoManager->GetListOfOverlaps());
00438          const TGeoOverlap* ovl;
00439          while((ovl = (TGeoOverlap*)next_ovl())) 
00440             ovl->IsOverlap() ? no++ : ne++;
00441       
00442          m_renderer.setData(Form("%s Ovl[%d] Ext[%d]", data.m_node->GetName(), no, ne), isSelected);
00443       }
00444       else {
00445          m_renderer.setData(data.name(), isSelected); 
00446       }
00447       m_renderer.setIsParent(nodeIsParent(data));
00448 
00449       m_renderer.setIsOpen( data.testBit(FWGeometryTableManagerBase::kExpanded));
00450 
00451       int level = data.m_level - m_levelOffset;
00452       if (nodeIsParent(data))
00453          m_renderer.setIndentation(20*level);
00454       else
00455          m_renderer.setIndentation(20*level + FWTextTreeCellRenderer::iconWidth());
00456    }
00457    else
00458    {
00459       m_renderer.setIsParent(false);
00460       m_renderer.setIndentation(0);
00461 
00462       if (iCol == 5)
00463       {
00464          if (data.testBit(kOverlap) ) 
00465          {
00466             std::string x;
00467             std::pair<std::multimap<int, int>::const_iterator, std::multimap<int, int>::const_iterator> ppp;
00468             ppp = m_mapNodeOverlaps.equal_range(unsortedRow);
00469 
00470             TEveGeoManagerHolder gmgr( FWGeometryTableViewManager::getGeoMangeur());
00471            
00472             for (std::multimap<int, int>::const_iterator it2 = ppp.first;it2 != ppp.second;++it2) {
00473                const TGeoOverlap* ovl = (const TGeoOverlap*) gGeoManager->GetListOfOverlaps()->At((*it2).second);
00474                if (ovl)
00475                   x +=  Form("%s: %g ", ovl->IsOverlap() ? "Ovl" : "Extr", ovl->GetOverlap());
00476                else
00477                   x += "err";
00478              
00479             }
00480             m_renderer.setData(x,  isSelected);
00481          }
00482          else
00483          {
00484             m_renderer.setData("",  isSelected);
00485          }
00486       }
00487       if (iCol == 1)
00488       {
00489          m_colorBoxRenderer.setData(data.m_color, isSelected);
00490          return  &m_colorBoxRenderer;
00491       }
00492       else if (iCol == 2 )
00493       { 
00494          m_renderer.setData(Form("%d", 100 -data.m_transparency), isSelected);
00495       }
00496       else if (iCol == 3 )
00497       {
00498          m_renderer.setData(data.testBit(kVisNodeSelf)  ? "On" : "-",  isSelected );
00499 
00500       }
00501       else if (iCol == 4 )
00502       {
00503          m_renderer.setData(data.testBit(kVisNodeChld)  ? "On" : "-",  isSelected);
00504 
00505       }
00506       else if (iCol == 6)
00507       { 
00508          bool motherV = false;
00509          if (data.testBit(kOverlapChild))
00510          {
00511             for (std::vector<int>::iterator i = m_browser->m_markerIndices.begin(); i!= m_browser->m_markerIndices.end(); i++)
00512             {
00513                if (TMath::Abs(*i) == unsortedRow) {
00514                   motherV = true;
00515                   break;
00516                }
00517             }
00518          }
00519 
00520          m_renderer.setData(motherV ? (data.testBit(kVisMarker) ? "On" : "-") : "", isSelected);         
00521       }
00522    }
00523    return &m_renderer;
00524 }
00525 
00526 
00527 
00528 //______________________________________________________________________________
00529 
00530 void FWOverlapTableManager::setDaughtersSelfVisibility(int selectedIdx, bool v)
00531 {
00532    int dOff = 0;
00533    TGeoNode* parentNode = m_entries[selectedIdx].m_node;
00534    int nD = parentNode->GetNdaughters();
00535    for (int n = 0; n != nD; ++n)
00536    {
00537       int idx = selectedIdx + 1 + n + dOff;
00538       NodeInfo& data = m_entries[idx];
00539 
00540       data.setBitVal(FWGeometryTableManagerBase::kVisNodeChld, v);
00541       data.setBitVal(FWGeometryTableManagerBase::kVisNodeSelf, v);
00542 
00543 
00544       FWGeometryTableManagerBase::getNNodesTotal(parentNode->GetDaughter(n), dOff);
00545    }
00546 }