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