#ifndef _GvsPreprocessor_H__ #define _GvsPreprocessor_H__ #include #include using namespace std; #include "Preprocessor.h" namespace GtpVisibilityPreprocessor { class Exporter; class VssRay; /** View space partition statistics. */ class GvsStatistics: public StatisticsBase { public: /// Constructor GvsStatistics() { Reset(); } void Reset() { mPass = 0; mTotalSamples = 0; mPassContribution = 0; mTotalContribution = 0; mReverseSamples = 0; mBorderSamples = 0; mGvsPass = 0; mTotalPvs = 0; mViewCells = 0; mPerViewCellSamples = 0; mPerViewCellPvs = 0; mTrianglePvs = 0; mViewCellId = 0; mTotalTime = 0; mTimePerViewCell = 0; mTotalTrianglePvs = 0; } public: int mPass; int mTotalSamples; int mPassContribution; int mTotalContribution; int mReverseSamples; int mBorderSamples; int mGvsPass; int mTotalPvs; int mViewCells; int mPerViewCellSamples; int mPerViewCellPvs; int mTrianglePvs; int mTotalTrianglePvs; int mViewCellId; float mTimePerViewCell; float mTotalTime; float RaysPerSec() const { if (!mTotalTime) return 0; return (float)mTotalSamples / mTotalTime * 1e-6f; } void Print(ostream &app) const; friend ostream &operator<<(ostream &s, const GvsStatistics &stat) { stat.Print(s); return s; } }; /** Sampling based visibility preprocessing. The implementation is based on heuristical sampling of view space. */ class GvsPreprocessor : public Preprocessor { public: GvsPreprocessor(); ~GvsPreprocessor(); virtual bool ComputeVisibility(); protected: #if 0 struct PendingRay { PendingRay(VssRay *ray, const bool d) : mRay(ray), mFoundDiscontinuity(d) {} VssRay *mRay; bool mFoundDiscontinuity; }; typedef stack PendingQueue; #endif typedef stack RayQueue; /** Runs the adaptive sampling until the ray queue is empty. The method starts with a number of random rays given by the queue and continues as long it finds new visible geometry (i.e., the queue is not empty). @returns the number of samples cast. */ int ProcessQueue(); /** Generates the rays starting the adaptive visibility sampling process. */ int CastInitialSamples(const int numSamples, const int sampleType); /** Uses the information gained from the ray for doing adaptive border sampling. This function tries to find the border of the triangle found visible by the current ray. New rays are generated which sample this border. We use the following strategies: a) if new triangle was found: adaptive border sampling b) if triangle was found reverse sampling */ bool HandleRay(VssRay *ray); /** The adaptive border sampling step. It aims to find neighbouring triangles of the one hit by the current ray. */ int AdaptiveBorderSampling(const VssRay ¤tRay); /** The reverse sampling step. It is started once the cast ray finds a discontinuity, i.e., a closer triangle. Then the process tries to find a ray from the old triangle passing through a gap. */ VssRay *ReverseSampling(const VssRay ¤tRay, const Triangle3 &hitTriangle, const VssRay &oldRay); /** Returns true if we sampled a closer triangle than with the previous ray. Does reverse sampling if gap found. */ int CheckDiscontinuity(const VssRay ¤tRay, const Triangle3 &hitTriangle, const VssRay &oldRay); /** Adds new samples to the ray queue and classifies them with respect to the previous ray. */ void EnqueueRays(VssRayContainer &samples, VssRayContainer &invalidSamples); /** Hepler function for adaptive border sampling. It finds new sample points around a triangle in a eps environment */ void EnlargeTriangle(VertexContainer &vertices, const Triangle3 &hitTriangle, const VssRay &ray) const; int SubdivideEdge(const Triangle3 &hitTriangle, const Vector3 &p1, const Vector3 &p2, const VssRay &ray1, const VssRay &ray2, const VssRay &oldRay); void Visualize(); void CreateDisplacedVertices(VertexContainer &vertices, const Triangle3 &hitTriangle, const VssRay &ray, const int index) const; Vector3 CalcPredictedHitPoint(const VssRay &newRay, const Triangle3 &hitTriangle, const VssRay &oldRay) const; bool GetPassingPoint(const VssRay ¤tRay, const Triangle3 &occluder, const VssRay &oldRay, Vector3 &newPoint) const; bool NextViewCell(); void GlobalComputation(); void PerViewCellComputation(); void VisualizeViewCells(); void VisualizeViewCell(ViewCell *vc); void VisualizeViewCell(const ObjectContainer &objects); /** Exchanges view cell triangle pvs with bvh leaf pvs. */ void UpdatePvs(ViewCell *currentViewCell); void ProcessViewCell(); void ClearRayQueue(); void CompileViewCellsList(); void GetObjectPvs(ObjectContainer &trianglePvs) const; bool HasContribution(VssRay &ray); void IntersectWithViewCell(); void DeterminePvsObjects(VssRayContainer &rays); //virtual void ComputeRenderError(); void StorePvs(const ObjectContainer &objectPvs); ////////////////////// //int mSamplesPerPass; //int mTotalSamples; int mInitialSamples; RayQueue mRayQueue; int mSamplingType; //AxisAlignedBox3 mViewSpaceBox; float mEps; float mThreshold; VssRayContainer mVssRays; /////////// // stats ofstream mGvsStatsStream; GvsStatistics mGvsStats; bool mPerViewCell; bool mOnlyRandomSampling; ViewCell *mCurrentViewCell; int mProcessedViewCells; int mMinContribution; ViewCellContainer mViewCells; int mMaxViewCells; ObjectContainer mTrianglePvs; // HACK int mGvsSamplesPerPass; bool mComputeRenderError; bool mEvaluatePixelError; }; }; #endif