[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
|
---|
[372] | 28 | float mT;
|
---|
| 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;
|
---|
[372] | 46 |
|
---|
[438] | 47 | //////////////////////////////
|
---|
| 48 | VssRay(const Vector3 &origin,
|
---|
| 49 | const Vector3 &termination,
|
---|
| 50 | Intersectable *originObject,
|
---|
| 51 | Intersectable *terminationObject):
|
---|
| 52 | mMailbox(-1),
|
---|
[372] | 53 | mOrigin(origin),
|
---|
| 54 | mTermination(termination),
|
---|
[438] | 55 | mOriginObject(originObject),
|
---|
| 56 | mTerminationObject(terminationObject),
|
---|
| 57 | mRefCount(0),
|
---|
[431] | 58 | mFlags(0),
|
---|
| 59 | mT(1.0f)
|
---|
[438] | 60 | {
|
---|
[372] | 61 | Precompute();
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[438] | 64 | VssRay(const Ray &ray):
|
---|
| 65 | mRefCount(0),
|
---|
| 66 | mFlags(0),
|
---|
| 67 | mMailbox(-1),
|
---|
| 68 | mOriginObject(ray.sourceObject.mObject),
|
---|
| 69 | mT(1.0f)
|
---|
[441] | 70 | {
|
---|
[444] | 71 | if (ray.sourceObject.mObject)
|
---|
| 72 | mOrigin = ray.Extrap(ray.sourceObject.mT);
|
---|
| 73 | else
|
---|
| 74 | mOrigin = ray.GetLoc();
|
---|
[426] | 75 |
|
---|
[444] | 76 | //Debug << "origin: " << mOrigin << endl;
|
---|
| 77 | if (!ray.intersections.empty())
|
---|
| 78 | {
|
---|
| 79 | mTermination = ray.Extrap(ray.intersections[0].mT);
|
---|
| 80 | mTerminationObject = ray.intersections[0].mObject;
|
---|
| 81 | }
|
---|
| 82 | else
|
---|
| 83 | {
|
---|
| 84 | mTermination = ray.Extrap(1e6);//TODO: should be Limits::Infinity
|
---|
| 85 | mTerminationObject = NULL;
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | Precompute();
|
---|
[438] | 89 | }
|
---|
[411] | 90 |
|
---|
[372] | 91 | void Precompute() {
|
---|
| 92 | mFlags = 0;
|
---|
[438] | 93 | Vector3 dir = GetDir();
|
---|
[376] | 94 |
|
---|
[386] | 95 | #define BIDIRECTIONAL_RAY 0
|
---|
| 96 | #if BIDIRECTIONAL_RAY
|
---|
[438] | 97 | if (dir.y < 0) {
|
---|
| 98 | // swap objects and poits
|
---|
| 99 | swap(mOriginObject, mTerminationObject);
|
---|
| 100 | swap(mOrigin, mTermination);
|
---|
| 101 | dir = -dir;
|
---|
| 102 | }
|
---|
[386] | 103 | #endif
|
---|
[438] | 104 | if (dir.x > 0.0f) mFlags |= FPosDirX;
|
---|
[376] | 105 | if (dir.y > 0.0f) mFlags |= FPosDirY;
|
---|
| 106 | if (dir.z > 0.0f) mFlags |= FPosDirZ;
|
---|
| 107 |
|
---|
[372] | 108 | mInvSize = 1.0f/Magnitude(GetDir());
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | void Mail() { mMailbox = mailID; }
|
---|
| 112 | static void NewMail() { mailID++; }
|
---|
| 113 | bool Mailed() const { return mMailbox == mailID; }
|
---|
| 114 |
|
---|
| 115 | bool Mailed(const int mail) {
|
---|
| 116 | return mMailbox >= mailID + mail;
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[438] | 119 | int HitCount() const {
|
---|
[386] | 120 | #if BIDIRECTIONAL_RAY
|
---|
[438] | 121 | if (mOriginObject && mTerminationObject)
|
---|
| 122 | return 2;
|
---|
| 123 | if (mOriginObject || mTerminationObject)
|
---|
| 124 | return 1;
|
---|
| 125 | return 0;
|
---|
[386] | 126 | #else
|
---|
[438] | 127 | return (mTerminationObject) ? 1 : 0;
|
---|
[386] | 128 | #endif
|
---|
[438] | 129 | }
|
---|
[372] | 130 |
|
---|
[438] | 131 | Vector3 GetOrigin() const { return mOrigin; }
|
---|
[372] | 132 | Vector3 GetTermination() const { return mTermination; }
|
---|
| 133 | Vector3 GetDir() const { return mTermination - mOrigin; }
|
---|
| 134 | // Vector3 GetNormalizedDir() const { return Normalize(termination - mOrigin); }
|
---|
| 135 | Vector3 GetNormalizedDir() const { return (mTermination - mOrigin)*mInvSize; }
|
---|
| 136 |
|
---|
[448] | 137 | float Length() const { return Distance(mOrigin, mTermination); }
|
---|
| 138 |
|
---|
[438] | 139 | Vector3 Extrap(const float t) const {
|
---|
| 140 | return GetOrigin() + t * GetDir();
|
---|
| 141 | }
|
---|
[434] | 142 |
|
---|
[438] | 143 | float GetDirParametrization(const int axis) const;
|
---|
| 144 |
|
---|
| 145 | static float VssRay::GetDirParam(const int axis, const Vector3 dir);
|
---|
| 146 |
|
---|
| 147 | float GetSize() const { return 1.0f/mInvSize; }
|
---|
| 148 | float GetInvSize() const { return mInvSize; }
|
---|
[372] | 149 | float GetOrigin(const int axis) const { return mOrigin[axis]; }
|
---|
| 150 | float GetTermination(const int axis) const { return mTermination[axis]; }
|
---|
| 151 | float GetDir(const int axis) const { return mTermination[axis] - mOrigin[axis]; }
|
---|
| 152 | float GetNormalizedDir(const int axis) const {
|
---|
| 153 | return (mTermination[axis] - mOrigin[axis])*mInvSize;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | bool
|
---|
| 157 | ComputeMinMaxT(const AxisAlignedBox3 &box,
|
---|
[438] | 158 | float &tmin,
|
---|
| 159 | float &tmax) const;
|
---|
[372] | 160 |
|
---|
| 161 | bool
|
---|
| 162 | Intersects(const AxisAlignedBox3 &box,
|
---|
[438] | 163 | float &tmin,
|
---|
| 164 | float &tmax) const;
|
---|
[372] | 165 |
|
---|
| 166 | bool
|
---|
| 167 | IntersectsSphere(const Vector3 ¢er,
|
---|
[438] | 168 | const float sqrRadius,
|
---|
| 169 | Vector3 &point,
|
---|
| 170 | float &t) const;
|
---|
[372] | 171 |
|
---|
| 172 | void
|
---|
| 173 | Translate(const Vector3 &translation) {
|
---|
| 174 | mOrigin += translation;
|
---|
| 175 | mTermination += translation;
|
---|
| 176 | }
|
---|
[427] | 177 |
|
---|
[438] | 178 | void SetupEndPoints(const Vector3 &origin,
|
---|
| 179 | const Vector3 &termination)
|
---|
| 180 | {
|
---|
| 181 | mOrigin = origin;
|
---|
| 182 | mTermination = termination;
|
---|
| 183 | Precompute();
|
---|
| 184 | }
|
---|
[427] | 185 |
|
---|
[372] | 186 | bool HasPosDir(const int axis) const { return mFlags & (1<<axis); }
|
---|
| 187 |
|
---|
| 188 | char Flags() const { return mFlags;}
|
---|
| 189 | void SetFlags(char orFlag) { mFlags |= orFlag;}
|
---|
| 190 |
|
---|
| 191 | bool IsActive() const { return mRefCount>0; }
|
---|
| 192 |
|
---|
| 193 | // reference counting for leaf nodes
|
---|
| 194 | int RefCount() const { return mRefCount; }
|
---|
| 195 | int Ref() { return mRefCount++; }
|
---|
| 196 |
|
---|
| 197 | void ScheduleForRemoval() { if (mRefCount>0) mRefCount = -mRefCount; }
|
---|
| 198 | bool ScheduledForRemoval() const { return mRefCount<0; }
|
---|
| 199 | void Unref() {
|
---|
| 200 | if (mRefCount > 0)
|
---|
| 201 | mRefCount--;
|
---|
| 202 | else
|
---|
| 203 | if (mRefCount < 0)
|
---|
[438] | 204 | mRefCount++;
|
---|
[372] | 205 | else {
|
---|
[438] | 206 | cerr<<"Trying to unref already deleted ray!"<<endl;
|
---|
| 207 | exit(1);
|
---|
[372] | 208 | }
|
---|
| 209 | }
|
---|
[434] | 210 |
|
---|
[438] | 211 | static Vector3
|
---|
| 212 | GetDirection(const float a, const float b) {
|
---|
| 213 | return Vector3(sin(a), sin(b), cos(a));
|
---|
| 214 | }
|
---|
[434] | 215 |
|
---|
[372] | 216 | };
|
---|
| 217 |
|
---|
[446] | 218 | void
|
---|
| 219 | GenerateExtendedConvexCombinationWeights(float &w1,
|
---|
| 220 | float &w2,
|
---|
| 221 | float &w3,
|
---|
| 222 | const float overlap
|
---|
| 223 | );
|
---|
[372] | 224 |
|
---|
[446] | 225 | // --------------------------------------------------------------
|
---|
| 226 | // For sorting rays
|
---|
| 227 | // --------------------------------------------------------------
|
---|
| 228 | struct SortableEntry
|
---|
| 229 | {
|
---|
| 230 | enum EType {
|
---|
| 231 | ERayMin,
|
---|
| 232 | ERayMax
|
---|
| 233 | };
|
---|
[372] | 234 |
|
---|
[446] | 235 | int type;
|
---|
| 236 | float value;
|
---|
| 237 | void *data;
|
---|
| 238 |
|
---|
| 239 | SortableEntry() {}
|
---|
| 240 | SortableEntry(const int t, const float v, void *d):type(t),
|
---|
| 241 | value(v),
|
---|
| 242 | data(d) {}
|
---|
| 243 |
|
---|
| 244 | friend bool operator<(const SortableEntry &a, const SortableEntry &b) {
|
---|
| 245 | return a.value < b.value;
|
---|
| 246 | }
|
---|
| 247 | };
|
---|
| 248 |
|
---|
[372] | 249 | typedef vector<VssRay *> VssRayContainer;
|
---|
| 250 |
|
---|
| 251 |
|
---|
| 252 |
|
---|
| 253 | #endif
|
---|