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