[162] | 1 | //#include <boost/algorithm/string.hpp>
|
---|
| 2 |
|
---|
| 3 | #include <stack>
|
---|
| 4 | #include "SceneGraph.h"
|
---|
| 5 | #include "X3dExporter.h"
|
---|
[339] | 6 | #include "Intersectable.h"
|
---|
[162] | 7 |
|
---|
| 8 |
|
---|
[2176] | 9 | using namespace std;
|
---|
| 10 |
|
---|
[863] | 11 | namespace GtpVisibilityPreprocessor {
|
---|
[860] | 12 |
|
---|
| 13 |
|
---|
[162] | 14 | bool
|
---|
| 15 | SceneGraph::Export( const string filename )
|
---|
| 16 | {
|
---|
| 17 | if (strstr(filename.c_str(), ".x3d")) {
|
---|
| 18 | X3dExporter exporter(filename);
|
---|
| 19 | exporter.ExportScene(mRoot);
|
---|
| 20 | return true;
|
---|
| 21 | } else {
|
---|
| 22 | cerr<<"Error: Currently unsuported export format, filename "<<filename<<endl;
|
---|
| 23 |
|
---|
| 24 | }
|
---|
| 25 |
|
---|
| 26 |
|
---|
| 27 | return false;
|
---|
| 28 |
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 |
|
---|
[1328] | 32 | SceneGraphNode::~SceneGraphNode()
|
---|
| 33 | {
|
---|
| 34 | CLEAR_CONTAINER(mGeometry);
|
---|
| 35 | // recursivly delete all children
|
---|
| 36 | CLEAR_CONTAINER(mChildren);
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 |
|
---|
| 40 | /************************************************************/
|
---|
| 41 | /* SceneGraph implementation */
|
---|
| 42 | /************************************************************/
|
---|
| 43 |
|
---|
| 44 |
|
---|
| 45 | SceneGraph::SceneGraph():
|
---|
| 46 | mRoot(NULL)
|
---|
| 47 | {
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 |
|
---|
[1002] | 51 | SceneGraph::~SceneGraph()
|
---|
| 52 | {
|
---|
| 53 | DEL_PTR(mRoot);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 |
|
---|
[1328] | 57 | SceneGraphNode *SceneGraph::GetRoot()
|
---|
[1002] | 58 | {
|
---|
[1328] | 59 | return mRoot;
|
---|
| 60 | }
|
---|
[1002] | 61 |
|
---|
[1328] | 62 |
|
---|
| 63 | void SceneGraph::SetRoot(SceneGraphNode *root)
|
---|
| 64 | {
|
---|
| 65 | mRoot = root;
|
---|
[1002] | 66 | }
|
---|
| 67 |
|
---|
| 68 |
|
---|
[162] | 69 | int
|
---|
[176] | 70 | SceneGraph::CollectObjects(ObjectContainer *instances)
|
---|
[162] | 71 | {
|
---|
[490] | 72 | instances->clear();
|
---|
[1328] | 73 | int number = 0;
|
---|
[162] | 74 |
|
---|
[1328] | 75 | stack<SceneGraphNode *> nodeStack;
|
---|
| 76 | nodeStack.push(mRoot);
|
---|
| 77 |
|
---|
| 78 | while (!nodeStack.empty()) {
|
---|
| 79 | SceneGraphNode *node = nodeStack.top();
|
---|
| 80 | nodeStack.pop();
|
---|
[1344] | 81 |
|
---|
| 82 | ObjectContainer::const_iterator mi = node->mGeometry.begin();
|
---|
[1328] | 83 | for (; mi != node->mGeometry.end(); mi++)
|
---|
| 84 | {
|
---|
| 85 | instances->push_back(*mi);
|
---|
| 86 | }
|
---|
[1344] | 87 |
|
---|
[1328] | 88 | SceneGraphNodeContainer::iterator ni = node->mChildren.begin();
|
---|
| 89 | for (; ni != node->mChildren.end(); ni++) {
|
---|
[1344] | 90 | nodeStack.push(*ni);
|
---|
[1328] | 91 | number++;
|
---|
| 92 | }
|
---|
| 93 | }
|
---|
[1344] | 94 |
|
---|
[1328] | 95 | return number;
|
---|
[162] | 96 | }
|
---|
[339] | 97 |
|
---|
| 98 |
|
---|
| 99 | int
|
---|
| 100 | SceneGraph::AssignObjectIds()
|
---|
| 101 | {
|
---|
[2544] | 102 | // matt: rather start with id zero
|
---|
| 103 | int id = 0;
|
---|
| 104 | stack<SceneGraphNode *> nodeStack;
|
---|
[1344] | 105 |
|
---|
[2544] | 106 | nodeStack.push(mRoot);
|
---|
| 107 |
|
---|
| 108 | while (!nodeStack.empty())
|
---|
| 109 | {
|
---|
| 110 | SceneGraphNode *node = nodeStack.top();
|
---|
| 111 | nodeStack.pop();
|
---|
| 112 |
|
---|
| 113 | ObjectContainer::iterator mi = node->mGeometry.begin();
|
---|
| 114 |
|
---|
| 115 | for (; mi != node->mGeometry.end(); mi ++)
|
---|
| 116 | {
|
---|
| 117 | (*mi)->SetId(id ++);
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | SceneGraphNodeContainer::iterator ni = node->mChildren.begin();
|
---|
| 121 | for (; ni != node->mChildren.end(); ni ++)
|
---|
| 122 | {
|
---|
| 123 | nodeStack.push(*ni);
|
---|
| 124 | }
|
---|
[576] | 125 | }
|
---|
[2544] | 126 |
|
---|
| 127 | // return max id
|
---|
| 128 | return id;
|
---|
[339] | 129 | }
|
---|
[387] | 130 |
|
---|
| 131 | void
|
---|
| 132 | SceneGraph::GetStatistics(int &intersectables, int &faces) const
|
---|
| 133 | {
|
---|
| 134 | stack<SceneGraphNode *> nodeStack;
|
---|
| 135 |
|
---|
| 136 | nodeStack.push(mRoot);
|
---|
| 137 | faces = 0;
|
---|
| 138 | intersectables = 0;
|
---|
| 139 | while (!nodeStack.empty()) {
|
---|
| 140 | SceneGraphNode *node = nodeStack.top();
|
---|
| 141 | nodeStack.pop();
|
---|
| 142 |
|
---|
| 143 | ObjectContainer::const_iterator mi = node->mGeometry.begin();
|
---|
| 144 | for (; mi != node->mGeometry.end(); mi++) {
|
---|
| 145 | intersectables++;
|
---|
| 146 | faces += (*mi)->NumberOfFaces();
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | SceneGraphNodeContainer::iterator ni = node->mChildren.begin();
|
---|
| 150 | for (; ni != node->mChildren.end(); ni++) {
|
---|
| 151 | nodeStack.push(*ni);
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | }
|
---|
[492] | 156 |
|
---|
| 157 |
|
---|
| 158 | void
|
---|
| 159 | SceneGraphNode::UpdateBox()
|
---|
| 160 | {
|
---|
| 161 | AxisAlignedBox3 box;
|
---|
| 162 |
|
---|
| 163 | box.Initialize();
|
---|
| 164 |
|
---|
| 165 | ObjectContainer::const_iterator mi = mGeometry.begin();
|
---|
| 166 | for (; mi != mGeometry.end(); mi++)
|
---|
| 167 | box.Include((*mi)->GetBox());
|
---|
| 168 |
|
---|
| 169 | SceneGraphNodeContainer::iterator ni = mChildren.begin();
|
---|
| 170 | for (; ni != mChildren.end(); ni++) {
|
---|
| 171 | (*ni)->UpdateBox();
|
---|
| 172 | box.Include((*ni)->mBox);
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | mBox = box;
|
---|
| 176 | }
|
---|
[860] | 177 |
|
---|
[1166] | 178 |
|
---|
| 179 | void SceneGraph::ExportScene(const string filename)
|
---|
| 180 | {
|
---|
[1194] | 181 |
|
---|
[1166] | 182 | }
|
---|
| 183 |
|
---|
[1194] | 184 |
|
---|
[1166] | 185 | void SceneGraph::LoadScene(const string filename)
|
---|
| 186 | {
|
---|
| 187 | // load binary version of mesh
|
---|
| 188 | }
|
---|
| 189 |
|
---|
[1958] | 190 |
|
---|
[860] | 191 | } |
---|