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