source: GTP/trunk/Lib/Vis/Preprocessing/src/Pvs.cpp @ 2066

Revision 2066, 3.7 KB checked in by mattausch, 17 years ago (diff)

worked on integration manual

RevLine 
[177]1#include <iostream>
[1141]2#include <stack>
[177]3#include "Pvs.h"
[1141]4#include "Intersectable.h"
[1315]5#include "IntersectableWrapper.h"
[1141]6#include "KdTree.h"
7#include "common.h"
[1233]8#include "BvHierarchy.h"
[177]9
[1141]10
[863]11namespace GtpVisibilityPreprocessor {
[860]12
[1189]13int MailablePvsData::sMailId = 1;
14int MailablePvsData::sReservedMailboxes = 1;
[1141]15
[1189]16
[1141]17int KdPvs::Compress()
[177]18{
[311]19        return 0; // TODO
[860]20}
21
[1233]22/** the pvs is the number of different objects in the node leaves
23        We eliminate already accounted kd nodes and objects using mailboxing.
24*/
[1586]25static int EvalKdNodeContribution(KdIntersectable *kdobj)
[1141]26{
27        int pvs = 0;
[1233]28        stack<KdNode *> tStack;
[1141]29
[1233]30        tStack.push(kdobj->GetItem());
[1141]31
[1233]32        while (!tStack.empty())
[1141]33        {
[1233]34                KdNode *node = tStack.top();
35                tStack.pop();
[1141]36
[1233]37                // already processed node (=> objects already in pvs)?
38                if (!node->Mailed())
[1141]39                {
[1233]40                        node->Mail();
[1141]41
[1233]42                        if (node->IsLeaf())
[1141]43                        {
[2017]44                                KdLeaf *leaf = static_cast<KdLeaf *>(node);
[1233]45                       
46                                // add #objects exclusivly in this node
47                                pvs += (int)(leaf->mObjects.size() - leaf->mMultipleObjects.size());
[1141]48
[1233]49                                // Objects already accounted for can only be found among those
50                                // which are referenced in more than one leaf
51                                ObjectContainer::const_iterator oit, oit_end = leaf->mMultipleObjects.end();
52                                               
53                                for (oit = leaf->mMultipleObjects.begin(); oit != oit_end; ++ oit)
[1141]54                                {
[1233]55                                        Intersectable *object = *oit;                                           
[1168]56                           
[1233]57                                        if (!object->Mailed())
[1168]58                                        {
[1233]59                                                object->Mail();
60                                                ++ pvs;
[1168]61                                        }
[1141]62                                }
63                        }
[1233]64                        else // traverse tree
65                        {
[2017]66                                KdInterior *interior = static_cast<KdInterior *>(node);
[1233]67
68                                tStack.push(interior->mFront);
69                                tStack.push(interior->mBack);
70                        }
[1141]71                }
[1233]72        }
73
74        return pvs;
75}
76
77
[1703]78/** Returns the the number of new (unmailed) objects in the leaves of the node.
79        We eliminate already accounted bvh nodes and objects using mailboxing.
[1233]80*/
[1758]81static float EvalBvhNodeContribution(BvhNode *bvhObj)
[1233]82{
[1758]83        BvhNode *node;
[1233]84
[1758]85        // hack for choosing which node to account for
86        if (bvhObj->IsLeaf())
[2017]87                node = static_cast<BvhLeaf *>(bvhObj)->GetActiveNode();
[1758]88        else
89                node = bvhObj;
90
[1233]91        // early exit
[1758]92        if (node->IsLeaf())     
93        {       
[2017]94                BvhLeaf *leaf = static_cast<BvhLeaf *>(node);
[1291]95                // objects already accounted for
[1707]96                if (leaf->Mailed())
[1233]97                        return 0;
[1291]98
[1707]99                leaf->Mail();
100                return BvHierarchy::EvalAbsCost(leaf->mObjects);
[1284]101        }                       
[1141]102
[1233]103        // compute leaf pvs
[1707]104        float pvs = 0;
[1233]105        stack<BvhNode *> tStack;
[1707]106        tStack.push(node);
[1233]107
108        while (!tStack.empty())
109        {
[1707]110                node = tStack.top();
[1233]111                tStack.pop();
112
113                // already processed node (=> objects already in pvs)?
114                if (!node->Mailed())
115                {
116                        node->Mail();
117
118                        if (node->IsLeaf())
119                        {
[2017]120                                BvhLeaf *leaf = static_cast<BvhLeaf *>(node);
[1586]121
[1233]122                                // add #objects exclusivly in this node
[1703]123                                pvs += BvHierarchy::EvalAbsCost(leaf->mObjects);
[1233]124                        }
125                        else // traverse tree
126                        {
[2017]127                                BvhInterior *interior = static_cast<BvhInterior *>(node);
[1233]128
129                                tStack.push(interior->GetFront());
130                                tStack.push(interior->GetBack());
131                        }
132                }
133        }
[1586]134
[1141]135        return pvs;
136}
137
[1233]138
[1707]139float ObjectPvs::EvalPvsCost() const
[1233]140{
[1707]141        float pvs = 0;
[1233]142
143        Intersectable::NewMail();
144        KdLeaf::NewMail();
145
[1738]146        ObjectPvsEntries::const_iterator it, it_end = mEntries.end();
[1233]147
148        for (it = mEntries.begin(); it != it_end; ++ it)
149        {
[1740]150                Intersectable *obj = (*it).mObject;
[1486]151       
[1233]152                switch (obj->Type())
153                {
154                        case Intersectable::KD_INTERSECTABLE:
155                                {
156                                        // found kd node
[2017]157                                        KdIntersectable *kdObj = static_cast<KdIntersectable *>(obj);
[1586]158                                        pvs += EvalKdNodeContribution(kdObj);   
[1233]159                                        break;
160                                }
161                        case Intersectable::BVH_INTERSECTABLE:
[2066]162                                {
[2017]163                                        BvhNode *bvhObj = static_cast<BvhNode *>(obj);
[1586]164                                        pvs += EvalBvhNodeContribution(bvhObj);
[1233]165                                        break;
166                                }
[1293]167                        default:
[2066]168                                // hack: should use assigned cost here
[1233]169                                ++ pvs;
[2066]170                               
[1233]171                                break;
172                }
173        }
174
175        return pvs;
176}
177
[1785]178}
Note: See TracBrowser for help on using the repository browser.