NvRayCast.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. NvRayCast.cpp : A code snippet to cast a ray against a triangle mesh. This implementation does not use any acceleration data structures. That is a 'to do' item.
  3. */
  4. /*!
  5. **
  6. ** Copyright (c) 2009 by John W. Ratcliff mailto:[email protected]
  7. **
  8. ** Portions of this source has been released with the PhysXViewer application, as well as
  9. ** Rocket, CreateDynamics, ODF, and as a number of sample code snippets.
  10. **
  11. ** If you find this code useful or you are feeling particularily generous I would
  12. ** ask that you please go to http://www.amillionpixels.us and make a donation
  13. ** to Troy DeMolay.
  14. **
  15. ** DeMolay is a youth group for young men between the ages of 12 and 21.
  16. ** It teaches strong moral principles, as well as leadership skills and
  17. ** public speaking. The donations page uses the 'pay for pixels' paradigm
  18. ** where, in this case, a pixel is only a single penny. Donations can be
  19. ** made for as small as $4 or as high as a $100 block. Each person who donates
  20. ** will get a link to their own site as well as acknowledgement on the
  21. ** donations blog located here http://www.amillionpixels.blogspot.com/
  22. **
  23. ** If you wish to contact me you can use the following methods:
  24. **
  25. ** Skype ID: jratcliff63367
  26. ** Yahoo: jratcliff63367
  27. ** AOL: jratcliff1961
  28. ** email: [email protected]
  29. **
  30. **
  31. ** The MIT license:
  32. **
  33. ** Permission is hereby granted, free of charge, to any person obtaining a copy
  34. ** of this software and associated documentation files (the "Software"), to deal
  35. ** in the Software without restriction, including without limitation the rights
  36. ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  37. ** copies of the Software, and to permit persons to whom the Software is furnished
  38. ** to do so, subject to the following conditions:
  39. **
  40. ** The above copyright notice and this permission notice shall be included in all
  41. ** copies or substantial portions of the Software.
  42. ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  43. ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  44. ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  45. ** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  46. ** WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  47. ** CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  48. */
  49. #include "NvRayCast.h"
  50. #include "NvUserMemAlloc.h"
  51. #include "NvFloatMath.h"
  52. #pragma warning(disable:4100)
  53. namespace CONVEX_DECOMPOSITION
  54. {
  55. class RayCast : public iRayCast, public Memalloc
  56. {
  57. public:
  58. RayCast(const NxF32 *vertices,NxU32 tcount,const NxU32 *indices)
  59. {
  60. mVertices = vertices;
  61. mTcount = tcount;
  62. mIndices = indices;
  63. }
  64. ~RayCast(void)
  65. {
  66. }
  67. virtual bool castRay(const NxF32 *orig,const NxF32 *dir,NxF32 *dest,NxF32 *hitNormal)
  68. {
  69. bool ret = false;
  70. NxF32 p2[3];
  71. const NxF32 RAY_DIST=50;
  72. dest[0] = p2[0] = orig[0]+ dir[0]*RAY_DIST;
  73. dest[1] = p2[1] = orig[1]+ dir[1]*RAY_DIST;
  74. dest[2] = p2[2] = orig[2]+ dir[2]*RAY_DIST;
  75. NxF32 nearest = 1e9;
  76. NxU32 near_face=0;
  77. for (NxU32 i=0; i<mTcount; i++)
  78. {
  79. NxU32 i1 = mIndices[i*3+0];
  80. NxU32 i2 = mIndices[i*3+1];
  81. NxU32 i3 = mIndices[i*3+2];
  82. const NxF32 *t1 = &mVertices[i1*3];
  83. const NxF32 *t2 = &mVertices[i2*3];
  84. const NxF32 *t3 = &mVertices[i3*3];
  85. NxF32 t;
  86. if ( fm_rayIntersectsTriangle(orig,dir,t1,t2,t3,t) )
  87. {
  88. if ( t < nearest )
  89. {
  90. dest[0] = orig[0]+dir[0]*t;
  91. dest[1] = orig[1]+dir[1]*t;
  92. dest[2] = orig[2]+dir[2]*t;
  93. ret = true;
  94. near_face = i;
  95. nearest = t;
  96. }
  97. }
  98. }
  99. if ( ret )
  100. {
  101. // If the nearest face we hit was back-facing, then reject this cast!
  102. NxU32 i1 = mIndices[near_face*3+0];
  103. NxU32 i2 = mIndices[near_face*3+1];
  104. NxU32 i3 = mIndices[near_face*3+2];
  105. const NxF32 *t1 = &mVertices[i1*3];
  106. const NxF32 *t2 = &mVertices[i2*3];
  107. const NxF32 *t3 = &mVertices[i3*3];
  108. fm_computePlane(t3,t2,t1,hitNormal);
  109. }
  110. return ret;
  111. }
  112. private:
  113. const NxF32 *mVertices;
  114. NxU32 mTcount;
  115. const NxU32 *mIndices;
  116. };
  117. iRayCast *createRayCast(const NxF32 *vertices,NxU32 tcount,const NxU32 *indices)
  118. {
  119. RayCast *rc = MEMALLOC_NEW(RayCast)(vertices,tcount,indices);
  120. return static_cast< iRayCast *>(rc);
  121. }
  122. void releaseRayCast(iRayCast *rc)
  123. {
  124. RayCast *r = static_cast< RayCast *>(rc);
  125. delete r;
  126. }
  127. };