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