1 | #include <iostream>
|
---|
2 | #include <stack>
|
---|
3 | #include "Pvs.h"
|
---|
4 | #include "Intersectable.h"
|
---|
5 | #include "KdIntersectable.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 CountNewObjectsInKdNode(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 = dynamic_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 = dynamic_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 | /** 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 | {
|
---|
88 | if (!node->Mailed())
|
---|
89 | {
|
---|
90 | node->Mail();
|
---|
91 | return (int)(((BvhLeaf *)node)->mObjects.size());
|
---|
92 | }
|
---|
93 | else
|
---|
94 | {
|
---|
95 | return 0;
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 |
|
---|
100 | // compute leaf pvs
|
---|
101 | int pvs = 0;
|
---|
102 |
|
---|
103 | stack<BvhNode *> tStack;
|
---|
104 |
|
---|
105 | tStack.push(bvhobj->GetItem());
|
---|
106 |
|
---|
107 | while (!tStack.empty())
|
---|
108 | {
|
---|
109 | BvhNode *node = tStack.top();
|
---|
110 | tStack.pop();
|
---|
111 |
|
---|
112 | // already processed node (=> objects already in pvs)?
|
---|
113 | if (!node->Mailed())
|
---|
114 | {
|
---|
115 | node->Mail();
|
---|
116 |
|
---|
117 | if (node->IsLeaf())
|
---|
118 | {
|
---|
119 | BvhLeaf *leaf = dynamic_cast<BvhLeaf *>(node);
|
---|
120 |
|
---|
121 | // add #objects exclusivly in this node
|
---|
122 | pvs += leaf->mObjects.size();
|
---|
123 | }
|
---|
124 | else // traverse tree
|
---|
125 | {
|
---|
126 | BvhInterior *interior = dynamic_cast<BvhInterior *>(node);
|
---|
127 |
|
---|
128 | tStack.push(interior->GetFront());
|
---|
129 | tStack.push(interior->GetBack());
|
---|
130 | }
|
---|
131 | }
|
---|
132 | }
|
---|
133 | return pvs;
|
---|
134 | }
|
---|
135 |
|
---|
136 |
|
---|
137 |
|
---|
138 | int ObjectPvs::CountObjectsInPvs() const
|
---|
139 | {
|
---|
140 | int pvs = 0;
|
---|
141 |
|
---|
142 | Intersectable::NewMail();
|
---|
143 | KdLeaf::NewMail();
|
---|
144 | BvhLeaf::NewMail();
|
---|
145 |
|
---|
146 | ObjectPvsMap::const_iterator it, it_end = mEntries.end();
|
---|
147 |
|
---|
148 | for (it = mEntries.begin(); it != it_end; ++ it)
|
---|
149 | {
|
---|
150 | Intersectable *obj = (*it).first;
|
---|
151 |
|
---|
152 | switch (obj->Type())
|
---|
153 | {
|
---|
154 | case Intersectable::KD_INTERSECTABLE:
|
---|
155 | {
|
---|
156 | // found kd node
|
---|
157 | KdIntersectable *kdObj = dynamic_cast<KdIntersectable *>(obj);
|
---|
158 | pvs += CountNewObjectsInKdNode(kdObj);
|
---|
159 | break;
|
---|
160 | }
|
---|
161 | case Intersectable::BVH_INTERSECTABLE:
|
---|
162 | {
|
---|
163 | BvhIntersectable *bvhObj = dynamic_cast<BvhIntersectable *>(obj);
|
---|
164 | pvs += CountNewObjectsInBvhNode(bvhObj);
|
---|
165 | break;
|
---|
166 | }
|
---|
167 | default:
|
---|
168 | ++ pvs;
|
---|
169 | break;
|
---|
170 | }
|
---|
171 | }
|
---|
172 |
|
---|
173 | return pvs;
|
---|
174 | }
|
---|
175 |
|
---|
176 | } |
---|