[162] | 1 | #include <stack>
|
---|
| 2 | #include "common.h"
|
---|
| 3 | #include "SceneGraph.h"
|
---|
| 4 | #include "X3dExporter.h"
|
---|
| 5 | #include "Mesh.h"
|
---|
| 6 | #include "KdTree.h"
|
---|
[242] | 7 | #include "ViewCellBsp.h"
|
---|
| 8 | #include "ViewCell.h"
|
---|
[260] | 9 | #include "Polygon3.h"
|
---|
[386] | 10 | #include "VssRay.h"
|
---|
[1233] | 11 | #include "HierarchyManager.h"
|
---|
[434] | 12 | #include "VssTree.h"
|
---|
[449] | 13 | #include "VspBspTree.h"
|
---|
[450] | 14 | #include "RssTree.h"
|
---|
[535] | 15 | #include "Beam.h"
|
---|
[162] | 16 |
|
---|
[863] | 17 | namespace GtpVisibilityPreprocessor {
|
---|
[449] | 18 |
|
---|
[694] | 19 |
|
---|
[162] | 20 | X3dExporter::X3dExporter(const string filename):Exporter(filename)
|
---|
| 21 | {
|
---|
| 22 | stream.open(mFilename.c_str());
|
---|
| 23 | stream<<"<?xml version=\"1.0\" encoding=\"UTF-8\"?>"<<endl;
|
---|
| 24 | stream<<"<X3D>"<<endl;
|
---|
| 25 | stream<<"<Scene>"<<endl;
|
---|
| 26 |
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | X3dExporter::~X3dExporter()
|
---|
| 30 | {
|
---|
| 31 | stream<<"</Scene>"<<endl;
|
---|
| 32 | stream<<"</X3D>"<<endl;
|
---|
| 33 | stream.close();
|
---|
| 34 | }
|
---|
| 35 |
|
---|
[176] | 36 |
|
---|
[1112] | 37 |
|
---|
[162] | 38 | bool
|
---|
[386] | 39 | X3dExporter::ExportRays(const RayContainer &rays,
|
---|
[444] | 40 | const float length,
|
---|
| 41 | const RgbColor &color)
|
---|
[162] | 42 | {
|
---|
[386] | 43 | RayContainer::const_iterator ri = rays.begin();
|
---|
| 44 |
|
---|
[162] | 45 | stream<<"<Shape>"<<endl;
|
---|
[176] | 46 | stream<<"<Appearance>"<<endl;
|
---|
[427] | 47 | stream<<"<Material diffuseColor=\""<<color.r<<" "<<color.g<<" "<<color.b<<
|
---|
[176] | 48 | "\" />"<<endl;
|
---|
| 49 | stream<<"</Appearance>"<<endl;
|
---|
| 50 |
|
---|
[209] | 51 | stream<<"<IndexedLineSet coordIndex=\""<<endl;
|
---|
[162] | 52 |
|
---|
| 53 | int index = 0;
|
---|
| 54 | for (; ri != rays.end(); ri++) {
|
---|
| 55 | stream<<index<<" "<<index+1<<" -1\n";
|
---|
| 56 | index+=2;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | stream<<"\" >"<<endl;
|
---|
| 60 |
|
---|
| 61 | stream<<"<Coordinate point=\""<<endl;
|
---|
| 62 |
|
---|
| 63 | ri = rays.begin();
|
---|
| 64 | for (; ri != rays.end(); ri++) {
|
---|
[386] | 65 | Vector3 a = (*ri)->GetLoc();
|
---|
[162] | 66 |
|
---|
[176] | 67 | Vector3 b;
|
---|
| 68 | if (length < 0)
|
---|
[386] | 69 | b = (*ri)->GetLoc() - length*(*ri)->GetDir();
|
---|
[176] | 70 | else
|
---|
[386] | 71 | if ((*ri)->intersections.size()==0)
|
---|
[492] | 72 | b = (*ri)->GetLoc() + length*(*ri)->GetDir();
|
---|
[176] | 73 | else
|
---|
[492] | 74 | b = (*ri)->Extrap((*ri)->intersections[0].mT);
|
---|
[176] | 75 |
|
---|
[162] | 76 | stream<<a.x<<" "<<a.y<<" "<<a.z<<" ,";
|
---|
| 77 | stream<<b.x<<" "<<b.y<<" "<<b.z<<" ,\n";
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | stream<<"\" >"<<endl;
|
---|
| 81 | stream<<"</Coordinate>"<<endl;
|
---|
| 82 | stream<<"</IndexedLineSet>"<<endl;
|
---|
| 83 | stream<<"</Shape>"<<endl;
|
---|
[386] | 84 |
|
---|
[162] | 85 | return true;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
[1112] | 88 |
|
---|
[349] | 89 | bool
|
---|
[1112] | 90 | X3dExporter::ExportRaySets(const vector<VssRayContainer> &rays,
|
---|
| 91 | const RgbColor &color)
|
---|
| 92 | {
|
---|
| 93 | vector<VssRayContainer>::const_iterator ri = rays.begin();
|
---|
| 94 |
|
---|
| 95 | stream<<"<Switch DEF=\"RAYS\" whichChoice=\"0\" >"<<endl;
|
---|
| 96 |
|
---|
| 97 | bool result = false;
|
---|
| 98 |
|
---|
| 99 | for (; ri != rays.end(); ri++) {
|
---|
| 100 | result = ExportRays(*ri, color);
|
---|
| 101 | if (!result) {
|
---|
| 102 | cerr<<"Error while exporting rays!\n";
|
---|
| 103 | break;
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | stream<<"</Switch>"<<endl;
|
---|
| 108 |
|
---|
| 109 | stream<<"<TimeSensor DEF='TIMER' loop='true' startTime='0' stopTime='0' cycleInterval='5'>"
|
---|
| 110 | <<endl;
|
---|
| 111 | stream<<"</TimeSensor>"<<endl;
|
---|
| 112 |
|
---|
| 113 | stream<<"<IntegerSequencer DEF='SEQ'"<<endl;
|
---|
| 114 | stream<<"key='0.0 ";
|
---|
| 115 | int i;
|
---|
[1178] | 116 | int size = (int)rays.size() + 2;
|
---|
[1112] | 117 | for (i=1; i < size; i++) {
|
---|
| 118 | stream<<i/(float)(size-1)<<" ";
|
---|
| 119 | }
|
---|
| 120 | stream<<"'\n";
|
---|
| 121 |
|
---|
| 122 | stream<<"keyValue='";
|
---|
| 123 | for (i=0; i < size; i++) {
|
---|
| 124 | stream<<i<<" ";
|
---|
| 125 | }
|
---|
| 126 | stream<<"'>\n";
|
---|
| 127 |
|
---|
| 128 | stream<<"</IntegerSequencer>"<<endl;
|
---|
| 129 |
|
---|
| 130 | stream<<
|
---|
| 131 | "<ROUTE fromNode='TIMER' fromField='fraction_changed' toNode='SEQ' toField='set_fraction'>";
|
---|
| 132 | stream<<"</ROUTE>"<<endl;
|
---|
| 133 |
|
---|
| 134 | stream<<"<ROUTE fromNode='SEQ' fromField='value_changed' toNode='RAYS' toField='set_whichChoice'>";
|
---|
| 135 |
|
---|
| 136 | stream<<"</ROUTE>"<<endl;
|
---|
| 137 |
|
---|
| 138 | return result;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | bool
|
---|
[386] | 142 | X3dExporter::ExportRays(const VssRayContainer &rays,
|
---|
[444] | 143 | const RgbColor &color)
|
---|
[349] | 144 | {
|
---|
[386] | 145 | VssRayContainer::const_iterator ri = rays.begin();
|
---|
[349] | 146 |
|
---|
| 147 | stream<<"<Shape>"<<endl;
|
---|
| 148 | stream<<"<Appearance>"<<endl;
|
---|
[427] | 149 | stream<<"<Material diffuseColor=\""<<color.r<<" "<<color.g<<" "<<color.b<<
|
---|
[349] | 150 | "\" />"<<endl;
|
---|
| 151 | stream<<"</Appearance>"<<endl;
|
---|
| 152 |
|
---|
| 153 | stream<<"<IndexedLineSet coordIndex=\""<<endl;
|
---|
| 154 |
|
---|
| 155 | int index = 0;
|
---|
| 156 | for (; ri != rays.end(); ri++) {
|
---|
| 157 | stream<<index<<" "<<index+1<<" -1\n";
|
---|
| 158 | index+=2;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | stream<<"\" >"<<endl;
|
---|
| 162 |
|
---|
| 163 | stream<<"<Coordinate point=\""<<endl;
|
---|
| 164 |
|
---|
| 165 | ri = rays.begin();
|
---|
| 166 | for (; ri != rays.end(); ri++) {
|
---|
[444] | 167 | const Vector3 a = (*ri)->GetOrigin();
|
---|
[483] | 168 | //const Vector3 b = (*ri)->mTerminationObject ? (*ri)->GetTermination() : a + 1000 * Normalize((*ri)->GetDir());
|
---|
| 169 | const Vector3 b = (*ri)->GetTermination(); // matt: change again!!
|
---|
| 170 |
|
---|
[349] | 171 | stream<<a.x<<" "<<a.y<<" "<<a.z<<" ,";
|
---|
[444] | 172 | stream<<b.x<<" "<<b.y<<" "<<b.z<<" ,\n";
|
---|
[349] | 173 | }
|
---|
| 174 |
|
---|
| 175 | stream<<"\" >"<<endl;
|
---|
| 176 | stream<<"</Coordinate>"<<endl;
|
---|
| 177 | stream<<"</IndexedLineSet>"<<endl;
|
---|
| 178 | stream<<"</Shape>"<<endl;
|
---|
[386] | 179 |
|
---|
[349] | 180 | return true;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[162] | 183 | void
|
---|
| 184 | X3dExporter::ExportSceneNode(SceneGraphNode *node)
|
---|
| 185 | {
|
---|
| 186 | stream<<"<Group>"<<endl;
|
---|
| 187 |
|
---|
| 188 | SceneGraphNodeContainer::iterator ni = node->mChildren.begin();
|
---|
| 189 | for (; ni != node->mChildren.end(); ni++)
|
---|
| 190 | ExportSceneNode(*ni);
|
---|
| 191 |
|
---|
| 192 |
|
---|
[176] | 193 | ObjectContainer::const_iterator mi = node->mGeometry.begin();
|
---|
[162] | 194 | for (; mi != node->mGeometry.end(); mi++) {
|
---|
| 195 | // export the transform...
|
---|
[176] | 196 | ExportIntersectable(*mi);
|
---|
[162] | 197 | }
|
---|
| 198 |
|
---|
| 199 | stream<<"</Group>"<<endl;
|
---|
| 200 |
|
---|
| 201 | }
|
---|
| 202 |
|
---|
[694] | 203 |
|
---|
[313] | 204 | void
|
---|
[396] | 205 | X3dExporter::ExportBspLeaves(const BspTree &tree, const int maxPvs)
|
---|
[373] | 206 | {
|
---|
[396] | 207 | stack<pair<BspNode *, BspNodeGeometry *> > tStack;
|
---|
| 208 | ViewCell::NewMail();
|
---|
[373] | 209 |
|
---|
[396] | 210 | BspNodeGeometry *geom = new BspNodeGeometry();
|
---|
| 211 | tree.ConstructGeometry(tree.GetRoot(), *geom);
|
---|
[373] | 212 |
|
---|
[396] | 213 | tStack.push(pair<BspNode *, BspNodeGeometry *>(tree.GetRoot(), geom));
|
---|
| 214 |
|
---|
[1020] | 215 | if (maxPvs)
|
---|
[396] | 216 | mUseForcedMaterial = true;
|
---|
| 217 |
|
---|
| 218 | while (!tStack.empty())
|
---|
[373] | 219 | {
|
---|
[396] | 220 | BspNode *node = tStack.top().first;
|
---|
| 221 | BspNodeGeometry *cell = tStack.top().second;
|
---|
| 222 | tStack.pop();
|
---|
| 223 |
|
---|
| 224 | if (!node->IsLeaf())
|
---|
| 225 | {
|
---|
| 226 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
| 227 |
|
---|
| 228 | BspNodeGeometry *front = new BspNodeGeometry();
|
---|
| 229 | BspNodeGeometry *back = new BspNodeGeometry();
|
---|
| 230 |
|
---|
[449] | 231 | cell->SplitGeometry(*front,
|
---|
| 232 | *back,
|
---|
| 233 | interior->GetPlane(),
|
---|
| 234 | tree.GetBoundingBox(),
|
---|
| 235 | tree.GetEpsilon());
|
---|
[396] | 236 |
|
---|
| 237 | tStack.push(pair<BspNode *, BspNodeGeometry *>(interior->GetFront(), front));
|
---|
| 238 | tStack.push(pair<BspNode *, BspNodeGeometry *>(interior->GetBack(), back));
|
---|
| 239 | }
|
---|
| 240 | else
|
---|
| 241 | {
|
---|
[1020] | 242 | if (maxPvs)
|
---|
[396] | 243 | {
|
---|
| 244 | BspLeaf *leaf = dynamic_cast<BspLeaf *>(node);
|
---|
| 245 |
|
---|
| 246 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
[1141] | 247 | const float importance =
|
---|
[1168] | 248 | (float)leaf->GetViewCell()->GetPvs().CountObjectsInPvs() / (float)maxPvs;
|
---|
[396] | 249 |
|
---|
| 250 | mForcedMaterial.mDiffuseColor.r = importance;
|
---|
| 251 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
| 252 | }
|
---|
[678] | 253 | ExportPolygons(cell->GetPolys());
|
---|
[1344] | 254 | }
|
---|
[396] | 255 | DEL_PTR(cell);
|
---|
[373] | 256 | }
|
---|
| 257 | }
|
---|
| 258 |
|
---|
[176] | 259 | void
|
---|
[261] | 260 | X3dExporter::ExportViewCell(ViewCell *viewCell)
|
---|
[242] | 261 | {
|
---|
[261] | 262 | if (viewCell->GetMesh())
|
---|
[1344] | 263 | {
|
---|
[261] | 264 | ExportMesh(viewCell->GetMesh());
|
---|
[1344] | 265 | }
|
---|
[242] | 266 | }
|
---|
| 267 |
|
---|
| 268 | void
|
---|
[162] | 269 | X3dExporter::ExportMesh(Mesh *mesh)
|
---|
| 270 | {
|
---|
| 271 |
|
---|
| 272 | stream<<"<Shape>"<<endl;
|
---|
| 273 | stream<<"<Appearance>"<<endl;
|
---|
[176] | 274 |
|
---|
[162] | 275 | // $$ tmp -> random material
|
---|
| 276 |
|
---|
| 277 | float r, g, b;
|
---|
| 278 |
|
---|
[176] | 279 | if (mUseForcedMaterial) {
|
---|
| 280 | r = mForcedMaterial.mDiffuseColor.r;
|
---|
| 281 | g = mForcedMaterial.mDiffuseColor.g;
|
---|
| 282 | b = mForcedMaterial.mDiffuseColor.b;
|
---|
| 283 |
|
---|
| 284 | } else
|
---|
| 285 | if (mesh->mMaterial) {
|
---|
| 286 | r = mesh->mMaterial->mDiffuseColor.r;
|
---|
| 287 | g = mesh->mMaterial->mDiffuseColor.g;
|
---|
| 288 | b = mesh->mMaterial->mDiffuseColor.b;
|
---|
| 289 | } else {
|
---|
| 290 | r = RandomValue(0.5, 1.0);
|
---|
| 291 | g = RandomValue(0.5, 1.0);
|
---|
| 292 | b = RandomValue(0.5, 1.0);
|
---|
| 293 | }
|
---|
[1001] | 294 |
|
---|
[162] | 295 | stream<<"<Material diffuseColor=\""<<r<<" "<<g<<" "<<b<<
|
---|
| 296 | "\" specularColor=\"0.0 0.0 0.0\"/>"<<endl;
|
---|
| 297 | stream<<"</Appearance>"<<endl;
|
---|
| 298 |
|
---|
| 299 |
|
---|
[191] | 300 | if (mWireframe)
|
---|
[427] | 301 | stream<<"<IndexedLineSet coordIndex=\""<<endl;
|
---|
[162] | 302 | else
|
---|
[465] | 303 | //stream<<"<IndexedFaceSet ccw=\"TRUE\" coordIndex=\""<<endl;
|
---|
| 304 | stream << "<IndexedFaceSet coordIndex=\"" << endl;
|
---|
[162] | 305 |
|
---|
| 306 | FaceContainer::const_iterator fi = mesh->mFaces.begin();
|
---|
| 307 |
|
---|
| 308 | int index = 0;
|
---|
| 309 |
|
---|
| 310 | for (; fi != mesh->mFaces.end(); fi++) {
|
---|
| 311 | Face *face = *fi;
|
---|
| 312 | VertexIndexContainer::const_iterator vi = face->mVertexIndices.begin();
|
---|
| 313 | for (; vi != face->mVertexIndices.end(); vi++)
|
---|
| 314 | stream<<*vi<<" ";
|
---|
[378] | 315 | if (mWireframe) // final line to finish polygon
|
---|
| 316 | stream << (*face->mVertexIndices.begin()) << " ";
|
---|
| 317 |
|
---|
[162] | 318 | stream<<"-1"<<endl;
|
---|
| 319 | }
|
---|
| 320 | stream<<"\" >"<<endl;
|
---|
| 321 |
|
---|
| 322 | stream<<"<Coordinate point=\""<<endl;
|
---|
| 323 |
|
---|
| 324 | VertexContainer::const_iterator vi = mesh->mVertices.begin();
|
---|
| 325 | for (; vi != mesh->mVertices.end(); vi++) {
|
---|
| 326 | stream<<(*vi).x<<" "<<(*vi).y<<" "<<(*vi).z;
|
---|
| 327 | stream<<","<<endl;
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | stream<<"\" >"<<endl;
|
---|
| 331 | stream<<"</Coordinate>"<<endl;
|
---|
| 332 |
|
---|
[191] | 333 | if (mWireframe)
|
---|
[162] | 334 | stream<<"</IndexedLineSet>"<<endl;
|
---|
| 335 | else
|
---|
| 336 | stream<<"</IndexedFaceSet>"<<endl;
|
---|
| 337 |
|
---|
| 338 | stream<<"</Shape>"<<endl;
|
---|
| 339 |
|
---|
| 340 | }
|
---|
| 341 |
|
---|
[242] | 342 |
|
---|
| 343 | void X3dExporter::ExportPolygon(Polygon3 *poly)
|
---|
| 344 | {
|
---|
| 345 | stream << "<Shape>" << endl;
|
---|
| 346 | stream << "<Appearance>" << endl;
|
---|
| 347 |
|
---|
| 348 | // $$ tmp -> random material
|
---|
| 349 |
|
---|
| 350 | float r, g, b;
|
---|
| 351 |
|
---|
| 352 | if (mUseForcedMaterial)
|
---|
| 353 | {
|
---|
| 354 | r = mForcedMaterial.mDiffuseColor.r;
|
---|
| 355 | g = mForcedMaterial.mDiffuseColor.g;
|
---|
| 356 | b = mForcedMaterial.mDiffuseColor.b;
|
---|
| 357 | }
|
---|
| 358 | else if (poly->mMaterial)
|
---|
| 359 | {
|
---|
| 360 | r = poly->mMaterial->mDiffuseColor.r;
|
---|
| 361 | g = poly->mMaterial->mDiffuseColor.g;
|
---|
| 362 | b = poly->mMaterial->mDiffuseColor.b;
|
---|
| 363 | } else
|
---|
| 364 | {
|
---|
| 365 | r = RandomValue(0.5, 1.0);
|
---|
| 366 | g = RandomValue(0.5, 1.0);
|
---|
| 367 | b = RandomValue(0.5, 1.0);
|
---|
| 368 | }
|
---|
| 369 |
|
---|
| 370 | stream << "<Material diffuseColor=\"" << r << " " << g << " " << b
|
---|
| 371 | << "\" specularColor=\"0.0 0.0 0.0\"/>" << endl;
|
---|
| 372 |
|
---|
| 373 | stream << "</Appearance>" << endl;
|
---|
| 374 |
|
---|
| 375 |
|
---|
[262] | 376 | //-- create and write indices
|
---|
[242] | 377 | if (mWireframe)
|
---|
[465] | 378 | stream<<"<IndexedLineSet coordIndex=\""<<endl;
|
---|
[242] | 379 | else
|
---|
[465] | 380 | //stream << "<IndexedFaceSet ccw=\"TRUE\" coordIndex=\"" << endl;
|
---|
| 381 | stream << "<IndexedFaceSet coordIndex=\"" << endl;
|
---|
[262] | 382 | int index = 0;
|
---|
| 383 |
|
---|
| 384 | VertexContainer::const_iterator vi;
|
---|
| 385 |
|
---|
[333] | 386 | for (index = 0; index < (int)poly->mVertices.size(); ++ index)
|
---|
[318] | 387 | stream << index << " ";
|
---|
| 388 |
|
---|
| 389 | if (mWireframe) // final line to finish polygon
|
---|
| 390 | stream << "0 ";
|
---|
| 391 |
|
---|
[262] | 392 | stream << "-1" << endl;
|
---|
| 393 | stream << "\" >" << endl;
|
---|
| 394 |
|
---|
[242] | 395 | stream << "<Coordinate point=\"" << endl;
|
---|
| 396 |
|
---|
[262] | 397 | for (vi = poly->mVertices.begin(); vi != poly->mVertices.end(); ++vi)
|
---|
[242] | 398 | {
|
---|
| 399 | stream << (*vi).x << " " << (*vi).y << " " << (*vi).z;
|
---|
| 400 | stream << "," << endl;
|
---|
| 401 | }
|
---|
| 402 |
|
---|
[262] | 403 | stream << "\" >" << endl;
|
---|
| 404 | stream << "</Coordinate>" << endl;
|
---|
[242] | 405 |
|
---|
| 406 | if (mWireframe)
|
---|
| 407 | stream << "</IndexedLineSet>" << endl;
|
---|
| 408 | else
|
---|
| 409 | stream << "</IndexedFaceSet>" << endl;
|
---|
| 410 |
|
---|
| 411 | stream << "</Shape>" << endl;
|
---|
| 412 | }
|
---|
| 413 |
|
---|
[712] | 414 |
|
---|
[1006] | 415 |
|
---|
[313] | 416 | void X3dExporter::ExportPolygons(const PolygonContainer &polys)
|
---|
[263] | 417 | {
|
---|
[318] | 418 | stream << "<Shape>" << endl;
|
---|
[263] | 419 | stream << "<Appearance>" << endl;
|
---|
| 420 |
|
---|
| 421 | // $$ tmp -> random material
|
---|
| 422 |
|
---|
| 423 | float r, g, b;
|
---|
[242] | 424 |
|
---|
[263] | 425 | if (mUseForcedMaterial)
|
---|
| 426 | {
|
---|
| 427 | r = mForcedMaterial.mDiffuseColor.r;
|
---|
| 428 | g = mForcedMaterial.mDiffuseColor.g;
|
---|
| 429 | b = mForcedMaterial.mDiffuseColor.b;
|
---|
| 430 | }
|
---|
| 431 | else
|
---|
| 432 | {
|
---|
| 433 | r = RandomValue(0.5, 1.0);
|
---|
| 434 | g = RandomValue(0.5, 1.0);
|
---|
| 435 | b = RandomValue(0.5, 1.0);
|
---|
| 436 | }
|
---|
| 437 |
|
---|
| 438 | stream << "<Material diffuseColor=\"" << r << " " << g << " " << b
|
---|
| 439 | << "\" specularColor=\"0.0 0.0 0.0\"/>" << endl;
|
---|
| 440 |
|
---|
| 441 | stream << "</Appearance>" << endl;
|
---|
| 442 |
|
---|
| 443 |
|
---|
| 444 | //-- create and write indices
|
---|
| 445 | if (mWireframe)
|
---|
[465] | 446 | stream<<"<IndexedLineSet coordIndex=\""<<endl;
|
---|
[263] | 447 | else
|
---|
[465] | 448 | //stream << "<IndexedFaceSet ccw=\"TRUE\" coordIndex=\"" << endl;
|
---|
| 449 | stream << "<IndexedFaceSet coordIndex=\"" << endl;
|
---|
[263] | 450 |
|
---|
| 451 | int index = 0;
|
---|
| 452 |
|
---|
| 453 | PolygonContainer::const_iterator pit;
|
---|
| 454 |
|
---|
| 455 | VertexContainer::const_iterator vi;
|
---|
| 456 |
|
---|
[313] | 457 | for (pit = polys.begin(); pit != polys.end(); ++pit)
|
---|
[263] | 458 | {
|
---|
| 459 | Polygon3 *poly = *pit;
|
---|
[318] | 460 | int startIdx = index;
|
---|
[263] | 461 | for (vi = poly->mVertices.begin(); vi != poly->mVertices.end(); ++vi)
|
---|
| 462 | {
|
---|
| 463 | stream << index ++ << " ";
|
---|
| 464 | }
|
---|
[318] | 465 |
|
---|
| 466 | stream << startIdx << " ";// finish line
|
---|
[263] | 467 | stream << "-1" << endl;
|
---|
| 468 | }
|
---|
| 469 |
|
---|
| 470 | stream << "\" >" << endl;
|
---|
| 471 |
|
---|
| 472 | stream << "<Coordinate point=\"" << endl;
|
---|
[318] | 473 | for (pit = polys.begin(); pit != polys.end(); ++ pit)
|
---|
[263] | 474 | {
|
---|
| 475 | Polygon3 *poly = *pit;
|
---|
| 476 | for (vi = poly->mVertices.begin(); vi != poly->mVertices.end(); ++vi)
|
---|
| 477 | {
|
---|
| 478 | stream << (*vi).x << " " << (*vi).y << " " << (*vi).z;
|
---|
| 479 | stream << "," << endl;
|
---|
| 480 | }
|
---|
| 481 | }
|
---|
| 482 | stream << "\" >" << endl;
|
---|
| 483 | stream << "</Coordinate>" << endl;
|
---|
| 484 |
|
---|
| 485 | if (mWireframe)
|
---|
| 486 | stream << "</IndexedLineSet>" << endl;
|
---|
| 487 | else
|
---|
| 488 | stream << "</IndexedFaceSet>" << endl;
|
---|
| 489 |
|
---|
| 490 | stream << "</Shape>" << endl;
|
---|
| 491 | }
|
---|
| 492 |
|
---|
[1006] | 493 |
|
---|
[162] | 494 | bool
|
---|
| 495 | X3dExporter::ExportBox(const AxisAlignedBox3 &box)
|
---|
| 496 | {
|
---|
| 497 | Mesh *mesh = new Mesh;
|
---|
[547] | 498 | // add 8 vertices of the box
|
---|
[289] | 499 | int index = (int)mesh->mVertices.size();
|
---|
[162] | 500 | for (int i=0; i < 8; i++) {
|
---|
| 501 | Vector3 v;
|
---|
| 502 | box.GetVertex(i, v);
|
---|
| 503 | mesh->mVertices.push_back(v);
|
---|
| 504 | }
|
---|
| 505 |
|
---|
| 506 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
| 507 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
| 508 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
| 509 |
|
---|
| 510 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
| 511 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
| 512 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
| 513 |
|
---|
| 514 | ExportMesh(mesh);
|
---|
| 515 | delete mesh;
|
---|
| 516 | return true;
|
---|
| 517 | }
|
---|
| 518 |
|
---|
[1006] | 519 |
|
---|
[162] | 520 | bool
|
---|
[242] | 521 | X3dExporter::ExportBspTree(const BspTree &tree)
|
---|
[162] | 522 | {
|
---|
[242] | 523 | if (mExportRayDensity)
|
---|
| 524 | {
|
---|
[263] | 525 | return ExportBspTreeRayDensity(tree);
|
---|
[242] | 526 | }
|
---|
| 527 |
|
---|
[264] | 528 | bool savedWireframe = mWireframe;
|
---|
| 529 |
|
---|
| 530 | SetWireframe();
|
---|
| 531 |
|
---|
[396] | 532 | ExportBox(tree.GetBoundingBox());
|
---|
| 533 |
|
---|
[264] | 534 | if (!savedWireframe)
|
---|
| 535 | SetFilled();
|
---|
| 536 |
|
---|
[373] | 537 | // export view cells
|
---|
[482] | 538 | ExportBspLeaves(tree);
|
---|
[289] | 539 |
|
---|
[263] | 540 | return true;
|
---|
[242] | 541 | }
|
---|
| 542 |
|
---|
[418] | 543 |
|
---|
[1197] | 544 | bool X3dExporter::ExportKdTree(const KdTree &tree,
|
---|
| 545 | const bool exportGeometry)
|
---|
[242] | 546 | {
|
---|
| 547 | if (mExportRayDensity) {
|
---|
[191] | 548 | return ExportKdTreeRayDensity(tree);
|
---|
| 549 | }
|
---|
| 550 |
|
---|
[162] | 551 | stack<KdNode *> tStack;
|
---|
| 552 |
|
---|
| 553 | tStack.push(tree.GetRoot());
|
---|
| 554 |
|
---|
| 555 | Mesh *mesh = new Mesh;
|
---|
| 556 |
|
---|
| 557 | while (!tStack.empty()) {
|
---|
| 558 | KdNode *node = tStack.top();
|
---|
| 559 | tStack.pop();
|
---|
| 560 | AxisAlignedBox3 box = tree.GetBox(node);
|
---|
| 561 | // add 6 vertices of the box
|
---|
[289] | 562 | int index = (int)mesh->mVertices.size();
|
---|
[162] | 563 | for (int i=0; i < 8; i++) {
|
---|
| 564 | Vector3 v;
|
---|
| 565 | box.GetVertex(i, v);
|
---|
| 566 | mesh->mVertices.push_back(v);
|
---|
| 567 | }
|
---|
| 568 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
| 569 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
| 570 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
| 571 |
|
---|
| 572 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
| 573 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
| 574 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
| 575 |
|
---|
| 576 | if (!node->IsLeaf()) {
|
---|
| 577 | KdInterior *interior = (KdInterior *)node;
|
---|
| 578 | tStack.push(interior->mFront);
|
---|
| 579 | tStack.push(interior->mBack);
|
---|
| 580 | }
|
---|
[1197] | 581 | else if (exportGeometry)
|
---|
| 582 | {
|
---|
| 583 | SetFilled();
|
---|
| 584 | SetForcedMaterial(RandomMaterial());
|
---|
| 585 | ExportGeometry(dynamic_cast<KdLeaf *>(node)->mObjects);
|
---|
| 586 | SetWireframe();
|
---|
| 587 | }
|
---|
[162] | 588 | }
|
---|
| 589 |
|
---|
| 590 | ExportMesh(mesh);
|
---|
| 591 | delete mesh;
|
---|
[1006] | 592 |
|
---|
[162] | 593 | return true;
|
---|
| 594 | }
|
---|
| 595 |
|
---|
| 596 |
|
---|
[191] | 597 | bool
|
---|
[438] | 598 | X3dExporter::ExportVssTree(const VssTree &tree
|
---|
| 599 | )
|
---|
[434] | 600 | {
|
---|
| 601 | stack<VssTreeNode *> tStack;
|
---|
| 602 |
|
---|
| 603 | tStack.push(tree.GetRoot());
|
---|
| 604 |
|
---|
| 605 | Mesh *mesh = new Mesh;
|
---|
| 606 | VssRayContainer rays;
|
---|
| 607 |
|
---|
| 608 | while (!tStack.empty()) {
|
---|
| 609 |
|
---|
| 610 | VssTreeNode *node = tStack.top();
|
---|
| 611 | tStack.pop();
|
---|
| 612 |
|
---|
| 613 |
|
---|
| 614 | if (!node->IsLeaf()) {
|
---|
| 615 | VssTreeInterior *interior = (VssTreeInterior *)node;
|
---|
| 616 | tStack.push(interior->front);
|
---|
| 617 | tStack.push(interior->back);
|
---|
| 618 | } else {
|
---|
| 619 | VssTreeLeaf *leaf = (VssTreeLeaf *)node;
|
---|
| 620 | AxisAlignedBox3 box;
|
---|
| 621 | box = tree.GetBBox(leaf);
|
---|
[860] | 622 | IncludeBoxInMesh(box, *mesh);
|
---|
[434] | 623 |
|
---|
| 624 | if (tree.ValidLeaf(leaf)) {
|
---|
| 625 |
|
---|
| 626 | Vector3 origin = box.Center();
|
---|
| 627 | box = tree.GetDirBBox(leaf);
|
---|
| 628 | VssRay *ray;
|
---|
| 629 |
|
---|
[1004] | 630 | const int indices[][2] = {{0,0}, {0,1}, {1,1}, {1,0}};
|
---|
[434] | 631 | MeshInstance dummy(mesh);
|
---|
| 632 | for (int i=0; i < 4; i++) {
|
---|
| 633 | // Vector3 v = box.GetVertex(indices[i][0], indices[i][1], 0);
|
---|
| 634 | Vector3 v = box.Center();
|
---|
| 635 |
|
---|
| 636 | Vector3 direction = VssRay::GetDirection(v.x, v.y);
|
---|
[438] | 637 | if (Magnitude(direction) > Limits::Small)
|
---|
| 638 | direction.Normalize();
|
---|
| 639 | else
|
---|
| 640 | direction = Vector3(0, 1, 0);
|
---|
[435] | 641 | float k = 100.0f*leaf->GetImportance();
|
---|
[434] | 642 | // get 4 corners of the ray directions
|
---|
| 643 |
|
---|
| 644 | ray = new VssRay(origin, origin + (direction*k), NULL, &dummy);
|
---|
| 645 | rays.push_back(ray);
|
---|
| 646 | }
|
---|
| 647 | }
|
---|
| 648 | }
|
---|
| 649 | }
|
---|
| 650 |
|
---|
| 651 | ExportMesh(mesh);
|
---|
[438] | 652 | ExportRays(rays);
|
---|
| 653 | CLEAR_CONTAINER(rays);
|
---|
[434] | 654 | delete mesh;
|
---|
| 655 | return true;
|
---|
| 656 | }
|
---|
| 657 |
|
---|
| 658 | bool
|
---|
[438] | 659 | X3dExporter::ExportVssTree2(const VssTree &tree,
|
---|
| 660 | const Vector3 direction
|
---|
| 661 | )
|
---|
| 662 | {
|
---|
| 663 | stack<VssTreeNode *> tStack;
|
---|
| 664 |
|
---|
| 665 |
|
---|
| 666 | mUseForcedMaterial = true;
|
---|
| 667 |
|
---|
| 668 | Vector3 dirParam;
|
---|
| 669 |
|
---|
| 670 | dirParam.x = VssRay::GetDirParam(0, Normalize(direction));
|
---|
| 671 | dirParam.y = VssRay::GetDirParam(1, Normalize(direction));
|
---|
| 672 |
|
---|
| 673 | float maxImportance = 0.0f;
|
---|
| 674 | tStack.push(tree.GetRoot());
|
---|
| 675 | while (!tStack.empty()) {
|
---|
| 676 |
|
---|
| 677 | VssTreeNode *node = tStack.top();
|
---|
| 678 | tStack.pop();
|
---|
| 679 |
|
---|
| 680 | if (!node->IsLeaf()) {
|
---|
| 681 | VssTreeInterior *interior = (VssTreeInterior *)node;
|
---|
| 682 | if (interior->axis < 3) {
|
---|
| 683 | tStack.push(interior->front);
|
---|
| 684 | tStack.push(interior->back);
|
---|
| 685 | } else {
|
---|
| 686 | if (dirParam[interior->axis-3] < interior->position)
|
---|
| 687 | tStack.push(interior->back);
|
---|
| 688 | else
|
---|
| 689 | tStack.push(interior->front);
|
---|
| 690 | }
|
---|
| 691 | } else {
|
---|
| 692 | VssTreeLeaf *leaf = (VssTreeLeaf *)node;
|
---|
| 693 | if (tree.ValidLeaf(leaf)) {
|
---|
| 694 | float i = leaf->GetImportance();
|
---|
| 695 | if (i > maxImportance)
|
---|
| 696 | maxImportance = i;
|
---|
| 697 | }
|
---|
| 698 | }
|
---|
| 699 | }
|
---|
| 700 |
|
---|
| 701 | tStack.push(tree.GetRoot());
|
---|
| 702 | while (!tStack.empty()) {
|
---|
| 703 |
|
---|
| 704 | VssTreeNode *node = tStack.top();
|
---|
| 705 | tStack.pop();
|
---|
| 706 |
|
---|
| 707 |
|
---|
| 708 | if (!node->IsLeaf()) {
|
---|
| 709 | VssTreeInterior *interior = (VssTreeInterior *)node;
|
---|
| 710 | if (interior->axis < 3) {
|
---|
| 711 | tStack.push(interior->front);
|
---|
| 712 | tStack.push(interior->back);
|
---|
| 713 | } else {
|
---|
| 714 | if (dirParam[interior->axis-3] < interior->position)
|
---|
| 715 | tStack.push(interior->back);
|
---|
| 716 | else
|
---|
| 717 | tStack.push(interior->front);
|
---|
| 718 | }
|
---|
| 719 | } else {
|
---|
| 720 | VssTreeLeaf *leaf = (VssTreeLeaf *)node;
|
---|
| 721 | if (tree.ValidLeaf(leaf)) {
|
---|
| 722 | AxisAlignedBox3 box;
|
---|
| 723 | box = tree.GetBBox(leaf);
|
---|
| 724 | Mesh *mesh = new Mesh;
|
---|
[860] | 725 | IncludeBoxInMesh(box, *mesh);
|
---|
[438] | 726 |
|
---|
| 727 | // get 4 corners of the ray directions
|
---|
| 728 |
|
---|
| 729 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
| 730 | mForcedMaterial.mDiffuseColor.r = leaf->GetImportance()/maxImportance;
|
---|
| 731 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
| 732 |
|
---|
| 733 | ExportMesh(mesh);
|
---|
| 734 | delete mesh;
|
---|
| 735 | }
|
---|
| 736 | }
|
---|
| 737 | }
|
---|
| 738 |
|
---|
| 739 | mUseForcedMaterial = false;
|
---|
| 740 |
|
---|
| 741 | return true;
|
---|
| 742 | }
|
---|
| 743 |
|
---|
| 744 | bool
|
---|
[263] | 745 | X3dExporter::ExportBspTreeRayDensity(const BspTree &tree)
|
---|
| 746 | {
|
---|
[311] | 747 | stack<BspNode *> tStack;
|
---|
| 748 |
|
---|
| 749 | tStack.push(tree.GetRoot());
|
---|
| 750 |
|
---|
| 751 | bool fm = mUseForcedMaterial;
|
---|
| 752 |
|
---|
| 753 | mUseForcedMaterial = true;
|
---|
| 754 |
|
---|
| 755 | mForcedMaterial.mDiffuseColor.g = 1.0f;
|
---|
| 756 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
| 757 |
|
---|
| 758 | while (!tStack.empty())
|
---|
| 759 | {
|
---|
| 760 | BspNode *node = tStack.top();
|
---|
| 761 | tStack.pop();
|
---|
| 762 |
|
---|
| 763 | if (node->IsLeaf())
|
---|
| 764 | {
|
---|
| 765 | ViewCell *vc = dynamic_cast<BspLeaf *>(node)->GetViewCell();
|
---|
[580] | 766 | #if 0
|
---|
[311] | 767 | // set the mesh material according to the ray density
|
---|
| 768 | if (vc->mPassingRays.mRays)
|
---|
| 769 | {
|
---|
[313] | 770 | float importance =
|
---|
[438] | 771 | vc->mPassingRays.mContributions / (float)vc->mPassingRays.mRays;
|
---|
| 772 |
|
---|
[311] | 773 | mForcedMaterial.mDiffuseColor.r = importance;
|
---|
| 774 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
| 775 | ExportViewCell(vc);
|
---|
[580] | 776 |
|
---|
[438] | 777 | }
|
---|
[580] | 778 | #endif
|
---|
| 779 | }
|
---|
| 780 | else
|
---|
[311] | 781 | {
|
---|
| 782 | BspInterior *interior = (BspInterior *)node;
|
---|
| 783 | tStack.push(interior->GetFront());
|
---|
| 784 | tStack.push(interior->GetBack());
|
---|
| 785 | }
|
---|
| 786 | }
|
---|
| 787 |
|
---|
| 788 | // restore the state of forced material
|
---|
| 789 | mUseForcedMaterial = fm;
|
---|
| 790 |
|
---|
[263] | 791 | return true;
|
---|
| 792 | }
|
---|
| 793 |
|
---|
| 794 | bool
|
---|
[191] | 795 | X3dExporter::ExportKdTreeRayDensity(const KdTree &tree)
|
---|
| 796 | {
|
---|
| 797 | stack<KdNode *> tStack;
|
---|
| 798 |
|
---|
| 799 | tStack.push(tree.GetRoot());
|
---|
| 800 |
|
---|
| 801 | bool fm = mUseForcedMaterial;
|
---|
| 802 | mUseForcedMaterial = true;
|
---|
| 803 | mForcedMaterial.mDiffuseColor.g = 1.0f;
|
---|
| 804 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
| 805 | while (!tStack.empty()) {
|
---|
| 806 | KdNode *node = tStack.top();
|
---|
| 807 | tStack.pop();
|
---|
| 808 | if (node->IsLeaf()) {
|
---|
| 809 | AxisAlignedBox3 box = tree.GetBox(node);
|
---|
| 810 | Mesh *mesh = new Mesh;
|
---|
| 811 |
|
---|
| 812 | // add 6 vertices of the box
|
---|
[289] | 813 | int index = (int)mesh->mVertices.size();
|
---|
[191] | 814 | for (int i=0; i < 8; i++) {
|
---|
| 815 | Vector3 v;
|
---|
| 816 | box.GetVertex(i, v);
|
---|
| 817 | mesh->mVertices.push_back(v);
|
---|
| 818 | }
|
---|
| 819 | mesh->AddFace(new Face(index + 0, index + 1, index + 3, index + 2) );
|
---|
| 820 | mesh->AddFace(new Face(index + 0, index + 2, index + 6, index + 4) );
|
---|
| 821 | mesh->AddFace(new Face(index + 4, index + 6, index + 7, index + 5) );
|
---|
| 822 |
|
---|
| 823 | mesh->AddFace(new Face(index + 3, index + 1, index + 5, index + 7) );
|
---|
| 824 | mesh->AddFace(new Face(index + 0, index + 4, index + 5, index + 1) );
|
---|
| 825 | mesh->AddFace(new Face(index + 2, index + 3, index + 7, index + 6) );
|
---|
| 826 |
|
---|
| 827 |
|
---|
| 828 | // set the mesh material according to the ray density
|
---|
| 829 | KdLeaf *leaf = (KdLeaf *) node;
|
---|
| 830 | if (leaf->mPassingRays.mRays) {
|
---|
| 831 | float importance = leaf->mPassingRays.mContributions/(float)leaf->mPassingRays.mRays;
|
---|
| 832 | // float importance = leaf->mPassingRays.mContributions/1000.0f;
|
---|
| 833 | // float importance = leaf->mPassingRays.mRays/1000.0f;
|
---|
| 834 | ///(float)leaf->mPassingRays.mRays;
|
---|
| 835 | // mForcedMaterial.mDiffuseColor.r = log10(leaf->mPassingRays.mRays)/3.0f;
|
---|
| 836 | mForcedMaterial.mDiffuseColor.r = importance;
|
---|
| 837 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
| 838 | ExportMesh(mesh);
|
---|
| 839 | }
|
---|
| 840 | delete mesh;
|
---|
| 841 | } else {
|
---|
| 842 | KdInterior *interior = (KdInterior *)node;
|
---|
| 843 | tStack.push(interior->mFront);
|
---|
| 844 | tStack.push(interior->mBack);
|
---|
| 845 | }
|
---|
| 846 | }
|
---|
| 847 | // restore the state of forced material
|
---|
| 848 | mUseForcedMaterial = fm;
|
---|
| 849 | return true;
|
---|
| 850 | }
|
---|
[312] | 851 |
|
---|
[329] | 852 |
|
---|
[312] | 853 | struct BspSplitData
|
---|
| 854 | {
|
---|
[372] | 855 | /// the current node
|
---|
| 856 | BspNode *mNode;
|
---|
| 857 |
|
---|
[449] | 858 | vector<Plane3> mPlanes;
|
---|
[372] | 859 | vector<bool> mSides;
|
---|
| 860 | bool mIsFront;
|
---|
[383] | 861 | int mDepth;
|
---|
[372] | 862 |
|
---|
[313] | 863 | BspSplitData(BspNode *node):
|
---|
[383] | 864 | mNode(node), mIsFront(false), mDepth(0)
|
---|
[372] | 865 | {};
|
---|
[383] | 866 |
|
---|
[312] | 867 | BspSplitData(BspNode *node,
|
---|
[449] | 868 | vector<Plane3> planes,
|
---|
[329] | 869 | vector<bool> sides,
|
---|
[383] | 870 | const bool isFront,
|
---|
| 871 | const int depth):
|
---|
| 872 | mNode(node), mPlanes(planes), mSides(sides),
|
---|
| 873 | mIsFront(isFront), mDepth(depth)
|
---|
[312] | 874 | {};
|
---|
| 875 | };
|
---|
| 876 |
|
---|
[383] | 877 | void X3dExporter::ExportLeavesGeometry(const BspTree &tree,
|
---|
| 878 | const vector<BspLeaf *> &leaves)
|
---|
[360] | 879 | {
|
---|
| 880 | vector<BspLeaf *>::const_iterator it, it_end = leaves.end();
|
---|
| 881 |
|
---|
| 882 | for (it = leaves.begin(); it != it_end; ++ it)
|
---|
| 883 | {
|
---|
[503] | 884 | BspNodeGeometry geom;
|
---|
| 885 | tree.ConstructGeometry(*it, geom);
|
---|
[360] | 886 |
|
---|
[678] | 887 | ExportPolygons(geom.GetPolys());
|
---|
[360] | 888 | }
|
---|
| 889 | }
|
---|
| 890 |
|
---|
[449] | 891 | void X3dExporter::ExportBspNodeSplits(BspNode *root,
|
---|
| 892 | const AxisAlignedBox3 &box,
|
---|
| 893 | const bool exportDepth,
|
---|
| 894 | const bool epsilon)
|
---|
[372] | 895 | {
|
---|
| 896 | std::stack<BspSplitData> tStack;
|
---|
| 897 |
|
---|
[449] | 898 | BspSplitData tData(root);
|
---|
[372] | 899 | tStack.push(tData);
|
---|
| 900 |
|
---|
| 901 | PolygonContainer polys;
|
---|
[383] | 902 | vector <int> depths;
|
---|
[372] | 903 |
|
---|
[383] | 904 | int maxDepth = 0;
|
---|
| 905 |
|
---|
[372] | 906 | while (!tStack.empty())
|
---|
| 907 | {
|
---|
| 908 | // filter polygons donw the tree
|
---|
| 909 | BspSplitData tData = tStack.top();
|
---|
| 910 | tStack.pop();
|
---|
| 911 |
|
---|
[383] | 912 | if (tData.mNode->IsLeaf())
|
---|
[372] | 913 | {
|
---|
[383] | 914 | if (tData.mDepth > maxDepth)
|
---|
| 915 | maxDepth = tData.mDepth;
|
---|
| 916 | }
|
---|
| 917 | else
|
---|
| 918 | {
|
---|
[372] | 919 | BspInterior *interior = dynamic_cast<BspInterior *>(tData.mNode);
|
---|
| 920 |
|
---|
[383] | 921 | // add current side of split plane
|
---|
[449] | 922 | if (tData.mNode != root)
|
---|
[383] | 923 | tData.mSides.push_back(tData.mIsFront);
|
---|
[372] | 924 |
|
---|
| 925 | // bounded plane is added to the polygons
|
---|
[383] | 926 | Polygon3 *planePoly =
|
---|
[449] | 927 | box.CrossSection(interior->GetPlane());
|
---|
[372] | 928 |
|
---|
| 929 | // do all the splits with the previous planes
|
---|
| 930 | for (int i = 0; i < (int)tData.mPlanes.size(); ++ i)
|
---|
[436] | 931 | {
|
---|
[449] | 932 | if (planePoly->ClassifyPlane(tData.mPlanes[i], epsilon)
|
---|
| 933 | == Polygon3::SPLIT)
|
---|
[372] | 934 | {
|
---|
| 935 | Polygon3 *frontPoly = new Polygon3();
|
---|
| 936 | Polygon3 *backPoly = new Polygon3();
|
---|
| 937 |
|
---|
[449] | 938 | planePoly->Split(tData.mPlanes[i],
|
---|
| 939 | *frontPoly,
|
---|
| 940 | *backPoly,
|
---|
| 941 | epsilon);
|
---|
| 942 |
|
---|
[372] | 943 | DEL_PTR(planePoly);
|
---|
| 944 |
|
---|
| 945 | if(tData.mSides[i] == true)
|
---|
| 946 | {
|
---|
| 947 | planePoly = frontPoly;
|
---|
| 948 | DEL_PTR(backPoly);
|
---|
| 949 | }
|
---|
| 950 | else
|
---|
| 951 | {
|
---|
| 952 | planePoly = backPoly;
|
---|
| 953 | DEL_PTR(frontPoly);
|
---|
| 954 | }
|
---|
| 955 | }
|
---|
| 956 | }
|
---|
| 957 |
|
---|
| 958 | tData.mPlanes.push_back(interior->GetPlane()); // add plane to split planes
|
---|
| 959 |
|
---|
[449] | 960 | if (planePoly->Valid(epsilon))
|
---|
[383] | 961 | {
|
---|
[372] | 962 | polys.push_back(planePoly);
|
---|
[383] | 963 | depths.push_back(tData.mDepth);
|
---|
| 964 | }
|
---|
[372] | 965 | else
|
---|
| 966 | DEL_PTR(planePoly);
|
---|
| 967 |
|
---|
| 968 | // push the children on the stack
|
---|
[383] | 969 | tStack.push(BspSplitData(interior->GetFront(), tData.mPlanes,
|
---|
| 970 | tData.mSides, true, tData.mDepth + 1));
|
---|
| 971 | tStack.push(BspSplitData(interior->GetBack(), tData.mPlanes,
|
---|
| 972 | tData.mSides, false, tData.mDepth + 1));
|
---|
[372] | 973 | }
|
---|
[383] | 974 | }
|
---|
| 975 |
|
---|
| 976 | if (maxDepth > 0)
|
---|
| 977 | {
|
---|
| 978 | mUseForcedMaterial = true;
|
---|
| 979 |
|
---|
| 980 | for (int i = 0; i < (int)polys.size(); ++ i)
|
---|
| 981 | {
|
---|
| 982 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
| 983 | float importance = (float)depths[i]/ (float)maxDepth;
|
---|
| 984 |
|
---|
| 985 | mForcedMaterial.mDiffuseColor.r = importance;
|
---|
| 986 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
| 987 |
|
---|
| 988 | ExportPolygon(polys[i]);
|
---|
| 989 | }
|
---|
| 990 | }
|
---|
| 991 | else
|
---|
| 992 | {
|
---|
| 993 | ExportPolygons(polys);
|
---|
| 994 | }
|
---|
| 995 |
|
---|
[372] | 996 | CLEAR_CONTAINER(polys);
|
---|
| 997 | }
|
---|
| 998 |
|
---|
[449] | 999 | void X3dExporter::ExportBspSplits(const BspTree &tree,
|
---|
| 1000 | const bool exportDepth)
|
---|
| 1001 | {
|
---|
| 1002 | ExportBspNodeSplits(tree.GetRoot(),
|
---|
| 1003 | tree.GetBoundingBox(),
|
---|
| 1004 | exportDepth,
|
---|
| 1005 | tree.GetEpsilon());
|
---|
| 1006 | }
|
---|
| 1007 |
|
---|
| 1008 | void X3dExporter::ExportBspSplits(const VspBspTree &tree,
|
---|
| 1009 | const bool exportDepth)
|
---|
| 1010 | {
|
---|
| 1011 | ExportBspNodeSplits(tree.GetRoot(),
|
---|
| 1012 | tree.GetBoundingBox(),
|
---|
| 1013 | exportDepth,
|
---|
| 1014 | tree.GetEpsilon());
|
---|
| 1015 | }
|
---|
| 1016 |
|
---|
[372] | 1017 | void X3dExporter::ExportBspSplitPlanes(const BspTree &tree)
|
---|
| 1018 | {
|
---|
| 1019 | std::stack<BspNode *> tStack;
|
---|
| 1020 |
|
---|
| 1021 | tStack.push(tree.GetRoot());
|
---|
| 1022 |
|
---|
| 1023 | PolygonContainer polys;
|
---|
| 1024 |
|
---|
| 1025 | while (!tStack.empty())
|
---|
| 1026 | {
|
---|
| 1027 | // filter polygons donw the tree
|
---|
| 1028 | BspNode *node = tStack.top();
|
---|
| 1029 | tStack.pop();
|
---|
| 1030 |
|
---|
| 1031 | if (!node->IsLeaf())
|
---|
| 1032 | {
|
---|
| 1033 | BspInterior *interior = dynamic_cast<BspInterior *>(node);
|
---|
| 1034 |
|
---|
| 1035 | // bounded plane is added to the polygons
|
---|
[449] | 1036 | polys.push_back(tree.GetBoundingBox().CrossSection(interior->GetPlane()));
|
---|
[372] | 1037 |
|
---|
| 1038 | // push the children on the stack
|
---|
| 1039 | tStack.push(interior->GetBack());
|
---|
| 1040 | tStack.push(interior->GetFront());
|
---|
| 1041 | }
|
---|
| 1042 | }
|
---|
| 1043 |
|
---|
| 1044 | ExportPolygons(polys);
|
---|
| 1045 | CLEAR_CONTAINER(polys);
|
---|
| 1046 | }
|
---|
[440] | 1047 |
|
---|
| 1048 |
|
---|
[449] | 1049 | void X3dExporter::ExportGeometry(const ObjectContainer &objects)
|
---|
[440] | 1050 | {
|
---|
[712] | 1051 | ObjectContainer::const_iterator oit, oit_end = objects.end();
|
---|
| 1052 |
|
---|
| 1053 | if (1)
|
---|
| 1054 | {
|
---|
| 1055 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
| 1056 | {
|
---|
[726] | 1057 | if (0)
|
---|
[712] | 1058 | SetForcedMaterial(RandomMaterial());
|
---|
[726] | 1059 |
|
---|
[712] | 1060 | ExportIntersectable(*oit);
|
---|
| 1061 | }
|
---|
| 1062 |
|
---|
| 1063 | return;
|
---|
| 1064 | }
|
---|
| 1065 |
|
---|
| 1066 | // hack: all object exported as one mesh
|
---|
[685] | 1067 | PolygonContainer polys;
|
---|
[712] | 1068 |
|
---|
| 1069 | for (oit = objects.begin(); oit != oit_end; ++ oit)
|
---|
| 1070 | {
|
---|
| 1071 | polys.push_back(new Polygon3(dynamic_cast<MeshInstance *>(*oit)->GetMesh()->mVertices));
|
---|
[685] | 1072 | }
|
---|
| 1073 |
|
---|
| 1074 | Mesh dummyMesh;
|
---|
[712] | 1075 | PolygonContainer::const_iterator pit, pit_end = polys.end();
|
---|
[685] | 1076 |
|
---|
[712] | 1077 | for (pit = polys.begin(); pit != pit_end; ++ pit)
|
---|
| 1078 | {
|
---|
[840] | 1079 | Polygon3 *poly = (*pit);
|
---|
| 1080 | IncludePolyInMesh(*poly, dummyMesh);
|
---|
[712] | 1081 | }
|
---|
| 1082 |
|
---|
[685] | 1083 | ExportMesh(&dummyMesh);
|
---|
[712] | 1084 |
|
---|
[685] | 1085 | CLEAR_CONTAINER(polys);
|
---|
[440] | 1086 | }
|
---|
[449] | 1087 |
|
---|
| 1088 |
|
---|
[450] | 1089 | bool
|
---|
| 1090 | X3dExporter::ExportRssTree2(const RssTree &tree,
|
---|
| 1091 | const Vector3 direction
|
---|
| 1092 | )
|
---|
| 1093 | {
|
---|
| 1094 | stack<RssTreeNode *> tStack;
|
---|
| 1095 |
|
---|
| 1096 |
|
---|
| 1097 | mUseForcedMaterial = true;
|
---|
| 1098 |
|
---|
| 1099 | Vector3 dirParam;
|
---|
| 1100 |
|
---|
| 1101 | dirParam.x = VssRay::GetDirParam(0, Normalize(direction));
|
---|
| 1102 | dirParam.y = VssRay::GetDirParam(1, Normalize(direction));
|
---|
| 1103 |
|
---|
| 1104 | float maxImportance = 0.0f;
|
---|
| 1105 | tStack.push(tree.GetRoot());
|
---|
| 1106 | while (!tStack.empty()) {
|
---|
| 1107 |
|
---|
| 1108 | RssTreeNode *node = tStack.top();
|
---|
| 1109 | tStack.pop();
|
---|
| 1110 |
|
---|
| 1111 | if (!node->IsLeaf()) {
|
---|
| 1112 | RssTreeInterior *interior = (RssTreeInterior *)node;
|
---|
| 1113 | if (interior->axis < 3) {
|
---|
| 1114 | tStack.push(interior->front);
|
---|
| 1115 | tStack.push(interior->back);
|
---|
| 1116 | } else {
|
---|
| 1117 | if (dirParam[interior->axis-3] < interior->position)
|
---|
| 1118 | tStack.push(interior->back);
|
---|
| 1119 | else
|
---|
| 1120 | tStack.push(interior->front);
|
---|
| 1121 | }
|
---|
| 1122 | } else {
|
---|
| 1123 | RssTreeLeaf *leaf = (RssTreeLeaf *)node;
|
---|
| 1124 | if (tree.ValidLeaf(leaf)) {
|
---|
| 1125 | float i = leaf->GetImportance();
|
---|
| 1126 | if (i > maxImportance)
|
---|
| 1127 | maxImportance = i;
|
---|
| 1128 | }
|
---|
| 1129 | }
|
---|
| 1130 | }
|
---|
| 1131 |
|
---|
| 1132 | tStack.push(tree.GetRoot());
|
---|
| 1133 | while (!tStack.empty()) {
|
---|
| 1134 |
|
---|
| 1135 | RssTreeNode *node = tStack.top();
|
---|
| 1136 | tStack.pop();
|
---|
| 1137 |
|
---|
| 1138 |
|
---|
| 1139 | if (!node->IsLeaf()) {
|
---|
| 1140 | RssTreeInterior *interior = (RssTreeInterior *)node;
|
---|
| 1141 | if (interior->axis < 3) {
|
---|
| 1142 | tStack.push(interior->front);
|
---|
| 1143 | tStack.push(interior->back);
|
---|
| 1144 | } else {
|
---|
| 1145 | if (dirParam[interior->axis-3] < interior->position)
|
---|
| 1146 | tStack.push(interior->back);
|
---|
| 1147 | else
|
---|
| 1148 | tStack.push(interior->front);
|
---|
| 1149 | }
|
---|
| 1150 | } else {
|
---|
| 1151 | RssTreeLeaf *leaf = (RssTreeLeaf *)node;
|
---|
| 1152 | if (tree.ValidLeaf(leaf)) {
|
---|
| 1153 | AxisAlignedBox3 box;
|
---|
| 1154 | box = tree.GetShrankedBBox(leaf);
|
---|
| 1155 | Mesh *mesh = new Mesh;
|
---|
[860] | 1156 | IncludeBoxInMesh(box, *mesh);
|
---|
[450] | 1157 |
|
---|
| 1158 | // get 4 corners of the ray directions
|
---|
| 1159 |
|
---|
| 1160 | mForcedMaterial.mDiffuseColor.b = 1.0f;
|
---|
| 1161 | mForcedMaterial.mDiffuseColor.r = leaf->GetImportance()/maxImportance;
|
---|
| 1162 | mForcedMaterial.mDiffuseColor.g = 1.0f - mForcedMaterial.mDiffuseColor.r;
|
---|
| 1163 |
|
---|
| 1164 | ExportMesh(mesh);
|
---|
| 1165 | delete mesh;
|
---|
| 1166 | }
|
---|
| 1167 | }
|
---|
| 1168 | }
|
---|
| 1169 |
|
---|
| 1170 | mUseForcedMaterial = false;
|
---|
| 1171 |
|
---|
| 1172 | return true;
|
---|
| 1173 | }
|
---|
[459] | 1174 |
|
---|
| 1175 |
|
---|
| 1176 | void
|
---|
| 1177 | X3dExporter::ExportViewpoint(const Vector3 &point,
|
---|
| 1178 | const Vector3 &direction)
|
---|
| 1179 | {
|
---|
| 1180 | stream<<"<Viewpoint "<<endl;
|
---|
| 1181 |
|
---|
| 1182 | stream<<"position=\""<<point.x<<" "<<point.y<<" "<<point.z<<"\""<<endl;
|
---|
| 1183 | // stream<<"orientation "<<direction.x<<direction.y<<direction.z<<endl;
|
---|
| 1184 |
|
---|
| 1185 | stream<<">"<<endl;
|
---|
| 1186 | stream<<"</Viewpoint>"<<endl;
|
---|
[535] | 1187 |
|
---|
[459] | 1188 | }
|
---|
[535] | 1189 |
|
---|
| 1190 |
|
---|
| 1191 |
|
---|
| 1192 | void X3dExporter::ExportBeam(const Beam &beam, const AxisAlignedBox3 &box)
|
---|
| 1193 | {
|
---|
[540] | 1194 | if (beam.mMesh)
|
---|
| 1195 | {
|
---|
| 1196 | ExportMesh(beam.mMesh);
|
---|
| 1197 | return;
|
---|
| 1198 | }
|
---|
| 1199 |
|
---|
[535] | 1200 | PolygonContainer polys;
|
---|
| 1201 | //ExportBox(beam.mBox);
|
---|
| 1202 |
|
---|
[540] | 1203 | const float zfar = 2.0f * Magnitude(box.Diagonal());
|
---|
| 1204 |
|
---|
| 1205 | // box should not never remove part of beam polygons
|
---|
[564] | 1206 | Vector3 bmin = beam.mBox.Min() - Vector3(zfar * 2.0f);
|
---|
| 1207 | Vector3 bmax = beam.mBox.Max() + Vector3(zfar * 2.0f);
|
---|
[540] | 1208 |
|
---|
| 1209 | AxisAlignedBox3 bbox(bmin, bmax);
|
---|
| 1210 | Plane3 fplane;
|
---|
| 1211 | fplane.mNormal = -beam.mPlanes[0].mNormal;
|
---|
| 1212 |
|
---|
| 1213 | fplane.mD = beam.mPlanes[0].mD - zfar - 1.0f;
|
---|
| 1214 |
|
---|
| 1215 | vector<Plane3> planes = beam.mPlanes;
|
---|
| 1216 | planes.push_back(fplane);
|
---|
| 1217 |
|
---|
| 1218 | for (int i = 0; i < planes.size(); ++ i)
|
---|
[535] | 1219 | {
|
---|
[540] | 1220 | Polygon3 *poly = bbox.CrossSection(planes[i]);
|
---|
| 1221 | if (!poly->Valid(Limits::Small))
|
---|
| 1222 | DEL_PTR(poly);
|
---|
[535] | 1223 |
|
---|
[540] | 1224 | for (int j = 0; (j < planes.size()) && poly; ++ j)
|
---|
[535] | 1225 | {
|
---|
| 1226 | if (j != i)
|
---|
| 1227 | {
|
---|
| 1228 | Polygon3 *front = new Polygon3();
|
---|
| 1229 | Polygon3 *back = new Polygon3();
|
---|
| 1230 |
|
---|
[540] | 1231 | poly->Split(planes[j], *front, *back, Limits::Small);
|
---|
[535] | 1232 | DEL_PTR(poly);
|
---|
| 1233 | DEL_PTR(front);
|
---|
| 1234 |
|
---|
| 1235 | if (!back->Valid(Limits::Small))
|
---|
| 1236 | DEL_PTR(back);
|
---|
| 1237 | poly = back;
|
---|
| 1238 | }
|
---|
| 1239 | }
|
---|
| 1240 | if (poly)
|
---|
| 1241 | polys.push_back(poly);
|
---|
| 1242 | }
|
---|
| 1243 |
|
---|
| 1244 | ExportPolygons(polys);
|
---|
| 1245 | CLEAR_CONTAINER(polys);
|
---|
[685] | 1246 | }
|
---|
[860] | 1247 |
|
---|
[1106] | 1248 |
|
---|
| 1249 | }
|
---|
| 1250 |
|
---|