1 | #include "Plane3.h" |
---|
2 | #include "ViewCellBsp.h" |
---|
3 | #include "Mesh.h" |
---|
4 | |
---|
5 | #include "ViewCell.h" |
---|
6 | #include <stack> |
---|
7 | //namespace GtpVisibilityPreprocessor { |
---|
8 | /****************************************************************/ |
---|
9 | /* class BSPNode implementation */ |
---|
10 | /****************************************************************/ |
---|
11 | bool BSPNode::IsRoot() const |
---|
12 | { |
---|
13 | return mParent == NULL; |
---|
14 | } |
---|
15 | |
---|
16 | /****************************************************************/ |
---|
17 | /* class BSPInterior implementation */ |
---|
18 | /****************************************************************/ |
---|
19 | |
---|
20 | bool BSPInterior::IsLeaf() const |
---|
21 | { |
---|
22 | return false; |
---|
23 | } |
---|
24 | |
---|
25 | void BSPInterior::ReplaceChildLink(BSPNode *oldChild, BSPNode *newChild) |
---|
26 | { |
---|
27 | if (mBack == oldChild) |
---|
28 | { |
---|
29 | mBack = newChild; |
---|
30 | } |
---|
31 | else |
---|
32 | { |
---|
33 | mFront = newChild; |
---|
34 | } |
---|
35 | }
|
---|
36 |
|
---|
37 | void BSPInterior::SetupChildLinks(BSPNode *b, BSPNode *f)
|
---|
38 | {
|
---|
39 | mBack = b;
|
---|
40 | mFront = f;
|
---|
41 | }
|
---|
42 | |
---|
43 | /****************************************************************/ |
---|
44 | /* class BSPLeaf implementation */ |
---|
45 | /****************************************************************/ |
---|
46 | |
---|
47 | BSPLeaf::BSPLeaf(ViewCell *viewCell): mViewCell(viewCell) |
---|
48 | { |
---|
49 | } |
---|
50 | |
---|
51 | bool BSPLeaf::IsLeaf() const |
---|
52 | { |
---|
53 | return true; |
---|
54 | } |
---|
55 | |
---|
56 | /****************************************************************/ |
---|
57 | /* class BSPTree implementation */ |
---|
58 | /****************************************************************/ |
---|
59 | |
---|
60 | BSPTree::BSPTree(ViewCell *cell) |
---|
61 | { |
---|
62 | mRootCell = cell; |
---|
63 | mRoot = new BSPLeaf(mRootCell); |
---|
64 | } |
---|
65 |
|
---|
66 |
|
---|
67 | void BSPTree::Subdivide() |
---|
68 | {
|
---|
69 | BSPNode *currentNode = mRoot;
|
---|
70 |
|
---|
71 | std::stack<BSPTraversalData> tStack;
|
---|
72 | // stack<STraversalData> tStack;
|
---|
73 |
|
---|
74 | //tStack.push(tdata);
|
---|
75 |
|
---|
76 | while (!tStack.empty())
|
---|
77 | {
|
---|
78 | BSPTraversalData data = tStack.top();
|
---|
79 | tStack.pop();
|
---|
80 |
|
---|
81 | Mesh backPolys;
|
---|
82 | Mesh frontPolys;
|
---|
83 |
|
---|
84 | BSPNode *node = SubdivideNode(dynamic_cast<BSPLeaf *>(data.mNode),
|
---|
85 | data.mParent,
|
---|
86 | data.mDepth,
|
---|
87 | backPolys,
|
---|
88 | frontPolys);
|
---|
89 |
|
---|
90 | if (!node->IsLeaf())
|
---|
91 | {
|
---|
92 | BSPInterior *interior = dynamic_cast<BSPInterior *>(node);
|
---|
93 |
|
---|
94 | // push the children on the stack
|
---|
95 | if (interior->GetBack())
|
---|
96 | tStack.push(BSPTraversalData(interior->GetBack(), interior, backPolys, data.mDepth+1));
|
---|
97 | if (interior->GetFront())
|
---|
98 | tStack.push(BSPTraversalData(interior->GetFront(), interior, frontPolys, data.mDepth+1));
|
---|
99 | }
|
---|
100 | } |
---|
101 | } |
---|
102 | |
---|
103 | Plane3 *BSPTree::SelectPlane(Mesh *viewcell) |
---|
104 | { |
---|
105 | return viewcell->GetFacePlane(0); |
---|
106 | } |
---|
107 | |
---|
108 | BSPNode *BSPTree::SubdivideNode(BSPLeaf *leaf, BSPInterior *parent,
|
---|
109 | Mesh *viewCell, const int depth,
|
---|
110 | Mesh &frontPolys, Mesh &backPolys)
|
---|
111 | {
|
---|
112 | static int sMaxDepth = 5; // HACK
|
---|
113 | // terminate traversal if no more faces in mesh
|
---|
114 | if ((viewCell->mFaces.size() == 0) && depth > sMaxDepth)
|
---|
115 | return leaf;
|
---|
116 |
|
---|
117 | // add the new nodes to the tree
|
---|
118 | BSPInterior *node = new BSPInterior;
|
---|
119 |
|
---|
120 | // select subdivision plane
|
---|
121 | node->mPlane = SelectPlane(viewCell);
|
---|
122 |
|
---|
123 | FaceContainer::const_iterator fi;
|
---|
124 |
|
---|
125 | for ( fi = viewCell->mFaces.begin(); fi != viewCell->mFaces.end(); ++ fi)
|
---|
126 | {
|
---|
127 | }
|
---|
128 |
|
---|
129 | BSPLeaf *back = new BSPLeaf(mRootCell);
|
---|
130 | BSPLeaf *front = new BSPLeaf(mRootCell);
|
---|
131 |
|
---|
132 | // replace a link from node's parent
|
---|
133 | if (parent)
|
---|
134 | {
|
---|
135 | parent->ReplaceChildLink(leaf, node);
|
---|
136 | }
|
---|
137 |
|
---|
138 | // and setup child links
|
---|
139 | node->SetupChildLinks(back, front);
|
---|
140 |
|
---|
141 | delete leaf;
|
---|
142 | return node;
|
---|
143 | }
|
---|
144 | |
---|
145 | //} // GtpVisibilityPreprocessor |
---|