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