CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/Fireworks/Core/src/FWGeoTopNode.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:     Core
00004 // Class  :     FWGeoTopNode
00005 // 
00006 // Implementation:
00007 //     [Notes on implementation]
00008 //
00009 // Original Author:  Matevz Tadel, Alja Mrak Tadel  
00010 //         Created:  Thu Jun 23 01:24:51 CEST 2011
00011 // $Id: FWGeoTopNode.cc,v 1.21 2012/02/22 23:03:47 amraktad Exp $
00012 //
00013 
00014 // system include files
00015 
00016 // user include files
00017 
00018 #include "TEveTrans.h"
00019 #include "TEveViewer.h"
00020 #include "TEveManager.h"
00021 #include "TEveUtil.h"
00022 
00023 
00024 #include "TROOT.h"
00025 #include "TBuffer3D.h"
00026 #include "TBuffer3DTypes.h"
00027 #include "TVirtualViewer3D.h"
00028 #include "TColor.h"
00029 #include "TGLScenePad.h"
00030 #include "TGLPhysicalShape.h"
00031 #include "TGLSelectRecord.h"
00032 
00033 #include "TGeoShape.h"
00034 #include "TGeoVolume.h"
00035 #include "TGeoNode.h"
00036 #include "TGeoShapeAssembly.h"
00037 #include "TGeoCompositeShape.h"
00038 #include "TGeoBoolNode.h"
00039 #include "TGeoManager.h"
00040 #include "TGeoMatrix.h"
00041 #include "TVirtualGeoPainter.h"
00042 
00043 #include "Fireworks/Core/interface/FWGeoTopNode.h"
00044 #include "Fireworks/Core/src/FWGeoTopNodeScene.h"
00045 #include "Fireworks/Core/interface/FWViewType.h"
00046 #include "Fireworks/Core/interface/fwLog.h"
00047 
00048 UInt_t FWGeoTopNode::phyID(int tableIdx) 
00049 {
00050 return UInt_t(tableIdx + 2);
00051 
00052 }
00053 
00054 int FWGeoTopNode::tableIdx(TGLPhysicalShape* ps) 
00055 {
00056    return ps->ID() - 2;
00057 }
00058 //______________________________________________________________________________
00059 void FWGeoTopNode::EraseFromSet(std::set<TGLPhysicalShape*>& sset, TGLPhysicalShape* id)
00060 {
00061    sset.erase(id);
00062    SetStateOf(id);
00063 }
00064 
00065 //______________________________________________________________________________
00066 void FWGeoTopNode::ClearSet(std::set<TGLPhysicalShape*>& sset)
00067 {
00068    while (!sset.empty())
00069    {
00070       TGLPhysicalShape *id = *sset.begin();
00071       sset.erase(id);
00072       SetStateOf(id);
00073    }
00074 }
00075 //______________________________________________________________________________
00076 void FWGeoTopNode::SetStateOf(TGLPhysicalShape* id)
00077 {
00078    FWGeometryTableManagerBase::NodeInfo& data =  tableManager()->refEntries().at(tableIdx(id));
00079 
00080    if (fSted.find(id) != fSted.end())
00081    {
00082       id->Select(1);
00083       data.setBit(FWGeometryTableManagerBase::kSelected);
00084    }
00085    else if (fHted.find(id) != fHted.end())
00086    {
00087       id->Select(3);
00088       data.setBit(FWGeometryTableManagerBase::kHighlighted);
00089    }
00090    else
00091    {
00092       id->Select(0);
00093       data.resetBit(FWGeometryTableManagerBase::kHighlighted);
00094       data.resetBit(FWGeometryTableManagerBase::kSelected);
00095    }
00096 
00097 }
00098 
00099 //______________________________________________________________________________
00100 void FWGeoTopNode::ProcessSelection(TGLSelectRecord& rec, std::set<TGLPhysicalShape*>& sset, TGLPhysicalShape* id)
00101 {
00102    // printf("FWGeoTopNode::ProcessSelection ===============================\n");
00103 
00104    fSceneJebo->BeginUpdate();
00105 
00106    if (sset.empty())
00107    {
00108       if (id)
00109       {
00110          sset.insert(id);
00111          rec.SetSecSelResult(TGLSelectRecord::kEnteringSelection);
00112       }  
00113    }
00114    else
00115    {
00116       if (id)
00117       {
00118          if (rec.GetMultiple())
00119          {
00120             if (sset.find(id) == sset.end())
00121             {
00122                sset.insert(id);
00123                rec.SetSecSelResult(TGLSelectRecord::kModifyingInternalSelection);
00124             }
00125             else
00126             {
00127                EraseFromSet(sset, id);
00128                if (sset.empty())
00129                   rec.SetSecSelResult(TGLSelectRecord::kLeavingSelection);
00130                else
00131                   rec.SetSecSelResult(TGLSelectRecord::kModifyingInternalSelection);
00132             }
00133          }
00134          else
00135          {
00136             if (sset.size() != 1 || sset.find(id) == sset.end())
00137             {
00138                ClearSet(sset);
00139                sset.insert(id);
00140                rec.SetSecSelResult(TGLSelectRecord::kModifyingInternalSelection);
00141             }
00142          }
00143       }
00144       else
00145       {
00146          if (!rec.GetMultiple())
00147          {
00148             ClearSet(sset);
00149             rec.SetSecSelResult(TGLSelectRecord::kLeavingSelection);
00150          }
00151       }
00152    }
00153 
00154    if (id)
00155    {
00156       SetStateOf(id);
00157    }
00158 
00159    if (rec.GetSecSelResult() != TGLSelectRecord::kNone)
00160    {
00161       fSceneJebo->EndUpdate(kTRUE, kFALSE, kTRUE);
00162       gEve->Redraw3D();
00163 
00164       tableManager()->dataChanged();
00165    }
00166    else
00167    {
00168       fSceneJebo->EndUpdate(kFALSE, kFALSE, kFALSE);
00169    }
00170 }
00171 
00172 //______________________________________________________________________________
00173 bool FWGeoTopNode::selectPhysicalFromTable( int tableIndex)
00174 {
00175    //   printf("FWGeoTopNode::selectPhysicalFromTable 
00176 
00177    TGLPhysicalShape* ps = fSceneJebo->FindPhysical(phyID(tableIndex));
00178    if (ps) {
00179       fSted.insert(ps);
00180       ps->Select(1);
00181       // printf("selectPhysicalFromTable found physical \n");
00182       return true;
00183    }
00184    else if ( tableManager()->refEntries().at(tableIndex).testBit(FWGeometryTableManagerBase::kVisNodeSelf));
00185    {
00186       fwLog(fwlog::kInfo) << "Selected entry not drawn in GL viewer. \n" ;
00187       return false;
00188    }
00189 }
00190 
00191 //______________________________________________________________________________
00192 void FWGeoTopNode::printSelected() 
00193 {
00194    for (std::set<TGLPhysicalShape*>::iterator it = fSted.begin(); it != fSted.end(); ++it)
00195    {
00196       printf("FWGeoTopNode::printSelected %s \n",  tableManager()->refEntries().at(tableIdx(*it)).name() );
00197    }
00198 }
00199 
00200 //______________________________________________________________________________
00201 
00202 int FWGeoTopNode::getFirstSelectedTableIndex() 
00203 {
00204    // Note: if object would be rendered, this would return fSted.begin().
00205 
00206    if (fSted.size() <= 1)
00207    {
00208       int cnt = 0;
00209       for (FWGeometryTableManagerBase::Entries_i i = tableManager()->refEntries().begin(); i != tableManager()->refEntries().end(); ++i, ++cnt)
00210       {
00211          if (i->testBit(FWGeometryTableManagerBase::kSelected)) return cnt; 
00212       }
00213    }
00214    return -1;
00215 }
00216 
00217 //______________________________________________________________________________
00218 void FWGeoTopNode::ComputeBBox()
00219 {
00220    // Fill bounding-box information. Virtual from TAttBBox.
00221 
00222    BBoxZero(1.0f);
00223 }
00224 
00225 //______________________________________________________________________________
00226 void FWGeoTopNode::setupBuffMtx(TBuffer3D& buff, const TGeoHMatrix& mat)
00227 {
00228    const Double_t *r = mat.GetRotationMatrix();
00229    const Double_t *t = mat.GetTranslation();
00230    const Double_t *s = mat.GetScale();
00231    Double_t       *m = buff.fLocalMaster;
00232    m[0]  = r[0]*s[0]; m[1]  = r[1]*s[1]; m[2]  = r[2]*s[2]; m[3]  = 0;
00233    m[4]  = r[3]*s[0]; m[5]  = r[4]*s[1]; m[6]  = r[5]*s[2]; m[7]  = 0;
00234    m[8]  = r[6]*s[0]; m[9]  = r[7]*s[1]; m[10] = r[8]*s[2]; m[11] = 0;
00235    m[12] = t[0];      m[13] = t[1];      m[15] = t[2];      m[15] = 1;
00236 
00237    buff.fLocalFrame = kTRUE;
00238 }
00239 
00240 // ______________________________________________________________________
00241 void FWGeoTopNode::paintShape(FWGeometryTableManagerBase::NodeInfo& data,  Int_t tableIndex, const TGeoHMatrix& nm, bool volumeColor)
00242 {
00243    static const TEveException eh("FWGeoTopNode::paintShape ");
00244  
00245    //  printf("paint sahpe %s idx %d\n", data.m_node->GetVolume()->GetName(), tableIndex );
00246 
00247    TGeoShape* shape = data.m_node->GetVolume()->GetShape();
00248    
00249    TGeoCompositeShape* compositeShape = dynamic_cast<TGeoCompositeShape*>(shape);
00250    if (compositeShape)
00251    {
00252       // fSceneJebo->fNextCompositeID = phyID(tableIndex);
00253 
00254       Double_t halfLengths[3] = { compositeShape->GetDX(), compositeShape->GetDY(), compositeShape->GetDZ() };
00255 
00256       TBuffer3D buff(TBuffer3DTypes::kComposite);
00257       buff.fID           = data.m_node->GetVolume();
00258       buff.fColor        = volumeColor ? data.m_node->GetVolume()->GetLineColor() : data.m_color ;
00259       buff.fTransparency = data.m_node->GetVolume()->GetTransparency(); 
00260 
00261       nm.GetHomogenousMatrix(buff.fLocalMaster);  
00262       buff.fLocalFrame   = kTRUE; // Always enforce local frame (no geo manager).
00263       buff.SetAABoundingBox(compositeShape->GetOrigin(), halfLengths);
00264       buff.SetSectionsValid(TBuffer3D::kCore|TBuffer3D::kBoundingBox);
00265 
00266       Bool_t paintComponents = kTRUE;
00267       // Start a composite shape, identified by this buffer
00268       if (TBuffer3D::GetCSLevel() == 0) {
00269          paintComponents = fSceneJebo->OpenCompositeWithPhyID(phyID(tableIndex), buff);
00270       }
00271 
00272       TBuffer3D::IncCSLevel();
00273       
00274       // Paint the boolean node - will add more buffers to viewer
00275       TGeoHMatrix xxx;
00276       TGeoMatrix *gst = TGeoShape::GetTransform();
00277       TGeoShape::SetTransform(&xxx);
00278 
00279       if (paintComponents) compositeShape->GetBoolNode()->Paint("");
00280       TGeoShape::SetTransform(gst);
00281       // Close the composite shape
00282       if (TBuffer3D::DecCSLevel() == 0)
00283          gPad->GetViewer3D()->CloseComposite();
00284 
00285 
00286       //  fSceneJebo->fNextCompositeID = 0;
00287    }
00288    else
00289    {
00290       TBuffer3D& buff = (TBuffer3D&) shape->GetBuffer3D (TBuffer3D::kCore, kFALSE);
00291       setupBuffMtx(buff, nm);
00292       buff.fID           = data.m_node->GetVolume();
00293       buff.fColor        = volumeColor ? data.m_node->GetVolume()->GetLineColor() : data.m_color ;
00294       buff.fTransparency =  data.m_node->GetVolume()->GetTransparency();
00295 
00296       nm.GetHomogenousMatrix(buff.fLocalMaster);
00297       buff.fLocalFrame   = kTRUE; // Always enforce local frame (no geo manager).
00298 
00299       Int_t sections = TBuffer3D::kBoundingBox | TBuffer3D::kShapeSpecific;
00300       shape->GetBuffer3D(sections, kTRUE);
00301 
00302       Int_t reqSec = gPad->GetViewer3D()->AddObject(phyID(tableIndex), buff);
00303 
00304       if (reqSec != TBuffer3D::kNone) {
00305          // This shouldn't happen, but I suspect it does sometimes.
00306          if (reqSec & TBuffer3D::kCore)
00307             Warning(eh, "Core section required again for shape='%s'. This shouldn't happen.", GetName());
00308          shape->GetBuffer3D(reqSec, kTRUE);
00309          reqSec = gPad->GetViewer3D()->AddObject(phyID(tableIndex), buff);
00310       }
00311 
00312       if (reqSec != TBuffer3D::kNone)  
00313          Warning(eh, "Extra section required: reqSec=%d, shape=%s.", reqSec, GetName());
00314    }
00315 }
00316 
00317 // ______________________________________________________________________
00318 void FWGeoTopNode::Paint(Option_t* opt)
00319 {
00320    static const TEveException eh("TEveTopNodeJebo::Paint ");
00321 
00322    TBuffer3D buff(TBuffer3DTypes::kGeneric);
00323 
00324    // Section kCore
00325    buff.fID           = this;
00326    buff.fColor        = GetMainColor();
00327    buff.fTransparency = GetMainTransparency();
00328    if (HasMainTrans())  RefMainTrans().SetBuffer3D(buff);
00329 
00330    buff.SetSectionsValid(TBuffer3D::kCore);
00331 
00332    Int_t reqSections = gPad->GetViewer3D()->AddObject(1, buff);
00333    if (reqSections != TBuffer3D::kNone)
00334    {
00335       Warning(eh, "IsA='%s'. Viewer3D requires more sections (%d). Only direct-rendering supported.",
00336               ClassName(), reqSections);
00337    }
00338 }
00339 
00340 // ______________________________________________________________________
00341 void FWGeoTopNode::UnSelected()
00342 {
00343    ClearSet(fSted);
00344    for (FWGeometryTableManagerBase::Entries_i i = tableManager()->refEntries().begin(); i != tableManager()->refEntries().end(); ++i)
00345       i->resetBit(FWGeometryTableManagerBase::kSelected);
00346 }
00347 
00348 // ______________________________________________________________________
00349 void FWGeoTopNode::UnHighlighted()
00350 {
00351    ClearSet(fHted);
00352    for (FWGeometryTableManagerBase::Entries_i i = tableManager()->refEntries().begin(); i != tableManager()->refEntries().end(); ++i)
00353       i->resetBit(FWGeometryTableManagerBase::kHighlighted);
00354 }