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

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

added bsp tree stuff

Line 
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/****************************************************************/
11bool BSPNode::IsRoot() const
12{
13        return mParent == NULL;
14}
15
16/****************************************************************/
17/*              class BSPInterior implementation                */
18/****************************************************************/
19
20bool BSPInterior::IsLeaf() const
21{
22        return false;
23}
24
25void 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
37void BSPInterior::SetupChildLinks(BSPNode *b, BSPNode *f)
38{
39    mBack = b;
40    mFront = f;
41}
42
43/****************************************************************/
44/*                  class BSPLeaf implementation                */
45/****************************************************************/
46
47BSPLeaf::BSPLeaf(ViewCell *viewCell): mViewCell(viewCell)
48{
49}
50
51bool BSPLeaf::IsLeaf() const
52{
53        return true;
54}
55
56/****************************************************************/
57/*                  class BSPTree implementation                */
58/****************************************************************/
59
60BSPTree::BSPTree(ViewCell *cell)
61{
62        mRootCell = cell;
63        mRoot = new BSPLeaf(mRootCell);
64}
65
66 
67void 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.mParent,
87                                                                          data.mDepth,
88                                                                          backPolys,
89                                                                          frontPolys);
90       
91                if (!node->IsLeaf())
92                {
93                        BSPInterior *interior = dynamic_cast<BSPInterior *>(node);
94
95                        // push the children on the stack
96                        if (interior->GetBack())
97                                tStack.push(BSPTraversalData(interior->GetBack(), interior, backPolys, data.mDepth+1));
98                        if (interior->GetFront())
99                                tStack.push(BSPTraversalData(interior->GetFront(), interior, frontPolys, data.mDepth+1));
100                }
101        }
102}
103
104Plane3 *BSPTree::SelectPlane(Mesh *viewcell)
105{
106        return &viewcell->GetFacePlane(0);
107}
108
109BSPNode *BSPTree::SubdivideNode(BSPLeaf *leaf, BSPInterior *parent,
110                                                                Mesh *viewCell, const int depth,
111                                                                Mesh &frontPolys, Mesh &backPolys)
112{
113static int sMaxDepth = 5; // HACK
114        // terminate traversal if no more faces in mesh
115        if ((viewCell->mFaces.size() == 0) && depth > sMaxDepth)
116                return leaf;
117 
118        // add the new nodes to the tree + select subdivision plane
119        Plane3 *plane = SelectPlane(viewCell);
120        BSPInterior *node = new BSPInterior(&plane); // ERROR!!
121 
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
Note: See TracBrowser for help on using the repository browser.