#include "SamplingStrategy.h" #include "Ray.h" #include "Intersectable.h" #include "Preprocessor.h" #include "ViewCellsManager.h" #include "AxisAlignedBox3.h" #include "RssTree.h" #include "Mutation.h" #include "FilterBasedDistribution.h" #include "DifferenceSampling.h" #ifdef USE_PERFTIMER #include "Timer/PerfTimer.h" #endif #define USE_HALTON 1 namespace GtpVisibilityPreprocessor { #ifdef USE_PERFTIMER extern PerfTimer haltonTimer; extern PerfTimer pvsTimer; extern PerfTimer viewCellCastTimer; #endif HaltonSequence ObjectBasedDistribution::sHalton; HaltonSequence MixtureDistribution::sHalton; HaltonSequence GlobalLinesDistribution::sHalton; HaltonSequence SpatialBoxBasedDistribution::sHalton; HaltonSequence ObjectDirectionBasedDistribution::sHalton; HaltonSequence DirectionBasedDistribution::sHalton; HaltonSequence HwGlobalLinesDistribution::sHalton; HaltonSequence ViewCellBasedDistribution::sHalton; HaltonSequence ViewCellBorderBasedDistribution::sHalton; HaltonSequence ProbablyVisibleDistribution::sHalton; SamplingStrategy::SamplingStrategy(Preprocessor &preprocessor): mPreprocessor(preprocessor), mRatio(1.0f), mTotalRays(0), mTotalContribution(0.0f) { } SamplingStrategy::~SamplingStrategy() { } int SamplingStrategy::GenerateSamples(int number, SimpleRayContainer &rays, int &invalidSamples) { SimpleRay ray; int oldSamples = (int)rays.size(); static const int maxTries = 20; // tmp changed matt. Q: should one rejected sample // terminate the whole method? for (int i = 0; i < number; ++ i) { for (int j = 0; j < maxTries; ++ j) { if (GenerateSample(ray)) { rays.push_back(ray); break; } cerr<<"x"; ++ invalidSamples; } } return (int)rays.size() - oldSamples; } /*********************************************************************/ /* Individual sampling strategies implementation */ /*********************************************************************/ bool ObjectBasedDistribution::GenerateSample(SimpleRay &ray) { Vector3 origin, direction; float r[5]; sHalton.GetNext(5, r); mPreprocessor.mViewCellsManager->GetViewPoint(origin, Vector3(r[2],r[3],r[4])); Vector3 point, normal; r[0] *= (float)mPreprocessor.mObjects.size() - 1; const int i = (int)r[0]; Intersectable *object = mPreprocessor.mObjects[i]; // take the remainder as a parameter over objects surface r[0] -= (float)i; object->GetRandomSurfacePoint(r[0], r[1], point, normal); direction = point - origin; const float c = Magnitude(direction); if (c <= Limits::Small) return false; // $$ jb the pdf is yet not correct for all sampling methods! const float pdf = 1.0f; direction *= 1.0f / c; ray = SimpleRay(origin, direction, OBJECT_BASED_DISTRIBUTION, pdf); return true; } bool ObjectDirectionBasedDistribution::GenerateSample(SimpleRay &ray) { Vector3 origin, direction; float r[4]; sHalton.GetNext(4, r); // static Halton<4> halton; // halton.GetNext(r); r[0] *= (float)mPreprocessor.mObjects.size() - 1; const int i = (int)r[0]; Intersectable *object = mPreprocessor.mObjects[i]; // take the remainder as a parameter over objects surface r[0] -= (float)i; Vector3 normal; object->GetRandomSurfacePoint(r[0], r[1], origin, normal); direction = Normalize(CosineRandomVector(r[2], r[3], normal)); origin += 1e-2f*direction; // $$ jb the pdf is yet not correct for all sampling methods! const float pdf = 1.0f; ray = SimpleRay(origin, direction, OBJECT_DIRECTION_BASED_DISTRIBUTION, pdf); return true; } bool DirectionBasedDistribution::GenerateSample(SimpleRay &ray) { float r[5]; sHalton.GetNext(5, r); Vector3 origin, direction; mPreprocessor.mViewCellsManager->GetViewPoint(origin, Vector3(r[2],r[3],r[4]) ); direction = UniformRandomVector(r[0], r[1]); const float c = Magnitude(direction); if (c <= Limits::Small) return false; const float pdf = 1.0f; direction *= 1.0f / c; ray = SimpleRay(origin, direction, DIRECTION_BASED_DISTRIBUTION, pdf); return true; } bool DirectionBoxBasedDistribution::GenerateSample(SimpleRay &ray) { Vector3 origin, direction; mPreprocessor.mViewCellsManager->GetViewPoint(origin); const float alpha = RandomValue(0.0f, 2.0f * (float)M_PI); const float beta = RandomValue((float)-M_PI * 0.5f, (float)M_PI * 0.5f); direction = VssRay::GetDirection(alpha, beta); const float c = Magnitude(direction); if (c <= Limits::Small) return false; const float pdf = 1.0f; direction *= 1.0f / c; ray = SimpleRay(origin, direction, DIRECTION_BOX_BASED_DISTRIBUTION, pdf); return true; } bool SpatialBoxBasedDistribution::GenerateSample(SimpleRay &ray) { Vector3 origin, direction; float r[6]; sHalton.GetNext(6, r); // mPreprocessor.mViewCellsManager->GetViewPoint(origin, Vector3(r[0], // r[1], // r[2])); origin = mPreprocessor.sceneBox.GetRandomPoint(Vector3(r[0], r[1], r[2])); direction = mPreprocessor.sceneBox.GetRandomPoint(Vector3(r[3], r[4], r[5])) - origin; // cout<GetViewPoint(origin); Vector3 point; Vector3 normal; const int i = (int)RandomValue(0, (float)mPreprocessor.mObjects.size() - 0.5f); Intersectable *object = mPreprocessor.mObjects[i]; object->GetRandomSurfacePoint(point, normal); direction = origin - point; // $$ jb the pdf is yet not correct for all sampling methods! const float c = Magnitude(direction); if ((c <= Limits::Small) || (DotProd(direction, normal) < 0)) { return false; } // $$ jb the pdf is yet not correct for all sampling methods! const float pdf = 1.0f; direction *= 1.0f / c; // a little offset point += direction * 0.001f; ray = SimpleRay(point, direction, REVERSE_OBJECT_BASED_DISTRIBUTION, pdf); return true; } bool ReverseViewSpaceBorderBasedDistribution::GenerateSample(SimpleRay &ray) { Vector3 origin, direction; origin = mPreprocessor.mViewCellsManager->GetViewSpaceBox().GetRandomSurfacePoint(); Vector3 point; Vector3 normal; const int i = (int)RandomValue(0, (float)mPreprocessor.mObjects.size() - 0.5f); Intersectable *object = mPreprocessor.mObjects[i]; object->GetRandomSurfacePoint(point, normal); direction = origin - point; // $$ jb the pdf is yet not correct for all sampling methods! const float c = Magnitude(direction); if ((c <= Limits::Small) || (DotProd(direction, normal) < 0)) { return false; } // $$ jb the pdf is yet not correct for all sampling methods! const float pdf = 1.0f; //cout << "p: " << point << " "; direction *= 1.0f / c; // a little offset point += direction * 0.001f; ray = SimpleRay(point, direction, REVERSE_VIEWSPACE_BORDER_BASED_DISTRIBUTION, pdf); return true; } bool ViewSpaceBorderBasedDistribution::GenerateSample(SimpleRay &ray) { Vector3 origin, direction; origin = mPreprocessor.mViewCellsManager->GetViewSpaceBox().GetRandomSurfacePoint(); Vector3 point; Vector3 normal; const int i = (int)RandomValue(0, (float)mPreprocessor.mObjects.size() - 0.5f); Intersectable *object = mPreprocessor.mObjects[i]; object->GetRandomSurfacePoint(point, normal); direction = point - origin; // $$ jb the pdf is yet not correct for all sampling methods! const float c = Magnitude(direction); if (c <= Limits::Small) return false; // $$ jb the pdf is yet not correct for all sampling methods! const float pdf = 1.0f; direction *= 1.0f / c; // a little offset origin += direction * 0.001f; ray = SimpleRay(origin, direction, VIEWSPACE_BORDER_BASED_DISTRIBUTION, pdf); return true; } bool GlobalLinesDistribution::GenerateSample(SimpleRay &ray) { Vector3 origin, termination, direction; float radius = 0.5f*Magnitude(mPreprocessor.mViewCellsManager->GetViewSpaceBox().Size()); Vector3 center = mPreprocessor.mViewCellsManager->GetViewSpaceBox().Center(); const int tries = 1000; int i; for (i=0; i < tries; i++) { float r[4]; sHalton.GetNext(4, r); origin = center + (radius*UniformRandomVector(r[0], r[1])); termination = center + (radius*UniformRandomVector(r[2], r[3])); direction = termination - origin; // $$ jb the pdf is yet not correct for all sampling methods! const float c = Magnitude(direction); if (c <= Limits::Small) return false; direction *= 1.0f / c; // check if the ray intersects the view space box static Ray ray; ray.Init(origin, direction, Ray::LOCAL_RAY); float tmin, tmax; if (mPreprocessor.mViewCellsManager-> GetViewSpaceBox().ComputeMinMaxT(ray, &tmin, &tmax) && (tmin < tmax)) break; } if (i!=tries) { // $$ jb the pdf is yet not correct for all sampling methods! const float pdf = 1.0f; ray = SimpleRay(origin, direction, GLOBAL_LINES_DISTRIBUTION, pdf); ray.mType = Ray::GLOBAL_RAY; return true; } return false; } /******************************************************************/ /* class MixtureDistribution implementation */ /******************************************************************/ // has to be called before first usage void MixtureDistribution::Init() { for (int i=0; i < mDistributions.size(); i++) { // small non-zero value mDistributions[i]->mRays = 1; mDistributions[i]->mGeneratedRays = 1; // unit contribution per ray if (1 || mDistributions[i]->mType != RSS_BASED_DISTRIBUTION) mDistributions[i]->mContribution = 1.0f; else mDistributions[i]->mContribution = 0.0f; } UpdateRatios(); } void MixtureDistribution::Reset() { for (int i=0; i < mDistributions.size(); i++) { // small non-zero value mDistributions[i]->mTotalRays = 0; // unit contribution per ray mDistributions[i]->mTotalContribution = 0.0f; } UpdateRatios(); } // Generate a new sample according to a mixture distribution bool MixtureDistribution::GenerateSample(SimpleRay &ray) { float r; sHalton.GetNext(1, &r); //static Halton<1> halton; // halton.GetNext(&r); int i; // pickup a distribution for (i=0; i < (int)mDistributions.size()-1; i++) if (r < mDistributions[i]->mRatio) break; bool result = mDistributions[i]->GenerateSample(ray); if (result) mDistributions[i]->mGeneratedRays++; return result; } #if TEST_PACKETS // add contributions of the sample to the strategies void MixtureDistribution::ComputeContributions(VssRayContainer &vssRays) { int i; mPreprocessor.mViewCellsManager->ComputeSampleContributions(vssRays, true, false); VssRayContainer::iterator it = vssRays.begin(); for (i=0; i < mDistributions.size(); i++) { mDistributions[i]->mContribution = 0; mDistributions[i]->mRays = 0; } for(; it != vssRays.end(); ++ it) { VssRay *ray = *it; for (i = 0; i < mDistributions.size() - 1; ++ i) { if (mDistributions[i]->mType == ray->mDistribution) break; } float contribution = mPreprocessor.mViewCellsManager->ComputeSampleContribution(*ray, true, false); mDistributions[i]->mContribution += contribution; mDistributions[i]->mRays ++; mDistributions[i]->mTotalContribution += contribution; mDistributions[i]->mTotalRays ++; } UpdateRatios(); const float vcTime = viewCellCastTimer.TotalTime(); const float pvsTime = pvsTimer.TotalTime(); const float haltonTime = haltonTimer.TotalTime(); cout << "view cell cast time: " << vcTime << " s" << endl; cout << "pvs time: " << pvsTime << " s" << endl; cout << "halton time: "<< haltonTime << " s" << endl; Debug << "view cell cast time: " << vcTime << " s" << endl; Debug << "pvs time: " << pvsTime << " s" << endl; Debug << "halton time: "<< haltonTime << " s" << endl; } #else // add contributions of the sample to the strategies void MixtureDistribution::ComputeContributions(VssRayContainer &vssRays) { int i; VssRayContainer::iterator it = vssRays.begin(); for (i=0; i < mDistributions.size(); i++) { mDistributions[i]->mContribution = 0; mDistributions[i]->mRays = 0; } for(; it != vssRays.end(); ++it) { VssRay *ray = *it; //if (ray->mTerminationObject->Type() == Intersectable::TRANSFORMED_MESH_INSTANCE) // cout << "found transofrmed mesh instance: " << Intersectable::GetTypeName(ray->mTerminationObject) << " " << endl; for (i=0; i < mDistributions.size()-1; i++) { if (mDistributions[i]->mType == ray->mDistribution) break; } const float contribution = mPreprocessor.mViewCellsManager->ComputeSampleContribution(*ray, true, false); mDistributions[i]->mContribution += contribution; mDistributions[i]->mRays ++; mDistributions[i]->mTotalContribution += contribution; mDistributions[i]->mTotalRays ++; } UpdateRatios(); if (1) { #ifdef USE_PERFTIMER const double vcTime = viewCellCastTimer.TotalTime(); const double pvsTime = pvsTimer.TotalTime(); const double haltonTime = haltonTimer.TotalTime(); cout << "view cell cast time: " << vcTime << " s" << endl; cout << "pvs time: " << pvsTime << " s" << endl; cout << "halton time: "<< haltonTime << " s" << endl; Debug << "view cell cast time: " << vcTime << " s" << endl; Debug << "pvs time: " << pvsTime << " s" << endl; Debug << "halton time: "<< haltonTime << " s" << endl; #else cout << "Timers not supported" << endl; Debug << "Timers for PVS,Halton,Viewcell not supported" << endl; #endif } } #endif void MixtureDistribution::UpdateDistributions(VssRayContainer &vssRays) { // now update the distributions with all the rays for (int i=0; i < mDistributions.size(); i++) { mDistributions[i]->Update(vssRays); } } #define RAY_CAST_TIME 0.7f #define VIEWCELL_CAST_TIME 0.3f void MixtureDistribution::UpdateRatios() { // now compute importance (ratio) of all distributions float sum = 0.0f; int i; for (i=0; i < mDistributions.size(); i++) { cout<mRatio-last<<" "; last = mDistributions[i]->mRatio; } cout<mGeneratedRays = 0; return SamplingStrategy::GenerateSamples(number, rays, invalidSamples); } HwGlobalLinesDistribution::HwGlobalLinesDistribution(Preprocessor &preprocessor): SamplingStrategy(preprocessor) { mType = HW_GLOBAL_LINES_DISTRIBUTION; preprocessor.mUseHwGlobalLines = true; } bool HwGlobalLinesDistribution::GenerateSample(SimpleRay &ray) { Vector3 origin, termination, direction; const float radius = 0.5f * Magnitude(mPreprocessor.mViewCellsManager->GetViewSpaceBox().Size()); Vector3 center = mPreprocessor.mViewCellsManager->GetViewSpaceBox().Center(); const int tries = 1000; int i; for (i=0; i < tries; i++) { float r[2]; sHalton.GetNext(2, r); origin = center + (radius * UniformRandomVector(r[0], r[1])); termination = center; if (0) { // add a small offset to provide some more randomness in the sampling Vector3 offset(Random(radius * 1e-3f), Random(radius * 1e-3f), Random(radius * 1e-3f)); termination += offset; } direction = termination - origin; // $$ jb the pdf is yet not correct for all sampling methods! const float c = Magnitude(direction); if (c <= Limits::Small) return false; direction *= 1.0f / c; // check if the ray intersects the view space box static Ray ray; ray.Init(origin, direction, Ray::LOCAL_RAY); float tmin, tmax; if (mPreprocessor.mViewCellsManager-> GetViewSpaceBox().ComputeMinMaxT(ray, &tmin, &tmax) && (tmin < tmax)) break; } if (i != tries) { // $$ jb the pdf is yet not correct for all sampling methods! const float pdf = 1.0f; ray = SimpleRay(origin, direction, HW_GLOBAL_LINES_DISTRIBUTION, pdf); ray.mType = Ray::GLOBAL_RAY; return true; } return false; } bool ViewCellBasedDistribution::GenerateSample(SimpleRay &ray) { static Vector3 origin; static Vector3 direction; static Vector3 point; static Vector3 normal; static float r[2]; sHalton.GetNext(2, r); direction = UniformRandomVector(r[0], r[1]); #if 0 const float c = Magnitude(direction); if (!((c < 1.00001f) && (c > 0.999999f))) cerr << "len wrong" << endl; if (c <= Limits::Small) return false; direction *= 1.0f / c; #endif // get point on view cell surface if (1) { //mViewCell->GetRandomEdgePoint(origin, normal); //origin = mViewCell->GetBox().GetRandomSurfacePoint(); origin = mViewCell->GetBox().GetRandomPoint(); // move a little bit back to avoid piercing through walls // that bound the view cell //origin -= 0.01f * normal; } else origin = mViewCell->GetBox().GetUniformRandomSurfacePoint(); static const float pdf = 1.0f; ray = SimpleRay(origin, direction, VIEWCELL_BASED_DISTRIBUTION, pdf); //cout << "ray: " << ray.mOrigin << " " << ray.mDirection << endl; return true; } bool ViewCellBorderBasedDistribution::GenerateSample(SimpleRay &ray) { float r[3]; sHalton.GetNext(3, r); Vector3 origin, direction; Vector3 normal; Vector3 point = mPreprocessor.sceneBox.GetRandomPoint(Vector3(r[0], r[1], r[2])); mViewCell->GetRandomEdgePoint(origin, normal); direction = point - origin; // $$ jb the pdf is yet not correct for all sampling methods! const float c = Magnitude(direction); if ((c <= Limits::Small) /*|| (DotProd(direction, normal) < 0)*/) { return false; } // $$ jb the pdf is yet not correct for all sampling methods! const float pdf = 1.0f; //cout << "p: " << point << " "; direction *= 1.0f / c; ray = SimpleRay(origin, direction, VIEWCELL_BORDER_BASED_DISTRIBUTION, pdf); //cout << "ray: " << ray.mOrigin << " " << ray.mDirection << endl; return true; } bool ProbablyVisibleDistribution::GenerateSample(SimpleRay &ray) { static Vector3 origin; static Vector3 direction; static Vector3 point; static Vector3 normal; float r[2]; sHalton.GetNext(2, r); r[0] *= (float)(*mObjects).size() - 1.0f; const int i = (int)r[0]; Intersectable *obj = (*mObjects)[i]; obj->GetRandomSurfacePoint(point, normal); // get point on view cell surface if (1) origin = mViewCell->GetBox().GetRandomSurfacePoint(); else origin = mViewCell->GetBox().GetUniformRandomSurfacePoint(); direction = point - origin; const float c = Magnitude(direction); if (c <= Limits::Small) return false; ray = SimpleRay(origin, direction, PROBABLY_VISIBLE_DISTRIBUTION, 1.0f); return true; } }