source: trunk/VUT/GtpVisibilityPreprocessor/src/ViewCellBsp.cpp @ 202

Revision 202, 3.4 KB checked in by mattausch, 19 years ago (diff)

stable

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