#include "SamplingStrategy.h" #include "Ray.h" #include "Intersectable.h" #include "Preprocessor.h" #include "ViewCellsManager.h" #include "AxisAlignedBox3.h" #include "RssTree.h" #include "Mutation.h" namespace GtpVisibilityPreprocessor { //HaltonSequence SamplingStrategy::sHalton; HaltonSequence ObjectBasedDistribution::sHalton; HaltonSequence MixtureDistribution::sHalton; HaltonSequence GlobalLinesDistribution::sHalton; HaltonSequence SpatialBoxBasedDistribution::sHalton; HaltonSequence ObjectDirectionBasedDistribution::sHalton; HaltonSequence DirectionBasedDistribution::sHalton; HaltonSequence HwGlobalLinesDistribution::sHalton; HaltonSequence ViewCellBasedDistribution::sHalton; SamplingStrategy::SamplingStrategy(Preprocessor &preprocessor): mPreprocessor(preprocessor), mRatio(1.0f), mTotalRays(0), mTotalContribution(0.0f) { } SamplingStrategy::~SamplingStrategy() { } int SamplingStrategy::GenerateSamples(const int number, SimpleRayContainer &rays) { SimpleRay ray; int samples = 0; int i = 0; const int maxTries = 20; // tmp changed matt. Q: should one rejected sample // terminate the whole method? for (; i < number; i++) { int j = 0; bool sampleGenerated = false; for (j = 0; !sampleGenerated && (j < maxTries); ++ j) { sampleGenerated = GenerateSample(ray); if (sampleGenerated) { ++ samples; rays.push_back(ray); } } } return samples; } /*********************************************************************/ /* 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); 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])); direction = mPreprocessor.mKdTree->GetBox().GetRandomPoint(Vector3(r[3], r[4], r[5]) ) - origin; //cout << "z"; const float c = Magnitude(direction); if (c <= Limits::Small) return false; const float pdf = 1.0f; direction *= 1.0f / c; ray = SimpleRay(origin, direction, SPATIAL_BOX_BASED_DISTRIBUTION, pdf); return true; } bool ReverseObjectBasedDistribution::GenerateSample(SimpleRay &ray) { Vector3 origin, direction; mPreprocessor.mViewCellsManager->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; //cout << "p: " << point << " "; direction *= 1.0f / c; // a little offset point += direction * 0.001f; ray = SimpleRay(point, direction, REVERSE_OBJECT_BASED_DISTRIBUTION, pdf); return true; } bool ViewCellBorderBasedDistribution::GenerateSample(SimpleRay &ray) { Vector3 origin, direction; ViewCellContainer &viewCells = mPreprocessor.mViewCellsManager->GetViewCells(); Vector3 point; Vector3 normal, normal2; const int vcIdx = (int)RandomValue(0, (float)viewCells.size() - 0.5f); const int objIdx = (int)RandomValue(0, (float)mPreprocessor.mObjects.size() - 0.5f); Intersectable *object = mPreprocessor.mObjects[objIdx]; ViewCell *viewCell = viewCells[vcIdx]; //cout << "vc: " << vcIdx << endl; //cout << "obj: " << objIdx << endl; object->GetRandomSurfacePoint(point, normal); viewCell->GetRandomEdgePoint(origin, normal2); 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; } #if 0 bool ObjectsInteriorDistribution::GenerateSample(SimpleRay &ray) { Vector3 origin, direction; // get random object const int i = RandomValue(0, mPreprocessor.mObjects.size() - 1); const Intersectable *obj = mPreprocessor.mObjects[i]; // note: if we load the polygons as meshes, // asymtotically every second sample is lost! origin = obj->GetBox().GetRandomPoint(); // uniformly distributed direction direction = UniformRandomVector(); const float c = Magnitude(direction); if (c <= Limits::Small) return false; const float pdf = 1.0f; direction *= 1.0f / c; ray = SimpleRay(origin, direction, pdf); return true; } #endif 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; //cout << "w"; 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; } // has to 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); int i; // pickup a distribution for (i=0; i < mDistributions.size()-1; i++) if (r < mDistributions[i]->mRatio) break; bool result = mDistributions[i]->GenerateSample(ray); if (result) mDistributions[i]->mGeneratedRays++; return result; } // 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; 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(); } 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); } bool HwGlobalLinesDistribution::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[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) { Vector3 origin, direction; ViewCellContainer &viewCells = mPreprocessor.mViewCellsManager->GetViewCells(); Vector3 point; Vector3 normal; //Vector normalObj; // float r[1]; // sHalton.GetNext(1, r); // const int objIdx = (int)(r[0] * (float)mPreprocessor.mObjects.size() - 1.0f); // Intersectable *object = mPreprocessor.mObjects[objIdx]; // object->GetRandomSurfacePoint(point, normal); // cout << "obj: " << objIdx << endl; float r[2]; sHalton.GetNext(2, r); direction = UniformRandomVector(r[0], r[1]); const float c = Magnitude(direction); if (c <= Limits::Small) return false; direction *= 1.0f / c; // get point on view cell surface mViewCell->GetRandomSurfacePoint(origin, normal); //direction = point - origin; // move a little bit back to avoid piercing through walls // that bound the view cell origin -= 0.01f * normal; // $$ jb the pdf is yet not correct for all sampling methods! const float pdf = 1.0f; ray = SimpleRay(origin, direction, VIEWCELL_BASED_DISTRIBUTION, pdf); //cout << "ray: " << ray.mOrigin << " " << ray.mDirection << endl; return true; } }