context.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2009-2021 Intel Corporation
  2. // SPDX-License-Identifier: Apache-2.0
  3. #pragma once
  4. #include "default.h"
  5. #include "rtcore.h"
  6. #include "point_query.h"
  7. namespace embree
  8. {
  9. class Scene;
  10. struct IntersectContext
  11. {
  12. public:
  13. __forceinline IntersectContext(Scene* scene, RTCIntersectContext* user_context)
  14. : scene(scene), user(user_context) {}
  15. __forceinline bool hasContextFilter() const {
  16. return user->filter != nullptr;
  17. }
  18. __forceinline bool isCoherent() const {
  19. return embree::isCoherent(user->flags);
  20. }
  21. __forceinline bool isIncoherent() const {
  22. return embree::isIncoherent(user->flags);
  23. }
  24. public:
  25. Scene* scene;
  26. RTCIntersectContext* user;
  27. };
  28. template<int M, typename Geometry>
  29. __forceinline Vec4vf<M> enlargeRadiusToMinWidth(const IntersectContext* context, const Geometry* geom, const Vec3vf<M>& ray_org, const Vec4vf<M>& v)
  30. {
  31. #if RTC_MIN_WIDTH
  32. const vfloat<M> d = length(Vec3vf<M>(v) - ray_org);
  33. const vfloat<M> r = clamp(context->user->minWidthDistanceFactor*d, v.w, geom->maxRadiusScale*v.w);
  34. return Vec4vf<M>(v.x,v.y,v.z,r);
  35. #else
  36. return v;
  37. #endif
  38. }
  39. template<typename Geometry>
  40. __forceinline Vec3ff enlargeRadiusToMinWidth(const IntersectContext* context, const Geometry* geom, const Vec3fa& ray_org, const Vec3ff& v)
  41. {
  42. #if RTC_MIN_WIDTH
  43. const float d = length(Vec3fa(v) - ray_org);
  44. const float r = clamp(context->user->minWidthDistanceFactor*d, v.w, geom->maxRadiusScale*v.w);
  45. return Vec3ff(v.x,v.y,v.z,r);
  46. #else
  47. return v;
  48. #endif
  49. }
  50. enum PointQueryType
  51. {
  52. POINT_QUERY_TYPE_UNDEFINED = 0,
  53. POINT_QUERY_TYPE_SPHERE = 1,
  54. POINT_QUERY_TYPE_AABB = 2,
  55. };
  56. typedef bool (*PointQueryFunction)(struct RTCPointQueryFunctionArguments* args);
  57. struct PointQueryContext
  58. {
  59. public:
  60. __forceinline PointQueryContext(Scene* scene,
  61. PointQuery* query_ws,
  62. PointQueryType query_type,
  63. PointQueryFunction func,
  64. RTCPointQueryContext* userContext,
  65. float similarityScale,
  66. void* userPtr)
  67. : scene(scene)
  68. , query_ws(query_ws)
  69. , query_type(query_type)
  70. , func(func)
  71. , userContext(userContext)
  72. , similarityScale(similarityScale)
  73. , userPtr(userPtr)
  74. , primID(RTC_INVALID_GEOMETRY_ID)
  75. , geomID(RTC_INVALID_GEOMETRY_ID)
  76. , query_radius(query_ws->radius)
  77. {
  78. if (query_type == POINT_QUERY_TYPE_AABB) {
  79. assert(similarityScale == 0.f);
  80. updateAABB();
  81. }
  82. if (userContext->instStackSize == 0) {
  83. assert(similarityScale == 1.f);
  84. }
  85. }
  86. public:
  87. __forceinline void updateAABB()
  88. {
  89. if (likely(query_ws->radius == (float)inf || userContext->instStackSize == 0)) {
  90. query_radius = Vec3fa(query_ws->radius);
  91. return;
  92. }
  93. const AffineSpace3fa m = AffineSpace3fa_load_unaligned((AffineSpace3fa*)userContext->world2inst[userContext->instStackSize-1]);
  94. BBox3fa bbox(Vec3fa(-query_ws->radius), Vec3fa(query_ws->radius));
  95. bbox = xfmBounds(m, bbox);
  96. query_radius = 0.5f * (bbox.upper - bbox.lower);
  97. }
  98. public:
  99. Scene* scene;
  100. PointQuery* query_ws; // the original world space point query
  101. PointQueryType query_type;
  102. PointQueryFunction func;
  103. RTCPointQueryContext* userContext;
  104. const float similarityScale;
  105. void* userPtr;
  106. unsigned int primID;
  107. unsigned int geomID;
  108. Vec3fa query_radius; // used if the query is converted to an AABB internally
  109. };
  110. }