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