context.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 RayQueryContext
  11. {
  12. public:
  13. __forceinline RayQueryContext(Scene* scene, RTCRayQueryContext* user_context, RTCIntersectArguments* args)
  14. : scene(scene), user(user_context), args(args) {}
  15. __forceinline RayQueryContext(Scene* scene, RTCRayQueryContext* user_context, RTCOccludedArguments* args)
  16. : scene(scene), user(user_context), args((RTCIntersectArguments*)args) {}
  17. __forceinline bool hasContextFilter() const {
  18. return args->filter != nullptr;
  19. }
  20. RTCFilterFunctionN getFilter() const {
  21. return args->filter;
  22. }
  23. RTCIntersectFunctionN getIntersectFunction() const {
  24. return args->intersect;
  25. }
  26. RTCOccludedFunctionN getOccludedFunction() const {
  27. return (RTCOccludedFunctionN) args->intersect;
  28. }
  29. __forceinline bool isCoherent() const {
  30. return embree::isCoherent(args->flags);
  31. }
  32. __forceinline bool isIncoherent() const {
  33. return embree::isIncoherent(args->flags);
  34. }
  35. __forceinline bool enforceArgumentFilterFunction() const {
  36. return args->flags & RTC_RAY_QUERY_FLAG_INVOKE_ARGUMENT_FILTER;
  37. }
  38. #if RTC_MIN_WIDTH
  39. __forceinline float getMinWidthDistanceFactor() const {
  40. return args->minWidthDistanceFactor;
  41. }
  42. #endif
  43. public:
  44. Scene* scene = nullptr;
  45. RTCRayQueryContext* user = nullptr;
  46. RTCIntersectArguments* args = nullptr;
  47. };
  48. template<int M, typename Geometry>
  49. __forceinline Vec4vf<M> enlargeRadiusToMinWidth(const RayQueryContext* context, const Geometry* geom, const Vec3vf<M>& ray_org, const Vec4vf<M>& v)
  50. {
  51. #if RTC_MIN_WIDTH
  52. const vfloat<M> d = length(Vec3vf<M>(v) - ray_org);
  53. const vfloat<M> r = clamp(context->getMinWidthDistanceFactor()*d, v.w, geom->maxRadiusScale*v.w);
  54. return Vec4vf<M>(v.x,v.y,v.z,r);
  55. #else
  56. return v;
  57. #endif
  58. }
  59. template<typename Geometry>
  60. __forceinline Vec3ff enlargeRadiusToMinWidth(const RayQueryContext* context, const Geometry* geom, const Vec3fa& ray_org, const Vec3ff& v)
  61. {
  62. #if RTC_MIN_WIDTH
  63. const float d = length(Vec3fa(v) - ray_org);
  64. const float r = clamp(context->getMinWidthDistanceFactor()*d, v.w, geom->maxRadiusScale*v.w);
  65. return Vec3ff(v.x,v.y,v.z,r);
  66. #else
  67. return v;
  68. #endif
  69. }
  70. template<typename Geometry>
  71. __forceinline Vec3ff enlargeRadiusToMinWidth(const RayQueryContext* context, const Geometry* geom, const Vec3fa& ray_org, const Vec4f& v) {
  72. return enlargeRadiusToMinWidth(context,geom,ray_org,Vec3ff(v.x,v.y,v.z,v.w));
  73. }
  74. enum PointQueryType
  75. {
  76. POINT_QUERY_TYPE_UNDEFINED = 0,
  77. POINT_QUERY_TYPE_SPHERE = 1,
  78. POINT_QUERY_TYPE_AABB = 2,
  79. };
  80. typedef bool (*PointQueryFunction)(struct RTCPointQueryFunctionArguments* args);
  81. struct PointQueryContext
  82. {
  83. public:
  84. __forceinline PointQueryContext(Scene* scene,
  85. PointQuery* query_ws,
  86. PointQueryType query_type,
  87. PointQueryFunction func,
  88. RTCPointQueryContext* userContext,
  89. float similarityScale,
  90. void* userPtr)
  91. : scene(scene)
  92. , tstate(nullptr)
  93. , query_ws(query_ws)
  94. , query_type(query_type)
  95. , func(func)
  96. , userContext(userContext)
  97. , similarityScale(similarityScale)
  98. , userPtr(userPtr)
  99. , primID(RTC_INVALID_GEOMETRY_ID)
  100. , geomID(RTC_INVALID_GEOMETRY_ID)
  101. , query_radius(query_ws->radius)
  102. {
  103. update();
  104. }
  105. public:
  106. __forceinline void update()
  107. {
  108. if (query_type == POINT_QUERY_TYPE_AABB) {
  109. assert(similarityScale == 0.f);
  110. updateAABB();
  111. }
  112. else{
  113. query_radius = Vec3fa(query_ws->radius * similarityScale);
  114. }
  115. if (userContext->instStackSize == 0) {
  116. assert(similarityScale == 1.f);
  117. }
  118. }
  119. __forceinline void updateAABB()
  120. {
  121. if (likely(query_ws->radius == (float)inf || userContext->instStackSize == 0)) {
  122. query_radius = Vec3fa(query_ws->radius);
  123. return;
  124. }
  125. const AffineSpace3fa m = AffineSpace3fa_load_unaligned((AffineSpace3fa*)userContext->world2inst[userContext->instStackSize-1]);
  126. BBox3fa bbox(Vec3fa(-query_ws->radius), Vec3fa(query_ws->radius));
  127. bbox = xfmBounds(m, bbox);
  128. query_radius = 0.5f * (bbox.upper - bbox.lower);
  129. }
  130. public:
  131. Scene* scene;
  132. void* tstate;
  133. PointQuery* query_ws; // the original world space point query
  134. PointQueryType query_type;
  135. PointQueryFunction func;
  136. RTCPointQueryContext* userContext;
  137. float similarityScale;
  138. void* userPtr;
  139. unsigned int primID;
  140. unsigned int geomID;
  141. Vec3fa query_radius; // used if the query is converted to an AABB internally
  142. };
  143. }