1 | #include <iostream>
|
---|
2 | #include <stack>
|
---|
3 | #include "Pvs.h"
|
---|
4 | #include "Intersectable.h"
|
---|
5 | #include "IntersectableWrapper.h"
|
---|
6 | #include "KdTree.h"
|
---|
7 | #include "common.h"
|
---|
8 | #include "BvHierarchy.h"
|
---|
9 |
|
---|
10 |
|
---|
11 | namespace GtpVisibilityPreprocessor {
|
---|
12 |
|
---|
13 | int MailablePvsData::sMailId = 1;
|
---|
14 | int MailablePvsData::sReservedMailboxes = 1;
|
---|
15 |
|
---|
16 |
|
---|
17 | int KdPvs::Compress()
|
---|
18 | {
|
---|
19 | return 0; // TODO
|
---|
20 | }
|
---|
21 |
|
---|
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 EvalKdNodeContribution(KdIntersectable *kdobj)
|
---|
26 | {
|
---|
27 | int pvs = 0;
|
---|
28 | stack<KdNode *> tStack;
|
---|
29 |
|
---|
30 | tStack.push(kdobj->GetItem());
|
---|
31 |
|
---|
32 | while (!tStack.empty())
|
---|
33 | {
|
---|
34 | KdNode *node = tStack.top();
|
---|
35 | tStack.pop();
|
---|
36 |
|
---|
37 | // already processed node (=> objects already in pvs)?
|
---|
38 | if (!node->Mailed())
|
---|
39 | {
|
---|
40 | node->Mail();
|
---|
41 |
|
---|
42 | if (node->IsLeaf())
|
---|
43 | {
|
---|
44 | KdLeaf *leaf = static_cast<KdLeaf *>(node);
|
---|
45 |
|
---|
46 | // add #objects exclusivly in this node
|
---|
47 | pvs += (int)(leaf->mObjects.size() - leaf->mMultipleObjects.size());
|
---|
48 |
|
---|
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)
|
---|
54 | {
|
---|
55 | Intersectable *object = *oit;
|
---|
56 |
|
---|
57 | if (!object->Mailed())
|
---|
58 | {
|
---|
59 | object->Mail();
|
---|
60 | ++ pvs;
|
---|
61 | }
|
---|
62 | }
|
---|
63 | }
|
---|
64 | else // traverse tree
|
---|
65 | {
|
---|
66 | KdInterior *interior = static_cast<KdInterior *>(node);
|
---|
67 |
|
---|
68 | tStack.push(interior->mFront);
|
---|
69 | tStack.push(interior->mBack);
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | return pvs;
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
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.
|
---|
80 | */
|
---|
81 | static float EvalBvhNodeContribution(BvhNode *bvhObj)
|
---|
82 | {
|
---|
83 | BvhNode *node;
|
---|
84 |
|
---|
85 | // hack for choosing which node to account for
|
---|
86 | if (bvhObj->IsLeaf())
|
---|
87 | node = static_cast<BvhLeaf *>(bvhObj)->GetActiveNode();
|
---|
88 | else
|
---|
89 | node = bvhObj;
|
---|
90 |
|
---|
91 | // early exit
|
---|
92 | if (node->IsLeaf())
|
---|
93 | {
|
---|
94 | BvhLeaf *leaf = static_cast<BvhLeaf *>(node);
|
---|
95 | // objects already accounted for
|
---|
96 | if (leaf->Mailed())
|
---|
97 | return 0;
|
---|
98 |
|
---|
99 | leaf->Mail();
|
---|
100 | return BvHierarchy::EvalAbsCost(leaf->mObjects);
|
---|
101 | }
|
---|
102 |
|
---|
103 | // compute leaf pvs
|
---|
104 | float pvs = 0;
|
---|
105 | stack<BvhNode *> tStack;
|
---|
106 | tStack.push(node);
|
---|
107 |
|
---|
108 | while (!tStack.empty())
|
---|
109 | {
|
---|
110 | node = tStack.top();
|
---|
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 | {
|
---|
120 | BvhLeaf *leaf = static_cast<BvhLeaf *>(node);
|
---|
121 |
|
---|
122 | // add #objects exclusivly in this node
|
---|
123 | pvs += BvHierarchy::EvalAbsCost(leaf->mObjects);
|
---|
124 | }
|
---|
125 | else // traverse tree
|
---|
126 | {
|
---|
127 | BvhInterior *interior = static_cast<BvhInterior *>(node);
|
---|
128 |
|
---|
129 | tStack.push(interior->GetFront());
|
---|
130 | tStack.push(interior->GetBack());
|
---|
131 | }
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | return pvs;
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | float ObjectPvs::EvalPvsCost() const
|
---|
140 | {
|
---|
141 | float pvs = 0;
|
---|
142 |
|
---|
143 | Intersectable::NewMail();
|
---|
144 | KdLeaf::NewMail();
|
---|
145 |
|
---|
146 | ObjectPvsEntries::const_iterator it, it_end = mEntries.end();
|
---|
147 |
|
---|
148 | for (it = mEntries.begin(); it != it_end; ++ it)
|
---|
149 | {
|
---|
150 | Intersectable *obj = (*it).mObject;
|
---|
151 |
|
---|
152 | switch (obj->Type())
|
---|
153 | {
|
---|
154 | case Intersectable::KD_INTERSECTABLE:
|
---|
155 | {
|
---|
156 | // found kd node
|
---|
157 | KdIntersectable *kdObj = static_cast<KdIntersectable *>(obj);
|
---|
158 | pvs += EvalKdNodeContribution(kdObj);
|
---|
159 | break;
|
---|
160 | }
|
---|
161 | case Intersectable::BVH_INTERSECTABLE:
|
---|
162 | {
|
---|
163 | BvhNode *bvhObj = static_cast<BvhNode *>(obj);
|
---|
164 | pvs += EvalBvhNodeContribution(bvhObj);
|
---|
165 | break;
|
---|
166 | }
|
---|
167 | default:
|
---|
168 | // hack: should use assigned cost here
|
---|
169 | ++ pvs;
|
---|
170 |
|
---|
171 | break;
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | return pvs;
|
---|
176 | }
|
---|
177 |
|
---|
178 | }
|
---|