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