[372] | 1 | #include "SceneGraph.h"
|
---|
| 2 | #include "Exporter.h"
|
---|
| 3 | #include "UnigraphicsParser.h"
|
---|
| 4 | #include "X3dParser.h"
|
---|
| 5 | #include "Preprocessor.h"
|
---|
| 6 | #include "ViewCell.h"
|
---|
| 7 | #include "Environment.h"
|
---|
[439] | 8 | #include "ViewCellsManager.h"
|
---|
[445] | 9 | #include "ViewCellBsp.h"
|
---|
| 10 | #include "VspBspTree.h"
|
---|
| 11 | #include "VspKdTree.h"
|
---|
[469] | 12 | #include "RenderSimulator.h"
|
---|
[496] | 13 | #include "GlRenderer.h"
|
---|
[372] | 14 |
|
---|
[492] | 15 | Preprocessor *preprocessor;
|
---|
| 16 |
|
---|
[372] | 17 | Preprocessor::Preprocessor():
|
---|
| 18 | mKdTree(NULL),
|
---|
[409] | 19 | mBspTree(NULL),
|
---|
[422] | 20 | mVspKdTree(NULL),
|
---|
[445] | 21 | mVspBspTree(NULL),
|
---|
[439] | 22 | mViewCellsManager(NULL)
|
---|
[308] | 23 | {
|
---|
[492] | 24 | environment->GetBoolValue("Preprocessor.useGlRenderer", mUseGlRenderer);
|
---|
[496] | 25 | // renderer will be constructed when the scene graph and viewcell manager will be known
|
---|
| 26 | renderer = NULL;
|
---|
| 27 |
|
---|
[372] | 28 | }
|
---|
| 29 |
|
---|
| 30 |
|
---|
| 31 | Preprocessor::~Preprocessor()
|
---|
| 32 | {
|
---|
[496] | 33 | DEL_PTR(mViewCellsManager);
|
---|
| 34 | DEL_PTR(mBspTree);
|
---|
| 35 | DEL_PTR(mKdTree);
|
---|
| 36 | DEL_PTR(mVspKdTree);
|
---|
| 37 | DEL_PTR(mVspBspTree);
|
---|
[372] | 38 | }
|
---|
| 39 |
|
---|
[387] | 40 | int
|
---|
[419] | 41 | SplitFilenames(const string str, vector<string> &filenames)
|
---|
[387] | 42 | {
|
---|
| 43 | int pos = 0;
|
---|
| 44 |
|
---|
| 45 | while(1) {
|
---|
[469] | 46 | int npos = (int)str.find(';', pos);
|
---|
[387] | 47 |
|
---|
| 48 | if (npos < 0 || npos - pos < 1)
|
---|
| 49 | break;
|
---|
| 50 | filenames.push_back(string(str, pos, npos - pos));
|
---|
| 51 | pos = npos + 1;
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | filenames.push_back(string(str, pos, str.size() - pos));
|
---|
[440] | 55 | return (int)filenames.size();
|
---|
[387] | 56 | }
|
---|
| 57 |
|
---|
[372] | 58 | bool
|
---|
| 59 | Preprocessor::LoadScene(const string filename)
|
---|
| 60 | {
|
---|
| 61 | // use leaf nodes of the original spatial hiearrchy as occludees
|
---|
| 62 |
|
---|
| 63 | mSceneGraph = new SceneGraph;
|
---|
| 64 |
|
---|
| 65 | Parser *parser;
|
---|
[387] | 66 | vector<string> filenames;
|
---|
| 67 | int files = SplitFilenames(filename, filenames);
|
---|
| 68 | cout<<files<<endl;
|
---|
| 69 | bool result = false;
|
---|
| 70 | if (files == 1) {
|
---|
| 71 |
|
---|
| 72 | if (strstr(filename.c_str(), ".x3d"))
|
---|
| 73 | parser = new X3dParser;
|
---|
| 74 | else
|
---|
| 75 | parser = new UnigraphicsParser;
|
---|
[372] | 76 |
|
---|
[387] | 77 | cout<<filename<<endl;
|
---|
| 78 | result = parser->ParseFile(filename, &mSceneGraph->mRoot);
|
---|
[372] | 79 |
|
---|
[387] | 80 | delete parser;
|
---|
[372] | 81 |
|
---|
[387] | 82 | } else {
|
---|
| 83 | // root for different files
|
---|
| 84 | mSceneGraph->mRoot = new SceneGraphNode;
|
---|
| 85 | for (int i= 0; i < filenames.size(); i++) {
|
---|
| 86 | if (strstr(filenames[i].c_str(), ".x3d"))
|
---|
| 87 | parser = new X3dParser;
|
---|
| 88 | else
|
---|
| 89 | parser = new UnigraphicsParser;
|
---|
| 90 |
|
---|
| 91 | SceneGraphNode *node;
|
---|
| 92 | if (parser->ParseFile(filenames[i], &node)) {
|
---|
| 93 | mSceneGraph->mRoot->mChildren.push_back(node);
|
---|
| 94 | // at least one file parsed
|
---|
| 95 | result = true;
|
---|
| 96 | }
|
---|
| 97 | delete parser;
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
[372] | 100 |
|
---|
| 101 |
|
---|
[387] | 102 | if (result) {
|
---|
[492] | 103 |
|
---|
| 104 | mSceneGraph->AssignObjectIds();
|
---|
| 105 | int intersectables, faces;
|
---|
| 106 | mSceneGraph->GetStatistics(intersectables, faces);
|
---|
| 107 | cout<<filename<<" parsed successfully."<<endl;
|
---|
| 108 | cout<<"#NUM_OBJECTS (Total numner of objects)\n"<<intersectables<<endl;
|
---|
| 109 | cout<<"#NUM_FACES (Total numner of faces)\n"<<faces<<endl;
|
---|
| 110 | mSceneGraph->CollectObjects(&mObjects);
|
---|
| 111 | mSceneGraph->mRoot->UpdateBox();
|
---|
[387] | 112 | }
|
---|
[372] | 113 |
|
---|
[492] | 114 |
|
---|
| 115 | return result;
|
---|
[372] | 116 | }
|
---|
| 117 |
|
---|
| 118 | bool
|
---|
| 119 | Preprocessor::ExportPreprocessedData(const string filename)
|
---|
| 120 | {
|
---|
| 121 | return false;
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | bool
|
---|
| 125 | Preprocessor::BuildKdTree()
|
---|
| 126 | {
|
---|
| 127 | mKdTree = new KdTree;
|
---|
| 128 | // add mesh instances of the scene graph to the root of the tree
|
---|
| 129 | KdLeaf *root = (KdLeaf *)mKdTree->GetRoot();
|
---|
| 130 | mSceneGraph->CollectObjects(&root->mObjects);
|
---|
| 131 |
|
---|
| 132 | mKdTree->Construct();
|
---|
| 133 | return true;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | void
|
---|
| 137 | Preprocessor::KdTreeStatistics(ostream &s)
|
---|
| 138 | {
|
---|
| 139 | s<<mKdTree->GetStatistics();
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | void
|
---|
| 143 | Preprocessor::BspTreeStatistics(ostream &s)
|
---|
| 144 | {
|
---|
| 145 | s << mBspTree->GetStatistics();
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | bool
|
---|
| 149 | Preprocessor::Export( const string filename,
|
---|
[492] | 150 | const bool scene,
|
---|
| 151 | const bool kdtree,
|
---|
| 152 | const bool bsptree
|
---|
| 153 | )
|
---|
[372] | 154 | {
|
---|
| 155 | Exporter *exporter = Exporter::GetExporter(filename);
|
---|
| 156 |
|
---|
| 157 | if (exporter) {
|
---|
| 158 | if (scene)
|
---|
| 159 | exporter->ExportScene(mSceneGraph->mRoot);
|
---|
| 160 |
|
---|
| 161 | if (kdtree) {
|
---|
| 162 | exporter->SetWireframe();
|
---|
| 163 | exporter->ExportKdTree(*mKdTree);
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | if (bsptree) {
|
---|
| 167 | //exporter->SetWireframe();
|
---|
| 168 | exporter->ExportBspTree(*mBspTree);
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | delete exporter;
|
---|
| 172 | return true;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | return false;
|
---|
| 176 | }
|
---|
[429] | 177 |
|
---|
[463] | 178 | bool Preprocessor::PrepareViewCells()
|
---|
| 179 | {
|
---|
[439] | 180 | //-- parse type of view cell container
|
---|
| 181 | char viewCellsStr[64];
|
---|
| 182 | environment->GetStringValue("ViewCells.type", viewCellsStr);
|
---|
| 183 |
|
---|
[445] | 184 | int constructionSamples = 0;
|
---|
[469] | 185 |
|
---|
[439] | 186 | if (strcmp(viewCellsStr, "kdTree") == 0)
|
---|
| 187 | {
|
---|
[440] | 188 | mViewCellsManager = new KdViewCellsManager(mKdTree);
|
---|
[439] | 189 | }
|
---|
[448] | 190 | else if (strcmp(viewCellsStr, "bspTree") == 0)
|
---|
[439] | 191 | {
|
---|
[441] | 192 | mBspTree = new BspTree();
|
---|
| 193 |
|
---|
[448] | 194 | Debug << "view cell type: Bsp" << endl;
|
---|
| 195 |
|
---|
[445] | 196 | environment->GetIntValue("BspTree.Construction.samples", constructionSamples);
|
---|
| 197 | mViewCellsManager = new BspViewCellsManager(mBspTree, constructionSamples);
|
---|
[439] | 198 | }
|
---|
[448] | 199 | else if (strcmp(viewCellsStr, "vspBspTree") == 0)
|
---|
[439] | 200 | {
|
---|
[445] | 201 | mVspBspTree = new VspBspTree();
|
---|
| 202 |
|
---|
[448] | 203 | Debug << "view cell type: VspBsp" << endl;
|
---|
| 204 |
|
---|
[445] | 205 | environment->GetIntValue("VspBspTree.Construction.samples", constructionSamples);
|
---|
| 206 | mViewCellsManager = new VspBspViewCellsManager(mVspBspTree, constructionSamples);
|
---|
[439] | 207 | }
|
---|
[445] | 208 | else if (strcmp(viewCellsStr, "vspKdTree") == 0)
|
---|
| 209 | {
|
---|
| 210 | mVspKdTree = new VspKdTree();
|
---|
| 211 |
|
---|
| 212 | environment->GetIntValue("VspKdTree.Construction.samples", constructionSamples);
|
---|
| 213 | mViewCellsManager = new VspKdViewCellsManager(mVspKdTree, constructionSamples);
|
---|
| 214 | }
|
---|
[439] | 215 | else if (strcmp(viewCellsStr, "sceneDependent") == 0)
|
---|
| 216 | {
|
---|
| 217 | //TODO
|
---|
[445] | 218 | mBspTree = new BspTree();
|
---|
[440] | 219 |
|
---|
[448] | 220 | Debug << "view cell type: Bsp" << endl;
|
---|
[445] | 221 | environment->GetIntValue("BspTree.Construction.samples", constructionSamples);
|
---|
| 222 | mViewCellsManager = new BspViewCellsManager(mBspTree, constructionSamples);
|
---|
[439] | 223 | }
|
---|
| 224 | else
|
---|
| 225 | {
|
---|
| 226 | cerr<<"Wrong view cells type" << viewCellsStr << endl;
|
---|
| 227 | exit(1);
|
---|
| 228 | }
|
---|
| 229 |
|
---|
[473] | 230 | float objRenderCost = 0, vcOverhead = 0, moveSpeed = 0;
|
---|
| 231 |
|
---|
| 232 | environment->GetFloatValue("Simulation.objRenderCost",objRenderCost);
|
---|
| 233 | environment->GetFloatValue("Simulation.vcOverhead", vcOverhead);
|
---|
| 234 | environment->GetFloatValue("Simulation.moveSpeed", moveSpeed);
|
---|
| 235 |
|
---|
| 236 | mRenderSimulator =
|
---|
| 237 | new RenderSimulator(mViewCellsManager, objRenderCost, vcOverhead, moveSpeed);
|
---|
| 238 |
|
---|
[440] | 239 | int postProcessSamples = 0;
|
---|
| 240 | int visSamples = 0;
|
---|
| 241 |
|
---|
[478] | 242 | environment->GetIntValue("ViewCells.PostProcess.samples", postProcessSamples);
|
---|
[445] | 243 | environment->GetIntValue("ViewCells.Visualization.samples", visSamples);
|
---|
[440] | 244 |
|
---|
| 245 | mViewCellsManager->SetPostProcessSamples(postProcessSamples);
|
---|
| 246 | mViewCellsManager->SetVisualizationSamples(visSamples);
|
---|
[480] | 247 | mViewCellsManager->SetRenderer(mRenderSimulator);
|
---|
[440] | 248 |
|
---|
[463] | 249 | //-- parse view cells construction method
|
---|
| 250 | bool loadViewCells = false;
|
---|
| 251 | environment->GetBoolValue("ViewCells.loadFromFile", loadViewCells);
|
---|
[439] | 252 |
|
---|
| 253 | //-- load view cells from file if requested
|
---|
| 254 | if (loadViewCells)
|
---|
| 255 | {
|
---|
| 256 | char buff[100];
|
---|
| 257 | environment->GetStringValue("ViewCells.filename", buff);
|
---|
| 258 | string vcFilename(buff);
|
---|
| 259 | mViewCellsManager->LoadViewCells(vcFilename);
|
---|
| 260 | }
|
---|
[463] | 261 |
|
---|
[496] | 262 |
|
---|
| 263 | if (mUseGlRenderer)
|
---|
| 264 | renderer = new GlRendererBuffer(1024, 768, mSceneGraph, mViewCellsManager);
|
---|
| 265 |
|
---|
[463] | 266 | return true;
|
---|
[490] | 267 | }
|
---|
| 268 |
|
---|
| 269 |
|
---|
[491] | 270 | // use ascii format to store rays
|
---|
| 271 | #define USE_ASCII 0
|
---|
| 272 |
|
---|
| 273 |
|
---|
[490] | 274 | inline bool ilt(Intersectable *obj1, Intersectable *obj2)
|
---|
| 275 | {
|
---|
| 276 | return obj1->mId < obj2->mId;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
| 279 |
|
---|
| 280 | bool Preprocessor::LoadSamples(VssRayContainer &samples,
|
---|
| 281 | ObjectContainer &objects) const
|
---|
| 282 | {
|
---|
| 283 | std::stable_sort(objects.begin(), objects.end(), ilt);
|
---|
| 284 | char fileName[100];
|
---|
| 285 | environment->GetStringValue("Preprocessor.samplesFilename", fileName);
|
---|
| 286 |
|
---|
[491] | 287 | Vector3 origin, termination;
|
---|
| 288 | // HACK: needed only for lower_bound algorithm to find the
|
---|
| 289 | // intersected objects
|
---|
| 290 | MeshInstance sObj(NULL);
|
---|
| 291 | MeshInstance tObj(NULL);
|
---|
[490] | 292 |
|
---|
[491] | 293 | #if USE_ASCII
|
---|
| 294 | ifstream samplesIn(fileName, ios::binary);
|
---|
[490] | 295 | if (!samplesIn.is_open())
|
---|
| 296 | return false;
|
---|
| 297 |
|
---|
| 298 | string buf;
|
---|
| 299 | while (!(getline(samplesIn, buf)).eof())
|
---|
| 300 | {
|
---|
[491] | 301 | sscanf(buf.c_str(), "%f %f %f %f %f %f %d %d",
|
---|
[490] | 302 | &origin.x, &origin.y, &origin.z,
|
---|
[491] | 303 | &termination.x, &termination.y, &termination.z,
|
---|
| 304 | &(sObj.mId), &(tObj.mId));
|
---|
[490] | 305 |
|
---|
[491] | 306 | Intersectable *sourceObj = NULL;
|
---|
| 307 | Intersectable *termObj = NULL;
|
---|
| 308 |
|
---|
| 309 | if (sObj.mId >= 0)
|
---|
[490] | 310 | {
|
---|
| 311 | ObjectContainer::iterator oit =
|
---|
[491] | 312 | lower_bound(objects.begin(), objects.end(), &sObj, ilt);
|
---|
| 313 | sourceObj = *oit;
|
---|
| 314 | }
|
---|
| 315 |
|
---|
| 316 | if (tObj.mId >= 0)
|
---|
| 317 | {
|
---|
| 318 | ObjectContainer::iterator oit =
|
---|
| 319 | lower_bound(objects.begin(), objects.end(), &tObj, ilt);
|
---|
| 320 | termObj = *oit;
|
---|
| 321 | }
|
---|
[490] | 322 |
|
---|
[491] | 323 | samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
|
---|
| 324 | }
|
---|
| 325 | #else
|
---|
| 326 | ifstream samplesIn(fileName, ios::binary);
|
---|
| 327 | if (!samplesIn.is_open())
|
---|
| 328 | return false;
|
---|
| 329 |
|
---|
| 330 | while (1)
|
---|
| 331 | {
|
---|
| 332 | samplesIn.read(reinterpret_cast<char *>(&origin), sizeof(Vector3));
|
---|
| 333 | samplesIn.read(reinterpret_cast<char *>(&termination), sizeof(Vector3));
|
---|
| 334 | samplesIn.read(reinterpret_cast<char *>(&(sObj.mId)), sizeof(int));
|
---|
| 335 | samplesIn.read(reinterpret_cast<char *>(&(tObj.mId)), sizeof(int));
|
---|
| 336 |
|
---|
| 337 | if (samplesIn.eof())
|
---|
| 338 | break;
|
---|
| 339 |
|
---|
| 340 | Intersectable *sourceObj = NULL;
|
---|
| 341 | Intersectable *termObj = NULL;
|
---|
| 342 |
|
---|
| 343 | if (sObj.mId >= 0)
|
---|
| 344 | {
|
---|
| 345 | ObjectContainer::iterator oit =
|
---|
| 346 | lower_bound(objects.begin(), objects.end(), &sObj, ilt);
|
---|
| 347 | sourceObj = *oit;
|
---|
[490] | 348 | }
|
---|
[491] | 349 |
|
---|
| 350 | if (tObj.mId >= 0)
|
---|
[490] | 351 | {
|
---|
[491] | 352 | ObjectContainer::iterator oit =
|
---|
| 353 | lower_bound(objects.begin(), objects.end(), &tObj, ilt);
|
---|
| 354 | termObj = *oit;
|
---|
[490] | 355 | }
|
---|
[491] | 356 |
|
---|
| 357 | samples.push_back(new VssRay(origin, termination, sourceObj, termObj));
|
---|
[490] | 358 | }
|
---|
[491] | 359 |
|
---|
| 360 | #endif
|
---|
[490] | 361 | samplesIn.close();
|
---|
| 362 |
|
---|
| 363 | return true;
|
---|
| 364 | }
|
---|
| 365 |
|
---|
| 366 | bool Preprocessor::WriteSamples(const VssRayContainer &samples) const
|
---|
| 367 | {
|
---|
[491] | 368 | char fileName[100];
|
---|
| 369 | environment->GetStringValue("Preprocessor.samplesFilename", fileName);
|
---|
| 370 |
|
---|
[490] | 371 |
|
---|
| 372 | VssRayContainer::const_iterator it, it_end = samples.end();
|
---|
| 373 |
|
---|
[491] | 374 | #if USE_ASCII
|
---|
| 375 | ofstream samplesOut(fileName);
|
---|
[490] | 376 | if (!samplesOut.is_open())
|
---|
| 377 | return false;
|
---|
| 378 |
|
---|
| 379 | for (it = samples.begin(); it != it_end; ++ it)
|
---|
| 380 | {
|
---|
| 381 | VssRay *ray = *it;
|
---|
[491] | 382 | int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;
|
---|
| 383 | int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;
|
---|
| 384 |
|
---|
[490] | 385 | samplesOut << ray->GetOrigin().x << " " << ray->GetOrigin().y << " " << ray->GetOrigin().z << " "
|
---|
| 386 | << ray->GetTermination().x << " " << ray->GetTermination().y << " " << ray->GetTermination().z << " "
|
---|
[491] | 387 | << sourceid << " " << termid << "\n";
|
---|
[490] | 388 | }
|
---|
[491] | 389 | #else
|
---|
| 390 | ofstream samplesOut(fileName, ios::binary);
|
---|
| 391 | if (!samplesOut.is_open())
|
---|
| 392 | return false;
|
---|
| 393 |
|
---|
| 394 | for (it = samples.begin(); it != it_end; ++ it)
|
---|
| 395 | {
|
---|
| 396 | VssRay *ray = *it;
|
---|
| 397 | Vector3 origin(ray->GetOrigin());
|
---|
| 398 | Vector3 termination(ray->GetTermination());
|
---|
| 399 |
|
---|
| 400 | int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1;
|
---|
| 401 | int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1;
|
---|
| 402 |
|
---|
| 403 | samplesOut.write(reinterpret_cast<char *>(&origin), sizeof(Vector3));
|
---|
| 404 | samplesOut.write(reinterpret_cast<char *>(&termination), sizeof(Vector3));
|
---|
| 405 | samplesOut.write(reinterpret_cast<char *>(&sourceid), sizeof(int));
|
---|
| 406 | samplesOut.write(reinterpret_cast<char *>(&termid), sizeof(int));
|
---|
| 407 | }
|
---|
| 408 | #endif
|
---|
[490] | 409 | samplesOut.close();
|
---|
| 410 | return true;
|
---|
| 411 | }
|
---|