#ifndef __VSS_RAY_H #define __VSS_RAY_H #include using namespace std; #include "Vector3.h" #include "Ray.h" #include "Containers.h" namespace GtpVisibilityPreprocessor { class AxisAlignedBox3; class Intersectable; class KdNode; #define ABS_CONTRIBUTION_WEIGHT 0.0f #define VSS_STORE_VIEWCELLS 1 class VssRay { public: // various flags enum { FPosDirX = 1, // the direction of ray in X-axis is positive FPosDirY = 2, // the direction of ray in Y-axis is positive FPosDirZ = 4, // the direction of ray in Z-axis is positive BorderSample = 8,// if this ray is an adaptive border ray ReverseSample = 16, // if this ray is a reverse sample Valid = 32 // this ray is a valid ray //(with respect to detect empty viewspace) }; // Id of the generating SimpleRay int mGeneratorId; static int mailID; int mMailbox; // side of the ray - used for the ray classification // char mSide; // computed t // float mT; // inverse of the ray size float mInvSize; // counter of references to this ray short mRefCount; // various flags char mFlags; Vector3 mOrigin; Vector3 mTermination; /// Termination object for the ray /// only the termination object is actually used Intersectable *mOriginObject; Intersectable *mTerminationObject; #if VSS_STORE_VIEWCELLS ViewCellContainer mViewCells; #endif //////////////////////// // members related to importance sampling // sampling pass in which this ray was generated short mPass; // Distribution used to generate this ray short mDistribution; // number of cells where this ray made a contribution to the PVS int mPvsContribution; // sum of relative ray contributions per object float mRelativePvsContribution; // weighted contribution to the pvs (based on the pass the ray was casted at) // computed by the prperocessor float mWeightedPvsContribution; // probability of this ray float mPdf; ////////////////////////////// /// the kd node holding the termination point KdNode *mTerminationNode; /// the kd node holding the origin point KdNode *mOriginNode; VssRay( const Vector3 &origin, const Vector3 &termination, Intersectable *originObject, Intersectable *terminationObject, const int pass = 0, const float pdf = 1.0f ); VssRay(const Ray &ray); void Precompute(); void Mail() { mMailbox = mailID; } static void NewMail() { mailID++; } bool Mailed() const { return mMailbox == mailID; } bool Mailed(const int mail) { return mMailbox >= mailID + mail; } int HitCount() const { #if BIDIRECTIONAL_RAY if (mOriginObject && mTerminationObject) return 2; if (mOriginObject || mTerminationObject) return 1; return 0; #else return (mTerminationObject) ? 1 : 0; #endif } Vector3 GetOrigin() const { return mOrigin; } Vector3 GetTermination() const { return mTermination; } Vector3 GetDir() const { return mTermination - mOrigin; } // Vector3 GetNormalizedDir() const { return Normalize(termination - mOrigin); } Vector3 GetNormalizedDir() const { return (mTermination - mOrigin)*mInvSize; } float Length() const { return Distance(mOrigin, mTermination); } Vector3 Extrap(const float t) const { return GetOrigin() + t * GetDir(); } float GetDirParametrization(const int axis) const; float GetOpositeDirParametrization(const int axis) const; static float VssRay::GetDirParam(const int axis, const Vector3 dir); static Vector3 VssRay::GetInvDirParam(const float alpha, const float beta); float GetSize() const { return 1.0f/mInvSize; } float GetInvSize() const { return mInvSize; } float GetOrigin(const int axis) const { return mOrigin[axis]; } float GetTermination(const int axis) const { return mTermination[axis]; } float GetDir(const int axis) const { return mTermination[axis] - mOrigin[axis]; } float GetNormalizedDir(const int axis) const { return (mTermination[axis] - mOrigin[axis])*mInvSize; } bool ComputeMinMaxT(const AxisAlignedBox3 &box, float &tmin, float &tmax) const; bool Intersects(const AxisAlignedBox3 &box, float &tmin, float &tmax) const; bool IntersectsSphere(const Vector3 ¢er, const float sqrRadius, Vector3 &point, float &t) const; void Translate(const Vector3 &translation) { mOrigin += translation; mTermination += translation; } void SetupEndPoints(const Vector3 &origin, const Vector3 &termination) { mOrigin = origin; mTermination = termination; Precompute(); } bool HasPosDir(const int axis) const { return mFlags & (1<0; } // reference counting for leaf nodes int RefCount() const { return mRefCount; } int Ref() { return mRefCount++; } void ScheduleForRemoval() { if (mRefCount>0) mRefCount = -mRefCount; } bool ScheduledForRemoval() const { return mRefCount<0; } void Unref() { if (mRefCount > 0) mRefCount--; else if (mRefCount < 0) mRefCount++; else { cerr<<"Trying to unref already deleted ray!"<mWeightedPvsContribution > b->mWeightedPvsContribution; } float SqrDistance(const Vector3 &point) const { Vector3 diff = point - mOrigin; float t = DotProd(diff, GetDir()); if ( t <= 0.0f ) { t = 0.0f; } else { if (t >= 1.0f) { t = 1.0f; } else { t /= SqrMagnitude(GetDir()); } diff -= t*GetDir(); } return SqrMagnitude(diff); } /** Returns the data sampled on either the ray origin or termination. */ void GetSampleData( const bool isTerminaton, Vector3 &pt, Intersectable **obj, KdNode **node) const; friend ostream& operator<< (ostream &s, const VssRay &vssRay); }; inline void VssRay::GetSampleData(const bool isTermination, Vector3 &pt, Intersectable **obj, KdNode **node) const { if (isTermination) { pt = mTermination; *obj = mTerminationObject; *node = mTerminationNode; } else { pt = mOrigin; *obj = mOriginObject; *node = mOriginNode; } } void GenerateExtendedConvexCombinationWeights(float &w1, float &w2, float &w3, const float overlap ); void GenerateExtendedConvexCombinationWeights2(float &w1, float &w2, const float overlap ); // Overload << operator for C++-style output inline ostream& operator<< (ostream &s, const VssRay &vssRay) { return s << "(" << vssRay.mPass << ", " << vssRay.mOrigin << ", " << vssRay.mTermination << ", " << vssRay.mOriginObject << ", " << vssRay.mTerminationObject << ", " << vssRay.mPdf << ")"; } // -------------------------------------------------------------- // For sorting rays // -------------------------------------------------------------- struct SortableEntry { enum EType { ERayMin, ERayMax }; int type; float value; void *data; SortableEntry() {} SortableEntry(const int t, const float v, void *d):type(t), value(v), data(d) {} friend bool operator<(const SortableEntry &a, const SortableEntry &b) { return a.value < b.value; } }; struct VssRayContainer : public vector { void PrintStatistics(ostream &s); int SelectRays(const int number, VssRayContainer &selected, const bool copy=false) const; int GetContributingRays(VssRayContainer &selected, const int minPass ) const; }; /* struct VssRayDistribution { VssRayDistribution() { mContribution = -1.0f; } SimpleRayContainer mRays; vector mVssRays; float mContribution; float mTime; }; struct VssRayDistributionMixture { VssRayDistributionMixture() {} vector distributions; }; */ }; #endif