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