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