[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 |
|
---|
[860] | 9 | namespace GtpVisibilityPreprocessor {
|
---|
| 10 |
|
---|
[162] | 11 | // forward declarations
|
---|
| 12 | class Plane3;
|
---|
| 13 | class Intersectable;
|
---|
| 14 | class KdLeaf;
|
---|
| 15 | class MeshInstance;
|
---|
[308] | 16 | class ViewCell;
|
---|
[362] | 17 | class BspLeaf;
|
---|
[426] | 18 | class VssRay;
|
---|
[162] | 19 |
|
---|
[448] | 20 |
|
---|
[162] | 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:
|
---|
[209] | 29 | enum RayType { LOCAL_RAY, GLOBAL_RAY, LINE_SEGMENT };
|
---|
[369] | 30 |
|
---|
[162] | 31 | enum { NO_INTERSECTION=0, INTERSECTION_OUT_OF_LIMITS, INTERSECTION };
|
---|
| 32 |
|
---|
[350] | 33 | /// if ray is on back (front) side of plane, or goes from the
|
---|
| 34 | /// front (back) to the back (front)
|
---|
[349] | 35 | enum {FRONT, BACK, BACK_FRONT, FRONT_BACK, COINCIDENT};
|
---|
[374] | 36 |
|
---|
[191] | 37 | struct Intersection {
|
---|
[162] | 38 | // the point of intersection
|
---|
| 39 | float mT;
|
---|
[176] | 40 |
|
---|
[1199] | 41 | // the normal of the intersection
|
---|
| 42 | Vector3 mNormal;
|
---|
| 43 |
|
---|
[162] | 44 | // can be either mesh or a viewcell
|
---|
| 45 | Intersectable *mObject;
|
---|
[369] | 46 |
|
---|
[162] | 47 | // the face of the intersectable
|
---|
| 48 | int mFace;
|
---|
[1199] | 49 |
|
---|
[191] | 50 | Intersection(const float t,
|
---|
[1199] | 51 | const Vector3 &normal,
|
---|
[392] | 52 | Intersectable *object,
|
---|
[1199] | 53 | const int face):mT(t),
|
---|
| 54 | mNormal(normal),
|
---|
| 55 | mObject(object), mFace(face) {}
|
---|
[369] | 56 |
|
---|
[1199] | 57 | Intersection(): mT(0), mNormal(0,0,0), mObject(NULL), mFace(0) {}
|
---|
[369] | 58 |
|
---|
[162] | 59 | bool operator<(
|
---|
[448] | 60 | const Intersection &b) const {
|
---|
| 61 |
|
---|
[162] | 62 | return
|
---|
[369] | 63 | mT
|
---|
| 64 | <
|
---|
| 65 | b.mT;
|
---|
[162] | 66 | }
|
---|
| 67 |
|
---|
| 68 | };
|
---|
[366] | 69 |
|
---|
| 70 |
|
---|
[176] | 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 */
|
---|
[191] | 74 | Intersection sourceObject;
|
---|
[176] | 75 |
|
---|
[191] | 76 | vector<Intersection> intersections;
|
---|
[475] | 77 | // vector<BspIntersection> bspIntersections;
|
---|
[362] | 78 | vector<KdLeaf *> kdLeaves;
|
---|
[374] | 79 | vector<Intersectable *> testedObjects;
|
---|
| 80 |
|
---|
[534] | 81 | // various flags
|
---|
| 82 | enum {STORE_KDLEAVES=1, STORE_BSP_INTERSECTIONS=2, STORE_TESTED_OBJECTS=4, CULL_BACKFACES=8};
|
---|
| 83 | int mFlags;
|
---|
[374] | 84 |
|
---|
| 85 |
|
---|
[162] | 86 | // constructors
|
---|
| 87 | Ray(const Vector3 &wherefrom,
|
---|
| 88 | const Vector3 &whichdir,
|
---|
[534] | 89 | const int _type):mFlags(CULL_BACKFACES) {
|
---|
[162] | 90 | loc = wherefrom;
|
---|
[209] | 91 | if (_type == LINE_SEGMENT)
|
---|
| 92 | dir = whichdir;
|
---|
| 93 | else
|
---|
| 94 | dir = Normalize(whichdir);
|
---|
[162] | 95 | mType = _type;
|
---|
| 96 | depth = 0;
|
---|
[374] | 97 | Init();
|
---|
[162] | 98 | }
|
---|
| 99 | // dummy constructor
|
---|
| 100 | Ray() {}
|
---|
[426] | 101 |
|
---|
| 102 | /** Construct ray from a vss ray.
|
---|
| 103 | */
|
---|
[466] | 104 | Ray(const VssRay &vssRay) {
|
---|
| 105 | Init(vssRay);
|
---|
[534] | 106 | mFlags |= CULL_BACKFACES;
|
---|
[466] | 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 |
|
---|
[191] | 118 | Intersectable *GetIntersectionObject(const int i) const {
|
---|
| 119 | return intersections[i].mObject;
|
---|
| 120 | }
|
---|
[162] | 121 |
|
---|
[191] | 122 | Vector3 GetIntersectionPoint(const int i) const {
|
---|
| 123 | return Extrap(intersections[i].mT);
|
---|
| 124 | }
|
---|
| 125 |
|
---|
[162] | 126 | // Inititalize the ray again when already constructed
|
---|
[245] | 127 | void Init(const Vector3 &wherefrom,
|
---|
[392] | 128 | const Vector3 &whichdir,
|
---|
| 129 | const int _type,
|
---|
| 130 | bool dirNormalized = false) {
|
---|
[162] | 131 | loc = wherefrom;
|
---|
[245] | 132 | dir = (dirNormalized || _type == LINE_SEGMENT) ? whichdir: Normalize(whichdir) ;
|
---|
[162] | 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)
|
---|
[328] | 180 | int GetId() const { return ID; }
|
---|
[162] | 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
|
---|
[328] | 189 | void SetId() {
|
---|
[162] | 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.
|
---|
[328] | 196 | void SetId(int newID) {
|
---|
[162] | 197 | ID = newID;
|
---|
| 198 | // note that transfID is not changed!
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 |
|
---|
| 202 | // the object on which the ray starts at
|
---|
[191] | 203 | const Intersection* GetStartObject() const { return &intersections[0]; }
|
---|
| 204 | const Intersection* GetStopObject() const { return &intersections[intersections.size()-1]; }
|
---|
[162] | 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 |
|
---|
[327] | 228 | /** Classifies ray with respect to the plane.
|
---|
| 229 | */
|
---|
[406] | 230 | int ClassifyPlane(const Plane3 &plane,
|
---|
| 231 | const float minT,
|
---|
| 232 | const float maxT,
|
---|
| 233 | Vector3 &entP,
|
---|
| 234 | Vector3 &extP) const;
|
---|
[327] | 235 |
|
---|
[162] | 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;
|
---|
[176] | 249 |
|
---|
[162] | 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)
|
---|
[374] | 261 | int depth;
|
---|
| 262 |
|
---|
| 263 |
|
---|
| 264 |
|
---|
[162] | 265 | void Init();
|
---|
| 266 |
|
---|
| 267 | // precompute some values that are necessary
|
---|
| 268 | void Precompute();
|
---|
| 269 |
|
---|
| 270 | friend class AxisAlignedBox3;
|
---|
[327] | 271 | friend class Plane3;
|
---|
[162] | 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);
|
---|
[340] | 278 |
|
---|
| 279 | friend ostream &operator<<(ostream &s, const Ray &r) {
|
---|
| 280 | return s<<"Ray:loc="<<r.loc<<" dir="<<r.dir;
|
---|
| 281 | }
|
---|
[162] | 282 | };
|
---|
| 283 |
|
---|
| 284 |
|
---|
[191] | 285 | class PassingRaySet {
|
---|
| 286 | public:
|
---|
| 287 | enum { Resolution = 2 };
|
---|
| 288 | int mDirectionalContributions[3*Resolution*Resolution];
|
---|
| 289 | int mRays;
|
---|
| 290 | int mContributions;
|
---|
[369] | 291 |
|
---|
| 292 | PassingRaySet() {
|
---|
[191] | 293 | Reset();
|
---|
| 294 | }
|
---|
[369] | 295 |
|
---|
| 296 | void
|
---|
[191] | 297 | Reset();
|
---|
| 298 |
|
---|
| 299 | void AddRay(const Ray &ray, const int contributions);
|
---|
[369] | 300 | void AddRay2(const Ray &ray,
|
---|
| 301 | const int objects,
|
---|
| 302 | const int viewcells);
|
---|
| 303 |
|
---|
[191] | 304 | int GetEntryIndex(const Vector3 &direction) const;
|
---|
[162] | 305 |
|
---|
[191] | 306 | friend ostream &operator<<(ostream &s, const PassingRaySet &set);
|
---|
[162] | 307 |
|
---|
[191] | 308 | };
|
---|
| 309 |
|
---|
[401] | 310 | struct SimpleRay
|
---|
| 311 | {
|
---|
[534] | 312 | Vector3 mOrigin;
|
---|
| 313 | Vector3 mDirection;
|
---|
[556] | 314 | float mPdf;
|
---|
[534] | 315 |
|
---|
| 316 | SimpleRay() {}
|
---|
[537] | 317 | SimpleRay(const Vector3 &o, const Vector3 &d, const float p=1.0f):
|
---|
[556] | 318 | mOrigin(o), mDirection(d), mPdf(p) {}
|
---|
[401] | 319 | };
|
---|
[191] | 320 |
|
---|
[537] | 321 | class SimpleRayContainer : public vector<SimpleRay>
|
---|
| 322 | {
|
---|
| 323 | public:
|
---|
| 324 |
|
---|
| 325 |
|
---|
[1112] | 326 | SimpleRayContainer():vector<SimpleRay>() {}
|
---|
[537] | 327 |
|
---|
[1112] | 328 | void NormalizePdf(float scale = 1.0f) {
|
---|
[537] | 329 | iterator it = begin();
|
---|
[1112] | 330 | float sumPdf = 0.0f;
|
---|
[537] | 331 | for (; it != end(); it++)
|
---|
[1112] | 332 | sumPdf += (*it).mPdf;
|
---|
| 333 |
|
---|
| 334 | float c = scale/sumPdf;
|
---|
| 335 |
|
---|
| 336 | for (it = begin(); it != end(); it++) {
|
---|
[556] | 337 | (*it).mPdf*=c;
|
---|
[1112] | 338 | }
|
---|
[537] | 339 | }
|
---|
[1112] | 340 |
|
---|
[563] | 341 | void AddRay(const SimpleRay &ray) {
|
---|
| 342 | push_back(ray);
|
---|
| 343 | }
|
---|
[537] | 344 | };
|
---|
[401] | 345 |
|
---|
[860] | 346 | }
|
---|
| 347 |
|
---|
[162] | 348 | #endif
|
---|
| 349 |
|
---|