#include "SceneGraph.h" #include "KdTree.h" #include "RssPreprocessor.h" #include "X3dExporter.h" #include "Environment.h" #include "MutualVisibility.h" #include "Polygon3.h" #include "ViewCell.h" #include "VssRay.h" #include "RssTree.h" #include "ViewCellsManager.h" static bool useViewSpaceBox = true;//true; static bool use2dSampling = false; static bool fromBoxVisibility = true; static bool exportPvs = false; RssPreprocessor::RssPreprocessor(): mPass(0), mVssRays() { // this should increase coherence of the samples environment->GetIntValue("RssPreprocessor.samplesPerPass", mSamplesPerPass); environment->GetIntValue("RssPreprocessor.initialSamples", mInitialSamples); environment->GetIntValue("RssPreprocessor.vssSamples", mRssSamples); environment->GetIntValue("RssPreprocessor.vssSamplesPerPass", mRssSamplesPerPass); environment->GetBoolValue("RssPreprocessor.useImportanceSampling", mUseImportanceSampling); environment->GetIntValue("BspTree.Construction.samples", mBspConstructionSamples); mStats.open("stats.log"); } RssPreprocessor::~RssPreprocessor() { CLEAR_CONTAINER(mVssRays); } void RssPreprocessor::SetupRay(Ray &ray, const Vector3 &point, const Vector3 &direction ) { ray.intersections.clear(); // do not store anything else then intersections at the ray ray.Init(point, direction, Ray::LOCAL_RAY); } int RssPreprocessor::CastRay( Vector3 &viewPoint, Vector3 &direction, VssRayContainer &vssRays ) { int hits = 0; static Ray ray; AxisAlignedBox3 box = mKdTree->GetBox(); AxisAlignedBox3 sbox = box; sbox.Enlarge(Vector3(-Limits::Small)); if (!sbox.IsInside(viewPoint)) return 0; SetupRay(ray, viewPoint, direction); // cast ray to KD tree to find intersection with other objects Intersectable *objectA, *objectB; Vector3 pointA, pointB; float bsize = Magnitude(box.Size()); if (mKdTree->CastRay(ray)) { objectA = ray.intersections[0].mObject; pointA = ray.Extrap(ray.intersections[0].mT); } else { objectA = NULL; // compute intersection with the scene bounding box float tmin, tmax; box.ComputeMinMaxT(ray, &tmin, &tmax); if (tmax > bsize) { //cerr<<"Warning: tmax > box size tmax="<CastRay(ray)) { objectB = ray.intersections[0].mObject; pointB = ray.Extrap(ray.intersections[0].mT); } else { objectB = NULL; float tmin, tmax; box.ComputeMinMaxT(ray, &tmin, &tmax); if (tmax > bsize) { //cerr<<"Warning: tmax > box size tmax="<GetBox(); // shrink the box in the y direction return box.GetRandomPoint(); } Vector3 RssPreprocessor::GetDirection(const Vector3 &viewpoint, AxisAlignedBox3 *viewSpaceBox ) { Vector3 point; if (!use2dSampling) { Vector3 normal; int i = RandomValue(0, mObjects.size()-1); Intersectable *object = mObjects[i]; object->GetRandomSurfacePoint(point, normal); } else { AxisAlignedBox3 box; if (viewSpaceBox) box =*viewSpaceBox; else box = mKdTree->GetBox(); point = box.GetRandomPoint(); point.y = viewpoint.y; } return point - viewpoint; } int RssPreprocessor::GenerateImportanceRays(RssTree *rssTree, const int desiredSamples, SimpleRayContainer &rays ) { int num; float avgPvsSize; float avgRayContribution; float avgPvsEntropy; float avgRayLengthEntropy; float avgImportance; rssTree->GetTreeStatistics( avgPvsSize, avgRayContribution, avgPvsEntropy, avgRayLengthEntropy, avgImportance); cout<< "#AVG_PVS_SIZE\n"<stat.Leaves()); num = rssTree->GenerateRays(p, rays); } else { int leaves = rssTree->stat.Leaves()/2; num = rssTree->GenerateRays(desiredSamples, leaves, rays); } cout<<"Generated "<SetWireframe(); // exporter->ExportKdTree(*mKdTree); exporter->SetFilled(); exporter->ExportScene(mSceneGraph->mRoot); exporter->SetWireframe(); if (mViewSpaceBox) { exporter->SetForcedMaterial(RgbColor(1,0,1)); exporter->ExportBox(*mViewSpaceBox); exporter->ResetForcedMaterial(); } VssRayContainer rays; for (int i=0; i < vssRays.size(); i++) if (RandomValue(0,1) < prob) rays.push_back(vssRays[i]); exporter->ExportRays(rays, RgbColor(1, 0, 0)); delete exporter; cout<<"done."<SetFilled(); exporter->ExportScene(mSceneGraph->mRoot); // exporter->SetWireframe(); bool result = exporter->ExportRssTree2( *tree, dir ); delete exporter; return result; } bool RssPreprocessor::ExportRssTreeLeaf(char *filename, RssTree *tree, RssTreeLeaf *leaf) { Exporter *exporter = NULL; exporter = Exporter::GetExporter(filename); exporter->SetWireframe(); exporter->ExportKdTree(*mKdTree); if (mViewSpaceBox) { exporter->SetForcedMaterial(RgbColor(1,0,0)); exporter->ExportBox(*mViewSpaceBox); exporter->ResetForcedMaterial(); } exporter->SetForcedMaterial(RgbColor(0,0,1)); exporter->ExportBox(tree->GetBBox(leaf)); exporter->ResetForcedMaterial(); VssRayContainer rays[4]; for (int i=0; i < leaf->rays.size(); i++) { int k = leaf->rays[i].GetRayClass(); rays[k].push_back(leaf->rays[i].mRay); } // SOURCE RAY exporter->ExportRays(rays[0], RgbColor(1, 0, 0)); // TERMINATION RAY exporter->ExportRays(rays[1], RgbColor(1, 1, 1)); // PASSING_RAY exporter->ExportRays(rays[2], RgbColor(1, 1, 0)); // CONTAINED_RAY exporter->ExportRays(rays[3], RgbColor(0, 0, 1)); delete exporter; return true; } void RssPreprocessor::ExportRssTreeLeaves(RssTree *tree, const int number) { vector leaves; tree->CollectLeaves(leaves); int num = 0; int i; float p = number / (float)leaves.size(); for (i=0; i < leaves.size(); i++) { if (RandomValue(0,1) < p) { char filename[64]; sprintf(filename, "rss-leaf-%04d.x3d", num); ExportRssTreeLeaf(filename, tree, leaves[i]); num++; } if (num >= number) break; } } float RssPreprocessor::GetAvgPvsSize(RssTree *tree, const vector &viewcells ) { vector::const_iterator it, it_end = viewcells.end(); int sum = 0; for (it = viewcells.begin(); it != it_end; ++ it) sum += tree->GetPvsSize(*it); return sum/(float)viewcells.size(); } void RssPreprocessor::ExportPvs(char *filename, RssTree *rssTree ) { ObjectContainer pvs; if (rssTree->CollectRootPvs(pvs)) { Exporter *exporter = Exporter::GetExporter(filename); exporter->SetFilled(); exporter->ExportGeometry(pvs); delete exporter; } } bool RssPreprocessor::ComputeVisibility() { mSceneGraph->CollectObjects(&mObjects); long startTime = GetTime(); int totalSamples = 0; /// Rays used for post processing and visualizations. RayContainer storedRays; AxisAlignedBox3 *box = new AxisAlignedBox3(mKdTree->GetBox()); if (fromBoxVisibility) { float size = 0.05f; float s = 0.5f - size; float olds = Magnitude(box->Size()); box->Enlarge(box->Size()*Vector3(-s)); Vector3 translation = Vector3(-olds*0.1f, 0, 0); box->SetMin(box->Min() + translation); box->SetMax(box->Max() + translation); } else { // sample city like heights box->SetMin(1, box->Min(1) + box->Size(1)*0.2); box->SetMax(1, box->Min(1) + box->Size(1)*0.3); } if (use2dSampling) box->SetMax(1, box->Min(1)); if (useViewSpaceBox) mViewSpaceBox = box; else mViewSpaceBox = NULL; RssTree *rssTree = NULL; while (totalSamples < mInitialSamples) { int passContributingSamples = 0; int passSampleContributions = 0; int passSamples = 0; int index = 0; int sampleContributions; int s = Min(mSamplesPerPass, mInitialSamples); for (int k=0; k < s; k++) { Vector3 viewpoint = GetViewpoint(mViewSpaceBox); Vector3 direction = GetDirection(viewpoint, mViewSpaceBox); CastRay(viewpoint, direction, mVssRays); //-- CORR matt: put block inside loop if (sampleContributions) { passContributingSamples ++; passSampleContributions += sampleContributions; } passSamples++; totalSamples++; } mPass++; int pvsSize = 0; float avgRayContrib = (passContributingSamples > 0) ? passSampleContributions/(float)passContributingSamples : 0; cout << "#Pass " << mPass << " : t = " << TimeDiff(startTime, GetTime())*1e-3 << "s" << endl; cout << "#TotalSamples=" << totalSamples/1000 << "k #SampleContributions=" << passSampleContributions << " (" << 100*passContributingSamples/(float)passSamples<<"%)" << " avgPVS=" << pvsSize/(float)mObjects.size() << endl << "avg ray contrib=" << avgRayContrib << endl; mStats << "#Pass\n" <Construct(mObjects, mVssRays, mViewSpaceBox); rssTree = new RssTree; // viewcells = Construct(mVssRays); if (fromBoxVisibility) rssTree->Construct(mVssRays, mViewSpaceBox); else rssTree->Construct(mVssRays, NULL); cout<<"RssTree root PVS size = "<GetRootPvsSize()<UpdatePVS(newVssRays); // get viewcells as kd tree boxes vector kdViewcells; if (0) { vector leaves; mKdTree->CollectLeaves(leaves); vector::const_iterator it; int targetLeaves = 50; float prob = targetLeaves/(float)leaves.size(); for (it = leaves.begin(); it != leaves.end(); ++it) if (RandomValue(0.0f,1.0f) < prob) kdViewcells.push_back(mKdTree->GetBox(*it)); float avgPvs = GetAvgPvsSize(rssTree, kdViewcells); cout<<"Initial average PVS size = "<AddRays(vssRays); if (1) { int subdivided = rssTree->UpdateSubdivision(); cout<<"subdivided leafs = "<ComputeSampleContributions(passRays, sampleContributions, contributingSamples); //-- save rays for post processing if (((int)storedRays.size() < mViewCellsManager->GetPostProcessSamples()) || ((int)storedRays.size() < mViewCellsManager->GetVisualizationSamples())) { RayContainer::const_iterator it, it_end = passRays.end(); for (it = passRays.begin(); it != it_end; ++ it) storedRays.push_back(new Ray(*(*it))); } else { CLEAR_CONTAINER(passRays); } samples+=num; float pvs = rssTree->GetAvgPvsSize(); cout<<"*****************************\n"; cout<= mRssSamples) break; pass++; } delete rssTree; return true; }