[376] | 1 | #ifndef __VSS_RAY_H
|
---|
| 2 | #define __VSS_RAY_H
|
---|
[372] | 3 |
|
---|
| 4 | #include <vector>
|
---|
| 5 | using namespace std;
|
---|
| 6 | #include "Vector3.h"
|
---|
| 7 |
|
---|
| 8 | class AxisAlignedBox3;
|
---|
| 9 | class Intersectable;
|
---|
| 10 |
|
---|
| 11 | class VssRay {
|
---|
| 12 | public:
|
---|
| 13 | // various flags
|
---|
| 14 | enum {
|
---|
| 15 | FPosDirX = 1, // the direction of ray in X-axis is positive
|
---|
| 16 | FPosDirY = 2, // the direction of ray in Y-axis is positive
|
---|
| 17 | FPosDirZ = 4 // the direction of ray in Z-axis is positive
|
---|
| 18 | };
|
---|
| 19 |
|
---|
| 20 | static int mailID;
|
---|
| 21 | int mMailbox;
|
---|
| 22 |
|
---|
| 23 | // side of the ray - used for the ray classification
|
---|
| 24 | char mSide;
|
---|
| 25 |
|
---|
| 26 | // computed t
|
---|
| 27 | float mT;
|
---|
| 28 |
|
---|
| 29 | // inverse of the ray size
|
---|
| 30 | float mInvSize;
|
---|
| 31 |
|
---|
| 32 | // counter of references to this ray
|
---|
| 33 | short mRefCount;
|
---|
| 34 |
|
---|
| 35 | // various flags
|
---|
| 36 | char mFlags;
|
---|
| 37 |
|
---|
| 38 | Vector3 mOrigin;
|
---|
| 39 | Vector3 mTermination;
|
---|
| 40 |
|
---|
| 41 | Intersectable *mOriginObject;
|
---|
| 42 | Intersectable *mTerminationObject;
|
---|
| 43 |
|
---|
| 44 | //////////////////////////////
|
---|
| 45 | VssRay(const Vector3 &origin,
|
---|
[376] | 46 | const Vector3 &termination,
|
---|
| 47 | Intersectable *originObject,
|
---|
| 48 | Intersectable *terminationObject):
|
---|
[372] | 49 | mMailbox(-1),
|
---|
| 50 | mOrigin(origin),
|
---|
| 51 | mTermination(termination),
|
---|
| 52 | mOriginObject(originObject),
|
---|
| 53 | mTerminationObject(terminationObject),
|
---|
| 54 | mRefCount(0),
|
---|
| 55 | mFlags(0)
|
---|
| 56 | {
|
---|
| 57 | Precompute();
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | void Precompute() {
|
---|
| 61 | mFlags = 0;
|
---|
[376] | 62 | Vector3 dir = GetDir();
|
---|
| 63 |
|
---|
| 64 | if (dir.y < 0) {
|
---|
| 65 | // swap objects and poits
|
---|
| 66 | swap(mOriginObject, mTerminationObject);
|
---|
| 67 | swap(mOrigin, mTermination);
|
---|
| 68 | dir = -dir;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | if (dir.x > 0.0f) mFlags |= FPosDirX;
|
---|
| 72 | if (dir.y > 0.0f) mFlags |= FPosDirY;
|
---|
| 73 | if (dir.z > 0.0f) mFlags |= FPosDirZ;
|
---|
| 74 |
|
---|
[372] | 75 | mInvSize = 1.0f/Magnitude(GetDir());
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | void Mail() { mMailbox = mailID; }
|
---|
| 79 | static void NewMail() { mailID++; }
|
---|
| 80 | bool Mailed() const { return mMailbox == mailID; }
|
---|
| 81 |
|
---|
| 82 | bool Mailed(const int mail) {
|
---|
| 83 | return mMailbox >= mailID + mail;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[376] | 86 | int HitCount() const {
|
---|
| 87 | if (mOriginObject && mTerminationObject)
|
---|
| 88 | return 2;
|
---|
| 89 | if (mOriginObject || mTerminationObject)
|
---|
| 90 | return 1;
|
---|
| 91 | return 0;
|
---|
| 92 | }
|
---|
[372] | 93 |
|
---|
| 94 | Vector3 GetOrigin() const { return mOrigin; }
|
---|
| 95 | Vector3 GetTermination() const { return mTermination; }
|
---|
| 96 | Vector3 GetDir() const { return mTermination - mOrigin; }
|
---|
| 97 | // Vector3 GetNormalizedDir() const { return Normalize(termination - mOrigin); }
|
---|
| 98 | Vector3 GetNormalizedDir() const { return (mTermination - mOrigin)*mInvSize; }
|
---|
| 99 |
|
---|
| 100 | float GetInvSize() const { return mInvSize; }
|
---|
| 101 | float GetOrigin(const int axis) const { return mOrigin[axis]; }
|
---|
| 102 | float GetTermination(const int axis) const { return mTermination[axis]; }
|
---|
| 103 | float GetDir(const int axis) const { return mTermination[axis] - mOrigin[axis]; }
|
---|
| 104 | float GetNormalizedDir(const int axis) const {
|
---|
| 105 | return (mTermination[axis] - mOrigin[axis])*mInvSize;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | bool
|
---|
| 109 | ComputeMinMaxT(const AxisAlignedBox3 &box,
|
---|
| 110 | float &tmin,
|
---|
| 111 | float &tmax) const;
|
---|
| 112 |
|
---|
| 113 | bool
|
---|
| 114 | Intersects(const AxisAlignedBox3 &box,
|
---|
| 115 | float &tmin,
|
---|
| 116 | float &tmax) const;
|
---|
| 117 |
|
---|
| 118 | bool
|
---|
| 119 | IntersectsSphere(const Vector3 ¢er,
|
---|
| 120 | const float sqrRadius,
|
---|
| 121 | Vector3 &point,
|
---|
| 122 | float &t) const;
|
---|
| 123 |
|
---|
| 124 | void
|
---|
| 125 | Translate(const Vector3 &translation) {
|
---|
| 126 | mOrigin += translation;
|
---|
| 127 | mTermination += translation;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | bool HasPosDir(const int axis) const { return mFlags & (1<<axis); }
|
---|
| 131 |
|
---|
| 132 | char Flags() const { return mFlags;}
|
---|
| 133 | void SetFlags(char orFlag) { mFlags |= orFlag;}
|
---|
| 134 |
|
---|
| 135 | bool IsActive() const { return mRefCount>0; }
|
---|
| 136 |
|
---|
| 137 | // reference counting for leaf nodes
|
---|
| 138 | int RefCount() const { return mRefCount; }
|
---|
| 139 | int Ref() { return mRefCount++; }
|
---|
| 140 |
|
---|
| 141 | void ScheduleForRemoval() { if (mRefCount>0) mRefCount = -mRefCount; }
|
---|
| 142 | bool ScheduledForRemoval() const { return mRefCount<0; }
|
---|
| 143 | void Unref() {
|
---|
| 144 | if (mRefCount > 0)
|
---|
| 145 | mRefCount--;
|
---|
| 146 | else
|
---|
| 147 | if (mRefCount < 0)
|
---|
| 148 | mRefCount++;
|
---|
| 149 | else {
|
---|
| 150 | cerr<<"Trying to unref already deleted ray!"<<endl;
|
---|
| 151 | exit(1);
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 | };
|
---|
| 155 |
|
---|
| 156 |
|
---|
| 157 |
|
---|
| 158 | typedef vector<VssRay *> VssRayContainer;
|
---|
| 159 |
|
---|
| 160 |
|
---|
| 161 |
|
---|
| 162 | #endif
|
---|