1 | #ifndef __VSS_RAY_H
|
---|
2 | #define __VSS_RAY_H
|
---|
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,
|
---|
46 | const Vector3 &termination,
|
---|
47 | Intersectable *originObject,
|
---|
48 | Intersectable *terminationObject):
|
---|
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;
|
---|
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 |
|
---|
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 |
|
---|
86 | int HitCount() const {
|
---|
87 | if (mOriginObject && mTerminationObject)
|
---|
88 | return 2;
|
---|
89 | if (mOriginObject || mTerminationObject)
|
---|
90 | return 1;
|
---|
91 | return 0;
|
---|
92 | }
|
---|
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 GetDirParametrization(const int axis) const;
|
---|
101 |
|
---|
102 | float GetInvSize() const { return mInvSize; }
|
---|
103 | float GetOrigin(const int axis) const { return mOrigin[axis]; }
|
---|
104 | float GetTermination(const int axis) const { return mTermination[axis]; }
|
---|
105 | float GetDir(const int axis) const { return mTermination[axis] - mOrigin[axis]; }
|
---|
106 | float GetNormalizedDir(const int axis) const {
|
---|
107 | return (mTermination[axis] - mOrigin[axis])*mInvSize;
|
---|
108 | }
|
---|
109 |
|
---|
110 | bool
|
---|
111 | ComputeMinMaxT(const AxisAlignedBox3 &box,
|
---|
112 | float &tmin,
|
---|
113 | float &tmax) const;
|
---|
114 |
|
---|
115 | bool
|
---|
116 | Intersects(const AxisAlignedBox3 &box,
|
---|
117 | float &tmin,
|
---|
118 | float &tmax) const;
|
---|
119 |
|
---|
120 | bool
|
---|
121 | IntersectsSphere(const Vector3 ¢er,
|
---|
122 | const float sqrRadius,
|
---|
123 | Vector3 &point,
|
---|
124 | float &t) const;
|
---|
125 |
|
---|
126 | void
|
---|
127 | Translate(const Vector3 &translation) {
|
---|
128 | mOrigin += translation;
|
---|
129 | mTermination += translation;
|
---|
130 | }
|
---|
131 |
|
---|
132 | bool HasPosDir(const int axis) const { return mFlags & (1<<axis); }
|
---|
133 |
|
---|
134 | char Flags() const { return mFlags;}
|
---|
135 | void SetFlags(char orFlag) { mFlags |= orFlag;}
|
---|
136 |
|
---|
137 | bool IsActive() const { return mRefCount>0; }
|
---|
138 |
|
---|
139 | // reference counting for leaf nodes
|
---|
140 | int RefCount() const { return mRefCount; }
|
---|
141 | int Ref() { return mRefCount++; }
|
---|
142 |
|
---|
143 | void ScheduleForRemoval() { if (mRefCount>0) mRefCount = -mRefCount; }
|
---|
144 | bool ScheduledForRemoval() const { return mRefCount<0; }
|
---|
145 | void Unref() {
|
---|
146 | if (mRefCount > 0)
|
---|
147 | mRefCount--;
|
---|
148 | else
|
---|
149 | if (mRefCount < 0)
|
---|
150 | mRefCount++;
|
---|
151 | else {
|
---|
152 | cerr<<"Trying to unref already deleted ray!"<<endl;
|
---|
153 | exit(1);
|
---|
154 | }
|
---|
155 | }
|
---|
156 | };
|
---|
157 |
|
---|
158 |
|
---|
159 |
|
---|
160 | typedef vector<VssRay *> VssRayContainer;
|
---|
161 |
|
---|
162 |
|
---|
163 |
|
---|
164 | #endif
|
---|