[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:
|
---|
| 14 | // various flags
|
---|
| 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 |
|
---|
| 21 | static int mailID;
|
---|
| 22 | int mMailbox;
|
---|
| 23 |
|
---|
| 24 | // side of the ray - used for the ray classification
|
---|
[382] | 25 | // char mSide;
|
---|
[372] | 26 |
|
---|
| 27 | // computed t
|
---|
| 28 | float mT;
|
---|
| 29 |
|
---|
| 30 | // inverse of the ray size
|
---|
| 31 | float mInvSize;
|
---|
| 32 |
|
---|
| 33 | // counter of references to this ray
|
---|
| 34 | short mRefCount;
|
---|
| 35 |
|
---|
| 36 | // various flags
|
---|
| 37 | char mFlags;
|
---|
| 38 |
|
---|
| 39 | Vector3 mOrigin;
|
---|
| 40 | Vector3 mTermination;
|
---|
| 41 |
|
---|
[386] | 42 | /// Termination object for the ray
|
---|
| 43 | /// only the termination object is actually used
|
---|
[372] | 44 | Intersectable *mOriginObject;
|
---|
| 45 | Intersectable *mTerminationObject;
|
---|
| 46 |
|
---|
| 47 | //////////////////////////////
|
---|
| 48 | VssRay(const Vector3 &origin,
|
---|
[376] | 49 | const Vector3 &termination,
|
---|
| 50 | Intersectable *originObject,
|
---|
| 51 | Intersectable *terminationObject):
|
---|
[372] | 52 | mMailbox(-1),
|
---|
| 53 | mOrigin(origin),
|
---|
| 54 | mTermination(termination),
|
---|
| 55 | mOriginObject(originObject),
|
---|
| 56 | mTerminationObject(terminationObject),
|
---|
| 57 | mRefCount(0),
|
---|
| 58 | mFlags(0)
|
---|
| 59 | {
|
---|
| 60 | Precompute();
|
---|
| 61 | }
|
---|
| 62 |
|
---|
[422] | 63 | VssRay(const Ray &ray):
|
---|
| 64 | mOrigin(ray.GetLoc()),
|
---|
[411] | 65 | mOriginObject(ray.sourceObject.mObject),
|
---|
| 66 | mRefCount(0),
|
---|
| 67 | mFlags(0),
|
---|
| 68 | mMailbox(-1)
|
---|
[422] | 69 | {
|
---|
| 70 | if (!ray.intersections.empty())
|
---|
| 71 | {
|
---|
| 72 | mTermination = ray.intersections[0].mT;
|
---|
| 73 | mTerminationObject = ray.intersections[0].mObject;
|
---|
| 74 | }
|
---|
| 75 | else
|
---|
| 76 | {
|
---|
| 77 | mTermination = Limits::Infinity;
|
---|
| 78 | mTerminationObject = NULL;
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
[411] | 81 |
|
---|
[372] | 82 | void Precompute() {
|
---|
| 83 | mFlags = 0;
|
---|
[376] | 84 | Vector3 dir = GetDir();
|
---|
| 85 |
|
---|
[386] | 86 | #define BIDIRECTIONAL_RAY 0
|
---|
| 87 | #if BIDIRECTIONAL_RAY
|
---|
[376] | 88 | if (dir.y < 0) {
|
---|
| 89 | // swap objects and poits
|
---|
| 90 | swap(mOriginObject, mTerminationObject);
|
---|
| 91 | swap(mOrigin, mTermination);
|
---|
| 92 | dir = -dir;
|
---|
| 93 | }
|
---|
[386] | 94 | #endif
|
---|
[376] | 95 | if (dir.x > 0.0f) mFlags |= FPosDirX;
|
---|
| 96 | if (dir.y > 0.0f) mFlags |= FPosDirY;
|
---|
| 97 | if (dir.z > 0.0f) mFlags |= FPosDirZ;
|
---|
| 98 |
|
---|
[372] | 99 | mInvSize = 1.0f/Magnitude(GetDir());
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | void Mail() { mMailbox = mailID; }
|
---|
| 103 | static void NewMail() { mailID++; }
|
---|
| 104 | bool Mailed() const { return mMailbox == mailID; }
|
---|
| 105 |
|
---|
| 106 | bool Mailed(const int mail) {
|
---|
| 107 | return mMailbox >= mailID + mail;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
[376] | 110 | int HitCount() const {
|
---|
[386] | 111 | #if BIDIRECTIONAL_RAY
|
---|
[376] | 112 | if (mOriginObject && mTerminationObject)
|
---|
| 113 | return 2;
|
---|
| 114 | if (mOriginObject || mTerminationObject)
|
---|
| 115 | return 1;
|
---|
| 116 | return 0;
|
---|
[386] | 117 | #else
|
---|
| 118 | return (mTerminationObject) ? 1 : 0;
|
---|
| 119 | #endif
|
---|
[376] | 120 | }
|
---|
[372] | 121 |
|
---|
| 122 | Vector3 GetOrigin() const { return mOrigin; }
|
---|
| 123 | Vector3 GetTermination() const { return mTermination; }
|
---|
| 124 | Vector3 GetDir() const { return mTermination - mOrigin; }
|
---|
| 125 | // Vector3 GetNormalizedDir() const { return Normalize(termination - mOrigin); }
|
---|
| 126 | Vector3 GetNormalizedDir() const { return (mTermination - mOrigin)*mInvSize; }
|
---|
| 127 |
|
---|
[382] | 128 | float GetDirParametrization(const int axis) const;
|
---|
| 129 |
|
---|
[372] | 130 | float GetInvSize() const { return mInvSize; }
|
---|
| 131 | float GetOrigin(const int axis) const { return mOrigin[axis]; }
|
---|
| 132 | float GetTermination(const int axis) const { return mTermination[axis]; }
|
---|
| 133 | float GetDir(const int axis) const { return mTermination[axis] - mOrigin[axis]; }
|
---|
| 134 | float GetNormalizedDir(const int axis) const {
|
---|
| 135 | return (mTermination[axis] - mOrigin[axis])*mInvSize;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | bool
|
---|
| 139 | ComputeMinMaxT(const AxisAlignedBox3 &box,
|
---|
| 140 | float &tmin,
|
---|
| 141 | float &tmax) const;
|
---|
| 142 |
|
---|
| 143 | bool
|
---|
| 144 | Intersects(const AxisAlignedBox3 &box,
|
---|
| 145 | float &tmin,
|
---|
| 146 | float &tmax) const;
|
---|
| 147 |
|
---|
| 148 | bool
|
---|
| 149 | IntersectsSphere(const Vector3 ¢er,
|
---|
| 150 | const float sqrRadius,
|
---|
| 151 | Vector3 &point,
|
---|
| 152 | float &t) const;
|
---|
| 153 |
|
---|
| 154 | void
|
---|
| 155 | Translate(const Vector3 &translation) {
|
---|
| 156 | mOrigin += translation;
|
---|
| 157 | mTermination += translation;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
| 160 | bool HasPosDir(const int axis) const { return mFlags & (1<<axis); }
|
---|
| 161 |
|
---|
| 162 | char Flags() const { return mFlags;}
|
---|
| 163 | void SetFlags(char orFlag) { mFlags |= orFlag;}
|
---|
| 164 |
|
---|
| 165 | bool IsActive() const { return mRefCount>0; }
|
---|
| 166 |
|
---|
| 167 | // reference counting for leaf nodes
|
---|
| 168 | int RefCount() const { return mRefCount; }
|
---|
| 169 | int Ref() { return mRefCount++; }
|
---|
| 170 |
|
---|
| 171 | void ScheduleForRemoval() { if (mRefCount>0) mRefCount = -mRefCount; }
|
---|
| 172 | bool ScheduledForRemoval() const { return mRefCount<0; }
|
---|
| 173 | void Unref() {
|
---|
| 174 | if (mRefCount > 0)
|
---|
| 175 | mRefCount--;
|
---|
| 176 | else
|
---|
| 177 | if (mRefCount < 0)
|
---|
| 178 | mRefCount++;
|
---|
| 179 | else {
|
---|
| 180 | cerr<<"Trying to unref already deleted ray!"<<endl;
|
---|
| 181 | exit(1);
|
---|
| 182 | }
|
---|
| 183 | }
|
---|
| 184 | };
|
---|
| 185 |
|
---|
| 186 |
|
---|
| 187 |
|
---|
| 188 | typedef vector<VssRay *> VssRayContainer;
|
---|
| 189 |
|
---|
| 190 |
|
---|
| 191 |
|
---|
| 192 | #endif
|
---|