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