raycast_mesh.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #ifndef RAYCAST_MESH_H
  2. #define RAYCAST_MESH_H
  3. // This code snippet allows you to create an axis aligned bounding volume tree for a triangle mesh so that you can do
  4. // high-speed raycasting.
  5. //
  6. // There are much better implementations of this available on the internet. In particular I recommend that you use
  7. // OPCODE written by Pierre Terdiman.
  8. // @see: http://www.codercorner.com/Opcode.htm
  9. //
  10. // OPCODE does a whole lot more than just raycasting, and is a rather significant amount of source code.
  11. //
  12. // I am providing this code snippet for the use case where you *only* want to do quick and dirty optimized raycasting.
  13. // I have not done performance testing between this version and OPCODE; so I don't know how much slower it is. However,
  14. // anytime you switch to using a spatial data structure for raycasting, you increase your performance by orders and orders
  15. // of magnitude; so this implementation should work fine for simple tools and utilities.
  16. //
  17. // It also serves as a nice sample for people who are trying to learn the algorithm of how to implement AABB trees.
  18. // AABB = Axis Aligned Bounding Volume trees.
  19. //
  20. // http://www.cgal.org/Manual/3.5/doc_html/cgal_manual/AABB_tree/Chapter_main.html
  21. //
  22. //
  23. // This code snippet was written by John W. Ratcliff on August 18, 2011 and released under the MIT. license.
  24. //
  25. // mailto:jratcliffscarab@gmail.com
  26. //
  27. // The official source can be found at: http://code.google.com/p/raycastmesh/
  28. //
  29. //
  30. typedef float RmReal;
  31. typedef unsigned int RmUint32;
  32. class RaycastMesh
  33. {
  34. public:
  35. virtual bool raycast(const RmReal *from,const RmReal *to,RmReal *hitLocation,RmReal *hitNormal,RmReal *hitDistance, RmUint32 *GridID) = 0;
  36. virtual bool bruteForceRaycast(const RmReal *from,const RmReal *to,RmReal *hitLocation,RmReal *hitNormal,RmReal *hitDistance, RmUint32 *GridID) = 0;
  37. virtual const RmReal * getBoundMin(void) const = 0; // return the minimum bounding box
  38. virtual const RmReal * getBoundMax(void) const = 0; // return the maximum bounding box.
  39. virtual void release(void) = 0;
  40. protected:
  41. virtual ~RaycastMesh(void) { };
  42. };
  43. RaycastMesh * createRaycastMesh(RmUint32 vcount, // The number of vertices in the source triangle mesh
  44. const RmReal *vertices, // The array of vertex positions in the format x1,y1,z1..x2,y2,z2.. etc.
  45. RmUint32 tcount, // The number of triangles in the source triangle mesh
  46. const RmUint32 *indices, // The triangle indices in the format of i1,i2,i3 ... i4,i5,i6, ...
  47. const RmUint32 *grids,
  48. RmUint32 maxDepth=15, // Maximum recursion depth for the triangle mesh.
  49. RmUint32 minLeafSize=4, // minimum triangles to treat as a 'leaf' node.
  50. RmReal minAxisSize=0.01f // once a particular axis is less than this size, stop sub-dividing.
  51. );
  52. #ifdef USE_MAP_MMFS
  53. #include <vector>
  54. RaycastMesh* loadRaycastMesh(std::vector<char>& rm_buffer, bool& load_success);
  55. void serializeRaycastMesh(RaycastMesh* rm, std::vector<char>& rm_buffer);
  56. #endif /*USE_MAP_MMFS*/
  57. #endif