[376] | 1 | #ifndef __VSS_RAY_H
|
---|
| 2 | #define __VSS_RAY_H
|
---|
[372] | 3 |
|
---|
| 4 | #include <vector>
|
---|
| 5 | using namespace std;
|
---|
| 6 | #include "Vector3.h"
|
---|
[410] | 7 | #include "Ray.h"
|
---|
[492] | 8 | #include "Containers.h"
|
---|
[372] | 9 |
|
---|
[860] | 10 | namespace GtpVisibilityPreprocessor {
|
---|
| 11 |
|
---|
[372] | 12 | class AxisAlignedBox3;
|
---|
| 13 | class Intersectable;
|
---|
[1133] | 14 | class KdNode;
|
---|
[372] | 15 |
|
---|
[1900] | 16 | #define ABS_CONTRIBUTION_WEIGHT 0.0f
|
---|
[1966] | 17 | #define VSS_STORE_VIEWCELLS 1
|
---|
| 18 |
|
---|
[372] | 19 | class VssRay {
|
---|
| 20 | public:
|
---|
[1528] | 21 |
|
---|
| 22 | // various flags
|
---|
[372] | 23 | enum {
|
---|
[1566] | 24 | FPosDirX = 1, // the direction of ray in X-axis is positive
|
---|
| 25 | FPosDirY = 2, // the direction of ray in Y-axis is positive
|
---|
| 26 | FPosDirZ = 4, // the direction of ray in Z-axis is positive
|
---|
| 27 | BorderSample = 8,// if this ray is an adaptive border ray
|
---|
[1771] | 28 | ReverseSample = 16, // if this ray is a reverse sample
|
---|
| 29 | Valid = 32 // this ray is a valid ray
|
---|
| 30 | //(with respect to detect empty viewspace)
|
---|
[372] | 31 | };
|
---|
| 32 |
|
---|
[1867] | 33 | // Id of the generating SimpleRay
|
---|
[1989] | 34 | int mGeneratorId;
|
---|
[1867] | 35 |
|
---|
[438] | 36 | static int mailID;
|
---|
[372] | 37 | int mMailbox;
|
---|
| 38 |
|
---|
[438] | 39 | // side of the ray - used for the ray classification
|
---|
| 40 | // char mSide;
|
---|
[372] | 41 |
|
---|
[438] | 42 | // computed t
|
---|
[463] | 43 | // float mT;
|
---|
[372] | 44 |
|
---|
[438] | 45 | // inverse of the ray size
|
---|
| 46 | float mInvSize;
|
---|
| 47 |
|
---|
| 48 | // counter of references to this ray
|
---|
[372] | 49 | short mRefCount;
|
---|
[438] | 50 |
|
---|
| 51 | // various flags
|
---|
[372] | 52 | char mFlags;
|
---|
| 53 |
|
---|
| 54 | Vector3 mOrigin;
|
---|
| 55 | Vector3 mTermination;
|
---|
| 56 |
|
---|
[438] | 57 | /// Termination object for the ray
|
---|
| 58 | /// only the termination object is actually used
|
---|
| 59 | Intersectable *mOriginObject;
|
---|
| 60 | Intersectable *mTerminationObject;
|
---|
[464] | 61 |
|
---|
[1966] | 62 | #if VSS_STORE_VIEWCELLS
|
---|
[492] | 63 | ViewCellContainer mViewCells;
|
---|
[1966] | 64 | #endif
|
---|
[1133] | 65 |
|
---|
[464] | 66 | ////////////////////////
|
---|
| 67 | // members related to importance sampling
|
---|
| 68 | // sampling pass in which this ray was generated
|
---|
| 69 | short mPass;
|
---|
| 70 |
|
---|
[1883] | 71 | // Distribution used to generate this ray
|
---|
| 72 | short mDistribution;
|
---|
| 73 |
|
---|
[464] | 74 | // number of cells where this ray made a contribution to the PVS
|
---|
[1883] | 75 | int mPvsContribution;
|
---|
[464] | 76 |
|
---|
| 77 | // sum of relative ray contributions per object
|
---|
| 78 | float mRelativePvsContribution;
|
---|
[492] | 79 |
|
---|
| 80 | // weighted contribution to the pvs (based on the pass the ray was casted at)
|
---|
| 81 | // computed by the prperocessor
|
---|
| 82 | float mWeightedPvsContribution;
|
---|
[534] | 83 |
|
---|
| 84 | // probability of this ray
|
---|
[556] | 85 | float mPdf;
|
---|
[464] | 86 |
|
---|
[438] | 87 | //////////////////////////////
|
---|
[1133] | 88 |
|
---|
| 89 |
|
---|
| 90 | /// the kd node holding the termination point
|
---|
| 91 | KdNode *mTerminationNode;
|
---|
| 92 | /// the kd node holding the origin point
|
---|
| 93 | KdNode *mOriginNode;
|
---|
| 94 |
|
---|
| 95 |
|
---|
[464] | 96 | VssRay(
|
---|
| 97 | const Vector3 &origin,
|
---|
[438] | 98 | const Vector3 &termination,
|
---|
| 99 | Intersectable *originObject,
|
---|
[464] | 100 | Intersectable *terminationObject,
|
---|
[537] | 101 | const int pass = 0,
|
---|
[556] | 102 | const float pdf = 1.0f
|
---|
[1528] | 103 | );
|
---|
[372] | 104 |
|
---|
[1528] | 105 | VssRay(const Ray &ray);
|
---|
[492] | 106 |
|
---|
| 107 |
|
---|
[1528] | 108 | void Precompute();
|
---|
[376] | 109 |
|
---|
[372] | 110 | void Mail() { mMailbox = mailID; }
|
---|
| 111 | static void NewMail() { mailID++; }
|
---|
| 112 | bool Mailed() const { return mMailbox == mailID; }
|
---|
| 113 |
|
---|
| 114 | bool Mailed(const int mail) {
|
---|
| 115 | return mMailbox >= mailID + mail;
|
---|
| 116 | }
|
---|
| 117 |
|
---|
[438] | 118 | int HitCount() const {
|
---|
[386] | 119 | #if BIDIRECTIONAL_RAY
|
---|
[438] | 120 | if (mOriginObject && mTerminationObject)
|
---|
| 121 | return 2;
|
---|
| 122 | if (mOriginObject || mTerminationObject)
|
---|
| 123 | return 1;
|
---|
| 124 | return 0;
|
---|
[386] | 125 | #else
|
---|
[438] | 126 | return (mTerminationObject) ? 1 : 0;
|
---|
[386] | 127 | #endif
|
---|
[438] | 128 | }
|
---|
[372] | 129 |
|
---|
[438] | 130 | Vector3 GetOrigin() const { return mOrigin; }
|
---|
[372] | 131 | Vector3 GetTermination() const { return mTermination; }
|
---|
| 132 | Vector3 GetDir() const { return mTermination - mOrigin; }
|
---|
| 133 | // Vector3 GetNormalizedDir() const { return Normalize(termination - mOrigin); }
|
---|
| 134 | Vector3 GetNormalizedDir() const { return (mTermination - mOrigin)*mInvSize; }
|
---|
| 135 |
|
---|
[448] | 136 | float Length() const { return Distance(mOrigin, mTermination); }
|
---|
| 137 |
|
---|
[438] | 138 | Vector3 Extrap(const float t) const {
|
---|
| 139 | return GetOrigin() + t * GetDir();
|
---|
| 140 | }
|
---|
[434] | 141 |
|
---|
[438] | 142 | float GetDirParametrization(const int axis) const;
|
---|
[492] | 143 | float GetOpositeDirParametrization(const int axis) const;
|
---|
[438] | 144 |
|
---|
| 145 | static float VssRay::GetDirParam(const int axis, const Vector3 dir);
|
---|
[1824] | 146 | static Vector3 VssRay::GetInvDirParam(const float alpha, const float beta);
|
---|
[438] | 147 |
|
---|
| 148 | float GetSize() const { return 1.0f/mInvSize; }
|
---|
| 149 | float GetInvSize() const { return mInvSize; }
|
---|
[372] | 150 | float GetOrigin(const int axis) const { return mOrigin[axis]; }
|
---|
| 151 | float GetTermination(const int axis) const { return mTermination[axis]; }
|
---|
| 152 | float GetDir(const int axis) const { return mTermination[axis] - mOrigin[axis]; }
|
---|
| 153 | float GetNormalizedDir(const int axis) const {
|
---|
| 154 | return (mTermination[axis] - mOrigin[axis])*mInvSize;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | bool
|
---|
| 158 | ComputeMinMaxT(const AxisAlignedBox3 &box,
|
---|
[438] | 159 | float &tmin,
|
---|
| 160 | float &tmax) const;
|
---|
[372] | 161 |
|
---|
| 162 | bool
|
---|
| 163 | Intersects(const AxisAlignedBox3 &box,
|
---|
[438] | 164 | float &tmin,
|
---|
| 165 | float &tmax) const;
|
---|
[372] | 166 |
|
---|
| 167 | bool
|
---|
| 168 | IntersectsSphere(const Vector3 ¢er,
|
---|
[438] | 169 | const float sqrRadius,
|
---|
| 170 | Vector3 &point,
|
---|
| 171 | float &t) const;
|
---|
[372] | 172 |
|
---|
| 173 | void
|
---|
| 174 | Translate(const Vector3 &translation) {
|
---|
| 175 | mOrigin += translation;
|
---|
| 176 | mTermination += translation;
|
---|
| 177 | }
|
---|
[427] | 178 |
|
---|
[438] | 179 | void SetupEndPoints(const Vector3 &origin,
|
---|
| 180 | const Vector3 &termination)
|
---|
| 181 | {
|
---|
| 182 | mOrigin = origin;
|
---|
| 183 | mTermination = termination;
|
---|
| 184 | Precompute();
|
---|
| 185 | }
|
---|
[427] | 186 |
|
---|
[372] | 187 | bool HasPosDir(const int axis) const { return mFlags & (1<<axis); }
|
---|
| 188 |
|
---|
[1112] | 189 | char Flags() const { return mFlags;} void SetFlags(char orFlag) { mFlags |= orFlag;}
|
---|
[372] | 190 |
|
---|
| 191 | bool IsActive() const { return mRefCount>0; }
|
---|
| 192 |
|
---|
| 193 | // reference counting for leaf nodes
|
---|
| 194 | int RefCount() const { return mRefCount; }
|
---|
| 195 | int Ref() { return mRefCount++; }
|
---|
| 196 |
|
---|
| 197 | void ScheduleForRemoval() { if (mRefCount>0) mRefCount = -mRefCount; }
|
---|
| 198 | bool ScheduledForRemoval() const { return mRefCount<0; }
|
---|
| 199 | void Unref() {
|
---|
| 200 | if (mRefCount > 0)
|
---|
| 201 | mRefCount--;
|
---|
| 202 | else
|
---|
| 203 | if (mRefCount < 0)
|
---|
[438] | 204 | mRefCount++;
|
---|
[372] | 205 | else {
|
---|
[438] | 206 | cerr<<"Trying to unref already deleted ray!"<<endl;
|
---|
| 207 | exit(1);
|
---|
[372] | 208 | }
|
---|
| 209 | }
|
---|
[434] | 210 |
|
---|
[438] | 211 | static Vector3
|
---|
| 212 | GetDirection(const float a, const float b) {
|
---|
[1824] | 213 | return GetInvDirParam(a, b);
|
---|
| 214 | //return Vector3(sin(a), sin(b), cos(a));
|
---|
[438] | 215 | }
|
---|
[434] | 216 |
|
---|
[492] | 217 | friend bool GreaterWeightedPvsContribution(const VssRay * a,
|
---|
| 218 | const VssRay *b) {
|
---|
| 219 | return a->mWeightedPvsContribution > b->mWeightedPvsContribution;
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | float SqrDistance(const Vector3 &point) const {
|
---|
| 223 | Vector3 diff = point - mOrigin;
|
---|
| 224 | float t = DotProd(diff, GetDir());
|
---|
| 225 |
|
---|
| 226 | if ( t <= 0.0f ) {
|
---|
| 227 | t = 0.0f;
|
---|
| 228 | } else {
|
---|
| 229 | if (t >= 1.0f) {
|
---|
| 230 | t = 1.0f;
|
---|
| 231 | } else {
|
---|
| 232 | t /= SqrMagnitude(GetDir());
|
---|
| 233 | }
|
---|
| 234 | diff -= t*GetDir();
|
---|
| 235 | }
|
---|
| 236 | return SqrMagnitude(diff);
|
---|
| 237 | }
|
---|
[1259] | 238 |
|
---|
[1528] | 239 | /** Returns the data sampled on either the ray origin or termination.
|
---|
| 240 | */
|
---|
[1259] | 241 | void GetSampleData(
|
---|
| 242 | const bool isTerminaton,
|
---|
| 243 | Vector3 &pt,
|
---|
| 244 | Intersectable **obj,
|
---|
| 245 | KdNode **node) const;
|
---|
| 246 |
|
---|
[1221] | 247 | friend ostream& operator<< (ostream &s, const VssRay &vssRay);
|
---|
| 248 |
|
---|
[372] | 249 | };
|
---|
| 250 |
|
---|
[1528] | 251 | inline void VssRay::GetSampleData(const bool isTermination,
|
---|
| 252 | Vector3 &pt,
|
---|
| 253 | Intersectable **obj,
|
---|
| 254 | KdNode **node) const
|
---|
| 255 | {
|
---|
| 256 | if (isTermination)
|
---|
| 257 | {
|
---|
| 258 | pt = mTermination;
|
---|
| 259 | *obj = mTerminationObject;
|
---|
| 260 | *node = mTerminationNode;
|
---|
| 261 | }
|
---|
| 262 | else
|
---|
| 263 | {
|
---|
| 264 | pt = mOrigin;
|
---|
| 265 | *obj = mOriginObject;
|
---|
| 266 | *node = mOriginNode;
|
---|
| 267 | }
|
---|
| 268 | }
|
---|
| 269 |
|
---|
[446] | 270 | void
|
---|
| 271 | GenerateExtendedConvexCombinationWeights(float &w1,
|
---|
| 272 | float &w2,
|
---|
| 273 | float &w3,
|
---|
| 274 | const float overlap
|
---|
| 275 | );
|
---|
[372] | 276 |
|
---|
[464] | 277 | void
|
---|
| 278 | GenerateExtendedConvexCombinationWeights2(float &w1,
|
---|
| 279 | float &w2,
|
---|
| 280 | const float overlap
|
---|
| 281 | );
|
---|
| 282 |
|
---|
[1221] | 283 | // Overload << operator for C++-style output
|
---|
| 284 | inline ostream&
|
---|
| 285 | operator<< (ostream &s, const VssRay &vssRay)
|
---|
| 286 | {
|
---|
| 287 | return s
|
---|
| 288 | << "(" << vssRay.mPass << ", " << vssRay.mOrigin << ", " << vssRay.mTermination
|
---|
| 289 | << ", " << vssRay.mOriginObject << ", " << vssRay.mTerminationObject << ", " << vssRay.mPdf << ")";
|
---|
| 290 | }
|
---|
| 291 |
|
---|
[446] | 292 | // --------------------------------------------------------------
|
---|
| 293 | // For sorting rays
|
---|
| 294 | // --------------------------------------------------------------
|
---|
[1528] | 295 | struct SortableEntry
|
---|
[446] | 296 | {
|
---|
| 297 | enum EType {
|
---|
| 298 | ERayMin,
|
---|
| 299 | ERayMax
|
---|
| 300 | };
|
---|
[372] | 301 |
|
---|
[446] | 302 | int type;
|
---|
| 303 | float value;
|
---|
| 304 | void *data;
|
---|
| 305 |
|
---|
| 306 | SortableEntry() {}
|
---|
| 307 | SortableEntry(const int t, const float v, void *d):type(t),
|
---|
| 308 | value(v),
|
---|
| 309 | data(d) {}
|
---|
| 310 |
|
---|
| 311 | friend bool operator<(const SortableEntry &a, const SortableEntry &b) {
|
---|
| 312 | return a.value < b.value;
|
---|
| 313 | }
|
---|
| 314 | };
|
---|
| 315 |
|
---|
[467] | 316 | struct VssRayContainer : public vector<VssRay *>
|
---|
| 317 | {
|
---|
| 318 | void PrintStatistics(ostream &s);
|
---|
[1112] | 319 | int SelectRays(const int number, VssRayContainer &selected, const bool copy=false) const;
|
---|
[492] | 320 | int
|
---|
| 321 | GetContributingRays(VssRayContainer &selected,
|
---|
| 322 | const int minPass
|
---|
| 323 | ) const;
|
---|
| 324 |
|
---|
[467] | 325 | };
|
---|
[372] | 326 |
|
---|
[1715] | 327 | /*
|
---|
| 328 | struct VssRayDistribution {
|
---|
| 329 | VssRayDistribution() { mContribution = -1.0f; }
|
---|
| 330 | SimpleRayContainer mRays;
|
---|
| 331 | vector<VssRayContainer> mVssRays;
|
---|
| 332 | float mContribution;
|
---|
| 333 | float mTime;
|
---|
[863] | 334 | };
|
---|
[372] | 335 |
|
---|
[1715] | 336 | struct VssRayDistributionMixture {
|
---|
| 337 | VssRayDistributionMixture() {}
|
---|
| 338 |
|
---|
| 339 | vector<VssRayDistribution> distributions;
|
---|
| 340 | };
|
---|
| 341 | */
|
---|
| 342 |
|
---|
| 343 | };
|
---|
| 344 |
|
---|
[372] | 345 | #endif
|
---|