TEveScene.cxx

Go to the documentation of this file.
00001 // @(#)root/eve:$Id: TEveScene.cxx 36373 2010-10-19 17:43:35Z matevz $
00002 // Authors: Matevz Tadel & Alja Mrak-Tadel: 2006, 2007
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2007, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 #include "TEveScene.h"
00013 #include "TEveViewer.h"
00014 #include "TEveManager.h"
00015 #include "TEveTrans.h"
00016 #include "TEvePad.h"
00017 
00018 #include "TGLScenePad.h"
00019 #include "TGLLogicalShape.h"
00020 #include "TGLPhysicalShape.h"
00021 
00022 #include "TList.h"
00023 #include "TExMap.h"
00024 
00025 
00026 //==============================================================================
00027 //==============================================================================
00028 // TEveScene
00029 //==============================================================================
00030 
00031 //______________________________________________________________________________
00032 //
00033 // Eve representation of TGLScene.
00034 // The GLScene is owned by this class - it is created on construction
00035 // time and deleted at destruction.
00036 //
00037 // Normally all objects are positioned directly in global scene-space.
00038 // By setting the fHierarchical flag, positions of children get
00039 // calculated by multiplying the transformation matrices of all parents.
00040 
00041 ClassImp(TEveScene);
00042 
00043 //______________________________________________________________________________
00044 TEveScene::TEveScene(const char* n, const char* t) :
00045    TEveElementList(n, t),
00046    fPad    (0),
00047    fGLScene(0),
00048    fChanged      (kFALSE),
00049    fSmartRefresh (kTRUE),
00050    fHierarchical (kFALSE)
00051 {
00052    // Constructor.
00053 
00054    fPad = new TEvePad;
00055    fPad->GetListOfPrimitives()->Add(this);
00056    fGLScene = new TGLScenePad(fPad);
00057    fGLScene->SetName(n);
00058    fGLScene->SetAutoDestruct(kFALSE);
00059    fGLScene->SetSmartRefresh(kTRUE);
00060 }
00061 
00062 //______________________________________________________________________________
00063 TEveScene::~TEveScene()
00064 {
00065    // Destructor.
00066 
00067    fDestructing = kStandard;
00068 
00069    gEve->GetViewers()->SceneDestructing(this);
00070    gEve->GetScenes()->RemoveElement(this);
00071    delete fGLScene;
00072    delete fPad;
00073 }
00074 
00075 /******************************************************************************/
00076 
00077 //______________________________________________________________________________
00078 void TEveScene::CollectSceneParents(List_t& scenes)
00079 {
00080    // Virtual from TEveElement; here we simply append this scene to
00081    // the list.
00082 
00083    scenes.push_back(this);
00084 }
00085 
00086 /******************************************************************************/
00087 
00088 //______________________________________________________________________________
00089 void TEveScene::Repaint(Bool_t dropLogicals)
00090 {
00091    // Repaint the scene.
00092 
00093    if (dropLogicals) fGLScene->SetSmartRefresh(kFALSE);
00094    fGLScene->PadPaint(fPad);
00095    if (dropLogicals) fGLScene->SetSmartRefresh(kTRUE);
00096    fChanged = kFALSE;
00097 
00098    // Hack to propagate selection state to physical shapes.
00099    //
00100    // Should actually be published in PadPaint() following a direct
00101    // AddObject() call, but would need some other stuff for that.
00102    // Optionally, this could be exported via the TAtt3D and everything
00103    // would be sweet.
00104 
00105    TGLScene::LogicalShapeMap_t& logs = fGLScene->RefLogicalShapes();
00106    TEveElement* elm;
00107    for (TGLScene::LogicalShapeMapIt_t li = logs.begin(); li != logs.end(); ++li)
00108    {
00109       elm = dynamic_cast<TEveElement*>(li->first);
00110       if (elm && li->second->Ref() == 1)
00111       {
00112          TGLPhysicalShape* pshp = const_cast<TGLPhysicalShape*>(li->second->GetFirstPhysical());
00113          pshp->Select(elm->GetSelectedLevel());
00114       }
00115    }
00116 
00117    // Fix positions for hierarchical scenes.
00118    if (fHierarchical)
00119    {
00120       RetransHierarchically();
00121    }
00122 }
00123 
00124 //______________________________________________________________________________
00125 void TEveScene::RetransHierarchically()
00126 {
00127    // Entry point for hierarchical transformation update.
00128    // Calls the recursive variant on all children.
00129 
00130    fGLScene->BeginUpdate();
00131 
00132    RetransHierarchicallyRecurse(this, RefMainTrans());
00133 
00134    fGLScene->EndUpdate();
00135 }
00136 
00137 //______________________________________________________________________________
00138 void TEveScene::RetransHierarchicallyRecurse(TEveElement* el, const TEveTrans& tp)
00139 {
00140    // Set transformation matrix for physical shape of element el in
00141    // the GL-scene and recursively descend into children (if enabled).
00142 
00143    static const TEveException eh("TEveScene::RetransHierarchicallyRecurse ");
00144 
00145    TEveTrans t(tp);
00146    if (el->HasMainTrans())
00147       t *= el->RefMainTrans();
00148 
00149    if (el->GetRnrSelf() && el != this)
00150    {
00151       fGLScene->UpdatePhysioLogical(el->GetRenderObject(eh), t.Array(), 0);
00152    }
00153 
00154    if (el->GetRnrChildren())
00155    {
00156       for (List_i i = el->BeginChildren(); i != el->EndChildren(); ++i)
00157       {
00158          if ((*i)->GetRnrAnything())
00159             RetransHierarchicallyRecurse(*i, t);
00160       }
00161    }
00162 }
00163 
00164 /******************************************************************************/
00165 
00166 //______________________________________________________________________________
00167 void TEveScene::SetName(const char* n)
00168 {
00169    // Set scene's name.
00170 
00171    TEveElementList::SetName(n);
00172    fGLScene->SetName(n);
00173 }
00174 
00175 //______________________________________________________________________________
00176 void TEveScene::Paint(Option_t* option)
00177 {
00178    // Paint the scene. Iterate over children and calls PadPaint().
00179 
00180    if (GetRnrState())
00181    {
00182       for(List_i i=fChildren.begin(); i!=fChildren.end(); ++i)
00183          (*i)->PadPaint(option);
00184    }
00185 }
00186 
00187 /******************************************************************************/
00188 
00189 //______________________________________________________________________________
00190 void TEveScene::DestroyElementRenderers(TEveElement* element)
00191 {
00192    // Remove element from the scene.
00193    // It is not an error if the element is not found in the scene.
00194 
00195    static const TEveException eh("TEveScene::DestroyElementRenderers ");
00196 
00197    fGLScene->BeginUpdate();
00198    Bool_t changed = fGLScene->DestroyLogical(element->GetRenderObject(eh), kFALSE);
00199    fGLScene->EndUpdate(changed, changed);
00200 }
00201 
00202 //______________________________________________________________________________
00203 void TEveScene::DestroyElementRenderers(TObject* rnrObj)
00204 {
00205    // Remove element represented by object rnrObj from the scene.
00206    // It is not an error if the element is not found in the scene.
00207 
00208    fGLScene->BeginUpdate();
00209    Bool_t changed = fGLScene->DestroyLogical(rnrObj, kFALSE);
00210    fGLScene->EndUpdate(changed, changed);
00211 }
00212 
00213 /******************************************************************************/
00214 
00215 //______________________________________________________________________________
00216 const TGPicture* TEveScene::GetListTreeIcon(Bool_t)
00217 {
00218    // Return icon for scene.
00219 
00220    return TEveElement::fgListTreeIcons[2];
00221 }
00222 
00223 
00224 //==============================================================================
00225 //==============================================================================
00226 // TEveSceneList
00227 //==============================================================================
00228 
00229 //______________________________________________________________________________
00230 //
00231 // List of Scenes providing common operations on TEveScene collections.
00232 
00233 ClassImp(TEveSceneList);
00234 
00235 //______________________________________________________________________________
00236 TEveSceneList::TEveSceneList(const char* n, const char* t) :
00237    TEveElementList(n, t)
00238 {
00239    // Constructor.
00240 
00241    SetChildClass(TEveScene::Class());
00242 }
00243 
00244 //______________________________________________________________________________
00245 void TEveSceneList::DestroyScenes()
00246 {
00247    // Destroy all scenes and their contents.
00248    // Tho object with non-zero deny-destroy will still survive.
00249 
00250    List_i i = fChildren.begin();
00251    while (i != fChildren.end())
00252    {
00253       TEveScene* s = (TEveScene*) *(i++);
00254       s->DestroyElements();
00255       s->DestroyOrWarn();
00256    }
00257 }
00258 
00259 /******************************************************************************/
00260 
00261 //______________________________________________________________________________
00262 void TEveSceneList::RepaintChangedScenes(Bool_t dropLogicals)
00263 {
00264    // Repaint scenes that are tagged as changed.
00265 
00266    for (List_i i=fChildren.begin(); i!=fChildren.end(); ++i)
00267    {
00268       TEveScene* s = (TEveScene*) *i;
00269       if (s->IsChanged())
00270       {
00271          s->Repaint(dropLogicals);
00272       }
00273    }
00274 }
00275 
00276 //______________________________________________________________________________
00277 void TEveSceneList::RepaintAllScenes(Bool_t dropLogicals)
00278 {
00279    // Repaint all scenes.
00280 
00281    for (List_i i=fChildren.begin(); i!=fChildren.end(); ++i)
00282    {
00283       ((TEveScene*) *i)->Repaint(dropLogicals);
00284    }
00285 }
00286 
00287 //______________________________________________________________________________
00288 void TEveSceneList::DestroyElementRenderers(TEveElement* element)
00289 {
00290    // Loop over all scenes and remove all instances of element from
00291    // them.
00292 
00293    static const TEveException eh("TEveSceneList::DestroyElementRenderers ");
00294 
00295    TObject* obj = element->GetRenderObject(eh);
00296    for (List_i i=fChildren.begin(); i!=fChildren.end(); ++i)
00297    {
00298       ((TEveScene*)*i)->DestroyElementRenderers(obj);
00299    }
00300 }
00301 
00302 //______________________________________________________________________________
00303 void TEveSceneList::ProcessSceneChanges(Bool_t dropLogicals, TExMap* stampMap)
00304 {
00305    // Loop over all scenes and update them accordingly:
00306    //   a) if scene is marked as changed, it is repainted;
00307    //   b) otherwise iteration is done over the set of stamped elements and
00308    //      their physical/logical shapes are updated accordingly.
00309    //
00310    // This allows much finer update granularity without resetting of
00311    // complex GL-viewer and GL-scene state.
00312 
00313    // We need changed elements sorted by their "render object" as we do
00314    // parallel iteration over this list and the list of logical shapes
00315    // in every scene.
00316 
00317    static const TEveException eh("TEveSceneList::ProcessSceneChanges ");
00318 
00319    typedef std::map<TObject*, TEveElement*> mObjectElement_t;
00320    typedef mObjectElement_t::iterator       mObjectElement_i;
00321 
00322    mObjectElement_t changed_objects;
00323    {
00324       Long64_t   key, value;
00325       TExMapIter stamped_elements(stampMap);
00326       while (stamped_elements.Next(key, value))
00327       {
00328          TEveElement *el = reinterpret_cast<TEveElement*>(key);
00329          changed_objects.insert(std::make_pair(el->GetRenderObject(eh), el));
00330       }
00331    }
00332 
00333    for (List_i i=fChildren.begin(); i!=fChildren.end(); ++i)
00334    {
00335       TEveScene* s = (TEveScene*) *i;
00336 
00337       if (s->IsChanged())
00338       {
00339          s->Repaint(dropLogicals);
00340       }
00341       else
00342       {
00343          Bool_t updateViewers = kFALSE;
00344          Bool_t incTimeStamp  = kFALSE;
00345          Bool_t transbboxChg  = kFALSE;
00346 
00347          s->GetGLScene()->BeginUpdate();
00348 
00349          // Process stamps.
00350          TGLScene::LogicalShapeMap_t   &logs = s->GetGLScene()->RefLogicalShapes();
00351          TGLScene::LogicalShapeMapIt_t  li   = logs.begin();
00352 
00353          mObjectElement_i ei = changed_objects.begin();
00354 
00355          while (li != logs.end() && ei != changed_objects.end())
00356          {
00357             if (li->first == ei->first)
00358             {
00359                if (li->second->Ref() != 1)
00360                   Warning("TEveSceneList::ProcessSceneChanges",
00361                           "Expect one physical, cnt=%u.", li->second->Ref());
00362 
00363                TGLLogicalShape  *lshp = li->second;
00364                TGLPhysicalShape *pshp = const_cast<TGLPhysicalShape*>(lshp->GetFirstPhysical());
00365                TEveElement      *el   = ei->second;
00366                UChar_t           bits = el->GetChangeBits();
00367 
00368                if (bits & kCBColorSelection)
00369                {
00370                   pshp->Select(el->GetSelectedLevel());
00371                   pshp->SetDiffuseColor(el->GetMainColor(),
00372                                         el->GetMainTransparency());
00373                }
00374 
00375                if (bits & kCBTransBBox)
00376                {
00377                   if (el->HasMainTrans())
00378                      pshp->SetTransform(el->PtrMainTrans()->Array());
00379                   lshp->UpdateBoundingBox();
00380                   incTimeStamp = kTRUE;
00381                   transbboxChg = kTRUE;
00382                }
00383 
00384                if (bits & kCBObjProps)
00385                {
00386                   lshp->DLCacheClear();
00387                }
00388 
00389                ++li; ++ei;
00390                updateViewers = kTRUE;
00391             }
00392             else if (li->first < ei->first)
00393             {
00394                ++li;
00395             }
00396             else
00397             {
00398                ++ei;
00399             }
00400          }
00401 
00402          s->GetGLScene()->EndUpdate(updateViewers, incTimeStamp, updateViewers);
00403 
00404          // Fix positions for hierarchical scenes.
00405          if (s->GetHierarchical() && transbboxChg)
00406          {
00407             s->RetransHierarchically();
00408          }
00409       }
00410    }
00411 }

Generated on Tue Jul 5 14:16:13 2011 for ROOT_528-00b_version by  doxygen 1.5.1