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