#include "SceneGraph.h" #include "KdTree.h" #include "SamplingPreprocessor.h" #include "X3dExporter.h" #include "Environment.h" #include "MutualVisibility.h" #include "Polygon3.h" #include "ViewCell.h" #include "ViewCellsManager.h" #include "RenderSimulator.h" namespace GtpVisibilityPreprocessor { SamplingPreprocessor::SamplingPreprocessor(): Preprocessor(), mPass(0) { // this should increase coherence of the samples Environment::GetSingleton()->GetIntValue("SamplingPreprocessor.samplesPerPass", mSamplesPerPass); Environment::GetSingleton()->GetIntValue("SamplingPreprocessor.totalSamples", mTotalSamples); mStats.open("stats.log"); } SamplingPreprocessor::~SamplingPreprocessor() { CLEAR_CONTAINER(mSampleRays); CLEAR_CONTAINER(mVssSampleRays); } Intersectable * SamplingPreprocessor::CastRay( const Vector3 &origin, const Vector3 &direction ) { AxisAlignedBox3 box = mViewCellsManager->GetViewSpaceBox(); AxisAlignedBox3 sbox = box; sbox.Enlarge(Vector3(-Limits::Small)); if (!sbox.IsInside(origin)) return 0; Vector3 point, normal; Intersectable *object; // cast ray to KD tree to find intersection with other objects #if 0 object = CastSimpleRay(origin, direction, mKdTree->GetBox(), point, normal ); #endif if (mDetectEmptyViewSpace && DotProd(normal, direction) >= 0) { object = NULL; } return object; } void SamplingPreprocessor::HoleSamplingPass() { vector leaves; mKdTree->CollectLeaves(leaves); // go through all the leaves and evaluate their passing contribution for (int i=0 ; i < leaves.size(); i++) { KdLeaf *leaf = leaves[i]; cout<mPassingRays<mKdPvs.mEntries.begin(); for (; i != object->mKdPvs.mEntries.end(); i++) { KdNode *node = (*i).first; node->Mail(); } Debug << "Get all neighbours from PVS" << endl; vector invisibleNeighbors; // get all neighbors of all PVS nodes i = object->mKdPvs.mEntries.begin(); for (; i != object->mKdPvs.mEntries.end(); i++) { KdNode *node = (*i).first; mKdTree->FindNeighbors(node, invisibleNeighbors, true); AxisAlignedBox3 box = object->GetBox(); for (int j=0; j < invisibleNeighbors.size(); j++) { int visibility = ComputeBoxVisibility(mSceneGraph, mKdTree, box, mKdTree->GetBox(invisibleNeighbors[j]), 1e-6f); // exit(0); } // now rank all the neighbors according to probability that a new // sample creates some contribution } } bool SamplingPreprocessor::ComputeVisibility() { Debug << "type: sampling" << endl; cout<<"Sampling Preprocessor started\n"<GetViewPoint(origin); direction = UniformRandomVector(); ViewCell *viewcell = mViewCellsManager->GetViewCell(origin); if (viewcell && viewcell->GetValid()) { // cast rays in both directions to make the number of samples comparable // with the global sampling method which also casts a "double" ray per sample for (int j=0; j < 2; j++) { Intersectable *object = CastRay(origin, direction); if (object) { // if ray not outside of view space float contribution; int pvsContribution = 0; float relativePvsContribution = 0; if (object) { float pdf = 1.0f; if (viewcell->GetPvs().GetSampleContribution(object, pdf, contribution)) ++pvsContribution; relativePvsContribution += contribution; viewcell->GetPvs().AddSample(object, pdf); } } direction = -direction; } } if (samples > mTotalSamples) break; } // mVssRays.PrintStatistics(mStats); mStats << "#Time\n" << TimeDiff(startTime, GetTime())*1e-3<PrintPvsStatistics(mStats); // ComputeRenderError(); } // HoleSamplingPass(); if (0) { Exporter *exporter = Exporter::GetExporter("ray-density.x3d"); exporter->SetExportRayDensity(true); exporter->ExportKdTree(*mKdTree); delete exporter; } // $$JB temporary removed // mViewCellsManager->PostProcess(objects, mSampleRays); //-- several visualizations and statistics Debug << "view cells after post processing: " << endl; mViewCellsManager->PrintStatistics(Debug); //-- render simulation after merge cout << "\nevaluating bsp view cells render time after merge ... "; mRenderSimulator->RenderScene(); SimulationStatistics ss; mRenderSimulator->GetStatistics(ss); cout << " finished" << endl; cout << ss << endl; Debug << ss << endl; // $$JB temporary removed //mViewCellsManager->Visualize(objects, mSampleRays); return true; } }