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