#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(): 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 ) { static Ray ray; AxisAlignedBox3 box = mViewCellsManager->GetViewSpaceBox(); AxisAlignedBox3 sbox = box; sbox.Enlarge(Vector3(-Limits::Small)); if (!sbox.IsInside(origin)) return 0; ray.intersections.clear(); // do not store anything else then intersections at the ray ray.Init(origin, direction, Ray::LOCAL_RAY); ray.mFlags &= ~Ray::CULL_BACKFACES; // cast ray to KD tree to find intersection with other objects Intersectable *objectA = NULL; Vector3 pointA; float bsize = Magnitude(box.Size()); if (mKdTree->CastRay(ray)) { if (!mDetectEmptyViewSpace || DotProd(ray.intersections[0].mNormal, direction) < 0) { objectA = ray.intersections[0].mObject; pointA = ray.Extrap(ray.intersections[0].mT); } } return objectA; } 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"<SetViewSpaceBox(mKdTree->GetBox()); // construct view cells using it's own set of samples mViewCellsManager->Construct(this); //-- several visualizations and statistics Debug << "view cells construction finished: " << endl; mViewCellsManager->PrintStatistics(Debug); } int samples = 0; int i=0; while (samples < mTotalSamples) { for (i=0; i < mSamplesPerPass; i++, samples++) { if (i%10000 == 0) cout<<"+"; Vector3 origin, direction; mViewCellsManager->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; } }