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