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