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