#ifndef __RAY_H__ #define __RAY_H__ #include #include "Matrix4x4.h" #include "Vector3.h" namespace GtpVisibilityPreprocessor { // forward declarations class Plane3; class Intersectable; class KdLeaf; class MeshInstance; class ViewCell; class BspLeaf; class VssRay; // ------------------------------------------------------------------- // CRay class. A ray is defined by a location and a direction. // The direction is always normalized (length == 1). // ------------------------------------------------------------------- class Ray { public: enum RayType { LOCAL_RAY, GLOBAL_RAY, LINE_SEGMENT }; enum { NO_INTERSECTION=0, INTERSECTION_OUT_OF_LIMITS, INTERSECTION }; /// if ray is on back (front) side of plane, or goes from the /// front (back) to the back (front) enum {FRONT, BACK, BACK_FRONT, FRONT_BACK, COINCIDENT}; struct Intersection { /// the point of intersection float mT; /// the normal of the intersection Vector3 mNormal; /// can be either mesh or a viewcell Intersectable *mObject; /// the face of the intersectable int mFace; Intersection(const float t, const Vector3 &normal, Intersectable *object, const int face): mT(t), mNormal(normal), mObject(object), mFace(face) {} Intersection(): mT(0), mNormal(0,0,0), mObject(NULL), mFace(0) {} bool operator<(const Intersection &b) const { return mT < b.mT; } }; // I should have some abstract cell data type !!! here // corresponds to the spatial elementary cell /** intersection with the source object if any */ Intersection sourceObject; vector intersections; // vector bspIntersections; vector kdLeaves; vector testedObjects; // various flags enum {STORE_KDLEAVES=1, STORE_BSP_INTERSECTIONS=2, STORE_TESTED_OBJECTS=4, CULL_BACKFACES=8}; int mFlags; // constructors Ray(const Vector3 &wherefrom, const Vector3 &whichdir, const int _type):mFlags(CULL_BACKFACES) { loc = wherefrom; if (_type == LINE_SEGMENT) dir = whichdir; else dir = Normalize(whichdir); mType = _type; depth = 0; Init(); } // dummy constructor Ray() {} /** Construct ray from a vss ray. */ Ray(const VssRay &vssRay) { Init(vssRay); mFlags |= CULL_BACKFACES; } void Clear() { intersections.clear(); kdLeaves.clear(); testedObjects.clear(); // bspIntersections.clear(); } void Init(const VssRay &vssRay); Intersectable *GetIntersectionObject(const int i) const { return intersections[i].mObject; } Vector3 GetIntersectionPoint(const int i) const { return Extrap(intersections[i].mT); } // Inititalize the ray again when already constructed void Init(const Vector3 &wherefrom, const Vector3 &whichdir, const int _type, bool dirNormalized = false) { loc = wherefrom; dir = (dirNormalized || _type == LINE_SEGMENT) ? whichdir: Normalize(whichdir) ; mType = _type; depth = 0; Init(); } // -------------------------------------------------------- // Extrapolate ray given a signed distance, returns a point // -------------------------------------------------------- Vector3 Extrap(float t) const { return loc + dir * t; } // ----------------------------------- // Return parameter given point on ray // ----------------------------------- float Interp(Vector3 &x) const { for (int i = 0; i < 3; i++) if (Abs(dir[i]) > Limits::Small) return (x[i] - loc[i]) / dir[i]; return 0; } // ----------------------------------- // Reflects direction of reflection for the ray, // given the normal to the surface. // ----------------------------------- Vector3 ReflectRay(const Vector3 &N) const { return N * 2.0 * DotProd(N, -dir) + dir; } void ReflectRay(Vector3 &result, const Vector3 &N) const { result = N * 2.0 * DotProd(N, -dir) + dir; } // Computes the inverted direction of the ray, used optionally by // a ray traversal algorithm. void ComputeInvertedDir() const; // Given the matrix 4x4, transforms the ray to another space void ApplyTransform(const Matrix4x4 &tform) { loc = tform * loc; dir = RotateOnly(tform, dir); // note that normalization to the unit size of the direction // is NOT computed -- this is what we want. Precompute(); } // returns ID of this ray (use for mailboxes) int GetId() const { return ID; } // returns the transfrom ID of the ray (use for ray transformations) int GetTransformID() const { return transfID; } // copy the transform ID from an input ray void CopyTransformID(const Ray &ray) { transfID = ray.transfID; } // set unique ID for a given ray - always avoid setting to zero void SetId() { if ((ID = ++genID) == 0) ID = ++genID; transfID = ID; } // set ID to explicit value - it can be even 0 for rays transformed // to the canonical object space to supress the mailbox failure. void SetId(int newID) { ID = newID; // note that transfID is not changed! } // the object on which the ray starts at const Intersection* GetStartObject() const { return &intersections[0]; } const Intersection* GetStopObject() const { return &intersections[intersections.size()-1]; } void SetLoc(const Vector3 &l); Vector3& GetLoc() { return loc; } Vector3 GetLoc() const { return loc; } float GetLoc(const int axis) const { return loc[axis]; } void SetDir(const Vector3 &ndir) { dir = ndir;} Vector3& GetDir() { return dir; } Vector3 GetDir() const { return dir; } float GetDir(const int axis) const { return dir[axis]; } int GetType() const { return mType; } // make such operation to slightly change the ray direction // in case any component of ray direction is zero. void CorrectZeroComponents(); // the depth of the ray - primary rays are in the depth 0 int GetDepth() const { return depth;} void SetDepth(int newDepth) { depth = newDepth;} /** Classifies ray with respect to the plane. */ int ClassifyPlane(const Plane3 &plane, const float minT, const float maxT, Vector3 &entP, Vector3 &extP) const; private: Vector3 loc, dir; // Describes ray origin and vector // The inverted direction of the ray components. It is computed optionally // by the ray traversal algorithm using function ComputeInvertedDir(); mutable Vector3 invDir; // Type of the ray: primary, shadow, dummy etc., see ERayType above int mType; // unique ID of a ray for the use in the mailboxes int ID; // unique ID of a ray for the use with a transformations - this one // never can be changed that allows the nesting of transformations // and caching the transformed rays correctly int transfID; // the ID generator fo each ray instantiated static int genID; // When ray shot from the source(camera/light), this number is equal // to the number of bounces of the ray, also called the depth of the // ray (primary ray has its depth zero) int depth; void Init(); // precompute some values that are necessary void Precompute(); friend class AxisAlignedBox3; friend class Plane3; // for CKDR GEMS friend float DistanceToXPlane(const Vector3 &vec, const Ray &ray); friend float DistanceToYPlane(const Vector3 &vec, const Ray &ray); friend float DistanceToZPlane(const Vector3 &vec, const Ray &ray); friend int MakeIntersectLine(const Plane3 &p, const Plane3 &q, Ray &ray); friend ostream &operator<<(ostream &s, const Ray &r) { return s<<"Ray:loc="<