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