[2123] | 1 | #include <iostream>
|
---|
| 2 | #include <stack>
|
---|
[2575] | 3 |
|
---|
| 4 | #include "Mesh.h"
|
---|
[2123] | 5 | #include "ObjectPvs.h"
|
---|
| 6 | #include "Intersectable.h"
|
---|
| 7 | #include "IntersectableWrapper.h"
|
---|
| 8 | #include "KdTree.h"
|
---|
| 9 | #include "common.h"
|
---|
| 10 | #include "BvHierarchy.h"
|
---|
| 11 |
|
---|
[2635] | 12 | // $$ switched off by JB for testing
|
---|
| 13 | #define USE_MULTIPLE_OBJECTS 0
|
---|
[2176] | 14 | using namespace std;
|
---|
[2123] | 15 |
|
---|
[2670] | 16 |
|
---|
[2123] | 17 | namespace GtpVisibilityPreprocessor {
|
---|
| 18 |
|
---|
[2643] | 19 | static ObjectPvs dummyPvs;
|
---|
[2123] | 20 |
|
---|
| 21 | /** the pvs is the number of different objects in the node leaves
|
---|
| 22 | We eliminate already accounted kd nodes and objects using mailboxing.
|
---|
| 23 | */
|
---|
| 24 | static int EvalKdNodeContribution(KdIntersectable *kdobj)
|
---|
| 25 | {
|
---|
| 26 | int pvs = 0;
|
---|
| 27 | stack<KdNode *> tStack;
|
---|
| 28 |
|
---|
| 29 | tStack.push(kdobj->GetItem());
|
---|
| 30 |
|
---|
| 31 | while (!tStack.empty())
|
---|
| 32 | {
|
---|
| 33 | KdNode *node = tStack.top();
|
---|
| 34 | tStack.pop();
|
---|
| 35 |
|
---|
| 36 | // already processed node (=> objects already in pvs)?
|
---|
[2727] | 37 | //node->Mail();
|
---|
| 38 | if (node->IsLeaf())
|
---|
[2123] | 39 | {
|
---|
[2727] | 40 | KdLeaf *leaf = static_cast<KdLeaf *>(node);
|
---|
| 41 |
|
---|
| 42 | // Objects already accounted for can only be found among those
|
---|
| 43 | // which are referenced in more than one leaf
|
---|
| 44 | ObjectContainer::const_iterator oit, oit_end = leaf->mObjects.end();
|
---|
| 45 |
|
---|
| 46 | for (oit = leaf->mObjects.begin(); oit != oit_end; ++ oit)
|
---|
[2123] | 47 | {
|
---|
[2727] | 48 | Intersectable *obj = *oit;
|
---|
[2123] | 49 |
|
---|
[2727] | 50 | if (!obj->Mailed2())
|
---|
[2123] | 51 | {
|
---|
[2727] | 52 | //if (dummyPvs.AddSample(obj, 1.0f))++ pvs;
|
---|
| 53 | obj->Mail2();
|
---|
| 54 | ++ pvs;
|
---|
[2123] | 55 | }
|
---|
| 56 | }
|
---|
[2727] | 57 | }
|
---|
| 58 | else // traverse tree
|
---|
| 59 | {
|
---|
| 60 | KdInterior *interior = static_cast<KdInterior *>(node);
|
---|
[2123] | 61 |
|
---|
[2727] | 62 | tStack.push(interior->mFront);
|
---|
| 63 | tStack.push(interior->mBack);
|
---|
[2123] | 64 | }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
[2643] | 67 | ///return dummyPvs.Size();
|
---|
[2123] | 68 | return pvs;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 |
|
---|
| 72 | /** Returns the the number of new (unmailed) objects in the leaves of the node.
|
---|
| 73 | We eliminate already accounted bvh nodes and objects using mailboxing.
|
---|
| 74 | */
|
---|
| 75 | static float EvalBvhNodeContribution(BvhNode *bvhObj)
|
---|
| 76 | {
|
---|
| 77 | BvhNode *node;
|
---|
| 78 |
|
---|
| 79 | // hack for choosing which node to account for
|
---|
| 80 | if (bvhObj->IsLeaf())
|
---|
| 81 | node = static_cast<BvhLeaf *>(bvhObj)->GetActiveNode();
|
---|
| 82 | else
|
---|
| 83 | node = bvhObj;
|
---|
| 84 |
|
---|
| 85 | // early exit
|
---|
| 86 | if (node->IsLeaf())
|
---|
| 87 | {
|
---|
| 88 | BvhLeaf *leaf = static_cast<BvhLeaf *>(node);
|
---|
| 89 | // objects already accounted for
|
---|
| 90 | if (leaf->Mailed())
|
---|
| 91 | return 0;
|
---|
| 92 |
|
---|
| 93 | leaf->Mail();
|
---|
[2332] | 94 | return (float)leaf->mObjects.size();
|
---|
[2123] | 95 | }
|
---|
| 96 |
|
---|
| 97 | // compute leaf pvs
|
---|
| 98 | float pvs = 0;
|
---|
| 99 | stack<BvhNode *> tStack;
|
---|
| 100 | tStack.push(node);
|
---|
| 101 |
|
---|
| 102 | while (!tStack.empty())
|
---|
| 103 | {
|
---|
| 104 | node = tStack.top();
|
---|
| 105 | tStack.pop();
|
---|
| 106 |
|
---|
| 107 | // already processed node (=> objects already in pvs)?
|
---|
| 108 | if (!node->Mailed())
|
---|
| 109 | {
|
---|
| 110 | node->Mail();
|
---|
| 111 |
|
---|
| 112 | if (node->IsLeaf())
|
---|
| 113 | {
|
---|
| 114 | BvhLeaf *leaf = static_cast<BvhLeaf *>(node);
|
---|
| 115 |
|
---|
| 116 | // add #objects exclusivly in this node
|
---|
[2332] | 117 | pvs += (float)leaf->mObjects.size();
|
---|
[2123] | 118 | }
|
---|
| 119 | else // traverse tree
|
---|
| 120 | {
|
---|
| 121 | BvhInterior *interior = static_cast<BvhInterior *>(node);
|
---|
| 122 |
|
---|
| 123 | tStack.push(interior->GetFront());
|
---|
| 124 | tStack.push(interior->GetBack());
|
---|
| 125 | }
|
---|
| 126 | }
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | return pvs;
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 |
|
---|
| 133 | float ObjectPvs::EvalPvsCost() const
|
---|
| 134 | {
|
---|
| 135 | float pvs = 0;
|
---|
| 136 |
|
---|
[2643] | 137 | Intersectable::NewMail2();
|
---|
[2723] | 138 |
|
---|
[2123] | 139 | ObjectPvsIterator pit = GetIterator();
|
---|
| 140 |
|
---|
| 141 | while (pit.HasMoreEntries())
|
---|
| 142 | {
|
---|
| 143 | Intersectable *obj = pit.Next();
|
---|
| 144 |
|
---|
| 145 | switch (obj->Type())
|
---|
| 146 | {
|
---|
| 147 | case Intersectable::KD_INTERSECTABLE:
|
---|
| 148 | {
|
---|
| 149 | // found kd node
|
---|
| 150 | KdIntersectable *kdObj = static_cast<KdIntersectable *>(obj);
|
---|
| 151 | pvs += EvalKdNodeContribution(kdObj);
|
---|
| 152 | break;
|
---|
| 153 | }
|
---|
| 154 | case Intersectable::BVH_INTERSECTABLE:
|
---|
| 155 | {
|
---|
| 156 | BvhNode *bvhObj = static_cast<BvhNode *>(obj);
|
---|
| 157 | pvs += EvalBvhNodeContribution(bvhObj);
|
---|
| 158 | break;
|
---|
| 159 | }
|
---|
| 160 | default:
|
---|
| 161 | // hack: should use assigned cost here
|
---|
| 162 | ++ pvs;
|
---|
| 163 |
|
---|
| 164 | break;
|
---|
| 165 | }
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | return pvs;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
[2723] | 171 |
|
---|
[2123] | 172 | }
|
---|