Ignore:
Timestamp:
12/13/05 17:28:02 (19 years ago)
Author:
mattausch
Message:

worked on vsp kd view cells

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/VUT/GtpVisibilityPreprocessor/src/VspKdTree.cpp

    r453 r462  
    2525#include "Ray.h" 
    2626#include "RayInfo.h" 
     27#include "ViewCell.h" 
    2728#include "ViewCellsManager.h" 
    28 #include "ViewCell.h" 
     29#include "ViewCellBsp.h" 
    2930 
    3031// Static variables 
    31 int VspKdTreeLeaf::mailID = 0; 
     32int VspKdLeaf::sMailId = 0; 
     33int MergeCandidate::sMaxPvsSize = 150; 
     34 
    3235 
    3336#define USE_FIXEDPOINT_T 0 
     
    7073 
    7174/**************************************************************/ 
    72 /*                class VspKdTreeNode implementation          */ 
     75/*                class VspKdNode implementation          */ 
    7376/**************************************************************/ 
    7477 
    7578// Inline constructor 
    76 VspKdTreeNode::VspKdTreeNode(VspKdTreeInterior *p): 
     79VspKdNode::VspKdNode(VspKdInterior *p): 
    7780mParent(p), mAxis(-1), mDepth(p ? p->mDepth + 1 : 0)  
    7881{} 
    7982 
    80 VspKdTreeNode::~VspKdTreeNode()  
     83VspKdNode::~VspKdNode()  
    8184{}; 
    8285 
    83 inline VspKdTreeInterior *VspKdTreeNode::GetParent() const 
     86inline VspKdInterior *VspKdNode::GetParent() const 
    8487{ 
    8588        return mParent; 
    8689} 
    8790 
    88 inline void VspKdTreeNode::SetParent(VspKdTreeInterior *p) 
     91inline void VspKdNode::SetParent(VspKdInterior *p) 
    8992{ 
    9093        mParent = p; 
    9194} 
    9295 
    93 bool VspKdTreeNode::IsLeaf() const  
     96bool VspKdNode::IsLeaf() const  
    9497{  
    9598        return mAxis == -1;  
    9699} 
    97100   
    98 int VspKdTreeNode::GetAccessTime()  
     101int VspKdNode::GetAccessTime()  
    99102{ 
    100103        return 0x7FFFFFF; 
     
    102105 
    103106/**************************************************************/ 
    104 /*                VspKdTreeInterior implementation            */ 
     107/*                VspKdInterior implementation            */ 
    105108/**************************************************************/ 
    106109 
    107 VspKdTreeInterior::VspKdTreeInterior(VspKdTreeInterior *p): 
    108 VspKdTreeNode(p), mBack(NULL), mFront(NULL), mAccesses(0), mLastAccessTime(-1) 
    109 { 
    110 } 
    111  
    112 int VspKdTreeInterior::GetAccessTime() 
     110VspKdInterior::VspKdInterior(VspKdInterior *p): 
     111VspKdNode(p), mBack(NULL), mFront(NULL), mAccesses(0), mLastAccessTime(-1) 
     112{ 
     113} 
     114 
     115int VspKdInterior::GetAccessTime() 
    113116{ 
    114117        return mLastAccessTime; 
    115118} 
    116119 
    117 void VspKdTreeInterior::SetupChildLinks(VspKdTreeNode *b, VspKdTreeNode *f)  
     120void VspKdInterior::SetupChildLinks(VspKdNode *b, VspKdNode *f)  
    118121{ 
    119122        mBack = b; 
     
    123126} 
    124127 
    125 void VspKdTreeInterior::ReplaceChildLink(VspKdTreeNode *oldChild,  
    126                                                                                  VspKdTreeNode *newChild) 
     128void VspKdInterior::ReplaceChildLink(VspKdNode *oldChild,  
     129                                                                                 VspKdNode *newChild) 
    127130{ 
    128131        if (mBack == oldChild) 
     
    132135} 
    133136 
    134 int VspKdTreeInterior::Type() const   
     137int VspKdInterior::Type() const   
    135138{  
    136139        return EInterior;  
    137140} 
    138141   
    139 VspKdTreeInterior::~VspKdTreeInterior() 
     142VspKdInterior::~VspKdInterior() 
    140143{ 
    141144        DEL_PTR(mBack); 
     
    143146} 
    144147   
    145 void VspKdTreeInterior::Print(ostream &s) const  
     148void VspKdInterior::Print(ostream &s) const  
    146149{ 
    147150        switch (mAxis) 
     
    161164} 
    162165         
    163 int VspKdTreeInterior::ComputeRayIntersection(const RayInfo &rayData, float &t)  
     166int VspKdInterior::ComputeRayIntersection(const RayInfo &rayData, float &t)  
    164167{ 
    165168        return rayData.ComputeRayIntersection(mAxis, mPosition, t); 
    166169} 
    167170 
    168 VspKdTreeNode *VspKdTreeInterior::GetBack() const 
     171VspKdNode *VspKdInterior::GetBack() const 
    169172{ 
    170173        return mBack; 
    171174} 
    172175 
    173 VspKdTreeNode *VspKdTreeInterior::GetFront() const 
     176VspKdNode *VspKdInterior::GetFront() const 
    174177{ 
    175178        return mFront; 
     
    178181 
    179182/**************************************************************/ 
    180 /*              class VspKdTreeLeaf implementation            */ 
     183/*              class VspKdLeaf implementation            */ 
    181184/**************************************************************/ 
    182 VspKdTreeLeaf::VspKdTreeLeaf(VspKdTreeInterior *p,      const int nRays): 
    183 VspKdTreeNode(p), mRays(), mPvsSize(0), mValidPvs(false), mViewCell(NULL) 
     185 
     186 
     187VspKdLeaf::VspKdLeaf(VspKdInterior *p,  const int nRays): 
     188VspKdNode(p), mRays(), mPvsSize(0), mValidPvs(false), mViewCell(NULL) 
    184189{ 
    185190        mRays.reserve(nRays); 
    186191} 
    187192 
    188 VspKdTreeLeaf::~VspKdTreeLeaf()  
    189 {} 
    190  
    191 int VspKdTreeLeaf::Type() const   
     193VspKdLeaf::~VspKdLeaf()  
     194{ 
     195} 
     196 
     197int VspKdLeaf::Type() const   
    192198{  
    193199        return ELeaf;  
    194200} 
    195201 
    196 void VspKdTreeLeaf::Print(ostream &s) const  
     202void VspKdLeaf::Print(ostream &s) const  
    197203{ 
    198204        s << endl << "L: r = " << (int)mRays.size() << endl; 
    199205}; 
    200206 
    201 void VspKdTreeLeaf::AddRay(const RayInfo &data)  
     207void VspKdLeaf::AddRay(const RayInfo &data)  
    202208{ 
    203209        mValidPvs = false; 
     
    206212} 
    207213 
    208 int VspKdTreeLeaf::GetPvsSize() const  
     214int VspKdLeaf::GetPvsSize() const  
    209215{ 
    210216        return mPvsSize; 
    211217} 
    212218 
    213 void VspKdTreeLeaf::SetPvsSize(const int s)  
     219void VspKdLeaf::SetPvsSize(const int s)  
    214220{ 
    215221        mPvsSize = s; 
    216222} 
    217223 
    218 void VspKdTreeLeaf::Mail() 
     224void VspKdLeaf::Mail() 
    219225{  
    220         mMailbox = mailID;  
    221 } 
    222  
    223 void VspKdTreeLeaf::NewMail()  
     226        mMailbox = sMailId;  
     227} 
     228 
     229void VspKdLeaf::NewMail()  
    224230{  
    225         ++ mailID;  
    226 } 
    227  
    228 bool VspKdTreeLeaf::Mailed() const  
     231        ++ sMailId;  
     232} 
     233 
     234bool VspKdLeaf::Mailed() const  
    229235{  
    230         return mMailbox == mailID;  
    231 } 
    232  
    233 bool VspKdTreeLeaf::Mailed(const int mail)  
    234 { 
    235         return mMailbox >= mailID + mail; 
    236 } 
    237  
    238 float VspKdTreeLeaf::GetAvgRayContribution() const  
     236        return mMailbox == sMailId;  
     237} 
     238 
     239bool VspKdLeaf::Mailed(const int mail) const 
     240{ 
     241        return mMailbox == mail; 
     242} 
     243 
     244int VspKdLeaf::GetMailbox() const 
     245{ 
     246        return mMailbox; 
     247} 
     248 
     249float VspKdLeaf::GetAvgRayContribution() const  
    239250{ 
    240251        return GetPvsSize() / ((float)mRays.size() + Limits::Small); 
    241252} 
    242253 
    243 float VspKdTreeLeaf::GetSqrRayContribution() const  
     254float VspKdLeaf::GetSqrRayContribution() const  
    244255{ 
    245256        return sqr(GetAvgRayContribution()); 
    246257} 
    247258 
    248 RayInfoContainer &VspKdTreeLeaf::GetRays() 
     259RayInfoContainer &VspKdLeaf::GetRays() 
    249260{ 
    250261        return mRays; 
    251262} 
    252263 
    253 void VspKdTreeLeaf::ExtractPvs(ObjectContainer &objects) const 
     264void VspKdLeaf::ExtractPvs(ObjectContainer &objects) const 
    254265{ 
    255266        RayInfoContainer::const_iterator it, it_end = mRays.end(); 
     
    264275} 
    265276 
    266 void VspKdTreeLeaf::GetRays(VssRayContainer &rays) 
     277void VspKdLeaf::GetRays(VssRayContainer &rays) 
    267278{ 
    268279        RayInfoContainer::const_iterator it, it_end = mRays.end(); 
     
    272283} 
    273284 
    274 ViewCell *VspKdTreeLeaf::GetViewCell() 
     285VspKdViewCell *VspKdLeaf::GetViewCell() 
    275286{ 
    276287        return mViewCell; 
    277288} 
    278289 
    279 /*********************************************************/ 
    280 /*            class VspKdTree implementation             */ 
    281 /*********************************************************/ 
    282  
    283  
    284 VspKdTree::VspKdTree(): mOnlyDrivingAxis(true) 
    285 {          
    286         environment->GetIntValue("VspKdTree.Termination.maxDepth", mTermMaxDepth); 
    287         environment->GetIntValue("VspKdTree.Termination.minPvs", mTermMinPvs); 
    288         environment->GetIntValue("VspKdTree.Termination.minRays", mTermMinRays); 
    289         environment->GetFloatValue("VspKdTree.Termination.maxRayContribution", mTermMaxRayContribution); 
    290         environment->GetFloatValue("VspKdTree.Termination.maxCostRatio", mTermMaxCostRatio); 
    291         environment->GetFloatValue("VspKdTree.Termination.minSize", mTermMinSize); 
    292  
    293         mTermMinSize = sqr(mTermMinSize); 
    294  
    295         environment->GetFloatValue("VspKdTree.epsilon", mEpsilon); 
    296         environment->GetFloatValue("VspKdTree.ct_div_ci", mCtDivCi); 
    297          
    298         environment->GetFloatValue("VspKdTree.maxTotalMemory", mMaxTotalMemory); 
    299         environment->GetFloatValue("VspKdTree.maxStaticMemory", mMaxStaticMemory); 
    300      
    301         environment->GetIntValue("VspKdTree.accessTimeThreshold", mAccessTimeThreshold); 
    302         environment->GetIntValue("VspKdTree.minCollapseDepth", mMinCollapseDepth); 
    303  
    304         // split type 
    305         char sname[128]; 
    306         environment->GetStringValue("VspKdTree.splitType", sname); 
    307         string name(sname); 
    308                  
    309         Debug << "======= vsp kd tree options ========" << endl; 
    310         Debug << "max depth: "<< mTermMaxDepth << endl; 
    311         Debug << "min pvs: "<< mTermMinPvs << endl; 
    312         Debug << "min rays: "<< mTermMinRays << endl; 
    313         Debug << "max ray contribution: "<< mTermMaxRayContribution << endl; 
    314         Debug << "max cost ratio: "<< mTermMaxCostRatio << endl; 
    315         Debug << "min size: "<<mTermMinSize << endl; 
    316  
    317         if (name.compare("regular") == 0) 
    318         { 
    319                 Debug << "using regular split" << endl; 
    320                 splitType = ESplitRegular; 
    321         } 
    322         else 
    323         { 
    324                 if (name.compare("heuristic") == 0) 
    325                 { 
    326                         Debug << "using heuristic split" << endl; 
    327                         splitType = ESplitHeuristic; 
    328                 } 
    329                 else  
    330                 { 
    331                         cerr << "Invalid VspKdTree split type " << name << endl; 
    332                         exit(1); 
    333                 } 
    334         } 
    335  
    336         mRoot = NULL; 
    337  
    338         mSplitCandidates = new vector<SortableEntry>; 
    339 } 
    340  
    341  
    342 VspKdTree::~VspKdTree() 
    343 { 
    344         DEL_PTR(mRoot); 
    345         DEL_PTR(mSplitCandidates); 
    346 } 
    347  
    348 void VspKdStatistics::Print(ostream &app) const 
    349 { 
    350         app << "===== VspKdTree statistics ===============\n"; 
    351  
    352         app << "#N_RAYS ( Number of rays )\n" 
    353                 << rays << endl; 
    354    
    355         app << "#N_INITPVS ( Initial PVS size )\n" 
    356                 << initialPvsSize << endl; 
    357    
    358         app << "#N_NODES ( Number of nodes )\n" << nodes << "\n"; 
    359  
    360         app << "#N_LEAVES ( Number of leaves )\n" << Leaves() << "\n"; 
    361  
    362         app << "#N_SPLITS ( Number of splits in axes x y z dx dy dz \n"; 
    363    
    364         for (int i=0; i<7; i++) 
    365                 app << splits[i] <<" "; 
    366         app << endl; 
    367  
    368         app << "#N_RAYREFS ( Number of rayRefs )\n" << 
    369                 rayRefs << "\n"; 
    370  
    371         app << "#N_RAYRAYREFS  ( Number of rayRefs / ray )\n" << 
    372                 rayRefs / (double)rays << "\n"; 
    373  
    374         app << "#N_LEAFRAYREFS  ( Number of rayRefs / leaf )\n" << 
    375                 rayRefs / (double)Leaves() << "\n"; 
    376  
    377         app << "#N_MAXRAYREFS  ( Max number of rayRefs / leaf )\n" << 
    378                 maxRayRefs << "\n"; 
    379  
    380   //  app << setprecision(4); 
    381  
    382         app << "#N_PMAXDEPTHLEAVES ( Percentage of leaves at maxdepth )\n" << 
    383                 maxDepthNodes * 100 / (double)Leaves() << endl; 
    384  
    385         app << "#N_PMINPVSLEAVES  ( Percentage of leaves with mininimal PVS )\n" << 
    386                 minPvsNodes * 100 / (double)Leaves() << endl; 
    387  
    388         app << "#N_PMINRAYSLEAVES  ( Percentage of leaves with minimal number of rays)\n" << 
    389                 minRaysNodes * 100 / (double)Leaves() << endl; 
    390  
    391         app << "#N_PMINSIZELEAVES  ( Percentage of leaves with minSize )\n"<< 
    392                 minSizeNodes * 100 / (double)Leaves() << endl; 
    393  
    394         app << "#N_PMAXRAYCONTRIBLEAVES  ( Percentage of leaves with maximal ray contribution )\n" << 
    395                 maxRayContribNodes * 100 / (double)Leaves() << endl; 
    396  
    397         app << "#N_ADDED_RAYREFS  ( Number of dynamically added ray references )\n"<< 
    398                 addedRayRefs << endl; 
    399  
    400         app << "#N_REMOVED_RAYREFS  ( Number of dynamically removed ray references )\n"<< 
    401                 removedRayRefs << endl; 
    402  
    403         //  app << setprecision(4); 
    404  
    405         app << "#N_MAXPVS ( Maximal PVS size / leaf)\n" 
    406                 << maxPvsSize << endl; 
    407  
    408         app << "#N_CTIME  ( Construction time [s] )\n" 
    409                 << Time() << " \n"; 
    410  
    411         app << "===== END OF VspKdTree statistics ==========\n"; 
    412 } 
    413  
    414  
    415 void 
    416 VspKdTreeLeaf::UpdatePvsSize() 
     290void VspKdLeaf::SetViewCell(VspKdViewCell *viewCell) 
     291{ 
     292        mViewCell = viewCell; 
     293} 
     294 
     295 
     296void VspKdLeaf::UpdatePvsSize() 
    417297{ 
    418298        if (!mValidPvs)  
     
    449329} 
    450330 
    451  
    452 void 
    453 VspKdTree::Construct(const VssRayContainer &rays, 
    454                                          AxisAlignedBox3 *forcedBoundingBox) 
     331/*********************************************************/ 
     332/*            class VspKdTree implementation             */ 
     333/*********************************************************/ 
     334 
     335 
     336 
     337VspKdTree::VspKdTree(): mOnlyDrivingAxis(true) 
     338{          
     339        environment->GetIntValue("VspKdTree.Termination.maxDepth", mTermMaxDepth); 
     340        environment->GetIntValue("VspKdTree.Termination.minPvs", mTermMinPvs); 
     341        environment->GetIntValue("VspKdTree.Termination.minRays", mTermMinRays); 
     342        environment->GetFloatValue("VspKdTree.Termination.maxRayContribution", mTermMaxRayContribution); 
     343        environment->GetFloatValue("VspKdTree.Termination.maxCostRatio", mTermMaxCostRatio); 
     344        environment->GetFloatValue("VspKdTree.Termination.minSize", mTermMinSize); 
     345 
     346        mTermMinSize = sqr(mTermMinSize); 
     347 
     348        environment->GetFloatValue("VspKdTree.epsilon", mEpsilon); 
     349        environment->GetFloatValue("VspKdTree.ct_div_ci", mCtDivCi); 
     350         
     351        environment->GetFloatValue("VspKdTree.maxTotalMemory", mMaxTotalMemory); 
     352        environment->GetFloatValue("VspKdTree.maxStaticMemory", mMaxStaticMemory); 
     353     
     354        environment->GetIntValue("VspKdTree.accessTimeThreshold", mAccessTimeThreshold); 
     355        environment->GetIntValue("VspKdTree.minCollapseDepth", mMinCollapseDepth); 
     356 
     357        environment->GetFloatValue("VspKdTree.maxCostRatio", mMaxCostRatio); 
     358        environment->GetIntValue("ViewCells.maxViewCells", mMaxViewCells); 
     359 
     360        MergeCandidate::sMaxPvsSize = 300; // TODO: add option 
     361 
     362        // split type 
     363        char sname[128]; 
     364        environment->GetStringValue("VspKdTree.splitType", sname); 
     365        string name(sname); 
     366                 
     367        Debug << "======= vsp kd tree options ========" << endl; 
     368        Debug << "max depth: "<< mTermMaxDepth << endl; 
     369        Debug << "min pvs: "<< mTermMinPvs << endl; 
     370        Debug << "min rays: "<< mTermMinRays << endl; 
     371        Debug << "max ray contribution: "<< mTermMaxRayContribution << endl; 
     372        Debug << "max cost ratio: "<< mTermMaxCostRatio << endl; 
     373        Debug << "min size: "<<mTermMinSize << endl; 
     374 
     375        if (name.compare("regular") == 0) 
     376        { 
     377                Debug << "using regular split" << endl; 
     378                splitType = ESplitRegular; 
     379        } 
     380        else 
     381        { 
     382                if (name.compare("heuristic") == 0) 
     383                { 
     384                        Debug << "using heuristic split" << endl; 
     385                        splitType = ESplitHeuristic; 
     386                } 
     387                else  
     388                { 
     389                        cerr << "Invalid VspKdTree split type " << name << endl; 
     390                        exit(1); 
     391                } 
     392        } 
     393 
     394        mRoot = NULL; 
     395 
     396        mSplitCandidates = new vector<SortableEntry>; 
     397} 
     398 
     399 
     400VspKdTree::~VspKdTree() 
     401{ 
     402        DEL_PTR(mRoot); 
     403        DEL_PTR(mSplitCandidates); 
     404} 
     405 
     406void VspKdStatistics::Print(ostream &app) const 
     407{ 
     408        app << "===== VspKdTree statistics ===============\n"; 
     409 
     410        app << "#N_RAYS ( Number of rays )\n" 
     411                << rays << endl; 
     412   
     413        app << "#N_INITPVS ( Initial PVS size )\n" 
     414                << initialPvsSize << endl; 
     415   
     416        app << "#N_NODES ( Number of nodes )\n" << nodes << "\n"; 
     417 
     418        app << "#N_LEAVES ( Number of leaves )\n" << Leaves() << "\n"; 
     419 
     420        app << "#N_SPLITS ( Number of splits in axes x y z dx dy dz \n"; 
     421   
     422        for (int i=0; i<7; i++) 
     423                app << splits[i] <<" "; 
     424        app << endl; 
     425 
     426        app << "#N_RAYREFS ( Number of rayRefs )\n" << 
     427                rayRefs << "\n"; 
     428 
     429        app << "#N_RAYRAYREFS  ( Number of rayRefs / ray )\n" << 
     430                rayRefs / (double)rays << "\n"; 
     431 
     432        app << "#N_LEAFRAYREFS  ( Number of rayRefs / leaf )\n" << 
     433                rayRefs / (double)Leaves() << "\n"; 
     434 
     435        app << "#N_MAXRAYREFS  ( Max number of rayRefs / leaf )\n" << 
     436                maxRayRefs << "\n"; 
     437 
     438  //  app << setprecision(4); 
     439 
     440        app << "#N_PMAXDEPTHLEAVES ( Percentage of leaves at maxdepth )\n" << 
     441                maxDepthNodes * 100 / (double)Leaves() << endl; 
     442 
     443        app << "#N_PMINPVSLEAVES  ( Percentage of leaves with mininimal PVS )\n" << 
     444                minPvsNodes * 100 / (double)Leaves() << endl; 
     445 
     446        app << "#N_PMINRAYSLEAVES  ( Percentage of leaves with minimal number of rays)\n" << 
     447                minRaysNodes * 100 / (double)Leaves() << endl; 
     448 
     449        app << "#N_PMINSIZELEAVES  ( Percentage of leaves with minSize )\n"<< 
     450                minSizeNodes * 100 / (double)Leaves() << endl; 
     451 
     452        app << "#N_PMAXRAYCONTRIBLEAVES  ( Percentage of leaves with maximal ray contribution )\n" << 
     453                maxRayContribNodes * 100 / (double)Leaves() << endl; 
     454 
     455        app << "#N_ADDED_RAYREFS  ( Number of dynamically added ray references )\n"<< 
     456                addedRayRefs << endl; 
     457 
     458        app << "#N_REMOVED_RAYREFS  ( Number of dynamically removed ray references )\n"<< 
     459                removedRayRefs << endl; 
     460 
     461        //  app << setprecision(4); 
     462 
     463        app << "#N_( Maximal PVS size / leaf)\n" 
     464                << maxPvsSize << endl; 
     465 
     466        app << "#N_CTIME  ( Construction time [s] )\n" 
     467                << Time() << " \n"; 
     468 
     469        app << "===== END OF VspKdTree statistics ==========\n"; 
     470} 
     471 
     472 
     473void VspKdTree::CollectViewCells(ViewCellContainer &viewCells) const 
     474{ 
     475        stack<VspKdNode *> nodeStack; 
     476        nodeStack.push(mRoot); 
     477 
     478        ViewCell::NewMail(); 
     479 
     480        while (!nodeStack.empty())  
     481        { 
     482                VspKdNode *node = nodeStack.top(); 
     483                nodeStack.pop(); 
     484 
     485                if (node->IsLeaf())  
     486                { 
     487                        ViewCell *viewCell = dynamic_cast<VspKdLeaf *>(node)->mViewCell; 
     488 
     489                        if (!viewCell->Mailed())  
     490                        { 
     491                                viewCell->Mail(); 
     492                                viewCells.push_back(viewCell); 
     493                        } 
     494                } 
     495                else  
     496                { 
     497                        VspKdInterior *interior = dynamic_cast<VspKdInterior *>(node); 
     498 
     499                        nodeStack.push(interior->GetFront()); 
     500                        nodeStack.push(interior->GetBack()); 
     501                } 
     502        } 
     503} 
     504 
     505void VspKdTree::Construct(const VssRayContainer &rays, 
     506                                                  AxisAlignedBox3 *forcedBoundingBox) 
    455507{ 
    456508        mStat.Start(); 
     
    459511        DEL_PTR(mRoot); 
    460512 
    461         mRoot = new VspKdTreeLeaf(NULL, (int)rays.size()); 
     513        mRoot = new VspKdLeaf(NULL, (int)rays.size()); 
    462514 
    463515        // first construct a leaf that will get subdivided 
    464         VspKdTreeLeaf *leaf = dynamic_cast<VspKdTreeLeaf *>(mRoot); 
     516        VspKdLeaf *leaf = dynamic_cast<VspKdLeaf *>(mRoot); 
    465517         
    466518        mStat.nodes = 1; 
     
    523575 
    524576 
    525 VspKdTreeNode *VspKdTree::Subdivide(const TraversalData &tdata) 
    526 { 
    527         VspKdTreeNode *result = NULL; 
     577VspKdNode *VspKdTree::Subdivide(const TraversalData &tdata) 
     578{ 
     579        VspKdNode *result = NULL; 
    528580 
    529581        priority_queue<TraversalData> tStack; 
     
    562614                tStack.pop();     
    563615                 
    564                 VspKdTreeNode *node = SubdivideNode((VspKdTreeLeaf *) data.mNode, 
     616                VspKdNode *node = SubdivideNode((VspKdLeaf *) data.mNode, 
    565617                                                                                        data.mBox, backBox,     frontBox); 
    566618                if (result == NULL) 
     
    569621                if (!node->IsLeaf())  
    570622                { 
    571                         VspKdTreeInterior *interior = dynamic_cast<VspKdTreeInterior *>(node); 
     623                        VspKdInterior *interior = dynamic_cast<VspKdInterior *>(node); 
    572624 
    573625                        // push the children on the stack 
     
    586638 
    587639// returns selected plane for subdivision 
    588 int VspKdTree::SelectPlane(VspKdTreeLeaf *leaf, 
     640int VspKdTree::SelectPlane(VspKdLeaf *leaf, 
    589641                                                   const AxisAlignedBox3 &box, 
    590642                                                   float &position, 
     
    641693 
    642694                                                         
    643 float VspKdTree::EvalCostRatio(VspKdTreeLeaf *leaf, 
     695float VspKdTree::EvalCostRatio(VspKdLeaf *leaf, 
    644696                                                           const int axis, 
    645697                                                           const float position, 
     
    711763} 
    712764 
    713 float VspKdTree::BestCostRatioRegular(VspKdTreeLeaf *leaf, 
     765float VspKdTree::BestCostRatioRegular(VspKdLeaf *leaf, 
    714766                                                                          int &axis, 
    715767                                                                          float &position, 
     
    772824} 
    773825 
    774 float VspKdTree::BestCostRatioHeuristic(VspKdTreeLeaf *leaf, 
     826float VspKdTree::BestCostRatioHeuristic(VspKdLeaf *leaf, 
    775827                                                                                int &axis, 
    776828                                                                                float &position, 
     
    895947} 
    896948 
    897 void VspKdTree::SortSplitCandidates(VspKdTreeLeaf *node, 
     949void VspKdTree::SortSplitCandidates(VspKdLeaf *node, 
    898950                                                                        const int axis) 
    899951{ 
     
    930982{ 
    931983        // the node became a leaf -> evaluate stats for leafs 
    932         VspKdTreeLeaf *leaf = dynamic_cast<VspKdTreeLeaf *>(data.mNode); 
     984        VspKdLeaf *leaf = dynamic_cast<VspKdLeaf *>(data.mNode); 
    933985 
    934986        if (leaf->GetPvsSize() > mStat.maxPvsSize) 
     
    9561008 
    9571009 
    958 inline bool VspKdTree::TerminationCriteriaMet(const VspKdTreeLeaf *leaf,  
     1010inline bool VspKdTree::TerminationCriteriaMet(const VspKdLeaf *leaf,  
    9591011                                                                                          const AxisAlignedBox3 &box) const 
    9601012{ 
    9611013        return ((leaf->GetPvsSize() < mTermMinPvs) ||  
    9621014                    (leaf->mRays.size() < mTermMinRays) || 
    963                         (leaf->GetAvgRayContribution() > mTermMaxRayContribution ) || 
     1015                        //(leaf->GetAvgRayContribution() > mTermMaxRayContribution ) || 
    9641016                        (leaf->mDepth >= mTermMaxDepth) ||  
    9651017                        (SqrMagnitude(box.Size()) <= mTermMinSize)); 
     
    9671019 
    9681020 
    969 VspKdTreeNode *VspKdTree::SubdivideNode(VspKdTreeLeaf *leaf, 
     1021VspKdNode *VspKdTree::SubdivideNode(VspKdLeaf *leaf, 
    9701022                                                                                const AxisAlignedBox3 &box, 
    9711023                                                                                AxisAlignedBox3 &backBBox, 
     
    10051057 
    10061058        // add the new nodes to the tree 
    1007         VspKdTreeInterior *node = new VspKdTreeInterior(leaf->mParent); 
     1059        VspKdInterior *node = new VspKdInterior(leaf->mParent); 
    10081060 
    10091061        node->mAxis = axis; 
     
    10141066        frontBBox = box; 
    10151067 
    1016         VspKdTreeLeaf *back = new VspKdTreeLeaf(node, raysBack); 
     1068        VspKdLeaf *back = new VspKdLeaf(node, raysBack); 
    10171069        back->SetPvsSize(pvsBack); 
    1018         VspKdTreeLeaf *front = new VspKdTreeLeaf(node, raysFront); 
     1070        VspKdLeaf *front = new VspKdLeaf(node, raysFront); 
    10191071        front->SetPvsSize(pvsFront); 
    10201072 
     
    10251077        node->SetupChildLinks(back, front); 
    10261078         
    1027         if (axis <= VspKdTreeNode::SPLIT_Z)  
     1079        if (axis <= VspKdNode::SPLIT_Z)  
    10281080        { 
    10291081                backBBox.SetMax(axis, position); 
     
    11101162int VspKdTree::ReleaseMemory(const int time) 
    11111163{ 
    1112         stack<VspKdTreeNode *> tstack; 
     1164        stack<VspKdNode *> tstack; 
    11131165 
    11141166        // find a node in the tree which subtree will be collapsed 
     
    11201172        while (!tstack.empty()) 
    11211173        { 
    1122                 VspKdTreeNode *node = tstack.top(); 
     1174                VspKdNode *node = tstack.top(); 
    11231175                tstack.pop(); 
    11241176     
    11251177                if (!node->IsLeaf())  
    11261178                { 
    1127                         VspKdTreeInterior *in = dynamic_cast<VspKdTreeInterior *>(node); 
     1179                        VspKdInterior *in = dynamic_cast<VspKdInterior *>(node); 
    11281180                        // cout<<"depth="<<(int)in->depth<<" time="<<in->lastAccessTime<<endl; 
    11291181                        if (in->mDepth >= mMinCollapseDepth && in->mLastAccessTime <= maxAccessTime)  
     
    11561208 
    11571209 
    1158 VspKdTreeNode *VspKdTree::SubdivideLeaf(VspKdTreeLeaf *leaf, 
     1210VspKdNode *VspKdTree::SubdivideLeaf(VspKdLeaf *leaf, 
    11591211                                                                                const float sizeThreshold) 
    11601212{ 
    1161         VspKdTreeNode *node = leaf; 
     1213        VspKdNode *node = leaf; 
    11621214 
    11631215        AxisAlignedBox3 leafBBox = GetBBox(leaf); 
     
    11881240                                                   VssRayContainer &add) 
    11891241{ 
    1190         VspKdTreeLeaf::NewMail(); 
     1242        VspKdLeaf::NewMail(); 
    11911243 
    11921244        // schedule rays for removal 
     
    12181270 
    12191271void VspKdTree::RemoveRay(VssRay *ray, 
    1220                                                   vector<VspKdTreeLeaf *> *affectedLeaves, 
     1272                                                  vector<VspKdLeaf *> *affectedLeaves, 
    12211273                                                  const bool removeAllScheduledRays) 
    12221274{ 
     
    12431295                        // remove the ray from the leaf 
    12441296                        // find the ray in the leaf and swap it with the last ray... 
    1245                         VspKdTreeLeaf *leaf = (VspKdTreeLeaf *)data.mNode; 
     1297                        VspKdLeaf *leaf = (VspKdLeaf *)data.mNode; 
    12461298       
    12471299                        if (!leaf->Mailed())  
     
    13291381                        // remove the ray from the leaf 
    13301382                        // find the ray in the leaf and swap it with the last ray 
    1331                         VspKdTreeLeaf *leaf = dynamic_cast<VspKdTreeLeaf *>(data.mNode); 
     1383                        VspKdLeaf *leaf = dynamic_cast<VspKdLeaf *>(data.mNode); 
    13321384 
    13331385                        leaf->AddRay(data.mRayData); 
     
    13401392                                                                         stack<RayTraversalData> &tstack) 
    13411393{ 
    1342         VspKdTreeInterior *in = dynamic_cast<VspKdTreeInterior *>(data.mNode); 
    1343    
    1344         if (in->mAxis <= VspKdTreeNode::SPLIT_Z)  
     1394        VspKdInterior *in = dynamic_cast<VspKdInterior *>(data.mNode); 
     1395   
     1396        if (in->mAxis <= VspKdNode::SPLIT_Z)  
    13451397        { 
    13461398            // determine the side of this ray with respect to the plane 
     
    13871439 
    13881440 
    1389 int VspKdTree::CollapseSubtree(VspKdTreeNode *sroot, const int time) 
     1441int VspKdTree::CollapseSubtree(VspKdNode *sroot, const int time) 
    13901442{ 
    13911443        // first count all rays in the subtree 
    13921444        // use mail 1 for this purpose 
    1393         stack<VspKdTreeNode *> tstack; 
     1445        stack<VspKdNode *> tstack; 
    13941446         
    13951447        int rayCount = 0; 
     
    14131465                ++ collapsedNodes; 
    14141466                 
    1415                 VspKdTreeNode *node = tstack.top(); 
     1467                VspKdNode *node = tstack.top(); 
    14161468                tstack.pop(); 
    14171469                if (node->IsLeaf())  
    14181470                { 
    1419                         VspKdTreeLeaf *leaf = (VspKdTreeLeaf *) node; 
     1471                        VspKdLeaf *leaf = (VspKdLeaf *) node; 
    14201472                         
    14211473                        for(RayInfoContainer::iterator ri = leaf->mRays.begin(); 
     
    14331485                else  
    14341486                { 
    1435                         tstack.push(((VspKdTreeInterior *)node)->GetFront()); 
    1436                         tstack.push(((VspKdTreeInterior *)node)->GetBack()); 
     1487                        tstack.push(((VspKdInterior *)node)->GetFront()); 
     1488                        tstack.push(((VspKdInterior *)node)->GetBack()); 
    14371489                } 
    14381490        } 
     
    14411493 
    14421494        // create a new node that will hold the rays 
    1443         VspKdTreeLeaf *newLeaf = new VspKdTreeLeaf(sroot->mParent, rayCount); 
     1495        VspKdLeaf *newLeaf = new VspKdLeaf(sroot->mParent, rayCount); 
    14441496         
    14451497        if (newLeaf->mParent) 
     
    14501502        while (!tstack.empty())  
    14511503        { 
    1452                 VspKdTreeNode *node = tstack.top(); 
     1504                VspKdNode *node = tstack.top(); 
    14531505                tstack.pop(); 
    14541506 
    14551507                if (node->IsLeaf())  
    14561508                { 
    1457                         VspKdTreeLeaf *leaf = dynamic_cast<VspKdTreeLeaf *>(node); 
     1509                        VspKdLeaf *leaf = dynamic_cast<VspKdLeaf *>(node); 
    14581510 
    14591511                        for(RayInfoContainer::iterator ri = leaf->mRays.begin(); 
     
    14761528                else  
    14771529                { 
    1478                         VspKdTreeInterior *interior =  
    1479                                 dynamic_cast<VspKdTreeInterior *>(node); 
     1530                        VspKdInterior *interior =  
     1531                                dynamic_cast<VspKdInterior *>(node); 
    14801532                        tstack.push(interior->GetBack()); 
    14811533                        tstack.push(interior->GetFront()); 
     
    14861538        DEL_PTR(sroot); 
    14871539   
    1488         // for(VspKdTreeNode::SRayContainer::iterator ri = newleaf->mRays.begin(); 
     1540        // for(VspKdNode::SRayContainer::iterator ri = newleaf->mRays.begin(); 
    14891541    //      ri != newleaf->mRays.end(); ++ ri) 
    14901542        //     (*ri).ray->UnMail(2); 
     
    15101562 
    15111563 
    1512 int VspKdTree::GetPvsSize(VspKdTreeNode *node, const AxisAlignedBox3 &box) const 
    1513 { 
    1514         stack<VspKdTreeNode *> tstack; 
     1564int VspKdTree::GetPvsSize(VspKdNode *node, const AxisAlignedBox3 &box) const 
     1565{ 
     1566        stack<VspKdNode *> tstack; 
    15151567        tstack.push(mRoot); 
    15161568 
     
    15201572        while (!tstack.empty())  
    15211573        { 
    1522                 VspKdTreeNode *node = tstack.top(); 
     1574                VspKdNode *node = tstack.top(); 
    15231575                tstack.pop(); 
    15241576     
    15251577          if (node->IsLeaf())  
    15261578                { 
    1527                         VspKdTreeLeaf *leaf = (VspKdTreeLeaf *)node; 
     1579                        VspKdLeaf *leaf = (VspKdLeaf *)node; 
    15281580                        for (RayInfoContainer::iterator ri = leaf->mRays.begin(); 
    15291581                                 ri != leaf->mRays.end(); ++ ri) 
     
    15521604                else  
    15531605                { 
    1554                         VspKdTreeInterior *in = dynamic_cast<VspKdTreeInterior *>(node); 
     1606                        VspKdInterior *in = dynamic_cast<VspKdInterior *>(node); 
    15551607         
    15561608                        if (in->mAxis < 3)  
     
    15781630                                                                                         float &avgRayContribution) 
    15791631{ 
    1580         stack<VspKdTreeNode *> tstack; 
     1632        stack<VspKdNode *> tstack; 
    15811633        tstack.push(mRoot); 
    15821634         
     
    15891641        while (!tstack.empty())  
    15901642        { 
    1591                 VspKdTreeNode *node = tstack.top(); 
     1643                VspKdNode *node = tstack.top(); 
    15921644                tstack.pop(); 
    15931645                if (node->IsLeaf())  
    15941646                { 
    15951647                        leaves++; 
    1596                         VspKdTreeLeaf *leaf = (VspKdTreeLeaf *)node; 
     1648                        VspKdLeaf *leaf = (VspKdLeaf *)node; 
    15971649                        float c = leaf->GetAvgRayContribution(); 
    15981650 
     
    16051657                }  
    16061658                else { 
    1607                         VspKdTreeInterior *in = (VspKdTreeInterior *)node; 
     1659                        VspKdInterior *in = (VspKdInterior *)node; 
    16081660                        // both nodes for directional splits 
    16091661                        tstack.push(in->GetFront()); 
     
    16211673                                                        SimpleRayContainer &rays) 
    16221674{ 
    1623         stack<VspKdTreeNode *> tstack; 
     1675        stack<VspKdNode *> tstack; 
    16241676        tstack.push(mRoot); 
    16251677         
    16261678        while (!tstack.empty())  
    16271679        { 
    1628                 VspKdTreeNode *node = tstack.top(); 
     1680                VspKdNode *node = tstack.top(); 
    16291681                tstack.pop(); 
    16301682                 
    16311683                if (node->IsLeaf())  
    16321684                { 
    1633                         VspKdTreeLeaf *leaf = dynamic_cast<VspKdTreeLeaf *>(node); 
     1685                        VspKdLeaf *leaf = dynamic_cast<VspKdLeaf *>(node); 
    16341686 
    16351687                        float c = leaf->GetAvgRayContribution(); 
     
    16491701                else  
    16501702                { 
    1651                         VspKdTreeInterior *in =  
    1652                                 dynamic_cast<VspKdTreeInterior *>(node); 
     1703                        VspKdInterior *in =  
     1704                                dynamic_cast<VspKdInterior *>(node); 
    16531705                        // both nodes for directional splits 
    16541706                        tstack.push(in->GetFront()); 
     
    16631715float VspKdTree::GetAvgPvsSize() 
    16641716{ 
    1665         stack<VspKdTreeNode *> tstack; 
     1717        stack<VspKdNode *> tstack; 
    16661718        tstack.push(mRoot); 
    16671719 
     
    16711723        while (!tstack.empty())  
    16721724        { 
    1673                 VspKdTreeNode *node = tstack.top(); 
     1725                VspKdNode *node = tstack.top(); 
    16741726                tstack.pop(); 
    16751727                 
    16761728                if (node->IsLeaf())  
    16771729                { 
    1678                         VspKdTreeLeaf *leaf = (VspKdTreeLeaf *)node; 
     1730                        VspKdLeaf *leaf = (VspKdLeaf *)node; 
    16791731                        // update pvs size 
    16801732                        leaf->UpdatePvsSize(); 
     
    16841736                else  
    16851737                { 
    1686                         VspKdTreeInterior *in = (VspKdTreeInterior *)node; 
     1738                        VspKdInterior *in = (VspKdInterior *)node; 
    16871739                        // both nodes for directional splits 
    16881740                        tstack.push(in->GetFront()); 
     
    16941746} 
    16951747 
    1696 VspKdTreeNode *VspKdTree::GetRoot() const 
     1748VspKdNode *VspKdTree::GetRoot() const 
    16971749{ 
    16981750        return mRoot; 
    16991751} 
    17001752 
    1701 AxisAlignedBox3 VspKdTree::GetBBox(VspKdTreeNode *node) const 
     1753AxisAlignedBox3 VspKdTree::GetBBox(VspKdNode *node) const 
    17021754{ 
    17031755        if (node->mParent == NULL) 
     
    17051757 
    17061758        if (!node->IsLeaf()) 
    1707                 return (dynamic_cast<VspKdTreeInterior *>(node))->mBox; 
     1759                return (dynamic_cast<VspKdInterior *>(node))->mBox; 
    17081760 
    17091761        if (node->mParent->mAxis >= 3) 
     
    17401792        return  
    17411793                (sizeof(VspKdTree) +  
    1742                  mStat.Leaves() * sizeof(VspKdTreeLeaf) + 
    1743                  mStat.Interior() * sizeof(VspKdTreeInterior) + 
     1794                 mStat.Leaves() * sizeof(VspKdLeaf) + 
     1795                 mStat.Interior() * sizeof(VspKdInterior) + 
    17441796                 mStat.rayRefs * sizeof(RayInfo)) / (1024.0f * 1024.0f); 
    17451797} 
     
    17501802} 
    17511803 
    1752 int VspKdTree::ComputePvsSize(VspKdTreeNode *node,  
     1804int VspKdTree::ComputePvsSize(VspKdNode *node,  
    17531805                                                          const RayInfoContainer &globalRays) const 
    17541806{ 
     
    17661818} 
    17671819 
    1768 void VspKdTree::CollectLeaves(vector<VspKdTreeLeaf *> &leaves) const 
    1769 { 
    1770         stack<VspKdTreeNode *> nodeStack; 
     1820void VspKdTree::CollectLeaves(vector<VspKdLeaf *> &leaves) const 
     1821{ 
     1822        stack<VspKdNode *> nodeStack; 
    17711823        nodeStack.push(mRoot); 
    17721824 
    17731825        while (!nodeStack.empty())  
    17741826        { 
    1775                 VspKdTreeNode *node = nodeStack.top(); 
     1827                VspKdNode *node = nodeStack.top(); 
    17761828                 
    17771829                nodeStack.pop(); 
     
    17791831                if (node->IsLeaf())  
    17801832                { 
    1781                         VspKdTreeLeaf *leaf = dynamic_cast<VspKdTreeLeaf *>(node); 
     1833                        VspKdLeaf *leaf = dynamic_cast<VspKdLeaf *>(node); 
    17821834                        leaves.push_back(leaf); 
    17831835                }  
    17841836                else  
    17851837                { 
    1786                         VspKdTreeInterior *interior = dynamic_cast<VspKdTreeInterior *>(node); 
     1838                        VspKdInterior *interior = dynamic_cast<VspKdInterior *>(node); 
    17871839 
    17881840                        nodeStack.push(interior->GetBack()); 
     
    17921844} 
    17931845 
    1794 int VspKdTree::FindNeighbors(VspKdTreeLeaf *n, 
    1795                                                          vector<VspKdTreeLeaf *> &neighbors, 
     1846int VspKdTree::FindNeighbors(VspKdLeaf *n, 
     1847                                                         vector<VspKdLeaf *> &neighbors, 
    17961848                                                         bool onlyUnmailed) 
    17971849{ 
    1798         stack<VspKdTreeNode *> nodeStack; 
     1850        stack<VspKdNode *> nodeStack; 
    17991851   
    18001852        nodeStack.push(mRoot); 
     
    18041856        while (!nodeStack.empty())  
    18051857        { 
    1806                 VspKdTreeNode *node = nodeStack.top(); 
     1858                VspKdNode *node = nodeStack.top(); 
    18071859                nodeStack.pop(); 
    18081860 
    18091861                if (node->IsLeaf())  
    18101862                { 
    1811                         VspKdTreeLeaf *leaf = dynamic_cast<VspKdTreeLeaf *>(node); 
     1863                        VspKdLeaf *leaf = dynamic_cast<VspKdLeaf *>(node); 
    18121864 
    18131865                        if (leaf != n && (!onlyUnmailed || !leaf->Mailed()))  
     
    18161868                else  
    18171869                { 
    1818                         VspKdTreeInterior *interior = dynamic_cast<VspKdTreeInterior *>(node); 
     1870                        VspKdInterior *interior = dynamic_cast<VspKdInterior *>(node); 
    18191871 
    18201872                        if (interior->mPosition > box.Max(interior->mAxis)) 
     
    18371889} 
    18381890 
    1839 void VspKdTree::MergeLeaves() 
    1840 { 
    1841         vector<VspKdTreeLeaf *> leaves; 
    1842         priority_queue<LeafPair> mergeCandidates; 
    1843         vector<VspKdTreeLeaf *> neighbors; 
    1844  
     1891 
     1892 
     1893void VspKdTree::SetViewCellsManager(ViewCellsManager *vcm) 
     1894{ 
     1895        mViewCellsManager = vcm; 
     1896} 
     1897 
     1898 
     1899int VspKdTree::CastRay(Ray &ray) 
     1900{ 
     1901        int hits = 0; 
     1902   
     1903        stack<RayTraversalData> tStack; 
     1904   
     1905        float maxt = 1e6; 
     1906        float mint = 0; 
     1907 
     1908        Intersectable::NewMail(); 
     1909 
     1910        if (!mBox.GetMinMaxT(ray, &mint, &maxt)) 
     1911                return 0; 
     1912 
     1913        if (mint < 0) 
     1914                mint = 0; 
     1915   
     1916        maxt += Limits::Threshold; 
     1917   
     1918        Vector3 entp = ray.Extrap(mint); 
     1919        Vector3 extp = ray.Extrap(maxt); 
     1920   
     1921        VspKdNode *node = mRoot; 
     1922        VspKdNode *farChild; 
     1923 
     1924        float position; 
     1925        int axis; 
     1926   
     1927        while (1)  
     1928        { 
     1929                if (!node->IsLeaf())  
     1930                { 
     1931                        VspKdInterior *in = dynamic_cast<VspKdInterior *>(node); 
     1932                        position = in->mPosition; 
     1933                        axis = in->mAxis; 
     1934 
     1935                        if (entp[axis] <= position)  
     1936                        { 
     1937                                if (extp[axis] <= position)  
     1938                                { 
     1939                                        node = in->mBack; 
     1940                                        // cases N1,N2,N3,P5,Z2,Z3 
     1941                                        continue; 
     1942                                } else  
     1943                                { 
     1944                                        // case N4 
     1945                                        node = in->mBack; 
     1946                                        farChild = in->mFront; 
     1947                                } 
     1948                        } 
     1949                        else  
     1950                        { 
     1951                                if (position <= extp[axis])  
     1952                                { 
     1953                                        node = in->mFront; 
     1954                                        // cases P1,P2,P3,N5,Z1 
     1955                                        continue; 
     1956                                }  
     1957                                else  
     1958                                { 
     1959                                        node = in->mFront; 
     1960                                        farChild = in->mBack; 
     1961                                        // case P4 
     1962                                } 
     1963                        } 
     1964 
     1965                        // $$ modification 3.5.2004 - hints from Kamil Ghais 
     1966                        // case N4 or P4 
     1967                        float tdist = (position - ray.GetLoc(axis)) / ray.GetDir(axis); 
     1968                        //tStack.push(RayTraversalData(farChild, extp, maxt)); //TODO 
     1969                        extp = ray.GetLoc() + ray.GetDir()*tdist; 
     1970                        maxt = tdist; 
     1971                }  
     1972                else  
     1973                { 
     1974                        // compute intersection with all objects in this leaf 
     1975                        /*KdLeaf *leaf = (KdLeaf *) node; 
     1976                        if (ray.mFlags & Ray::STORE_KDLEAVES) 
     1977                                ray.kdLeaves.push_back(leaf); 
     1978                         
     1979                        ObjectContainer::const_iterator mi; 
     1980                        for ( mi = leaf->mObjects.begin(); mi != leaf->mObjects.end(); mi++)  
     1981                        { 
     1982                                Intersectable *object = *mi; 
     1983                                if (!object->Mailed() )  
     1984                                { 
     1985                                        object->Mail(); 
     1986                                        if (ray.mFlags & Ray::STORE_TESTED_OBJECTS) 
     1987                                                ray.testedObjects.push_back(object); 
     1988                                        hits += object->CastRay(ray); 
     1989                                } 
     1990                        } 
     1991                         
     1992                        if (hits && ray.GetType() == Ray::LOCAL_RAY) 
     1993                                if (ray.intersections[0].mT <= maxt) 
     1994                                        break; 
     1995                         
     1996                        // get the next node from the stack 
     1997                        if (tStack.empty()) 
     1998                                break; 
     1999                         
     2000                        entp = extp; 
     2001                        mint = maxt; 
     2002                        if (ray.GetType() == Ray::LINE_SEGMENT && mint > 1.0f) 
     2003                                break; 
     2004                         
     2005                        RayTraversalData &s  = tStack.top(); 
     2006                        node = s.mNode; 
     2007                        extp = s.mExitPoint; 
     2008                        maxt = s.mMaxT; 
     2009                        tStack.pop(); 
     2010                        */ 
     2011                } 
     2012        } 
     2013         
     2014        return hits; 
     2015} 
     2016 
     2017 
     2018void VspKdTree::GenerateViewCell(VspKdLeaf *leaf) 
     2019{ 
     2020        RayInfoContainer::const_iterator it, it_end = leaf->GetRays().end(); 
     2021 
     2022        VspKdViewCell *vc = dynamic_cast<VspKdViewCell *>(mViewCellsManager->GenerateViewCell()); 
     2023        leaf->SetViewCell(vc); 
     2024 
     2025        vc->SetSize(GetBBox(leaf).GetVolume()); 
     2026        vc->mVspKdLeaves.push_back(leaf); 
     2027 
     2028        for (it = leaf->GetRays().begin(); it != it_end; ++ it) 
     2029        { 
     2030                VssRay *ray = (*it).mRay; 
     2031 
     2032                if (ray->mTerminationObject) 
     2033                        vc->GetPvs().AddSample(ray->mTerminationObject); 
     2034                 
     2035                if (ray->mOriginObject) 
     2036                        vc->GetPvs().AddSample(ray->mOriginObject); 
     2037        } 
     2038} 
     2039 
     2040 
     2041void VspKdTree::EvaluateViewCellsStats(ViewCellsStatistics &vcStat) const 
     2042{ 
     2043        vcStat.Reset(); 
     2044 
     2045        stack<VspKdNode *> nodeStack; 
     2046        nodeStack.push(mRoot); 
     2047 
     2048        ViewCell::NewMail(); 
     2049 
     2050        // exclude root cell 
     2051        //mRootCell->Mail(); 
     2052 
     2053        while (!nodeStack.empty())  
     2054        { 
     2055                VspKdNode *node = nodeStack.top(); 
     2056                nodeStack.pop(); 
     2057 
     2058                if (node->IsLeaf())  
     2059                { 
     2060                        ++ vcStat.leaves; 
     2061 
     2062                        VspKdViewCell *viewCell = dynamic_cast<VspKdLeaf *>(node)->mViewCell; 
     2063 
     2064                        if (!viewCell->Mailed())  
     2065                        { 
     2066                                viewCell->Mail(); 
     2067                                 
     2068                                ++ vcStat.viewCells; 
     2069                                const int pvsSize = viewCell->GetPvs().GetSize(); 
     2070 
     2071                vcStat.pvs += pvsSize; 
     2072 
     2073                                if (pvsSize < 1) 
     2074                                        ++ vcStat.emptyPvs; 
     2075 
     2076                                if (pvsSize > vcStat.maxPvs) 
     2077                                        vcStat.maxPvs = pvsSize; 
     2078 
     2079                                if (pvsSize < vcStat.minPvs) 
     2080                                        vcStat.minPvs = pvsSize; 
     2081 
     2082                                if ((int)viewCell->mVspKdLeaves.size() > vcStat.maxLeaves) 
     2083                                        vcStat.maxLeaves = (int)viewCell->mVspKdLeaves.size();           
     2084                        } 
     2085                } 
     2086                else  
     2087                { 
     2088                        VspKdInterior *interior = dynamic_cast<VspKdInterior *>(node); 
     2089 
     2090                        nodeStack.push(interior->GetFront()); 
     2091                        nodeStack.push(interior->GetBack()); 
     2092                } 
     2093        } 
     2094} 
     2095 
     2096 
     2097bool VspKdTree::MergeViewCells(VspKdLeaf *l1, VspKdLeaf *l2) 
     2098{ 
     2099        //-- change pointer to view cells of all leaves associated with the previous view cells 
     2100        VspKdViewCell *fVc = l1->GetViewCell(); 
     2101        VspKdViewCell *bVc = l2->GetViewCell(); 
     2102 
     2103        VspKdViewCell *vc = dynamic_cast<VspKdViewCell *>( 
     2104                mViewCellsManager->MergeViewCells(*fVc, *bVc)); 
     2105 
     2106        // if merge was unsuccessful 
     2107        if (!vc) return false; 
     2108 
     2109        // set new size of view cell 
     2110        vc->SetSize(fVc->GetSize() + bVc->GetSize()); 
     2111 
     2112        vector<VspKdLeaf *> fLeaves = fVc->mVspKdLeaves; 
     2113        vector<VspKdLeaf *> bLeaves = bVc->mVspKdLeaves; 
     2114 
     2115        vector<VspKdLeaf *>::const_iterator it; 
     2116 
     2117        //-- change view cells of all the other leaves the view cell belongs to 
     2118        for (it = fLeaves.begin(); it != fLeaves.end(); ++ it) 
     2119        { 
     2120                (*it)->SetViewCell(vc); 
     2121                vc->mVspKdLeaves.push_back(*it); 
     2122        } 
     2123 
     2124        for (it = bLeaves.begin(); it != bLeaves.end(); ++ it) 
     2125        { 
     2126                (*it)->SetViewCell(vc); 
     2127                vc->mVspKdLeaves.push_back(*it); 
     2128        } 
     2129 
     2130        // clean up old view cells 
     2131        DEL_PTR(fVc); 
     2132        DEL_PTR(bVc); 
     2133 
     2134        return true; 
     2135} 
     2136 
     2137 
     2138 
     2139int VspKdTree::MergeLeaves() 
     2140{ 
     2141        vector<VspKdLeaf *> leaves; 
     2142        priority_queue<MergeCandidate> mergeQueue; 
     2143         
     2144        // collect the leaves, e.g., the "voxels" that will build the view cells 
    18452145        CollectLeaves(leaves); 
    18462146 
    1847         VspKdTreeLeaf::NewMail(); 
    1848  
    1849         vector<VspKdTreeLeaf *>::const_iterator it, it_end = leaves.end(); 
    1850  
    1851  
     2147        int vcSize = (int)leaves.size(); 
     2148 
     2149        VspKdLeaf::NewMail(); 
     2150 
     2151        vector<VspKdLeaf *>::const_iterator it, it_end = leaves.end(); 
     2152 
     2153        // generate view cells 
    18522154        for (it = leaves.begin(); it != it_end; ++ it) 
    1853         { 
    1854                 VspKdTreeLeaf *leaf = *it; 
    1855  
     2155                GenerateViewCell(*it); 
     2156 
     2157        // find merge candidates and push them into queue 
     2158        for (it = leaves.begin(); it != it_end; ++ it) 
     2159        { 
     2160                VspKdLeaf *leaf = *it; 
     2161                 
     2162                // no leaf is part of two merge candidates 
    18562163                if (!leaf->Mailed()) 
    18572164                { 
     2165                        leaf->Mail(); 
     2166 
     2167                        vector<VspKdLeaf *> neighbors; 
    18582168                        FindNeighbors(leaf, neighbors, true); 
    18592169 
    1860                         vector<VspKdTreeLeaf *>::const_iterator nit, nit_end = neighbors.end(); 
    1861  
    1862                         for (nit = neighbors.begin(); nit != neighbors.end(); ++ nit) 
    1863                                 mergeCandidates.push(LeafPair(leaf, *nit)); 
     2170                        vector<VspKdLeaf *>::const_iterator nit,  
     2171                                nit_end = neighbors.end(); 
     2172 
     2173                        for (nit = neighbors.begin(); nit != nit_end; ++ nit) 
     2174                                mergeQueue.push(MergeCandidate(leaf, *nit)); 
     2175                } 
     2176        } 
     2177 
     2178        int merged = 0; 
     2179 
     2180        float savedMaxCostRatio = 0; 
     2181 
     2182        Debug << "merge cost " << mergeQueue.top().GetMergeCost() << " of " << mMaxCostRatio << endl; 
     2183 
     2184        // use priority queue to merge leaves 
     2185        while (!mergeQueue.empty() &&  
     2186                   ((mMaxViewCells < vcSize) || 
     2187                    (mergeQueue.top().GetMergeCost() < mMaxCostRatio))) 
     2188        { 
     2189                ViewCell::NewMail(); 
     2190 
     2191                Debug << "=====================" << endl; 
     2192                MergeCandidate mc = mergeQueue.top(); 
     2193                mergeQueue.pop(); 
     2194                 
     2195                if (mc.GetLeaf1()->GetViewCell() == mc.GetLeaf2()->GetViewCell()) 
     2196                // both view cells the same! 
     2197                { 
     2198                        Debug << "same view cell!!" << endl; 
     2199                        -- vcSize; 
     2200 
     2201                        continue; 
     2202                } 
     2203 
     2204                if (mc.Valid()) 
     2205                { 
     2206                        mc.GetLeaf1()->GetViewCell()->Mail(); 
     2207                        mc.GetLeaf2()->GetViewCell()->Mail(); 
    18642208                         
    1865                         leaf->Mail(); 
    1866                 } 
    1867         } 
    1868 } 
    1869  
     2209                        Debug << "merging cells with cost " << mc.GetMergeCost() << " of " << mMaxCostRatio << endl; 
     2210                        savedMaxCostRatio = mc.GetMergeCost(); 
     2211 
     2212                        MergeViewCells(mc.GetLeaf1(), mc.GetLeaf2()); 
     2213                         
     2214                        ++ merged; 
     2215                        -- vcSize; 
     2216                } 
     2217                // merge candidate not valid, because one of the leaves was already 
     2218                // merged with another one 
     2219                else  
     2220                { 
     2221                        Debug << "not valid " << mc.GetMergeCost() << endl; 
     2222                         
     2223                        // validate and reinsert into queue 
     2224                        mc.SetValid(); 
     2225                        Debug << "ag. valid " << mc.GetMergeCost() << endl; 
     2226 
     2227                        mergeQueue.push(mc); 
     2228                } 
     2229        } 
     2230 
     2231        if (mergeQueue.empty()) 
     2232                Debug << "empty queue" << endl; 
     2233         
     2234        Debug << "max cost ratio: " << savedMaxCostRatio << endl; 
     2235 
     2236        return merged; 
     2237} 
    18702238 
    18712239 
     
    18752243 
    18762244 
    1877 MergeCandidate::MergeCandidate(VspKdTreeLeaf *l1, VspKdTreeLeaf *l2): 
    1878 mLeaf1(l1), mLeaf2(l2) 
    1879 {} 
    1880  
    1881 float MergeCandidate::EvaluateMergeCost() const 
    1882 { 
    1883         // pvs difference 
    1884         VspKdViewCell *vc1 = dynamic_cast<VspKdViewCell *>(mLeaf1->GetViewCell()); 
    1885         VspKdViewCell *vc2 = dynamic_cast<VspKdViewCell *>(mLeaf2->GetViewCell()); 
     2245MergeCandidate::MergeCandidate(VspKdLeaf *l1, VspKdLeaf *l2): 
     2246mMergeCost(0), 
     2247mLeaf1(l1), 
     2248mLeaf2(l2), 
     2249mLeaf1Id(l1->GetViewCell()->mMailbox), 
     2250mLeaf2Id(l2->GetViewCell()->mMailbox) 
     2251{ 
     2252        EvalMergeCost(); 
     2253} 
     2254 
     2255 
     2256void MergeCandidate::EvalMergeCost() 
     2257{ 
     2258        // compute pvs differences 
     2259        VspKdViewCell *vc1 = mLeaf1->GetViewCell(); 
     2260        VspKdViewCell *vc2 = mLeaf2->GetViewCell(); 
     2261         
     2262        //VspKdViewCell *vc1 = dynamic_cast<VspKdViewCell *>(mLeaf1->GetViewCell()); 
     2263        //VspKdViewCell *vc2 = dynamic_cast<VspKdViewCell *>(mLeaf2->GetViewCell()); 
    18862264 
    18872265        const int diff1 = vc1->GetPvs().Diff(vc2->GetPvs()); 
    1888          
    1889         //const int diff2 = vc2->GetPvs().Diff(vc1->GetPvs()); 
    1890         //const float simDiff = (float)max(diff1, diff2); 
    1891  
     2266        const int vcPvs = diff1 + vc1->GetPvs().GetSize(); 
     2267 
     2268        //-- compute ratio of old cost  
     2269        //-- (added size of left and right view cell times pvs size) 
     2270        //-- to new rendering cost (size of merged view cell times pvs size) 
    18922271        const float oldCost =  
    1893                 vc1->GetSize() * vc1->GetPvs().GetSize() +  
    1894                 vc2->GetSize() * vc2->GetPvs().GetSize(); 
    1895  
    1896         const float mergedPvsCost =  
    1897                 (diff1 + vc1->GetPvs().GetSize()) * 
    1898                 (vc1->GetSize() + vc2->GetSize()); 
    1899  
    1900         return mergedPvsCost / oldCost; 
    1901 } 
     2272                (float)vc1->GetPvs().GetSize() * vc1->GetSize() +  
     2273                (float)vc2->GetPvs().GetSize() * vc2->GetSize(); 
     2274 
     2275        const float newCost =  
     2276                (float)vcPvs * (vc1->GetSize() + vc2->GetSize()); 
     2277 
     2278        mMergeCost = newCost / oldCost; 
     2279 
     2280        Debug << "******************************" << endl; 
     2281        Debug << "pvs size: " << vcPvs << " max: " << sMaxPvsSize << endl; 
     2282        if (vcPvs > sMaxPvsSize) // strong penalty if pvs size too large 
     2283                mMergeCost += 1.0; 
     2284 
     2285        Debug << "old cost: " << oldCost << endl; 
     2286        Debug << "new cost: " << newCost << endl; 
     2287} 
     2288 
     2289void MergeCandidate::SetLeaf1(VspKdLeaf *l) 
     2290{ 
     2291        mLeaf1 = l; 
     2292} 
     2293 
     2294void MergeCandidate::SetLeaf2(VspKdLeaf *l) 
     2295{ 
     2296        mLeaf2 = l; 
     2297} 
     2298 
     2299VspKdLeaf *MergeCandidate::GetLeaf1() 
     2300{ 
     2301        return mLeaf1; 
     2302} 
     2303 
     2304VspKdLeaf *MergeCandidate::GetLeaf2() 
     2305{ 
     2306        return mLeaf2; 
     2307} 
     2308 
     2309bool MergeCandidate::Valid() const 
     2310{        
     2311        //Debug << mLeaf1->GetViewCell()->mMailbox << " " << mLeaf1Id << endl; 
     2312        //Debug << mLeaf2->GetViewCell()->mMailbox << " " << mLeaf2Id << endl; 
     2313 
     2314        return  
     2315                (mLeaf1->GetViewCell()->mMailbox == mLeaf1Id) &&  
     2316                (mLeaf2->GetViewCell()->mMailbox == mLeaf2Id); 
     2317} 
     2318 
     2319float MergeCandidate::GetMergeCost() const 
     2320{ 
     2321        return mMergeCost; 
     2322} 
     2323 
     2324void MergeCandidate::SetValid() 
     2325{ 
     2326        mLeaf1Id = mLeaf1->GetViewCell()->mMailbox; 
     2327        mLeaf2Id = mLeaf2->GetViewCell()->mMailbox; 
     2328         
     2329        EvalMergeCost(); 
     2330} 
Note: See TracChangeset for help on using the changeset viewer.