#include "SceneGraph.h" #include "Exporter.h" #include "UnigraphicsParser.h" #include "X3dParser.h" #include "Preprocessor.h" #include "ViewCell.h" #include "Environment.h" #include "ViewCellsManager.h" #include "ViewCellBsp.h" #include "VspBspTree.h" #include "RenderSimulator.h" #include "GlRenderer.h" #include "PlyParser.h" #include "SamplingStrategy.h" #include "VspOspTree.h" namespace GtpVisibilityPreprocessor { const static bool ADDITIONAL_GEOMETRY_HACK = false; //Preprocessor *preprocessor; // HACK: Artificially modify scene to watch rendercost changes static void AddGeometry(SceneGraph *scene) { scene->mRoot->UpdateBox(); AxisAlignedBox3 sceneBox = scene->GetBox(); int n = 200; if (0){ // form grid of boxes for (int i = 0; i < n; ++ i) { for (int j = 0; j < n; ++ j) { const Vector3 scale2((float)j * 0.8f / n + 0.1f, 0.05f, (float)i * 0.8f / (float)n + 0.1f); const Vector3 pt2 = sceneBox.Min() + scale2 * (sceneBox.Max() - sceneBox.Min()); const Vector3 boxSize = sceneBox.Size() * Vector3(0.0025f, 0.01f, 0.0025f); AxisAlignedBox3 box(pt2, pt2 + boxSize); Mesh *mesh = CreateMeshFromBox(box); mesh->Preprocess(); MeshInstance *mi = new MeshInstance(mesh); scene->mRoot->mGeometry.push_back(mi); } } for (int i = 0; i < n; ++ i) { for (int j = 0; j < n; ++ j) { const Vector3 scale2(0.15f, (float)j * 0.8f / n + 0.1f, (float)i * 0.8f / (float)n + 0.1f); Vector3 pt2 = sceneBox.Min() + scale2 * (sceneBox.Max() - sceneBox.Min()); Vector3 boxSize = sceneBox.Size() * Vector3(0.0025f, 0.01f, 0.0025f); AxisAlignedBox3 box(pt2, pt2 + boxSize); Mesh *mesh = CreateMeshFromBox(box); mesh->Preprocess(); MeshInstance *mi = new MeshInstance(mesh); scene->mRoot->mGeometry.push_back(mi); } } for (int i = 0; i < n; ++ i) { const Vector3 scale2(2, 0.2f, (float)i * 0.8f / (float)n + 0.1f); Vector3 pt2 = sceneBox.Min() + scale2 * (sceneBox.Max() - sceneBox.Min()); //Vector3 boxSize = sceneBox.Size() * Vector3(0.0025, 0.01, 0.0025); Vector3 boxSize = sceneBox.Size() * Vector3(0.005f, 0.02f, 0.005f); AxisAlignedBox3 box(pt2 + 0.1f, pt2 + boxSize); Mesh *mesh = CreateMeshFromBox(box); mesh->Preprocess(); MeshInstance *mi = new MeshInstance(mesh); scene->mRoot->mGeometry.push_back(mi); } scene->mRoot->UpdateBox(); } // plane separating view space regions if (1) { const Vector3 scale(1.0f, 0.0, 0); Vector3 pt = sceneBox.Min() + scale * (sceneBox.Max() - sceneBox.Min()); Plane3 cuttingPlane(Vector3(1, 0, 0), pt); Mesh *planeMesh = new Mesh(); Polygon3 *poly = sceneBox.CrossSection(cuttingPlane); IncludePolyInMesh(*poly, *planeMesh); planeMesh->Preprocess(); MeshInstance *planeMi = new MeshInstance(planeMesh); scene->mRoot->mGeometry.push_back(planeMi); } } Preprocessor::Preprocessor(): mKdTree(NULL), mBspTree(NULL), mVspBspTree(NULL), mViewCellsManager(NULL), mRenderSimulator(NULL) { Environment::GetSingleton()->GetBoolValue("Preprocessor.useGlRenderer", mUseGlRenderer); // renderer will be constructed when the scene graph and viewcell manager will be known renderer = NULL; Environment::GetSingleton()->GetBoolValue("Preprocessor.useGlDebugger", mUseGlDebugger); Environment::GetSingleton()->GetBoolValue("Preprocessor.loadPolygonsAsMeshes", mLoadPolygonsAsMeshes); Environment::GetSingleton()->GetBoolValue("Preprocessor.quitOnFinish", mQuitOnFinish); Environment::GetSingleton()->GetBoolValue("Preprocessor.computeVisibility", mComputeVisibility); Environment::GetSingleton()->GetBoolValue("Preprocessor.detectEmptyViewSpace", mDetectEmptyViewSpace); Environment::GetSingleton()->GetBoolValue("Preprocessor.exportVisibility", mExportVisibility ); char buffer[256]; Environment::GetSingleton()->GetStringValue("Preprocessor.visibilityFile", buffer); mVisibilityFileName = buffer; Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilityFilter", mApplyVisibilityFilter ); Environment::GetSingleton()->GetBoolValue("Preprocessor.applyVisibilitySpatialFilter", mApplyVisibilitySpatialFilter ); Environment::GetSingleton()->GetFloatValue("Preprocessor.visibilityFilterWidth", mVisibilityFilterWidth); Debug << "detect empty view space=" << mDetectEmptyViewSpace << endl; Debug << "load polygons as meshes: " << mLoadPolygonsAsMeshes << endl; } Preprocessor::~Preprocessor() { cout << "cleaning up" << endl; cout << "Deleting view cells manager ... \n"; DEL_PTR(mViewCellsManager); cout << "done.\n"; cout << "Deleting bsp tree ... \n"; DEL_PTR(mBspTree); cout << "done.\n"; cout << "Deleting kd tree...\n"; DEL_PTR(mKdTree); cout << "done.\n"; cout << "Deleting vsp tree...\n"; DEL_PTR(mVspTree); cout << "done.\n"; cout << "Deleting osp tree...\n"; DEL_PTR(mOspTree); cout << "done.\n"; cout << "Deleting vspbsp tree...\n"; DEL_PTR(mVspBspTree); cout << "done.\n"; cout << "Deleting scene graph...\n"; DEL_PTR(mSceneGraph); cout << "done.\n"; DEL_PTR(mRenderSimulator); DEL_PTR(renderer); } int SplitFilenames(const string str, vector &filenames) { int pos = 0; while(1) { int npos = (int)str.find(';', pos); if (npos < 0 || npos - pos < 1) break; filenames.push_back(string(str, pos, npos - pos)); pos = npos + 1; } filenames.push_back(string(str, pos, str.size() - pos)); return (int)filenames.size(); } bool Preprocessor::LoadScene(const string filename) { // use leaf nodes of the original spatial hierarchy as occludees mSceneGraph = new SceneGraph; Parser *parser; vector filenames; int files = SplitFilenames(filename, filenames); cout << "number of input files: " << files << endl; bool result = false; if (files == 1) { if (strstr(filename.c_str(), ".x3d")) parser = new X3dParser; else if (strstr(filename.c_str(), ".ply") || strstr(filename.c_str(), ".plb")) parser = new PlyParser; else parser = new UnigraphicsParser; cout<ParseFile(filename, &mSceneGraph->mRoot, mLoadPolygonsAsMeshes); delete parser; } else { // root for different files mSceneGraph->mRoot = new SceneGraphNode; for (int i= 0; i < filenames.size(); i++) { if (strstr(filenames[i].c_str(), ".x3d")) parser = new X3dParser; else parser = new UnigraphicsParser; SceneGraphNode *node; if (parser->ParseFile(filenames[i], &node)) { mSceneGraph->mRoot->mChildren.push_back(node); // at least one file parsed result = true; } delete parser; } } if (result) { // HACK if (ADDITIONAL_GEOMETRY_HACK) AddGeometry(mSceneGraph); mSceneGraph->AssignObjectIds(); int intersectables, faces; mSceneGraph->GetStatistics(intersectables, faces); cout<CollectObjects(&mObjects); mSceneGraph->mRoot->UpdateBox(); if (0) { Exporter *exporter = Exporter::GetExporter("testload.x3d"); if (exporter) { exporter->ExportGeometry(mObjects); delete exporter; } } } return result; } bool Preprocessor::ExportPreprocessedData(const string filename) { mViewCellsManager->ExportViewCells(filename, true, mObjects); return true; } bool Preprocessor::PostProcessVisibility() { if (mApplyVisibilityFilter || mApplyVisibilitySpatialFilter) { cout<<"Applying visibility filter ..."; cout<<"filter width = " << mVisibilityFilterWidth << endl; if (!mViewCellsManager) return false; mViewCellsManager->ApplyFilter(mKdTree, mApplyVisibilityFilter ? mVisibilityFilterWidth : -1.0f, mApplyVisibilitySpatialFilter ? mVisibilityFilterWidth : -1.0f); cout << "done." << endl; } // export the preprocessed information to a file if (mExportVisibility) ExportPreprocessedData(mVisibilityFileName); return true; } 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); long startTime = GetTime(); cout << "building kd tree ... " << endl; mKdTree->Construct(); cout << "construction finished in " << TimeDiff(startTime, GetTime()) * 1e-3 << " secs " << endl; 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; } bool Preprocessor::PrepareViewCells() { //-- parse view cells construction method Environment::GetSingleton()->GetBoolValue("ViewCells.loadFromFile", mLoadViewCells); char buf[100]; if (mLoadViewCells) { Environment::GetSingleton()->GetStringValue("ViewCells.filename", buf); mViewCellsManager = ViewCellsManager::LoadViewCells(buf, &mObjects, true); } else { //-- parse type of view cell container Environment::GetSingleton()->GetStringValue("ViewCells.type", buf); mViewCellsManager = CreateViewCellsManager(buf); // default view space is the extent of the scene mViewCellsManager->SetViewSpaceBox(mSceneGraph->GetBox()); } //-- parameters for render heuristics evaluation float objRenderCost = 0, vcOverhead = 0, moveSpeed = 0; Environment::GetSingleton()->GetFloatValue("Simulation.objRenderCost",objRenderCost); Environment::GetSingleton()->GetFloatValue("Simulation.vcOverhead", vcOverhead); Environment::GetSingleton()->GetFloatValue("Simulation.moveSpeed", moveSpeed); mRenderSimulator = new RenderSimulator(mViewCellsManager, objRenderCost, vcOverhead, moveSpeed); mViewCellsManager->SetRenderer(mRenderSimulator); if (mUseGlRenderer || mUseGlDebugger) { // NOTE: render texture should be power of 2 and square // renderer must be initialised // $$matt // renderer = new GlRendererBuffer(1024, 768, mSceneGraph, mViewCellsManager, mKdTree); // renderer->makeCurrent(); } return true; } ViewCellsManager *Preprocessor::CreateViewCellsManager(const char *name) { if (strcmp(name, "kdTree") == 0) { mViewCellsManager = new KdViewCellsManager(mKdTree); } else if (strcmp(name, "bspTree") == 0) { Debug << "view cell type: Bsp" << endl; mBspTree = new BspTree(); mViewCellsManager = new BspViewCellsManager(mBspTree); } else if (strcmp(name, "vspBspTree") == 0) { Debug << "view cell type: VspBsp" << endl; mVspBspTree = new VspBspTree(); mViewCellsManager = new VspBspViewCellsManager(mVspBspTree); } else if (strcmp(name, "vspOspTree") == 0) { mVspTree = new VspTree(); mOspTree = new OspTree(); // HACK for testing if per kd evaluation works!! mOspTree = new OspTree(*mKdTree); mViewCellsManager = new VspOspViewCellsManager(mVspTree, mOspTree); } else if (strcmp(name, "sceneDependent") == 0) { Debug << "view cell type: Bsp" << endl; //TODO mBspTree = new BspTree(); mViewCellsManager = new BspViewCellsManager(mBspTree); } else { cerr << "Wrong view cells type " << name << "!!!" << endl; exit(1); } return mViewCellsManager; } // use ascii format to store rays #define USE_ASCII 0 static inline bool ilt(Intersectable *obj1, Intersectable *obj2) { return obj1->mId < obj2->mId; } bool Preprocessor::LoadKdTree() { return true; } bool Preprocessor::ExportKdTree() { return true; } bool Preprocessor::LoadSamples(VssRayContainer &samples, ObjectContainer &objects) const { std::stable_sort(objects.begin(), objects.end(), ilt); char fileName[100]; Environment::GetSingleton()->GetStringValue("Preprocessor.samplesFilename", fileName); Vector3 origin, termination; // HACK: needed only for lower_bound algorithm to find the // intersected objects MeshInstance sObj(NULL); MeshInstance tObj(NULL); #if USE_ASCII ifstream samplesIn(fileName); if (!samplesIn.is_open()) return false; string buf; while (!(getline(samplesIn, buf)).eof()) { sscanf(buf.c_str(), "%f %f %f %f %f %f %d %d", &origin.x, &origin.y, &origin.z, &termination.x, &termination.y, &termination.z, &(sObj.mId), &(tObj.mId)); Intersectable *sourceObj = NULL; Intersectable *termObj = NULL; if (sObj.mId >= 0) { ObjectContainer::iterator oit = lower_bound(objects.begin(), objects.end(), &sObj, ilt); sourceObj = *oit; } if (tObj.mId >= 0) { ObjectContainer::iterator oit = lower_bound(objects.begin(), objects.end(), &tObj, ilt); termObj = *oit; } samples.push_back(new VssRay(origin, termination, sourceObj, termObj)); } #else ifstream samplesIn(fileName, ios::binary); if (!samplesIn.is_open()) return false; while (1) { samplesIn.read(reinterpret_cast(&origin), sizeof(Vector3)); samplesIn.read(reinterpret_cast(&termination), sizeof(Vector3)); samplesIn.read(reinterpret_cast(&(sObj.mId)), sizeof(int)); samplesIn.read(reinterpret_cast(&(tObj.mId)), sizeof(int)); if (samplesIn.eof()) break; Intersectable *sourceObj = NULL; Intersectable *termObj = NULL; if (sObj.mId >= 0) { ObjectContainer::iterator oit = lower_bound(objects.begin(), objects.end(), &sObj, ilt); sourceObj = *oit; } if (tObj.mId >= 0) { ObjectContainer::iterator oit = lower_bound(objects.begin(), objects.end(), &tObj, ilt); termObj = *oit; } samples.push_back(new VssRay(origin, termination, sourceObj, termObj)); } #endif samplesIn.close(); return true; } bool Preprocessor::ExportSamples(const VssRayContainer &samples) const { char fileName[100]; Environment::GetSingleton()->GetStringValue("Preprocessor.samplesFilename", fileName); VssRayContainer::const_iterator it, it_end = samples.end(); #if USE_ASCII ofstream samplesOut(fileName); if (!samplesOut.is_open()) return false; for (it = samples.begin(); it != it_end; ++ it) { VssRay *ray = *it; int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1; int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1; samplesOut << ray->GetOrigin().x << " " << ray->GetOrigin().y << " " << ray->GetOrigin().z << " " << ray->GetTermination().x << " " << ray->GetTermination().y << " " << ray->GetTermination().z << " " << sourceid << " " << termid << "\n"; } #else ofstream samplesOut(fileName, ios::binary); if (!samplesOut.is_open()) return false; for (it = samples.begin(); it != it_end; ++ it) { VssRay *ray = *it; Vector3 origin(ray->GetOrigin()); Vector3 termination(ray->GetTermination()); int sourceid = ray->mOriginObject ? ray->mOriginObject->mId : -1; int termid = ray->mTerminationObject ? ray->mTerminationObject->mId : -1; samplesOut.write(reinterpret_cast(&origin), sizeof(Vector3)); samplesOut.write(reinterpret_cast(&termination), sizeof(Vector3)); samplesOut.write(reinterpret_cast(&sourceid), sizeof(int)); samplesOut.write(reinterpret_cast(&termid), sizeof(int)); } #endif samplesOut.close(); return true; } #if 0 // matt: implemented interface samplestrategy bool Preprocessor::GenerateRays( const int number, const int sampleType, SimpleRayContainer &rays ) { Vector3 origin, direction; int startSize = (int)rays.size(); for (int i=0; (int)rays.size() - startSize < number; i ++) { // now get the direction switch (sampleType) { case OBJECT_BASED_DISTRIBUTION: { mViewCellsManager->GetViewPoint(origin); Vector3 point; Vector3 normal; int i = RandomValue(0, mObjects.size() - 1); Intersectable *object = mObjects[i]; object->GetRandomSurfacePoint(point, normal); direction = point - origin; } break; case OBJECT_DIRECTION_BASED_DISTRIBUTION: { int i = RandomValue(0, mObjects.size() - 1); Intersectable *object = mObjects[i]; Vector3 normal; object->GetRandomSurfacePoint(origin, normal); direction = UniformRandomVector(normal); origin += 0.1f*direction; } break; case DIRECTION_BASED_DISTRIBUTION: mViewCellsManager->GetViewPoint(origin); direction = UniformRandomVector(); break; case DIRECTION_BOX_BASED_DISTRIBUTION: { mViewCellsManager->GetViewPoint(origin); float alpha = RandomValue(0.0f, 2*M_PI); float beta = RandomValue(-M_PI/2, M_PI/2); direction = VssRay::GetDirection(alpha, beta); break; } case SPATIAL_BOX_BASED_DISTRIBUTION: mViewCellsManager->GetViewPoint(origin); direction = mKdTree->GetBox().GetRandomPoint() - origin; break; default: // unsuported distribution type return false; } // $$ jb the pdf is yet not correct for all sampling methods! float pdf = 1.0f; float c = Magnitude(direction); if (c > Limits::Small) { direction*=1.0f/c; rays.AddRay(SimpleRay(origin, direction, pdf)); } } return true; } #endif bool Preprocessor::GenerateRays(const int number, const int sampleType, SimpleRayContainer &rays) { Vector3 origin, direction; const int startSize = (int)rays.size(); SamplingStrategy *strategy = GenerateSamplingStrategy(sampleType); if (!strategy) return false; for (int i=0; (int)rays.size() - startSize < number; ++ i) { SimpleRay newRay; bool success = strategy->GenerateSample(newRay); if (success) rays.AddRay(newRay); } delete strategy; return true; } SamplingStrategy *Preprocessor::GenerateSamplingStrategy(const int strategyId) const { switch (strategyId) { case OBJECT_BASED_DISTRIBUTION: return new ObjectBasedDistribution(*this); case OBJECT_DIRECTION_BASED_DISTRIBUTION: return new ObjectDirectionBasedDistribution(*this); case DIRECTION_BASED_DISTRIBUTION: return new DirectionBasedDistribution(*this); case DIRECTION_BOX_BASED_DISTRIBUTION: return new DirectionBoxBasedDistribution(*this); case SPATIAL_BOX_BASED_DISTRIBUTION: return new SpatialBoxBasedDistribution(*this); //case OBJECTS_INTERIOR_DISTRIBUTION: // return new ObjectsInteriorDistribution(*this); default: // no valid strategy return NULL; } // should never come here return NULL; } }