[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] | 11 | namespace GtpVisibilityPreprocessor {
|
---|
[860] | 12 |
|
---|
[1189] | 13 | int MailablePvsData::sMailId = 1;
|
---|
| 14 | int MailablePvsData::sReservedMailboxes = 1;
|
---|
[1141] | 15 |
|
---|
[1189] | 16 |
|
---|
[1141] | 17 | int 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 | */
|
---|
| 25 | static int CountNewObjectsInKdNode(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 | {
|
---|
[1233] | 44 | KdLeaf *leaf = dynamic_cast<KdLeaf *>(node);
|
---|
| 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 | {
|
---|
| 66 | KdInterior *interior = dynamic_cast<KdInterior *>(node);
|
---|
| 67 |
|
---|
| 68 | tStack.push(interior->mFront);
|
---|
| 69 | tStack.push(interior->mBack);
|
---|
| 70 | }
|
---|
[1141] | 71 | }
|
---|
[1233] | 72 | }
|
---|
| 73 |
|
---|
| 74 | return pvs;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 |
|
---|
| 78 | /** the pvs is the number of different objects in the node leaves
|
---|
| 79 | We eliminate already accounted kd nodes and objects using mailboxing.
|
---|
| 80 | */
|
---|
| 81 | static int CountNewObjectsInBvhNode(BvhIntersectable *bvhobj)
|
---|
| 82 | {
|
---|
| 83 | BvhNode *node= bvhobj->GetItem();
|
---|
| 84 |
|
---|
| 85 | // early exit
|
---|
| 86 | if (node->IsLeaf())
|
---|
| 87 | {
|
---|
[1291] | 88 | // objects already accounted for
|
---|
| 89 | if (node->Mailed())
|
---|
[1233] | 90 | return 0;
|
---|
[1291] | 91 |
|
---|
| 92 | node->Mail();
|
---|
| 93 |
|
---|
| 94 | return (int)(((BvhLeaf *)node)->mObjects.size());
|
---|
[1284] | 95 | }
|
---|
[1141] | 96 |
|
---|
[1233] | 97 | // compute leaf pvs
|
---|
| 98 | int pvs = 0;
|
---|
| 99 |
|
---|
| 100 | stack<BvhNode *> tStack;
|
---|
| 101 |
|
---|
| 102 | tStack.push(bvhobj->GetItem());
|
---|
| 103 |
|
---|
| 104 | while (!tStack.empty())
|
---|
| 105 | {
|
---|
| 106 | BvhNode *node = tStack.top();
|
---|
| 107 | tStack.pop();
|
---|
| 108 |
|
---|
| 109 | // already processed node (=> objects already in pvs)?
|
---|
| 110 | if (!node->Mailed())
|
---|
| 111 | {
|
---|
| 112 | node->Mail();
|
---|
| 113 |
|
---|
| 114 | if (node->IsLeaf())
|
---|
| 115 | {
|
---|
| 116 | BvhLeaf *leaf = dynamic_cast<BvhLeaf *>(node);
|
---|
| 117 |
|
---|
| 118 | // add #objects exclusivly in this node
|
---|
[1284] | 119 | pvs += (int)leaf->mObjects.size();
|
---|
[1233] | 120 | }
|
---|
| 121 | else // traverse tree
|
---|
| 122 | {
|
---|
| 123 | BvhInterior *interior = dynamic_cast<BvhInterior *>(node);
|
---|
| 124 |
|
---|
| 125 | tStack.push(interior->GetFront());
|
---|
| 126 | tStack.push(interior->GetBack());
|
---|
| 127 | }
|
---|
| 128 | }
|
---|
| 129 | }
|
---|
[1141] | 130 | return pvs;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[1233] | 133 |
|
---|
| 134 |
|
---|
| 135 | int ObjectPvs::CountObjectsInPvs() const
|
---|
| 136 | {
|
---|
| 137 | int pvs = 0;
|
---|
| 138 |
|
---|
| 139 | Intersectable::NewMail();
|
---|
| 140 | KdLeaf::NewMail();
|
---|
| 141 | BvhLeaf::NewMail();
|
---|
| 142 |
|
---|
| 143 | ObjectPvsMap::const_iterator it, it_end = mEntries.end();
|
---|
| 144 |
|
---|
| 145 | for (it = mEntries.begin(); it != it_end; ++ it)
|
---|
| 146 | {
|
---|
| 147 | Intersectable *obj = (*it).first;
|
---|
| 148 |
|
---|
| 149 | switch (obj->Type())
|
---|
| 150 | {
|
---|
| 151 | case Intersectable::KD_INTERSECTABLE:
|
---|
| 152 | {
|
---|
| 153 | // found kd node
|
---|
| 154 | KdIntersectable *kdObj = dynamic_cast<KdIntersectable *>(obj);
|
---|
| 155 | pvs += CountNewObjectsInKdNode(kdObj);
|
---|
| 156 | break;
|
---|
| 157 | }
|
---|
| 158 | case Intersectable::BVH_INTERSECTABLE:
|
---|
| 159 | {
|
---|
| 160 | BvhIntersectable *bvhObj = dynamic_cast<BvhIntersectable *>(obj);
|
---|
| 161 | pvs += CountNewObjectsInBvhNode(bvhObj);
|
---|
| 162 | break;
|
---|
| 163 | }
|
---|
[1293] | 164 | default:
|
---|
[1233] | 165 | ++ pvs;
|
---|
| 166 | break;
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | return pvs;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
[311] | 173 | } |
---|