1 | #ifndef __RAY_H__
|
---|
2 | #define __RAY_H__
|
---|
3 |
|
---|
4 | #include <vector>
|
---|
5 | #include "Matrix4x4.h"
|
---|
6 | #include "Vector3.h"
|
---|
7 |
|
---|
8 | // forward declarations
|
---|
9 | class Plane3;
|
---|
10 | class Intersectable;
|
---|
11 | class KdLeaf;
|
---|
12 | class MeshInstance;
|
---|
13 | class ViewCell;
|
---|
14 | class BspLeaf;
|
---|
15 | class VssRay;
|
---|
16 |
|
---|
17 |
|
---|
18 | // -------------------------------------------------------------------
|
---|
19 | // CRay class. A ray is defined by a location and a direction.
|
---|
20 | // The direction is always normalized (length == 1).
|
---|
21 | // -------------------------------------------------------------------
|
---|
22 |
|
---|
23 | class Ray
|
---|
24 | {
|
---|
25 | public:
|
---|
26 | enum RayType { LOCAL_RAY, GLOBAL_RAY, LINE_SEGMENT };
|
---|
27 |
|
---|
28 | enum { NO_INTERSECTION=0, INTERSECTION_OUT_OF_LIMITS, INTERSECTION };
|
---|
29 |
|
---|
30 | /// if ray is on back (front) side of plane, or goes from the
|
---|
31 | /// front (back) to the back (front)
|
---|
32 | enum {FRONT, BACK, BACK_FRONT, FRONT_BACK, COINCIDENT};
|
---|
33 |
|
---|
34 | struct Intersection {
|
---|
35 | // the point of intersection
|
---|
36 | float mT;
|
---|
37 |
|
---|
38 | // can be either mesh or a viewcell
|
---|
39 | Intersectable *mObject;
|
---|
40 |
|
---|
41 | // the face of the intersectable
|
---|
42 | int mFace;
|
---|
43 |
|
---|
44 | Intersection(const float t,
|
---|
45 | Intersectable *object,
|
---|
46 | const int face):mT(t), mObject(object), mFace(face) {}
|
---|
47 |
|
---|
48 | Intersection(): mT(0), mObject(NULL), mFace(0) {}
|
---|
49 |
|
---|
50 | bool operator<(
|
---|
51 | const Intersection &b) const {
|
---|
52 |
|
---|
53 | return
|
---|
54 | mT
|
---|
55 | <
|
---|
56 | b.mT;
|
---|
57 | }
|
---|
58 |
|
---|
59 | };
|
---|
60 |
|
---|
61 | struct BspIntersection {
|
---|
62 | // the point of intersection
|
---|
63 | float mT;
|
---|
64 |
|
---|
65 | BspLeaf *mLeaf;
|
---|
66 |
|
---|
67 | BspIntersection(const float t, BspLeaf *l):
|
---|
68 | mT(t), mLeaf(l) {}
|
---|
69 |
|
---|
70 | BspIntersection() {}
|
---|
71 |
|
---|
72 | bool operator<(const BspIntersection &b) const {
|
---|
73 | return mT <b.mT; }
|
---|
74 | };
|
---|
75 |
|
---|
76 | // I should have some abstract cell data type !!! here
|
---|
77 | // corresponds to the spatial elementary cell
|
---|
78 | /** intersection with the source object if any */
|
---|
79 | Intersection sourceObject;
|
---|
80 |
|
---|
81 | vector<Intersection> intersections;
|
---|
82 | vector<BspIntersection> bspIntersections;
|
---|
83 | vector<KdLeaf *> kdLeaves;
|
---|
84 | vector<Intersectable *> testedObjects;
|
---|
85 |
|
---|
86 | // various flags
|
---|
87 | enum {STORE_KDLEAVES=1, STORE_BSP_INTERSECTIONS=2, STORE_TESTED_OBJECTS=4};
|
---|
88 | int mFlags;
|
---|
89 |
|
---|
90 |
|
---|
91 | // constructors
|
---|
92 | Ray(const Vector3 &wherefrom,
|
---|
93 | const Vector3 &whichdir,
|
---|
94 | const int _type):mFlags(0) {
|
---|
95 | loc = wherefrom;
|
---|
96 | if (_type == LINE_SEGMENT)
|
---|
97 | dir = whichdir;
|
---|
98 | else
|
---|
99 | dir = Normalize(whichdir);
|
---|
100 | mType = _type;
|
---|
101 | depth = 0;
|
---|
102 | Init();
|
---|
103 | }
|
---|
104 | // dummy constructor
|
---|
105 | Ray() {}
|
---|
106 |
|
---|
107 | /** Construct ray from a vss ray.
|
---|
108 | */
|
---|
109 | Ray(const VssRay &vssRay);
|
---|
110 |
|
---|
111 | Intersectable *GetIntersectionObject(const int i) const {
|
---|
112 | return intersections[i].mObject;
|
---|
113 | }
|
---|
114 |
|
---|
115 | Vector3 GetIntersectionPoint(const int i) const {
|
---|
116 | return Extrap(intersections[i].mT);
|
---|
117 | }
|
---|
118 |
|
---|
119 | // Inititalize the ray again when already constructed
|
---|
120 | void Init(const Vector3 &wherefrom,
|
---|
121 | const Vector3 &whichdir,
|
---|
122 | const int _type,
|
---|
123 | bool dirNormalized = false) {
|
---|
124 | loc = wherefrom;
|
---|
125 | dir = (dirNormalized || _type == LINE_SEGMENT) ? whichdir: Normalize(whichdir) ;
|
---|
126 | mType = _type;
|
---|
127 | depth = 0;
|
---|
128 | Init();
|
---|
129 | }
|
---|
130 |
|
---|
131 | // --------------------------------------------------------
|
---|
132 | // Extrapolate ray given a signed distance, returns a point
|
---|
133 | // --------------------------------------------------------
|
---|
134 | Vector3 Extrap(float t) const {
|
---|
135 | return loc + dir * t;
|
---|
136 | }
|
---|
137 |
|
---|
138 | // -----------------------------------
|
---|
139 | // Return parameter given point on ray
|
---|
140 | // -----------------------------------
|
---|
141 | float Interp(Vector3 &x) const {
|
---|
142 | for (int i = 0; i < 3; i++)
|
---|
143 | if (Abs(dir[i]) > Limits::Small)
|
---|
144 | return (x[i] - loc[i]) / dir[i];
|
---|
145 | return 0;
|
---|
146 | }
|
---|
147 |
|
---|
148 | // -----------------------------------
|
---|
149 | // Reflects direction of reflection for the ray,
|
---|
150 | // given the normal to the surface.
|
---|
151 | // -----------------------------------
|
---|
152 | Vector3 ReflectRay(const Vector3 &N) const {
|
---|
153 | return N * 2.0 * DotProd(N, -dir) + dir;
|
---|
154 | }
|
---|
155 | void ReflectRay(Vector3 &result, const Vector3 &N) const {
|
---|
156 | result = N * 2.0 * DotProd(N, -dir) + dir;
|
---|
157 | }
|
---|
158 |
|
---|
159 | // Computes the inverted direction of the ray, used optionally by
|
---|
160 | // a ray traversal algorithm.
|
---|
161 | void ComputeInvertedDir() const;
|
---|
162 |
|
---|
163 | // Given the matrix 4x4, transforms the ray to another space
|
---|
164 | void ApplyTransform(const Matrix4x4 &tform) {
|
---|
165 | loc = tform * loc;
|
---|
166 | dir = RotateOnly(tform, dir);
|
---|
167 | // note that normalization to the unit size of the direction
|
---|
168 | // is NOT computed -- this is what we want.
|
---|
169 | Precompute();
|
---|
170 | }
|
---|
171 |
|
---|
172 | // returns ID of this ray (use for mailboxes)
|
---|
173 | int GetId() const { return ID; }
|
---|
174 |
|
---|
175 | // returns the transfrom ID of the ray (use for ray transformations)
|
---|
176 | int GetTransformID() const { return transfID; }
|
---|
177 |
|
---|
178 | // copy the transform ID from an input ray
|
---|
179 | void CopyTransformID(const Ray &ray) { transfID = ray.transfID; }
|
---|
180 |
|
---|
181 | // set unique ID for a given ray - always avoid setting to zero
|
---|
182 | void SetId() {
|
---|
183 | if ((ID = ++genID) == 0)
|
---|
184 | ID = ++genID;
|
---|
185 | transfID = ID;
|
---|
186 | }
|
---|
187 | // set ID to explicit value - it can be even 0 for rays transformed
|
---|
188 | // to the canonical object space to supress the mailbox failure.
|
---|
189 | void SetId(int newID) {
|
---|
190 | ID = newID;
|
---|
191 | // note that transfID is not changed!
|
---|
192 | }
|
---|
193 |
|
---|
194 |
|
---|
195 | // the object on which the ray starts at
|
---|
196 | const Intersection* GetStartObject() const { return &intersections[0]; }
|
---|
197 | const Intersection* GetStopObject() const { return &intersections[intersections.size()-1]; }
|
---|
198 |
|
---|
199 |
|
---|
200 | void SetLoc(const Vector3 &l);
|
---|
201 | Vector3& GetLoc() { return loc; }
|
---|
202 | Vector3 GetLoc() const { return loc; }
|
---|
203 |
|
---|
204 | float GetLoc(const int axis) const { return loc[axis]; }
|
---|
205 |
|
---|
206 | void SetDir(const Vector3 &ndir) { dir = ndir;}
|
---|
207 | Vector3& GetDir() { return dir; }
|
---|
208 | Vector3 GetDir() const { return dir; }
|
---|
209 | float GetDir(const int axis) const { return dir[axis]; }
|
---|
210 |
|
---|
211 | int GetType() const { return mType; }
|
---|
212 |
|
---|
213 | // make such operation to slightly change the ray direction
|
---|
214 | // in case any component of ray direction is zero.
|
---|
215 | void CorrectZeroComponents();
|
---|
216 |
|
---|
217 | // the depth of the ray - primary rays are in the depth 0
|
---|
218 | int GetDepth() const { return depth;}
|
---|
219 | void SetDepth(int newDepth) { depth = newDepth;}
|
---|
220 |
|
---|
221 | /** Classifies ray with respect to the plane.
|
---|
222 | */
|
---|
223 | int ClassifyPlane(const Plane3 &plane,
|
---|
224 | const float minT,
|
---|
225 | const float maxT,
|
---|
226 | Vector3 &entP,
|
---|
227 | Vector3 &extP) const;
|
---|
228 |
|
---|
229 | private:
|
---|
230 | Vector3 loc, dir; // Describes ray origin and vector
|
---|
231 |
|
---|
232 | // The inverted direction of the ray components. It is computed optionally
|
---|
233 | // by the ray traversal algorithm using function ComputeInvertedDir();
|
---|
234 | mutable Vector3 invDir;
|
---|
235 |
|
---|
236 | // Type of the ray: primary, shadow, dummy etc., see ERayType above
|
---|
237 | int mType;
|
---|
238 |
|
---|
239 |
|
---|
240 | // unique ID of a ray for the use in the mailboxes
|
---|
241 | int ID;
|
---|
242 |
|
---|
243 | // unique ID of a ray for the use with a transformations - this one
|
---|
244 | // never can be changed that allows the nesting of transformations
|
---|
245 | // and caching the transformed rays correctly
|
---|
246 | int transfID;
|
---|
247 |
|
---|
248 | // the ID generator fo each ray instantiated
|
---|
249 | static int genID;
|
---|
250 |
|
---|
251 | // When ray shot from the source(camera/light), this number is equal
|
---|
252 | // to the number of bounces of the ray, also called the depth of the
|
---|
253 | // ray (primary ray has its depth zero)
|
---|
254 | int depth;
|
---|
255 |
|
---|
256 |
|
---|
257 |
|
---|
258 | void Init();
|
---|
259 |
|
---|
260 | // precompute some values that are necessary
|
---|
261 | void Precompute();
|
---|
262 |
|
---|
263 | friend class AxisAlignedBox3;
|
---|
264 | friend class Plane3;
|
---|
265 |
|
---|
266 | // for CKDR GEMS
|
---|
267 | friend float DistanceToXPlane(const Vector3 &vec, const Ray &ray);
|
---|
268 | friend float DistanceToYPlane(const Vector3 &vec, const Ray &ray);
|
---|
269 | friend float DistanceToZPlane(const Vector3 &vec, const Ray &ray);
|
---|
270 | friend int MakeIntersectLine(const Plane3 &p, const Plane3 &q, Ray &ray);
|
---|
271 |
|
---|
272 | friend ostream &operator<<(ostream &s, const Ray &r) {
|
---|
273 | return s<<"Ray:loc="<<r.loc<<" dir="<<r.dir;
|
---|
274 | }
|
---|
275 | };
|
---|
276 |
|
---|
277 |
|
---|
278 | class PassingRaySet {
|
---|
279 | public:
|
---|
280 | enum { Resolution = 2 };
|
---|
281 | int mDirectionalContributions[3*Resolution*Resolution];
|
---|
282 | int mRays;
|
---|
283 | int mContributions;
|
---|
284 |
|
---|
285 | PassingRaySet() {
|
---|
286 | Reset();
|
---|
287 | }
|
---|
288 |
|
---|
289 | void
|
---|
290 | Reset();
|
---|
291 |
|
---|
292 | void AddRay(const Ray &ray, const int contributions);
|
---|
293 | void AddRay2(const Ray &ray,
|
---|
294 | const int objects,
|
---|
295 | const int viewcells);
|
---|
296 |
|
---|
297 | int GetEntryIndex(const Vector3 &direction) const;
|
---|
298 |
|
---|
299 | friend ostream &operator<<(ostream &s, const PassingRaySet &set);
|
---|
300 |
|
---|
301 | };
|
---|
302 |
|
---|
303 | struct SimpleRay
|
---|
304 | {
|
---|
305 | Vector3 mOrigin;
|
---|
306 | Vector3 mDirection;
|
---|
307 | SimpleRay() {}
|
---|
308 | SimpleRay(const Vector3 &o, const Vector3 &d):mOrigin(o), mDirection(d) {}
|
---|
309 | };
|
---|
310 |
|
---|
311 | typedef vector<SimpleRay> SimpleRayContainer;
|
---|
312 |
|
---|
313 | #endif
|
---|
314 |
|
---|