[190] | 1 | #include "Plane3.h" |
---|
| 2 | #include "ViewCellBsp.h" |
---|
[194] | 3 | #include "Mesh.h" |
---|
[190] | 4 | |
---|
[195] | 5 | #include "ViewCell.h" |
---|
| 6 | #include <stack> |
---|
[190] | 7 | //namespace GtpVisibilityPreprocessor { |
---|
[195] | 8 | /****************************************************************/ |
---|
| 9 | /* class BSPNode implementation */ |
---|
| 10 | /****************************************************************/ |
---|
[190] | 11 | bool BSPNode::IsRoot() const |
---|
| 12 | { |
---|
| 13 | return mParent == NULL; |
---|
| 14 | } |
---|
| 15 | |
---|
[195] | 16 | /****************************************************************/ |
---|
| 17 | /* class BSPInterior implementation */ |
---|
| 18 | /****************************************************************/ |
---|
[194] | 19 | |
---|
[190] | 20 | bool BSPInterior::IsLeaf() const |
---|
| 21 | { |
---|
| 22 | return false; |
---|
| 23 | } |
---|
| 24 | |
---|
[195] | 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 | |
---|
[190] | 47 | BSPLeaf::BSPLeaf(ViewCell *viewCell): mViewCell(viewCell) |
---|
| 48 | { |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | bool BSPLeaf::IsLeaf() const |
---|
| 52 | { |
---|
| 53 | return true; |
---|
| 54 | } |
---|
| 55 | |
---|
[195] | 56 | /****************************************************************/ |
---|
| 57 | /* class BSPTree implementation */ |
---|
| 58 | /****************************************************************/ |
---|
| 59 | |
---|
[190] | 60 | BSPTree::BSPTree(ViewCell *cell) |
---|
| 61 | { |
---|
| 62 | mRootCell = cell; |
---|
| 63 | mRoot = new BSPLeaf(mRootCell); |
---|
| 64 | } |
---|
[195] | 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),
|
---|
[197] | 85 | data.mParent,
|
---|
[195] | 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 | } |
---|
[190] | 103 | |
---|
[195] | 104 | Plane3 *BSPTree::SelectPlane(Mesh *viewcell) |
---|
| 105 | { |
---|
[197] | 106 | return &viewcell->GetFacePlane(0); |
---|
[195] | 107 | } |
---|
| 108 | |
---|
| 109 | BSPNode *BSPTree::SubdivideNode(BSPLeaf *leaf, BSPInterior *parent,
|
---|
| 110 | Mesh *viewCell, const int depth,
|
---|
| 111 | Mesh &frontPolys, Mesh &backPolys)
|
---|
| 112 | {
|
---|
| 113 | static 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 |
|
---|
[197] | 118 | // add the new nodes to the tree + select subdivision plane
|
---|
| 119 | Plane3 *plane = SelectPlane(viewCell);
|
---|
| 120 | BSPInterior *node = new BSPInterior(&plane); // ERROR!!
|
---|
[195] | 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 | |
---|
[190] | 145 | //} // GtpVisibilityPreprocessor |
---|