#include "SceneGraph.h" #include "Exporter.h" #include "UnigraphicsParser.h" #include "X3dParser.h" #include "Preprocessor.h" #include "ViewCell.h" #include "Environment.h" Preprocessor::Preprocessor(): mKdTree(NULL), mBspTree(NULL) { } Preprocessor::~Preprocessor() { CLEAR_CONTAINER(mViewCells); DEL_PTR(mBspTree); DEL_PTR(mKdTree); } bool Preprocessor::LoadViewCells(const string filename) { X3dParser *parser = new X3dParser; bool result = parser->ParseFile(filename, mViewCells); if (result) { Exporter *exporter = Exporter::GetExporter("viewcells.x3d"); if (exporter) { exporter->ExportViewCells(&mViewCells); delete exporter; } Debug << "Generating view cells" << endl; GenerateViewCells(); Debug << "Generated view cells" << endl; } DEL_PTR(parser); return result; } bool Preprocessor::GenerateViewCells() { return BuildBspTree(); } bool Preprocessor::LoadScene(const string filename) { // use leaf nodes of the original spatial hiearrchy as occludees mSceneGraph = new SceneGraph; Parser *parser; if (strstr(filename.c_str(), ".x3d")) parser = new X3dParser; else parser = new UnigraphicsParser; bool result = parser->ParseFile(filename, &mSceneGraph->mRoot); delete parser; return result; } bool Preprocessor::ExportPreprocessedData(const string filename) { return false; } bool Preprocessor::BuildKdTree() { mKdTree = new KdTree; // add mesh instances of the scene graph to the root of the tree KdLeaf *root = (KdLeaf *)mKdTree->GetRoot(); mSceneGraph->CollectObjects(&root->mObjects); mKdTree->Construct(); return true; } bool Preprocessor::BuildBspTree() { DEL_PTR(mBspTree); mBspTree = new BspTree(); char constructionMethodStr[64]; int maxViewCells = 0; environment->GetIntValue("BspTree.maxViewCells", maxViewCells); environment->GetStringValue("BspTree.constructionMethod", constructionMethodStr); int constructionMethod = BspTree::VIEWCELLS; if (strcmp(constructionMethodStr, "viewCells") == 0) constructionMethod = BspTree::VIEWCELLS; else if (strcmp(constructionMethodStr, "sceneGeometry") == 0) constructionMethod = BspTree::SCENE_GEOMETRY; else if (strcmp(constructionMethodStr, "rays") == 0) constructionMethod = BspTree::RAYS; else { cerr << "Wrong bsp construction method " << constructionMethodStr << endl; exit(1); } ObjectContainer objects; RayContainer rays; switch (constructionMethod) { case BspTree::VIEWCELLS: Debug << "Construction method: view cells\n"; // derive view cells from the scene objects if (mViewCells.empty()) { Debug << "View cells empty => generating new ones\n"; Debug.flush(); mSceneGraph->CollectObjects(&objects); ViewCell::DeriveViewCells(objects, mViewCells, maxViewCells); } mBspTree->Construct(mViewCells); break; case BspTree::SCENE_GEOMETRY: Debug << "Construction method: geometry\n"; CLEAR_CONTAINER(mViewCells); // we generate new view cells mSceneGraph->CollectObjects(&objects); mBspTree->Construct(objects, &mViewCells); break; case BspTree::RAYS: Debug << "Construction method: rays\n"; CLEAR_CONTAINER(mViewCells); // we generate new view cells mBspTree->Construct(rays, &mViewCells); break; default: Debug << "Error: Method not available\n"; break; } return true; } void Preprocessor::KdTreeStatistics(ostream &s) { s<GetStatistics(); } void Preprocessor::BspTreeStatistics(ostream &s) { s << mBspTree->GetStatistics(); } bool Preprocessor::Export( const string filename, const bool scene, const bool kdtree, const bool bsptree ) { Exporter *exporter = Exporter::GetExporter(filename); if (exporter) { if (scene) exporter->ExportScene(mSceneGraph->mRoot); if (kdtree) { exporter->SetWireframe(); exporter->ExportKdTree(*mKdTree); } if (bsptree) { exporter->SetWireframe(); exporter->ExportBspTree(*mBspTree); } delete exporter; return true; } return false; }