#include "SamplingStrategy.h" #include "Ray.h" #include "Intersectable.h" #include "Preprocessor.h" #include "ViewCellsManager.h" #include "AxisAlignedBox3.h" namespace GtpVisibilityPreprocessor { SamplingStrategy::SamplingStrategy(const Preprocessor &preprocessor): mPreprocessor(preprocessor) { } SamplingStrategy::~SamplingStrategy() { } /*********************************************************************/ /* Individual sampling strategies implementation */ /*********************************************************************/ bool ObjectBasedDistribution::GenerateSample(SimpleRay &ray) const { Vector3 origin, direction; mPreprocessor.mViewCellsManager->GetViewPoint(origin); Vector3 point; Vector3 normal; const int i = (int)RandomValue(0, float((int)mPreprocessor.mObjects.size() - 1)); 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; ray = SimpleRay(origin, direction, pdf); return true; } bool ObjectDirectionBasedDistribution::GenerateSample(SimpleRay &ray) const { Vector3 origin, direction; const int i = (int)RandomValue(0, (Real)mPreprocessor.mObjects.size() - 0.5f); Intersectable *object = mPreprocessor.mObjects[i]; Vector3 normal; object->GetRandomSurfacePoint(origin, normal); direction = UniformRandomVector(normal); origin += 0.1f * direction; 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, pdf); return true; } bool DirectionBasedDistribution::GenerateSample(SimpleRay &ray) const { Vector3 origin, direction; mPreprocessor.mViewCellsManager->GetViewPoint(origin); 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; } bool DirectionBoxBasedDistribution::GenerateSample(SimpleRay &ray) const { 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, pdf); return true; } bool SpatialBoxBasedDistribution::GenerateSample(SimpleRay &ray) const { Vector3 origin, direction; mPreprocessor.mViewCellsManager->GetViewPoint(origin); direction = mPreprocessor.mKdTree->GetBox().GetRandomPoint() - origin; 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; } #if 0 bool ObjectsInteriorDistribution::GenerateSample(SimpleRay &ray) const { 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 }