Advertisement
Guest User

Untitled

a guest
May 7th, 2012
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. // original
  2. bool IntersectBoxWithSphere(const Math::AABB& rhs, const Physics::Sphere& sphere) const {
  3. __m128 zero = _mm_setzero_ps();
  4. __m128 center = _mm_loadu_ps(reinterpret_cast<const float*>(&sphere.origin));
  5. __m128 boxmin = _mm_loadu_ps(reinterpret_cast<const float*>(&rhs.BottomLeftClosest));
  6. __m128 boxmax = _mm_loadu_ps(reinterpret_cast<const float*>(&rhs.TopRightFurthest));
  7.  
  8. __m128 e = _mm_add_ps(_mm_max_ps(_mm_sub_ps(boxmin, center), zero), _mm_max_ps(_mm_sub_ps(center, boxmax), zero));
  9. e = _mm_mul_ps(e, e);
  10.  
  11. __declspec(align(16)) float arr[4];
  12. _mm_store_ps(arr, e);
  13.  
  14. float r = sphere.radius;
  15. return (arr[0] + arr[1] + arr[2] <= r * r);
  16. }
  17. // modified
  18. std::array<bool, 4> IntersectBoxWithSphere(std::array<const Math::AABB*, 4> boxes, const Physics::Sphere& sphere) const {
  19. float radius = sphere.radius * sphere.radius;
  20. __m128 zero = _mm_setzero_ps();
  21. __m128 radius = _mm_load1_ps(&radius);
  22. __m128 centerx = _mm_load1_ps(&sphere.origin.x);
  23. __m128 centery = _mm_load1_ps(&sphere.origin.y);
  24. __m128 centerz = _mm_load1_ps(&sphere.origin.z);
  25. __declspec(align(16)) float temparr[4];
  26. for(int i = 0; i < 4; i++)
  27. temparr[i] = boxes[i]->BottomLeftClosest.x;
  28. __m128 boxminx = mm_load_ps(temparr);
  29. for(int i = 0; i < 4; i++)
  30. temparr[i] = boxes[i]->BottomLeftClosest.y;
  31. __m128 boxminy = mm_load_ps(temparr);
  32. for(int i = 0; i < 4; i++)
  33. temparr[i] = boxes[i]->BottomLeftClosest.z;
  34. __m128 boxminz = mm_load_ps(temparr);
  35. for(int i = 0; i < 4; i++)
  36. temparr[i] = boxes[i]->TopRightFurthest.x;
  37. __m128 boxmaxx = mm_load_ps(temparr);
  38. for(int i = 0; i < 4; i++)
  39. temparr[i] = boxes[i]->TopRightFurthest.y;
  40. __m128 boxmaxy = mm_load_ps(temparr);
  41. for(int i = 0; i < 4; i++)
  42. temparr[i] = boxes[i]->TopRightFurthest.z;
  43. __m128 boxmaxz = mm_load_ps(temparr);
  44.  
  45. // _mm_max_ps(_mm_sub_ps(boxmin, center), zero)
  46. __m128 temp1x = _mm_sub_ps(boxminx, centerx);
  47. __m128 temp1y = _mm_sub_ps(boxminy, centery);
  48. __m128 temp1z = _mm_sub_ps(boxminz, centerz);
  49.  
  50. temp1x = _mm_max_ps(temp1x, zero);
  51. temp1y = _mm_max_py(temp1x, zero);
  52. temp1z = _mm_max_pz(temp1x, zero);
  53.  
  54. // _mm_max_ps(_mm_sub_ps(center, boxmin), zero)
  55. __m128 temp2x = _mm_sub_ps(centerx, boxmaxx);
  56. __m128 temp2y = _mm_sub_ps(centery, boxmaxy);
  57. __m128 temp2z = _mm_sub_ps(centerz, boxmaxz);
  58. temp2x = _mm_max_ps(temp2x, zero);
  59. temp2y = _mm_max_ps(temp2y, zero);
  60. temp2z = _mm_max_ps(temp2z, zero);
  61.  
  62. __m128 ex = _mm_add_ps(temp1x, temp2x);
  63. __m128 ey = _mm_add_ps(temp1y, temp2y);
  64. __m128 ez = _mm_add_ps(temp1z, temp2z);
  65. __m128 final_e = _mm_add_ps(ex, _mm_add_ps(ez, ey));
  66. __m128 result = _mm_cmple_ps(final_e, radius);
  67. float output[4];
  68. _mm_store_ps(output, result);
  69. std::array<bool, 4> ret;
  70. for(int i = 0; i < 4; i++) {
  71. ret[i] = output[i];
  72. }
  73. return ret;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement