#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), mViewCellsType(BSP_VIEW_CELLS) { } Preprocessor::~Preprocessor() { DeleteViewCells(); DEL_PTR(mBspTree); DEL_PTR(mKdTree); } bool Preprocessor::LoadViewCells(const string filename) { X3dParser parser; int maxViewCells = 0; environment->GetFloatValue("ViewCells.height", parser.mViewCellHeight); environment->GetIntValue("ViewCells.maxViewCells", maxViewCells); bool loaded = parser.ParseFile(filename, mViewCells); if (maxViewCells > 0) { while (mViewCells.size() > maxViewCells) { ViewCell *vc = mViewCells.back(); DEL_PTR(vc); mViewCells.pop_back(); } } return loaded; } bool Preprocessor::ParseViewCellsOptions() { // parse type of view cells char viewCellsStr[64]; environment->GetStringValue("ViewCells.hierarchyType", viewCellsStr); int vcType = BSP_VIEW_CELLS; if (strcmp(viewCellsStr, "bspTree") == 0) vcType = BSP_VIEW_CELLS; else if (strcmp(viewCellsStr, "kdTree") == 0) vcType = KD_VIEW_CELLS; else if (strcmp(viewCellsStr, "sceneDependent") == 0) vcType = SCENE_DEPENDENT; else { cerr<<"Wrong view cells type" << viewCellsStr << endl; exit(1); } // decide about view cell subdivision type used for preprocessing switch (vcType) { case BSP_VIEW_CELLS: case KD_VIEW_CELLS: mViewCellsType = vcType; break; case SCENE_DEPENDENT: mViewCellsType = BSP_VIEW_CELLS; // TODO break; } return true; } void Preprocessor::DeleteViewCells() { for (int i = 0; i < mViewCells.size(); ++ i) delete mViewCells[i]->GetMesh(); CLEAR_CONTAINER(mViewCells); } bool Preprocessor::GenerateViewCells() { // TODO // HACK: derive view cells from the scene objects ObjectContainer objects; int maxViewCells = 0; environment->GetIntValue("ViewCells.maxViewCells", maxViewCells); mSceneGraph->CollectObjects(&objects); ViewCell::DeriveViewCells(objects, mViewCells, maxViewCells); return true; } 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(&mUnbounded); ObjectContainer objects; RayContainer *rays = new RayContainer(); switch (BspTree::sConstructionMethod) { case BspTree::FROM_INPUT_VIEW_CELLS: mBspTree->Construct(mViewCells); break; case BspTree::FROM_SCENE_GEOMETRY: DeleteViewCells(); // we generate new view cells mSceneGraph->CollectObjects(&objects); mBspTree->Construct(objects, &mViewCells); break; case BspTree::FROM_RAYS: DeleteViewCells(); // we generate new view cells mSceneGraph->CollectObjects(&objects); 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; }