raytri.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include "float_math.h"
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include <math.h>
  7. #include "raytri.h"
  8. /*----------------------------------------------------------------------
  9. Copyright (c) 2004 Open Dynamics Framework Group
  10. www.physicstools.org
  11. All rights reserved.
  12. Redistribution and use in source and binary forms, with or without modification, are permitted provided
  13. that the following conditions are met:
  14. Redistributions of source code must retain the above copyright notice, this list of conditions
  15. and the following disclaimer.
  16. Redistributions in binary form must reproduce the above copyright notice,
  17. this list of conditions and the following disclaimer in the documentation
  18. and/or other materials provided with the distribution.
  19. Neither the name of the Open Dynamics Framework Group nor the names of its contributors may
  20. be used to endorse or promote products derived from this software without specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 'AS IS' AND ANY EXPRESS OR IMPLIED WARRANTIES,
  22. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. DISCLAIMED. IN NO EVENT SHALL THE INTEL OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  26. IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. -----------------------------------------------------------------------*/
  29. // http://codesuppository.blogspot.com
  30. //
  31. // mailto: [email protected]
  32. //
  33. // http://www.amillionpixels.us
  34. //
  35. /* a = b - c */
  36. #define vector(a,b,c) \
  37. (a)[0] = (b)[0] - (c)[0]; \
  38. (a)[1] = (b)[1] - (c)[1]; \
  39. (a)[2] = (b)[2] - (c)[2];
  40. #define innerProduct(v,q) \
  41. ((v)[0] * (q)[0] + \
  42. (v)[1] * (q)[1] + \
  43. (v)[2] * (q)[2])
  44. #define crossProduct(a,b,c) \
  45. (a)[0] = (b)[1] * (c)[2] - (c)[1] * (b)[2]; \
  46. (a)[1] = (b)[2] * (c)[0] - (c)[2] * (b)[0]; \
  47. (a)[2] = (b)[0] * (c)[1] - (c)[0] * (b)[1];
  48. bool rayIntersectsTriangle(const float *p,const float *d,const float *v0,const float *v1,const float *v2,float &t)
  49. {
  50. float e1[3],e2[3],h[3],s[3],q[3];
  51. float a,f,u,v;
  52. vector(e1,v1,v0);
  53. vector(e2,v2,v0);
  54. crossProduct(h,d,e2);
  55. a = innerProduct(e1,h);
  56. if (a > -0.00001 && a < 0.00001)
  57. return(false);
  58. f = 1/a;
  59. vector(s,p,v0);
  60. u = f * (innerProduct(s,h));
  61. if (u < 0.0 || u > 1.0)
  62. return(false);
  63. crossProduct(q,s,e1);
  64. v = f * innerProduct(d,q);
  65. if (v < 0.0 || u + v > 1.0)
  66. return(false);
  67. // at this stage we can compute t to find out where
  68. // the intersection point is on the line
  69. t = f * innerProduct(e2,q);
  70. if (t > 0) // ray intersection
  71. return(true);
  72. else // this means that there is a line intersection
  73. // but not a ray intersection
  74. return (false);
  75. }
  76. bool lineIntersectsTriangle(const float *rayStart,const float *rayEnd,const float *p1,const float *p2,const float *p3,float *sect)
  77. {
  78. float dir[3];
  79. dir[0] = rayEnd[0] - rayStart[0];
  80. dir[1] = rayEnd[1] - rayStart[1];
  81. dir[2] = rayEnd[2] - rayStart[2];
  82. float d = sqrtf(dir[0]*dir[0] + dir[1]*dir[1] + dir[2]*dir[2]);
  83. float r = 1.0f / d;
  84. dir[0]*=r;
  85. dir[1]*=r;
  86. dir[2]*=r;
  87. float t;
  88. bool ret = rayIntersectsTriangle(rayStart, dir, p1, p2, p3, t );
  89. if ( ret )
  90. {
  91. if ( t > d )
  92. {
  93. sect[0] = rayStart[0] + dir[0]*t;
  94. sect[1] = rayStart[1] + dir[1]*t;
  95. sect[2] = rayStart[2] + dir[2]*t;
  96. }
  97. else
  98. {
  99. ret = false;
  100. }
  101. }
  102. return ret;
  103. }