[508] | 1 | // ---------------------------------------------------------------------------
|
---|
| 2 | // Includes for all the program files to see
|
---|
| 3 | // ---------------------------------------------------------------------------
|
---|
| 4 | #include <string.h>
|
---|
| 5 | #include <stdlib.h>
|
---|
| 6 | #include <iostream>
|
---|
| 7 | using namespace std;
|
---|
| 8 | #include <xercesc/util/PlatformUtils.hpp>
|
---|
| 9 |
|
---|
| 10 | // ---------------------------------------------------------------------------
|
---|
| 11 | // Includes
|
---|
| 12 | // ---------------------------------------------------------------------------
|
---|
| 13 | #include <xercesc/framework/StdInInputSource.hpp>
|
---|
| 14 | #include <xercesc/parsers/SAXParser.hpp>
|
---|
| 15 | #include <xercesc/util/OutOfMemoryException.hpp>
|
---|
| 16 |
|
---|
| 17 | // ---------------------------------------------------------------------------
|
---|
| 18 | // Includes
|
---|
| 19 | // ---------------------------------------------------------------------------
|
---|
| 20 | #include <xercesc/sax/AttributeList.hpp>
|
---|
| 21 | #include <xercesc/sax/SAXParseException.hpp>
|
---|
| 22 | #include <xercesc/sax/SAXException.hpp>
|
---|
| 23 |
|
---|
| 24 | #include "ViewCellsParser.h"
|
---|
| 25 |
|
---|
| 26 | #include "ViewCellsParserXerces.h"
|
---|
| 27 | #include "Mesh.h"
|
---|
| 28 | #include "VspBspTree.h"
|
---|
| 29 | #include "ViewCellBsp.h"
|
---|
| 30 | #include "ViewCellsManager.h"
|
---|
[971] | 31 | #include "GzFileInputSource.h"
|
---|
[508] | 32 |
|
---|
[863] | 33 | namespace GtpVisibilityPreprocessor {
|
---|
[860] | 34 |
|
---|
| 35 |
|
---|
[508] | 36 | // ---------------------------------------------------------------------------
|
---|
| 37 | // Local data
|
---|
| 38 | //
|
---|
| 39 | // doNamespaces
|
---|
| 40 | // Indicates whether namespace processing should be enabled or not.
|
---|
| 41 | // The default is no, but -n overrides that.
|
---|
| 42 | //
|
---|
| 43 | // doSchema
|
---|
| 44 | // Indicates whether schema processing should be enabled or not.
|
---|
| 45 | // The default is no, but -s overrides that.
|
---|
| 46 | //
|
---|
| 47 | // schemaFullChecking
|
---|
| 48 | // Indicates whether full schema constraint checking should be enabled or not.
|
---|
| 49 | // The default is no, but -s overrides that.
|
---|
| 50 | //
|
---|
| 51 | // valScheme
|
---|
| 52 | // Indicates what validation scheme to use. It defaults to 'auto', but
|
---|
| 53 | // can be set via the -v= command.
|
---|
| 54 | // ---------------------------------------------------------------------------
|
---|
| 55 | static bool doNamespaces = false;
|
---|
| 56 | static bool doSchema = false;
|
---|
| 57 | static bool schemaFullChecking = false;
|
---|
| 58 | static SAXParser::ValSchemes valScheme = SAXParser::Val_Auto;
|
---|
| 59 |
|
---|
| 60 |
|
---|
| 61 |
|
---|
| 62 |
|
---|
| 63 |
|
---|
| 64 | // ---------------------------------------------------------------------------
|
---|
| 65 | // StdInParseHandlers: Constructors and Destructor
|
---|
| 66 | // ---------------------------------------------------------------------------
|
---|
[938] | 67 | ViewCellsParseHandlers::ViewCellsParseHandlers(ObjectContainer *objects,
|
---|
[1004] | 68 | BoundingBoxConverter *bconverter):
|
---|
[508] | 69 | mElementCount(0)
|
---|
| 70 | , mAttrCount(0)
|
---|
| 71 | , mCharacterCount(0)
|
---|
| 72 | , mSpaceCount(0)
|
---|
[577] | 73 | , mViewCellsManager(NULL)
|
---|
| 74 | , mVspBspTree(NULL)
|
---|
| 75 | , mBspTree(NULL)
|
---|
[651] | 76 | , mViewCellsTree(NULL)
|
---|
| 77 | , mParseViewCells(true)
|
---|
| 78 | , mCurrentViewCell(NULL)
|
---|
| 79 | , mCurrentBspNode(NULL)
|
---|
[931] | 80 | , mObjects(objects)
|
---|
| 81 | , mBoundingBoxConverter(bconverter)
|
---|
[975] | 82 | {
|
---|
[508] | 83 | }
|
---|
| 84 |
|
---|
[575] | 85 |
|
---|
[508] | 86 | ViewCellsParseHandlers::~ViewCellsParseHandlers()
|
---|
| 87 | {
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 |
|
---|
| 91 | // ---------------------------------------------------------------------------
|
---|
| 92 | // StdInParseHandlers: Implementation of the SAX DocumentHandler interface
|
---|
| 93 | // ---------------------------------------------------------------------------
|
---|
| 94 |
|
---|
| 95 |
|
---|
| 96 | void ViewCellsParseHandlers::endElement(const XMLCh* const name)
|
---|
| 97 | {
|
---|
| 98 | StrX lname(name);
|
---|
| 99 | string element(lname.LocalForm());
|
---|
[651] | 100 |
|
---|
[508] | 101 | if (element == "ViewCells")
|
---|
| 102 | EndViewCells();
|
---|
[651] | 103 |
|
---|
[938] | 104 | if (element == "BoundingBoxes")
|
---|
| 105 | EndBoundingBoxes();
|
---|
| 106 |
|
---|
| 107 | // inside the view cell description
|
---|
[651] | 108 | if (mParseViewCells)
|
---|
| 109 | {
|
---|
| 110 | if (element == "Interior")
|
---|
| 111 | EndViewCellInterior();
|
---|
| 112 | }
|
---|
| 113 | else
|
---|
| 114 | {
|
---|
[975] | 115 | if (element == "Interior")
|
---|
[651] | 116 | EndBspInterior();
|
---|
| 117 | }
|
---|
[508] | 118 | }
|
---|
| 119 |
|
---|
| 120 |
|
---|
[575] | 121 | void ViewCellsParseHandlers::EndBspInterior()
|
---|
[508] | 122 | {
|
---|
| 123 | // go one up in the tree
|
---|
[590] | 124 | if (mCurrentBspNode->GetParent())
|
---|
[975] | 125 | { Debug << "]";
|
---|
[590] | 126 | mCurrentBspNode = mCurrentBspNode->GetParent();
|
---|
[508] | 127 | }
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 |
|
---|
[651] | 131 | void ViewCellsParseHandlers::EndViewCellInterior()
|
---|
| 132 | {
|
---|
| 133 | // go one up in the tree
|
---|
| 134 | if (mCurrentViewCell->GetParent())
|
---|
[971] | 135 | { Debug << "]";
|
---|
[651] | 136 | mCurrentViewCell = mCurrentViewCell->GetParent();
|
---|
| 137 | }
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[931] | 140 |
|
---|
[863] | 141 | inline static bool vlt(ViewCell *v1, ViewCell *v2)
|
---|
[508] | 142 | {
|
---|
| 143 | return v1->mId < v2->mId;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 |
|
---|
| 147 | void ViewCellsParseHandlers::EndViewCells()
|
---|
| 148 | {
|
---|
[575] | 149 | // sort view cells to help associating view cells according to their id
|
---|
[508] | 150 | stable_sort(mViewCells.begin(), mViewCells.end(), vlt);
|
---|
[975] | 151 | mParseViewCells = false;
|
---|
[508] | 152 | }
|
---|
| 153 |
|
---|
| 154 |
|
---|
[931] | 155 | void ViewCellsParseHandlers::EndBoundingBoxes()
|
---|
| 156 | {
|
---|
[938] | 157 | // all bounding boxes gathered in this step =>
|
---|
| 158 | // associate object ids with bounding boxes
|
---|
[944] | 159 | long startTime = GetTime();
|
---|
| 160 |
|
---|
[931] | 161 | if (mBoundingBoxConverter)
|
---|
| 162 | mBoundingBoxConverter->IdentifyObjects(mIBoundingBoxes, *mObjects);
|
---|
[944] | 163 |
|
---|
[975] | 164 | Debug << "\nconverted bounding boxes to objects in "
|
---|
[944] | 165 | << TimeDiff(startTime, GetTime()) * 1e-3 << " secs" << endl;
|
---|
[931] | 166 | }
|
---|
[575] | 167 |
|
---|
[931] | 168 |
|
---|
[575] | 169 | void ViewCellsParseHandlers::StartHierarchy(AttributeList& attributes)
|
---|
[508] | 170 | {
|
---|
[575] | 171 | int len = attributes.getLength();
|
---|
| 172 |
|
---|
| 173 | for (int i = 0; i < len; ++ i)
|
---|
| 174 | {
|
---|
| 175 | string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
| 176 |
|
---|
| 177 | if (attrName == "name")
|
---|
| 178 | {
|
---|
| 179 | StrX attrValue(attributes.getValue(i));
|
---|
| 180 |
|
---|
| 181 | const char *ptr = attrValue.LocalForm();
|
---|
| 182 |
|
---|
[870] | 183 | //-- the view cells manager is created here
|
---|
[575] | 184 | CreateViewCellsManager(ptr);
|
---|
| 185 | }
|
---|
| 186 | }
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 |
|
---|
| 190 | void ViewCellsParseHandlers::startBspElement(string element,
|
---|
| 191 | AttributeList& attributes)
|
---|
| 192 | {
|
---|
[508] | 193 | if (element == "Interior")
|
---|
| 194 | {
|
---|
[971] | 195 | Debug << "[";
|
---|
[575] | 196 | StartBspInterior(attributes);
|
---|
[508] | 197 | }
|
---|
| 198 |
|
---|
| 199 | if (element == "Leaf")
|
---|
| 200 | {
|
---|
[971] | 201 | Debug << "l";
|
---|
[575] | 202 | StartBspLeaf(attributes);
|
---|
[508] | 203 | }
|
---|
[575] | 204 | }
|
---|
[508] | 205 |
|
---|
[575] | 206 |
|
---|
| 207 | void ViewCellsParseHandlers::startElement(const XMLCh* const name,
|
---|
| 208 | AttributeList& attributes)
|
---|
[975] | 209 | {
|
---|
[575] | 210 | StrX lname(name);
|
---|
| 211 | string element(lname.LocalForm());
|
---|
[971] | 212 |
|
---|
[975] | 213 |
|
---|
[651] | 214 | if (element == "ViewCells")
|
---|
| 215 | {
|
---|
[971] | 216 | Debug << "parsing view cells" << endl;
|
---|
[651] | 217 | mParseViewCells = true;
|
---|
| 218 | }
|
---|
| 219 |
|
---|
[975] | 220 | // decides about the view cell hierarchy
|
---|
| 221 | if (element == "HierarchyType")
|
---|
| 222 | //if (element == "Hierarchy")
|
---|
[575] | 223 | {
|
---|
[975] | 224 | //Debug << "parsing spatial hierarchy" << endl;
|
---|
| 225 | //mParseViewCells = false;
|
---|
[575] | 226 | StartHierarchy(attributes);
|
---|
| 227 | }
|
---|
[577] | 228 |
|
---|
| 229 | // decides the used view cell hierarchy
|
---|
| 230 | if (element == "ViewSpaceBox")
|
---|
| 231 | {
|
---|
[971] | 232 | Debug << "v";
|
---|
[577] | 233 | StartViewSpaceBox(attributes);
|
---|
| 234 | }
|
---|
[575] | 235 |
|
---|
[931] | 236 | // decides the used view cell hierarchy
|
---|
| 237 | if (element == "BoundingBox")
|
---|
| 238 | {
|
---|
[971] | 239 | Debug << "b";
|
---|
[931] | 240 | StartBoundingBox(attributes);
|
---|
| 241 | }
|
---|
[577] | 242 |
|
---|
[975] | 243 |
|
---|
[651] | 244 | if (!mParseViewCells)
|
---|
[577] | 245 | {
|
---|
[975] | 246 | //-- use different methods for the given view cell hierarchy types
|
---|
[651] | 247 | if (mViewCellsManager)
|
---|
[577] | 248 | {
|
---|
[651] | 249 | switch (mViewCellsManager->GetType())
|
---|
| 250 | {
|
---|
| 251 | case ViewCellsManager::BSP:
|
---|
| 252 | case ViewCellsManager::VSP_BSP:
|
---|
| 253 | startBspElement(element, attributes);
|
---|
| 254 | break;
|
---|
[575] | 255 |
|
---|
[651] | 256 | default:
|
---|
| 257 | Debug << "not implemented" << endl;
|
---|
| 258 | break;
|
---|
| 259 | }
|
---|
[577] | 260 | }
|
---|
| 261 | }
|
---|
[651] | 262 | else
|
---|
| 263 | {
|
---|
[975] | 264 | // interiors + leaves interpreted view cells else
|
---|
[651] | 265 | if (element == "Interior")
|
---|
| 266 | {
|
---|
[971] | 267 | Debug << "[";
|
---|
[651] | 268 | StartViewCellInterior(attributes);
|
---|
| 269 | }
|
---|
[575] | 270 |
|
---|
[651] | 271 | if (element == "Leaf")
|
---|
| 272 | {
|
---|
[971] | 273 | Debug << "l";
|
---|
[651] | 274 | StartViewCellLeaf(attributes);
|
---|
| 275 | }
|
---|
| 276 | }
|
---|
| 277 |
|
---|
[508] | 278 | ++ mElementCount;
|
---|
| 279 | mAttrCount += attributes.getLength();
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 |
|
---|
| 283 | inline bool ilt(Intersectable *obj1, Intersectable *obj2)
|
---|
| 284 | {
|
---|
| 285 | return obj1->mId < obj2->mId;
|
---|
| 286 | }
|
---|
| 287 |
|
---|
| 288 |
|
---|
[651] | 289 | void ViewCellsParseHandlers::StartViewCell(ViewCell *viewCell, AttributeList& attributes)
|
---|
[508] | 290 | {
|
---|
| 291 | int len = attributes.getLength();
|
---|
| 292 | vector<int> objIndices;
|
---|
[590] | 293 |
|
---|
[508] | 294 | for (int i = 0; i < len; ++ i)
|
---|
| 295 | {
|
---|
| 296 | string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
| 297 |
|
---|
| 298 | if (attrName == "pvs")
|
---|
| 299 | {
|
---|
| 300 | StrX attrValue(attributes.getValue(i));
|
---|
| 301 |
|
---|
| 302 | // handle coordIndex
|
---|
| 303 | objIndices.clear();
|
---|
| 304 | const char *ptr = attrValue.LocalForm();
|
---|
| 305 | char *endptr;
|
---|
| 306 |
|
---|
| 307 | while (1)
|
---|
| 308 | {
|
---|
| 309 | int index = strtol(ptr, &endptr, 10);
|
---|
| 310 |
|
---|
| 311 | if (ptr == endptr)
|
---|
| 312 | break;
|
---|
| 313 |
|
---|
| 314 | objIndices.push_back(index);
|
---|
| 315 |
|
---|
| 316 | ptr = endptr;
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | //TODO: find objects and add them to pvs
|
---|
| 320 | // TODO: get view cell with specified id
|
---|
| 321 | MeshInstance dummyInst(NULL);
|
---|
| 322 |
|
---|
| 323 | vector<int>::const_iterator it, it_end = objIndices.end();
|
---|
| 324 | for (it = objIndices.begin(); it != it_end; ++ it)
|
---|
| 325 | {
|
---|
| 326 | const int objId = *it;
|
---|
| 327 | dummyInst.SetId(objId);
|
---|
| 328 |
|
---|
| 329 | ObjectContainer::iterator oit =
|
---|
[863] | 330 | lower_bound(mObjects->begin(), mObjects->end(), (Intersectable *)&dummyInst, ilt);
|
---|
[556] | 331 |
|
---|
[880] | 332 |
|
---|
| 333 | if ((oit != mObjects->end()) && ((*oit)->GetId() == objId))
|
---|
[508] | 334 | {
|
---|
[880] | 335 | // $$JB we should store a float a per object which corresponds
|
---|
| 336 | // to sumof pdfs, i.e. its relative visibility
|
---|
| 337 | // temporarily set to 1.0f
|
---|
| 338 | viewCell->GetPvs().AddSample(*oit, 1.0f);
|
---|
[508] | 339 | }
|
---|
| 340 | else
|
---|
| 341 | {
|
---|
[880] | 342 | Debug << "error: object with id " << objId << " does not exist" << endl;
|
---|
[508] | 343 | }
|
---|
| 344 | }
|
---|
| 345 | }
|
---|
| 346 | else if (attrName == "id")
|
---|
| 347 | {
|
---|
| 348 | StrX attrValue(attributes.getValue(i));
|
---|
| 349 |
|
---|
| 350 | const char *ptr = attrValue.LocalForm();
|
---|
| 351 | char *endptr = NULL;
|
---|
| 352 | const int id = strtol(ptr, &endptr, 10);
|
---|
| 353 |
|
---|
| 354 | viewCell->SetId(id);
|
---|
| 355 | }
|
---|
[881] | 356 | /*else if (attrName == "active")
|
---|
[651] | 357 | {
|
---|
| 358 | StrX attrValue(attributes.getValue(i));
|
---|
| 359 |
|
---|
| 360 | const char *ptr = attrValue.LocalForm();
|
---|
| 361 | char *endptr = NULL;
|
---|
| 362 | const bool isActive = (bool)strtol(ptr, &endptr, 10);
|
---|
| 363 |
|
---|
[660] | 364 | if (isActive)
|
---|
| 365 | viewCell->SetActive();
|
---|
[881] | 366 | }*/
|
---|
[651] | 367 | else if (attrName == "mergecost")
|
---|
| 368 | {
|
---|
| 369 | StrX attrValue(attributes.getValue(i));
|
---|
| 370 |
|
---|
| 371 | const char *ptr = attrValue.LocalForm();
|
---|
| 372 | char *endptr = NULL;
|
---|
| 373 | const float cost = (float)strtod(ptr, &endptr);
|
---|
| 374 |
|
---|
| 375 | viewCell->SetMergeCost(cost);
|
---|
| 376 | }
|
---|
[508] | 377 | }
|
---|
| 378 | }
|
---|
| 379 |
|
---|
| 380 |
|
---|
[577] | 381 | void ViewCellsParseHandlers::StartViewSpaceBox(AttributeList& attributes)
|
---|
| 382 | {
|
---|
| 383 | int len = attributes.getLength();
|
---|
| 384 |
|
---|
| 385 | Vector3 bmin, bmax;
|
---|
| 386 |
|
---|
| 387 | for (int i = 0; i < len; ++ i)
|
---|
| 388 | {
|
---|
| 389 | string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
| 390 | StrX attrValue(attributes.getValue(i));
|
---|
| 391 | const char *ptr = attrValue.LocalForm();
|
---|
| 392 |
|
---|
| 393 | if (attrName == "min")
|
---|
| 394 | {
|
---|
| 395 | sscanf(ptr, "%f %f %f",
|
---|
| 396 | &bmin.x, &bmin.y, &bmin.z);
|
---|
| 397 | }
|
---|
| 398 | else if (attrName == "max")
|
---|
| 399 | {
|
---|
| 400 | sscanf(ptr, "%f %f %f",
|
---|
| 401 | &bmax.x, &bmax.y, &bmax.z);
|
---|
| 402 | }
|
---|
| 403 | }
|
---|
| 404 |
|
---|
| 405 | mViewSpaceBox = AxisAlignedBox3(bmin, bmax);
|
---|
| 406 |
|
---|
[944] | 407 | Debug << "\nview space box: " << mViewSpaceBox << endl;
|
---|
[577] | 408 | }
|
---|
| 409 |
|
---|
| 410 |
|
---|
[931] | 411 | void ViewCellsParseHandlers::StartBoundingBox(AttributeList& attributes)
|
---|
| 412 | {
|
---|
| 413 | int len = attributes.getLength();
|
---|
| 414 |
|
---|
| 415 | Vector3 bmin, bmax;
|
---|
| 416 | int id;
|
---|
| 417 |
|
---|
| 418 | for (int i = 0; i < len; ++ i)
|
---|
| 419 | {
|
---|
| 420 | string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
| 421 | StrX attrValue(attributes.getValue(i));
|
---|
| 422 | const char *ptr = attrValue.LocalForm();
|
---|
| 423 |
|
---|
| 424 |
|
---|
| 425 | if (attrName == "id")
|
---|
| 426 | {
|
---|
| 427 | sscanf(ptr, "%d", &id);
|
---|
| 428 | }
|
---|
| 429 |
|
---|
| 430 | if (attrName == "min")
|
---|
| 431 | {
|
---|
| 432 | sscanf(ptr, "%f %f %f",
|
---|
| 433 | &bmin.x, &bmin.y, &bmin.z);
|
---|
| 434 | }
|
---|
| 435 | else if (attrName == "max")
|
---|
| 436 | {
|
---|
| 437 | sscanf(ptr, "%f %f %f",
|
---|
| 438 | &bmax.x, &bmax.y, &bmax.z);
|
---|
| 439 | }
|
---|
| 440 | }
|
---|
| 441 |
|
---|
| 442 | AxisAlignedBox3 box(bmin, bmax);
|
---|
| 443 | mIBoundingBoxes.push_back(IndexedBoundingBox(id, box));
|
---|
| 444 |
|
---|
[944] | 445 | //Debug << "bbox: " << box << endl;
|
---|
[931] | 446 | }
|
---|
| 447 |
|
---|
| 448 |
|
---|
[575] | 449 | void ViewCellsParseHandlers::StartBspLeaf(AttributeList& attributes)
|
---|
[508] | 450 | {
|
---|
| 451 | BspLeaf * leaf =
|
---|
[590] | 452 | new BspLeaf(dynamic_cast<BspInterior *>(mCurrentBspNode), NULL);
|
---|
[508] | 453 |
|
---|
[590] | 454 | if (mCurrentBspNode) // replace front or (if not NULL) back child
|
---|
[508] | 455 | {
|
---|
[590] | 456 | dynamic_cast<BspInterior *>(mCurrentBspNode)->ReplaceChildLink(NULL, leaf);
|
---|
[508] | 457 | }
|
---|
| 458 | else
|
---|
| 459 | {
|
---|
[575] | 460 | if (mViewCellsManager->GetType() == ViewCellsManager::BSP)
|
---|
[577] | 461 | {
|
---|
[575] | 462 | mBspTree->mRoot = leaf;
|
---|
[577] | 463 | }
|
---|
| 464 | else if (mViewCellsManager->GetType() == ViewCellsManager::VSP_BSP)
|
---|
| 465 | {
|
---|
[575] | 466 | mVspBspTree->mRoot = leaf;
|
---|
[577] | 467 | }
|
---|
[508] | 468 | }
|
---|
| 469 |
|
---|
| 470 | //-- find associated view cell
|
---|
| 471 | int viewCellId;
|
---|
| 472 |
|
---|
| 473 | int len = attributes.getLength();
|
---|
| 474 |
|
---|
| 475 | for (int i = 0; i < len; ++ i)
|
---|
| 476 | {
|
---|
| 477 | string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
| 478 | StrX attrValue(attributes.getValue(i));
|
---|
| 479 |
|
---|
| 480 | const char *ptr = attrValue.LocalForm();
|
---|
| 481 | char *endptr = NULL;
|
---|
| 482 |
|
---|
| 483 | if (attrName == "viewCellId")
|
---|
| 484 | {
|
---|
| 485 | viewCellId = strtol(ptr, &endptr, 10);
|
---|
| 486 | }
|
---|
| 487 | }
|
---|
[654] | 488 |
|
---|
[590] | 489 |
|
---|
[508] | 490 | if (viewCellId >= 0) // valid view cell
|
---|
| 491 | {
|
---|
| 492 | // TODO: get view cell with specified id
|
---|
[590] | 493 | ViewCellInterior dummyVc;
|
---|
[508] | 494 | dummyVc.SetId(viewCellId);
|
---|
| 495 |
|
---|
| 496 | ViewCellContainer::iterator vit =
|
---|
| 497 | lower_bound(mViewCells.begin(), mViewCells.end(), &dummyVc, vlt);
|
---|
| 498 |
|
---|
[651] | 499 | BspViewCell *viewCell = dynamic_cast<BspViewCell *>(*vit);
|
---|
[580] | 500 |
|
---|
[654] | 501 |
|
---|
[508] | 502 | if (viewCell->GetId() == viewCellId)
|
---|
| 503 | {
|
---|
[651] | 504 | leaf->SetViewCell(viewCell);
|
---|
| 505 | viewCell->mLeaf = leaf;
|
---|
[508] | 506 | }
|
---|
| 507 | else
|
---|
| 508 | {
|
---|
| 509 | Debug << "error: view cell does not exist" << endl;
|
---|
| 510 | }
|
---|
| 511 | }
|
---|
| 512 | else
|
---|
| 513 | {
|
---|
[863] | 514 | // add to invalid view space
|
---|
[575] | 515 | if (mViewCellsManager->GetType() == ViewCellsManager::VSP_BSP)
|
---|
| 516 | {
|
---|
| 517 | leaf->SetViewCell(mVspBspTree->GetOrCreateOutOfBoundsCell());
|
---|
| 518 | leaf->SetTreeValid(false);
|
---|
| 519 | mVspBspTree->PropagateUpValidity(leaf);
|
---|
| 520 | }
|
---|
[654] | 521 | }
|
---|
[508] | 522 | }
|
---|
| 523 |
|
---|
| 524 |
|
---|
[575] | 525 | void ViewCellsParseHandlers::StartBspInterior(AttributeList& attributes)
|
---|
[508] | 526 | {
|
---|
| 527 | Plane3 plane;
|
---|
| 528 | int len = attributes.getLength();
|
---|
| 529 |
|
---|
| 530 | for (int i = 0; i < len; ++ i)
|
---|
| 531 | {
|
---|
| 532 | string attrName(StrX(attributes.getName(i)).LocalForm());
|
---|
| 533 | StrX attrValue(attributes.getValue(i));
|
---|
| 534 | const char *ptr = attrValue.LocalForm();
|
---|
| 535 |
|
---|
| 536 | if (attrName == "plane")
|
---|
| 537 | {
|
---|
| 538 | sscanf(ptr, "%f %f %f %f",
|
---|
| 539 | &plane.mNormal.x, &plane.mNormal.y, &plane.mNormal.z, &plane.mD);
|
---|
| 540 | }
|
---|
| 541 | }
|
---|
| 542 |
|
---|
| 543 | BspInterior* interior = new BspInterior(plane);
|
---|
| 544 |
|
---|
[590] | 545 | if (mCurrentBspNode) // replace NULL child of parent with current node
|
---|
[508] | 546 | {
|
---|
[590] | 547 | BspInterior *current = dynamic_cast<BspInterior *>(mCurrentBspNode);
|
---|
[508] | 548 |
|
---|
| 549 | current->ReplaceChildLink(NULL, interior);
|
---|
| 550 | interior->SetParent(current);
|
---|
| 551 | }
|
---|
| 552 | else
|
---|
| 553 | {
|
---|
[575] | 554 | if (mViewCellsManager->GetType() == ViewCellsManager::BSP)
|
---|
[577] | 555 | {
|
---|
[575] | 556 | mBspTree->mRoot = interior;
|
---|
[577] | 557 | }
|
---|
[575] | 558 | else
|
---|
[577] | 559 | {
|
---|
[575] | 560 | mVspBspTree->mRoot = interior;
|
---|
[577] | 561 | }
|
---|
[508] | 562 | }
|
---|
| 563 |
|
---|
[590] | 564 | mCurrentBspNode = interior;
|
---|
[508] | 565 | }
|
---|
| 566 |
|
---|
| 567 |
|
---|
[651] | 568 | void ViewCellsParseHandlers::StartViewCellLeaf(AttributeList& attributes)
|
---|
| 569 | {
|
---|
| 570 | BspViewCell *viewCell = new BspViewCell();
|
---|
[575] | 571 |
|
---|
[651] | 572 | if (mCurrentViewCell) // replace front or (if not NULL) back child
|
---|
| 573 | {
|
---|
| 574 | ViewCellInterior *interior = dynamic_cast<ViewCellInterior *>(mCurrentViewCell);
|
---|
| 575 | interior->SetupChildLink(viewCell);
|
---|
| 576 | }
|
---|
| 577 | else // root
|
---|
| 578 | {
|
---|
| 579 | mViewCellsTree->SetRoot(viewCell);
|
---|
| 580 | }
|
---|
| 581 |
|
---|
| 582 | StartViewCell(viewCell, attributes);
|
---|
| 583 |
|
---|
| 584 | // collect leaves
|
---|
| 585 | mViewCells.push_back(viewCell);
|
---|
| 586 | }
|
---|
| 587 |
|
---|
| 588 |
|
---|
| 589 | void ViewCellsParseHandlers::StartViewCellInterior(AttributeList& attributes)
|
---|
| 590 | {
|
---|
| 591 | ViewCellInterior* interior = new ViewCellInterior();
|
---|
| 592 |
|
---|
| 593 | if (mCurrentViewCell) // replace NULL child of parent with current node
|
---|
| 594 | {
|
---|
| 595 | ViewCellInterior *current = dynamic_cast<ViewCellInterior *>(mCurrentViewCell);
|
---|
| 596 |
|
---|
| 597 | current->SetupChildLink(interior);
|
---|
| 598 | }
|
---|
| 599 | else
|
---|
| 600 | {
|
---|
| 601 | mViewCellsTree->SetRoot(interior);
|
---|
| 602 | }
|
---|
| 603 |
|
---|
| 604 | mCurrentViewCell = interior;
|
---|
| 605 |
|
---|
| 606 | StartViewCell(interior, attributes);
|
---|
| 607 | }
|
---|
| 608 |
|
---|
| 609 |
|
---|
| 610 |
|
---|
[575] | 611 | void ViewCellsParseHandlers::CreateViewCellsManager(const char *name)
|
---|
| 612 | {
|
---|
| 613 | if (strcmp(name, "bspTree") == 0)
|
---|
| 614 | {
|
---|
| 615 | Debug << "view cell type: Bsp" << endl;
|
---|
| 616 |
|
---|
[577] | 617 | mBspTree = new BspTree();
|
---|
[590] | 618 | mBspTree->mBox = mViewSpaceBox;
|
---|
[651] | 619 | //mCurrentBspNode = mBspTree->GetRoot();
|
---|
[575] | 620 |
|
---|
[1004] | 621 | mViewCellsManager = new BspViewCellsManager(mBspTree);
|
---|
[575] | 622 | }
|
---|
[975] | 623 | else // vspBspTree
|
---|
| 624 | {
|
---|
[577] | 625 | Debug << "view cell type: VspBsp" << endl;
|
---|
| 626 |
|
---|
[575] | 627 | mVspBspTree = new VspBspTree();
|
---|
[651] | 628 | //mCurrentBspNode = mVspBspTree->GetRoot();
|
---|
[1004] | 629 | mViewCellsManager = new VspBspViewCellsManager(mVspBspTree);
|
---|
[577] | 630 |
|
---|
[590] | 631 | mVspBspTree->mBox = mViewSpaceBox;
|
---|
[575] | 632 | }
|
---|
[577] | 633 |
|
---|
[651] | 634 | mViewCellsTree = mViewCellsManager->GetViewCellsTree();
|
---|
[577] | 635 | mViewCellsManager->SetViewSpaceBox(mViewSpaceBox);
|
---|
[575] | 636 | }
|
---|
| 637 |
|
---|
| 638 |
|
---|
[508] | 639 | void ViewCellsParseHandlers::characters(const XMLCh* const chars,
|
---|
| 640 | const unsigned int length)
|
---|
| 641 | {
|
---|
| 642 | mCharacterCount += length;
|
---|
| 643 | }
|
---|
| 644 |
|
---|
| 645 |
|
---|
| 646 | void ViewCellsParseHandlers::ignorableWhitespace(const XMLCh* const chars,
|
---|
| 647 | const unsigned int length)
|
---|
| 648 | {
|
---|
| 649 | mSpaceCount += length;
|
---|
| 650 | }
|
---|
| 651 |
|
---|
| 652 |
|
---|
| 653 | void ViewCellsParseHandlers::resetDocument()
|
---|
| 654 | {
|
---|
| 655 | mAttrCount = 0;
|
---|
| 656 | mCharacterCount = 0;
|
---|
| 657 | mElementCount = 0;
|
---|
| 658 | mSpaceCount = 0;
|
---|
| 659 | }
|
---|
| 660 |
|
---|
| 661 |
|
---|
| 662 | // ---------------------------------------------------------------------------
|
---|
| 663 | // StdInParseHandlers: Overrides of the SAX ErrorHandler interface
|
---|
| 664 | // ---------------------------------------------------------------------------
|
---|
| 665 | void
|
---|
| 666 | ViewCellsParseHandlers::error(const SAXParseException& e)
|
---|
| 667 | {
|
---|
| 668 | XERCES_STD_QUALIFIER cerr << "\nError at (file " << StrX(e.getSystemId())
|
---|
| 669 | << ", line " << e.getLineNumber()
|
---|
| 670 | << ", char " << e.getColumnNumber()
|
---|
| 671 | << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
| 672 | }
|
---|
| 673 |
|
---|
| 674 | void
|
---|
| 675 | ViewCellsParseHandlers::fatalError(const SAXParseException& e)
|
---|
| 676 | {
|
---|
| 677 | XERCES_STD_QUALIFIER cerr << "\nFatal Error at (file " << StrX(e.getSystemId())
|
---|
| 678 | << ", line " << e.getLineNumber()
|
---|
| 679 | << ", char " << e.getColumnNumber()
|
---|
| 680 | << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
| 681 | }
|
---|
| 682 |
|
---|
| 683 | void
|
---|
| 684 | ViewCellsParseHandlers::warning(const SAXParseException& e)
|
---|
| 685 | {
|
---|
| 686 | XERCES_STD_QUALIFIER cerr << "\nWarning at (file " << StrX(e.getSystemId())
|
---|
| 687 | << ", line " << e.getLineNumber()
|
---|
| 688 | << ", char " << e.getColumnNumber()
|
---|
| 689 | << "): " << StrX(e.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
| 690 | }
|
---|
| 691 |
|
---|
| 692 |
|
---|
| 693 | bool ViewCellsParser::ParseFile(const string filename,
|
---|
[575] | 694 | ViewCellsManager **viewCells,
|
---|
[931] | 695 | ObjectContainer *objects,
|
---|
[1004] | 696 | BoundingBoxConverter *bconverter)
|
---|
[508] | 697 | {
|
---|
| 698 | // Initialize the XML4C system
|
---|
| 699 | try {
|
---|
| 700 | XMLPlatformUtils::Initialize();
|
---|
| 701 | }
|
---|
| 702 |
|
---|
| 703 | catch (const XMLException& toCatch)
|
---|
| 704 | {
|
---|
| 705 | XERCES_STD_QUALIFIER cerr << "Error during initialization! Message:\n"
|
---|
| 706 | << StrX(toCatch.getMessage()) << XERCES_STD_QUALIFIER endl;
|
---|
| 707 | return false;
|
---|
| 708 | }
|
---|
| 709 |
|
---|
| 710 | //
|
---|
| 711 | // Create a SAX parser object. Then, according to what we were told on
|
---|
| 712 | // the command line, set the options.
|
---|
| 713 | //
|
---|
| 714 | SAXParser* parser = new SAXParser;
|
---|
| 715 | parser->setValidationScheme(valScheme);
|
---|
| 716 | parser->setDoNamespaces(doNamespaces);
|
---|
| 717 | parser->setDoSchema(doSchema);
|
---|
| 718 | parser->setValidationSchemaFullChecking(schemaFullChecking);
|
---|
| 719 |
|
---|
| 720 |
|
---|
| 721 | //
|
---|
| 722 | // Create our SAX handler object and install it on the parser, as the
|
---|
| 723 | // document and error handler. We are responsible for cleaning them
|
---|
| 724 | // up, but since its just stack based here, there's nothing special
|
---|
| 725 | // to do.
|
---|
| 726 | //
|
---|
[1004] | 727 | ViewCellsParseHandlers handler(objects, bconverter);
|
---|
[508] | 728 | parser->setDocumentHandler(&handler);
|
---|
| 729 | parser->setErrorHandler(&handler);
|
---|
| 730 |
|
---|
| 731 | unsigned long duration;
|
---|
| 732 | int errorCount = 0;
|
---|
| 733 | // create a faux scope so that 'src' destructor is called before
|
---|
| 734 | // XMLPlatformUtils::Terminate
|
---|
| 735 | {
|
---|
| 736 | //
|
---|
| 737 | // Kick off the parse and catch any exceptions. Create a standard
|
---|
| 738 | // input input source and tell the parser to parse from that.
|
---|
| 739 | //
|
---|
| 740 | // StdInInputSource src;
|
---|
| 741 | try
|
---|
| 742 | {
|
---|
| 743 | const unsigned long startMillis = XMLPlatformUtils::getCurrentMillis();
|
---|
[971] | 744 | #if ZIPPED_VIEWCELLS
|
---|
| 745 | XMLCh *myFilePath = XMLString::transcode(filename.c_str());
|
---|
| 746 |
|
---|
| 747 | GzFileInputSource isource(myFilePath);
|
---|
| 748 | parser->parse(isource);
|
---|
| 749 |
|
---|
| 750 | #else
|
---|
[508] | 751 | parser->parse(filename.c_str());
|
---|
[971] | 752 | #endif
|
---|
| 753 |
|
---|
[508] | 754 | const unsigned long endMillis = XMLPlatformUtils::getCurrentMillis();
|
---|
| 755 | duration = endMillis - startMillis;
|
---|
| 756 | errorCount = parser->getErrorCount();
|
---|
| 757 | }
|
---|
| 758 | catch (const OutOfMemoryException&)
|
---|
| 759 | {
|
---|
| 760 | XERCES_STD_QUALIFIER cerr << "OutOfMemoryException" << XERCES_STD_QUALIFIER endl;
|
---|
| 761 | errorCount = 2;
|
---|
| 762 | return false;
|
---|
| 763 | }
|
---|
| 764 | catch (const XMLException& e)
|
---|
| 765 | {
|
---|
[870] | 766 | XERCES_STD_QUALIFIER cerr << "\nError during parsing: \n"
|
---|
[508] | 767 | << StrX(e.getMessage())
|
---|
| 768 | << "\n" << XERCES_STD_QUALIFIER endl;
|
---|
[870] | 769 | errorCount = 1;
|
---|
| 770 | return false;
|
---|
[508] | 771 | }
|
---|
| 772 |
|
---|
| 773 |
|
---|
| 774 | // Print out the stats that we collected and time taken
|
---|
| 775 | if (!errorCount) {
|
---|
[870] | 776 | XERCES_STD_QUALIFIER cerr << filename << ": " << duration << " ms ("
|
---|
[508] | 777 | << handler.GetElementCount() << " elems, "
|
---|
| 778 | << handler.GetAttrCount() << " attrs, "
|
---|
| 779 | << handler.GetSpaceCount() << " spaces, "
|
---|
| 780 | << handler.GetCharacterCount() << " chars)" << XERCES_STD_QUALIFIER endl;
|
---|
| 781 | }
|
---|
| 782 | }
|
---|
| 783 |
|
---|
| 784 | //
|
---|
| 785 | // Delete the parser itself. Must be done prior to calling Terminate, below.
|
---|
| 786 | //
|
---|
| 787 | delete parser;
|
---|
| 788 |
|
---|
| 789 | XMLPlatformUtils::Terminate();
|
---|
| 790 |
|
---|
[931] | 791 | //-- assign new view cells manager
|
---|
[577] | 792 | *viewCells = handler.mViewCellsManager;
|
---|
[651] | 793 |
|
---|
[508] | 794 | if (errorCount > 0)
|
---|
| 795 | return false;
|
---|
| 796 | else
|
---|
| 797 | return true;
|
---|
[556] | 798 | }
|
---|
[860] | 799 |
|
---|
| 800 | } |
---|