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