[162] | 1 | //#include <boost/algorithm/string.hpp>
|
---|
| 2 |
|
---|
| 3 | #include <stack>
|
---|
| 4 | #include "SceneGraph.h"
|
---|
| 5 | #include "X3dExporter.h"
|
---|
[339] | 6 | #include "Intersectable.h"
|
---|
[2615] | 7 | #include "IntersectableWrapper.h"
|
---|
[162] | 8 |
|
---|
| 9 |
|
---|
[2176] | 10 | using namespace std;
|
---|
| 11 |
|
---|
[863] | 12 | namespace GtpVisibilityPreprocessor {
|
---|
[860] | 13 |
|
---|
| 14 |
|
---|
[162] | 15 | bool
|
---|
| 16 | SceneGraph::Export( const string filename )
|
---|
| 17 | {
|
---|
| 18 | if (strstr(filename.c_str(), ".x3d")) {
|
---|
| 19 | X3dExporter exporter(filename);
|
---|
| 20 | exporter.ExportScene(mRoot);
|
---|
| 21 | return true;
|
---|
| 22 | } else {
|
---|
| 23 | cerr<<"Error: Currently unsuported export format, filename "<<filename<<endl;
|
---|
| 24 |
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 |
|
---|
| 28 | return false;
|
---|
| 29 |
|
---|
| 30 | }
|
---|
| 31 |
|
---|
| 32 |
|
---|
[1328] | 33 | SceneGraphNode::~SceneGraphNode()
|
---|
| 34 | {
|
---|
[2600] | 35 | }
|
---|
| 36 |
|
---|
| 37 |
|
---|
| 38 | SceneGraphLeaf::~SceneGraphLeaf()
|
---|
| 39 | {
|
---|
| 40 | CLEAR_CONTAINER(mGeometry);
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 |
|
---|
| 44 | SceneGraphInterior::~SceneGraphInterior()
|
---|
| 45 | {
|
---|
[1328] | 46 | // recursivly delete all children
|
---|
| 47 | CLEAR_CONTAINER(mChildren);
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 |
|
---|
[2600] | 51 |
|
---|
[1328] | 52 | /************************************************************/
|
---|
| 53 | /* SceneGraph implementation */
|
---|
| 54 | /************************************************************/
|
---|
| 55 |
|
---|
| 56 |
|
---|
| 57 | SceneGraph::SceneGraph():
|
---|
| 58 | mRoot(NULL)
|
---|
| 59 | {
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 |
|
---|
[1002] | 63 | SceneGraph::~SceneGraph()
|
---|
| 64 | {
|
---|
| 65 | DEL_PTR(mRoot);
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 |
|
---|
[2601] | 69 | SceneGraphInterior *SceneGraph::GetRoot()
|
---|
[1002] | 70 | {
|
---|
[1328] | 71 | return mRoot;
|
---|
| 72 | }
|
---|
[1002] | 73 |
|
---|
[1328] | 74 |
|
---|
[2601] | 75 | void SceneGraph::SetRoot(SceneGraphInterior *root)
|
---|
[1328] | 76 | {
|
---|
| 77 | mRoot = root;
|
---|
[1002] | 78 | }
|
---|
| 79 |
|
---|
| 80 |
|
---|
[2600] | 81 | int SceneGraph::CollectObjects(ObjectContainer &instances)
|
---|
[162] | 82 | {
|
---|
[2601] | 83 | instances.clear();
|
---|
[1328] | 84 | int number = 0;
|
---|
[162] | 85 |
|
---|
[1328] | 86 | stack<SceneGraphNode *> nodeStack;
|
---|
| 87 | nodeStack.push(mRoot);
|
---|
| 88 |
|
---|
[2600] | 89 | while (!nodeStack.empty())
|
---|
| 90 | {
|
---|
[1328] | 91 | SceneGraphNode *node = nodeStack.top();
|
---|
| 92 | nodeStack.pop();
|
---|
[2600] | 93 |
|
---|
| 94 | if (node->IsLeaf())
|
---|
[1328] | 95 | {
|
---|
[2601] | 96 | SceneGraphLeaf *leaf = static_cast<SceneGraphLeaf *>(node);
|
---|
[2600] | 97 |
|
---|
| 98 | ObjectContainer::const_iterator mi = leaf->mGeometry.begin();
|
---|
| 99 |
|
---|
| 100 | for (; mi != leaf->mGeometry.end(); mi++)
|
---|
| 101 | {
|
---|
[2601] | 102 | instances.push_back(*mi);
|
---|
[2600] | 103 | }
|
---|
[1328] | 104 | }
|
---|
[2600] | 105 | else
|
---|
| 106 | {
|
---|
[2601] | 107 | SceneGraphInterior *interior = static_cast<SceneGraphInterior *>(node);
|
---|
[2600] | 108 | SceneGraphNodeContainer::iterator ni = interior->mChildren.begin();
|
---|
| 109 |
|
---|
| 110 | for (; ni != interior->mChildren.end(); ++ ni)
|
---|
| 111 | {
|
---|
| 112 | nodeStack.push(*ni);
|
---|
| 113 | number++;
|
---|
| 114 | }
|
---|
[1328] | 115 | }
|
---|
| 116 | }
|
---|
[1344] | 117 |
|
---|
[1328] | 118 | return number;
|
---|
[162] | 119 | }
|
---|
[339] | 120 |
|
---|
| 121 |
|
---|
| 122 | int
|
---|
| 123 | SceneGraph::AssignObjectIds()
|
---|
| 124 | {
|
---|
[2544] | 125 | // matt: rather start with id zero
|
---|
| 126 | int id = 0;
|
---|
| 127 | stack<SceneGraphNode *> nodeStack;
|
---|
[1344] | 128 |
|
---|
[2544] | 129 | nodeStack.push(mRoot);
|
---|
| 130 |
|
---|
| 131 | while (!nodeStack.empty())
|
---|
| 132 | {
|
---|
| 133 | SceneGraphNode *node = nodeStack.top();
|
---|
| 134 | nodeStack.pop();
|
---|
| 135 |
|
---|
[2601] | 136 | if (node->IsLeaf())
|
---|
[2600] | 137 | {
|
---|
| 138 | SceneGraphLeaf *leaf = static_cast<SceneGraphLeaf *>(node);
|
---|
| 139 | ObjectContainer::iterator mi = leaf->mGeometry.begin();
|
---|
[2544] | 140 |
|
---|
[2600] | 141 | for (; mi != leaf->mGeometry.end(); mi ++)
|
---|
| 142 | {
|
---|
| 143 | (*mi)->SetId(id ++);
|
---|
| 144 | }
|
---|
[2544] | 145 | }
|
---|
[2600] | 146 | else
|
---|
[2544] | 147 | {
|
---|
[2600] | 148 | SceneGraphInterior *interior = static_cast<SceneGraphInterior *>(node);
|
---|
[2601] | 149 |
|
---|
[2600] | 150 | SceneGraphNodeContainer::iterator ni = interior->mChildren.begin();
|
---|
[2601] | 151 |
|
---|
| 152 | for (; ni != interior->mChildren.end(); ni ++)
|
---|
[2600] | 153 | {
|
---|
| 154 | nodeStack.push(*ni);
|
---|
| 155 | }
|
---|
[2544] | 156 | }
|
---|
[576] | 157 | }
|
---|
[2544] | 158 |
|
---|
| 159 | // return max id
|
---|
| 160 | return id;
|
---|
[339] | 161 | }
|
---|
[387] | 162 |
|
---|
| 163 | void
|
---|
| 164 | SceneGraph::GetStatistics(int &intersectables, int &faces) const
|
---|
| 165 | {
|
---|
[2600] | 166 | stack<SceneGraphNode *> nodeStack;
|
---|
| 167 |
|
---|
| 168 | nodeStack.push(mRoot);
|
---|
| 169 | faces = 0;
|
---|
[387] | 170 | intersectables = 0;
|
---|
[2601] | 171 |
|
---|
| 172 | while (!nodeStack.empty())
|
---|
| 173 | {
|
---|
[2600] | 174 | SceneGraphNode *node = nodeStack.top();
|
---|
| 175 | nodeStack.pop();
|
---|
| 176 |
|
---|
[2601] | 177 | if (node->IsLeaf())
|
---|
| 178 | {
|
---|
| 179 | SceneGraphLeaf *leaf = static_cast<SceneGraphLeaf *>(node);
|
---|
| 180 |
|
---|
| 181 | ObjectContainer::const_iterator mi = leaf->mGeometry.begin();
|
---|
| 182 | for (; mi != leaf->mGeometry.end(); mi++)
|
---|
| 183 | {
|
---|
| 184 | intersectables++;
|
---|
| 185 | faces += (*mi)->NumberOfFaces();
|
---|
| 186 | }
|
---|
[387] | 187 | }
|
---|
[2601] | 188 | else
|
---|
| 189 | {
|
---|
| 190 | SceneGraphInterior *interior = static_cast<SceneGraphInterior *>(node);
|
---|
[2600] | 191 |
|
---|
[2601] | 192 | SceneGraphNodeContainer::iterator ni = interior->mChildren.begin();
|
---|
| 193 | for (; ni != interior->mChildren.end(); ni++)
|
---|
| 194 | {
|
---|
| 195 | nodeStack.push(*ni);
|
---|
| 196 | }
|
---|
[2600] | 197 | }
|
---|
| 198 | }
|
---|
[387] | 199 | }
|
---|
[492] | 200 |
|
---|
| 201 |
|
---|
[2601] | 202 | void SceneGraphLeaf::UpdateBox()
|
---|
[492] | 203 | {
|
---|
[2600] | 204 | AxisAlignedBox3 box;
|
---|
| 205 | box.Initialize();
|
---|
| 206 |
|
---|
| 207 | ObjectContainer::const_iterator mi = mGeometry.begin();
|
---|
| 208 | for (; mi != mGeometry.end(); mi++)
|
---|
| 209 | box.Include((*mi)->GetBox());
|
---|
| 210 |
|
---|
| 211 | mBox = box;
|
---|
[492] | 212 | }
|
---|
[860] | 213 |
|
---|
[1166] | 214 |
|
---|
[2601] | 215 | void SceneGraphInterior::UpdateBox()
|
---|
[2600] | 216 | {
|
---|
| 217 | AxisAlignedBox3 box;
|
---|
| 218 |
|
---|
| 219 | box.Initialize();
|
---|
| 220 |
|
---|
| 221 | SceneGraphNodeContainer::iterator ni = mChildren.begin();
|
---|
| 222 |
|
---|
| 223 | for (; ni != mChildren.end(); ++ ni)
|
---|
| 224 | {
|
---|
| 225 | (*ni)->UpdateBox();
|
---|
[2615] | 226 | box.Include((*ni)->GetBox());
|
---|
[2600] | 227 | }
|
---|
| 228 |
|
---|
| 229 | mBox = box;
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 |
|
---|
| 233 |
|
---|
[1166] | 234 | void SceneGraph::ExportScene(const string filename)
|
---|
| 235 | {
|
---|
[1194] | 236 |
|
---|
[1166] | 237 | }
|
---|
| 238 |
|
---|
[1194] | 239 |
|
---|
[1166] | 240 | void SceneGraph::LoadScene(const string filename)
|
---|
| 241 | {
|
---|
| 242 | // load binary version of mesh
|
---|
| 243 | }
|
---|
| 244 |
|
---|
[1958] | 245 |
|
---|
[2601] | 246 | int SceneGraph::GetSize() const
|
---|
| 247 | {
|
---|
| 248 | stack<SceneGraphNode *> nodeStack;
|
---|
| 249 |
|
---|
| 250 | nodeStack.push(mRoot);
|
---|
| 251 | int size = 0;
|
---|
| 252 |
|
---|
| 253 | while (!nodeStack.empty())
|
---|
| 254 | {
|
---|
| 255 | SceneGraphNode *node = nodeStack.top();
|
---|
| 256 | nodeStack.pop();
|
---|
| 257 |
|
---|
| 258 | if (node->IsLeaf())
|
---|
| 259 | {
|
---|
| 260 | SceneGraphLeaf *leaf = static_cast<SceneGraphLeaf *>(node);
|
---|
| 261 |
|
---|
| 262 | size += leaf->mGeometry.size();
|
---|
| 263 | }
|
---|
| 264 | else
|
---|
| 265 | {
|
---|
| 266 | SceneGraphInterior *interior = static_cast<SceneGraphInterior *>(node);
|
---|
| 267 |
|
---|
| 268 | SceneGraphNodeContainer::iterator ni = interior->mChildren.begin();
|
---|
| 269 | for (; ni != interior->mChildren.end(); ni++)
|
---|
| 270 | {
|
---|
| 271 | nodeStack.push(*ni);
|
---|
| 272 | }
|
---|
| 273 | }
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | return size;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
[2609] | 279 |
|
---|
[2615] | 280 | SceneGraphLeaf::SceneGraphLeaf(): mIsDynamic(false)
|
---|
| 281 | {
|
---|
| 282 | mTrafo = IdentityMatrix();
|
---|
| 283 | #if DYNAMIC_OBJECTS_HACK
|
---|
| 284 | mIntersectable = new SceneGraphLeafIntersectable(this, mBox);
|
---|
| 285 | #endif
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 |
|
---|
| 289 | SceneGraphLeaf::SceneGraphLeaf(bool isDynamic): mIsDynamic(isDynamic)
|
---|
| 290 | {
|
---|
| 291 | mTrafo = IdentityMatrix();
|
---|
| 292 | } |
---|
| 293 | |
---|
| 294 |
|
---|
| 295 | void SceneGraphLeaf::ApplyTransform(const Matrix4x4 &trafo) |
---|
[2609] | 296 | { |
---|
[2615] | 297 | mTrafo = trafo * mTrafo; |
---|
[2609] | 298 | } |
---|
[2615] | 299 | |
---|
[2609] | 300 |
|
---|
[2615] | 301 | void SceneGraphLeaf::LoadTransform(const Matrix4x4 &m)
|
---|
| 302 | {
|
---|
| 303 | mTrafo = m;
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 |
|
---|
| 307 | void SceneGraphLeaf::GetTransform(Matrix4x4 &m) const
|
---|
| 308 | {
|
---|
| 309 | m = mTrafo;
|
---|
| 310 | }
|
---|
| 311 |
|
---|
| 312 |
|
---|
| 313 | AxisAlignedBox3 SceneGraphLeaf::GetBox() const
|
---|
| 314 | {
|
---|
| 315 | return Transform(mBox, mTrafo);
|
---|
| 316 | }
|
---|
| 317 | |
---|
| 318 | } |
---|