Advertisement
Guest User

Untitled

a guest
Mar 15th, 2012
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.53 KB | None | 0 0
  1. class FindNearestIntersection
  2. {
  3. public:
  4.     /// When finished, this will contain the distance from the origin of the ray
  5.     /// to the nearest intersection, or FLOAT_MAX if there was no intersection.
  6.     float nearestD;
  7.     /// When finished, this will store the index of the triangle the ray hit, or -1
  8.     /// if there was no hit.
  9.     int nearestIndex;
  10.  
  11.     FindNearestIntersection()
  12.     {
  13.         // Init to 'no hit' values.
  14.         nearestD = FLOAT_MAX;
  15.         nearestIndex = -1;
  16.     }
  17.  
  18.     /// The KdTree class will call this function for each leaf (only leaves contain triangles) of the KdTree it travels.
  19.     /// This function is expected to test the ray against the objects in the leaf node.
  20.     /// The input ray is capped to a line segment interval [tNear, tFar].
  21.     /// @param tree The tree object that is being traversed.
  22.     /// @param leaf The leaf object that contains triangles the ray potentially hits.
  23.     /// @param ray The ray used in the query.
  24.     /// @param tNear The starting distance along the ray, specifies the portion of the ray to be used in this query.
  25.     /// @param tNear The end distance along the ray, specifies the portion of the ray to be used in this query.
  26.     /// @return If this function returns true, the KdTree class will immediately stop the query and return back to caller.
  27.     ///         If false is returned, the query is to continue. Use this return value to control whether to perform
  28.     ///         'any intersection', 'all intersections' or 'nearest intersection' type of queries.
  29.     bool operator()(KdTree<Triangle> &tree, const KdTreeNode &leaf, const Ray &ray, float tNear, float tFar)
  30.     {
  31.         // Get a list of all the triangles in this leaf node.
  32.         const u32 *tris = tree.Bucket(leaf.bucketIndex);
  33.  
  34.         if (!tris)
  35.             return false; // Empty? Skip this leaf.
  36.  
  37.         // Find the nearest triangle in this leaf.
  38.         float nearestDistance = tFar;
  39.         while(*tris != -1)
  40.         {
  41.             TrianglePtr tri = *tris;
  42.             ++tris;
  43.  
  44.             const Triangle &t = tree.Object(tri);
  45.  
  46.             // Intersect the ray against each triangle in this leaf, and remember which one was the nearest.
  47.             float d, u, v;
  48.             d = Triangle::IntersectLineTri(ray.pos, ray.dir, t.a, t.b, t.c, u, v);
  49.             if (d >= tNear && d < nearestDistance) // Found a better hit, remember the nearest.
  50.             {
  51.                 nearestDistance = d;
  52.                 nearestIndex = tri;
  53.             }
  54.         }
  55.  
  56.         if (nearestIndex == -1)
  57.             return false; // No hit? continue RayQuery to other leaves.
  58.  
  59.         nearestD = min(nearestD, nearestDistance);
  60.  
  61.         // We got a hit, so stop the query, since we are only were interested in the nearest intersection, and got it.
  62.         return true;
  63.     }
  64. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement