// @(#)root/geom:$Id$
// Author: Andrei Gheata   18/03/02

/*************************************************************************
 * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers.               *
 * All rights reserved.                                                  *
 *                                                                       *
 * For the licensing terms see $ROOTSYS/LICENSE.                         *
 * For the list of contributors see $ROOTSYS/README/CREDITS.             *
 *************************************************************************/
// Author : Andrei Gheata -           date Wed 12 Dec 2001 09:45:08 AM CET

#ifndef ROOT_TGeoCache
#define ROOT_TGeoCache

#ifndef ROOT_TGeoNode
#include "TGeoNode.h"
#endif

#ifndef ROOT_TGeoStateInfo
#include "TGeoStateInfo.h"
#endif

// forward declarations
class TGeoManager;
class TGeoHMatrix;

/////////////////////////////////////////////////////////////////////////////
//                                                                         //
// TGeoCacheState - class storing the state of the cache at a given moment //
//                                                                         //
/////////////////////////////////////////////////////////////////////////////

class TGeoCacheState : public TObject
{
protected:
   Int_t                fCapacity;      // maximum level stored
   Int_t                fLevel;         // level in the current branch
   Int_t                fNmany;         // number of overlapping nodes on current branch
   Int_t                fStart;         // start level
   Int_t                fIdBranch[30];  // ID branch
   Double_t             fPoint[3];      // last point in master frame
   Bool_t               fOverlapping;   // overlap flag

   TGeoNode           **fNodeBranch;    // last node branch stored
   TGeoHMatrix        **fMatrixBranch;  // global matrices for last branch
   TGeoHMatrix        **fMatPtr;        // array of matrix pointers

   TGeoCacheState(const TGeoCacheState&);
   TGeoCacheState& operator=(const TGeoCacheState&);

public:
   TGeoCacheState();
   TGeoCacheState(Int_t capacity);
   virtual ~TGeoCacheState();

   void                 SetState(Int_t level, Int_t startlevel, Int_t nmany, Bool_t ovlp, Double_t *point=0);
   Bool_t               GetState(Int_t &level, Int_t &nmany, Double_t *point) const;

   ClassDef(TGeoCacheState, 0)       // class storing the cache state
};

/////////////////////////////////////////////////////////////////////////////
//                                                                         //
// TGeoNodeCache - cache of reusable physical nodes                        //
//                                                                         //
/////////////////////////////////////////////////////////////////////////////

class TGeoNodeCache : public TObject
{
private:
   Int_t                 fGeoCacheMaxLevels;// maximum supported number of levels
   Int_t                 fGeoCacheStackSize;// maximum size of the stack
   Int_t                 fGeoInfoStackSize; // maximum size of the stack of info states
   Int_t                 fLevel;            // level in the current branch
   Int_t                 fStackLevel;       // current level in the stack
   Int_t                 fInfoLevel;       // current level in the stack
   Int_t                 fCurrentID;        // unique ID of current node
   Int_t                 fIndex;            // index in array of ID's
   Int_t                 fIdBranch[100];    // current branch of indices
   TString               fPath;             // path for current branch
   TGeoNode             *fTop;              // top node
   TGeoNode             *fNode;             //! current node
   TGeoHMatrix          *fMatrix;           //! current matrix
   TObjArray            *fStack;            // stack of cache states
   TGeoHMatrix         **fMatrixBranch;     // current branch of global matrices
   TGeoHMatrix         **fMPB;              // pre-built matrices
   TGeoNode            **fNodeBranch;       // current branch of nodes
   TGeoStateInfo       **fInfoBranch;       // current branch of nodes
   TGeoStateInfo        *fPWInfo;           //! State info for the parallel world
   Int_t                *fNodeIdArray;      //! array of node id's

   TGeoNodeCache(const TGeoNodeCache&); // Not implemented
   TGeoNodeCache& operator=(const TGeoNodeCache&); // Not implemented

public:
   TGeoNodeCache();
   TGeoNodeCache(TGeoNode *top, Bool_t nodeid=kFALSE, Int_t capacity=30);
   virtual ~TGeoNodeCache();

   void                 BuildIdArray();
   void                 BuildInfoBranch();
   void                 CdNode(Int_t nodeid);
   Bool_t               CdDown(Int_t index);
   Bool_t               CdDown(TGeoNode *node);
   void                 CdTop() {fLevel=1; CdUp();}
   void                 CdUp();
   void                 FillIdBranch(const Int_t *br, Int_t startlevel=0) {memcpy(fIdBranch+startlevel,br,(fLevel+1-startlevel)*sizeof(Int_t)); fIndex=fIdBranch[fLevel];}
   const Int_t         *GetIdBranch() const {return fIdBranch;}
   void                *GetBranch() const   {return fNodeBranch;}
   void                 GetBranchNames(Int_t *names) const;
   void                 GetBranchNumbers(Int_t *copyNumbers, Int_t *volumeNumbers) const;
   void                 GetBranchOnlys(Int_t *isonly) const;
   void                *GetMatrices() const {return fMatrixBranch;}
   TGeoHMatrix         *GetCurrentMatrix() const {return fMatrix;}
   Int_t                GetCurrentNodeId() const;
   TGeoNode            *GetMother(Int_t up=1) const {return ((fLevel-up)>=0)?fNodeBranch[fLevel-up]:0;}
   TGeoHMatrix         *GetMotherMatrix(Int_t up=1) const {return ((fLevel-up)>=0)?fMatrixBranch[fLevel-up]:0;}
   TGeoNode            *GetNode() const        {return fNode;}
   TGeoNode            *GetTopNode() const     {return fTop;}
   TGeoStateInfo       *GetInfo();
   TGeoStateInfo       *GetMakePWInfo(Int_t nd);
   void                 ReleaseInfo();
   Int_t                GetLevel() const       {return fLevel;}
   const char          *GetPath();
   Int_t                GetStackLevel() const  {return fStackLevel;}
   Int_t                GetNodeId() const;
   Bool_t               HasIdArray() const {return (fNodeIdArray)?kTRUE:kFALSE;}
   Bool_t               IsDummy() const {return kTRUE;}

   void                 LocalToMaster(const Double_t *local, Double_t *master) const;
   void                 MasterToLocal(const Double_t *master, Double_t *local) const;
   void                 LocalToMasterVect(const Double_t *local, Double_t *master) const;
   void                 MasterToLocalVect(const Double_t *master, Double_t *local) const;
   void                 LocalToMasterBomb(const Double_t *local, Double_t *master) const;
   void                 MasterToLocalBomb(const Double_t *master, Double_t *local) const;
   Int_t                PushState(Bool_t ovlp, Int_t ntmany=0, Int_t startlevel=0, Double_t *point=0);
   Bool_t               PopState(Int_t &nmany, Double_t *point=0);
   Bool_t               PopState(Int_t &nmany, Int_t level, Double_t *point=0);
   void                 PopDummy(Int_t ipop=9999) {fStackLevel=(ipop>fStackLevel)?(fStackLevel-1):(ipop-1);}
   void                 Refresh() {fNode=fNodeBranch[fLevel]; fMatrix=fMatrixBranch[fLevel];}
   Bool_t               RestoreState(Int_t &nmany, TGeoCacheState *state, Double_t *point=0);

   ClassDef(TGeoNodeCache, 0)        // cache of reusable physical nodes
};

#endif
 TGeoCache.h:1
 TGeoCache.h:2
 TGeoCache.h:3
 TGeoCache.h:4
 TGeoCache.h:5
 TGeoCache.h:6
 TGeoCache.h:7
 TGeoCache.h:8
 TGeoCache.h:9
 TGeoCache.h:10
 TGeoCache.h:11
 TGeoCache.h:12
 TGeoCache.h:13
 TGeoCache.h:14
 TGeoCache.h:15
 TGeoCache.h:16
 TGeoCache.h:17
 TGeoCache.h:18
 TGeoCache.h:19
 TGeoCache.h:20
 TGeoCache.h:21
 TGeoCache.h:22
 TGeoCache.h:23
 TGeoCache.h:24
 TGeoCache.h:25
 TGeoCache.h:26
 TGeoCache.h:27
 TGeoCache.h:28
 TGeoCache.h:29
 TGeoCache.h:30
 TGeoCache.h:31
 TGeoCache.h:32
 TGeoCache.h:33
 TGeoCache.h:34
 TGeoCache.h:35
 TGeoCache.h:36
 TGeoCache.h:37
 TGeoCache.h:38
 TGeoCache.h:39
 TGeoCache.h:40
 TGeoCache.h:41
 TGeoCache.h:42
 TGeoCache.h:43
 TGeoCache.h:44
 TGeoCache.h:45
 TGeoCache.h:46
 TGeoCache.h:47
 TGeoCache.h:48
 TGeoCache.h:49
 TGeoCache.h:50
 TGeoCache.h:51
 TGeoCache.h:52
 TGeoCache.h:53
 TGeoCache.h:54
 TGeoCache.h:55
 TGeoCache.h:56
 TGeoCache.h:57
 TGeoCache.h:58
 TGeoCache.h:59
 TGeoCache.h:60
 TGeoCache.h:61
 TGeoCache.h:62
 TGeoCache.h:63
 TGeoCache.h:64
 TGeoCache.h:65
 TGeoCache.h:66
 TGeoCache.h:67
 TGeoCache.h:68
 TGeoCache.h:69
 TGeoCache.h:70
 TGeoCache.h:71
 TGeoCache.h:72
 TGeoCache.h:73
 TGeoCache.h:74
 TGeoCache.h:75
 TGeoCache.h:76
 TGeoCache.h:77
 TGeoCache.h:78
 TGeoCache.h:79
 TGeoCache.h:80
 TGeoCache.h:81
 TGeoCache.h:82
 TGeoCache.h:83
 TGeoCache.h:84
 TGeoCache.h:85
 TGeoCache.h:86
 TGeoCache.h:87
 TGeoCache.h:88
 TGeoCache.h:89
 TGeoCache.h:90
 TGeoCache.h:91
 TGeoCache.h:92
 TGeoCache.h:93
 TGeoCache.h:94
 TGeoCache.h:95
 TGeoCache.h:96
 TGeoCache.h:97
 TGeoCache.h:98
 TGeoCache.h:99
 TGeoCache.h:100
 TGeoCache.h:101
 TGeoCache.h:102
 TGeoCache.h:103
 TGeoCache.h:104
 TGeoCache.h:105
 TGeoCache.h:106
 TGeoCache.h:107
 TGeoCache.h:108
 TGeoCache.h:109
 TGeoCache.h:110
 TGeoCache.h:111
 TGeoCache.h:112
 TGeoCache.h:113
 TGeoCache.h:114
 TGeoCache.h:115
 TGeoCache.h:116
 TGeoCache.h:117
 TGeoCache.h:118
 TGeoCache.h:119
 TGeoCache.h:120
 TGeoCache.h:121
 TGeoCache.h:122
 TGeoCache.h:123
 TGeoCache.h:124
 TGeoCache.h:125
 TGeoCache.h:126
 TGeoCache.h:127
 TGeoCache.h:128
 TGeoCache.h:129
 TGeoCache.h:130
 TGeoCache.h:131
 TGeoCache.h:132
 TGeoCache.h:133
 TGeoCache.h:134
 TGeoCache.h:135
 TGeoCache.h:136
 TGeoCache.h:137
 TGeoCache.h:138
 TGeoCache.h:139
 TGeoCache.h:140
 TGeoCache.h:141
 TGeoCache.h:142
 TGeoCache.h:143
 TGeoCache.h:144
 TGeoCache.h:145
 TGeoCache.h:146
 TGeoCache.h:147