6 #include "VectorMath.h"
7 #include "RayCasterResult.h"
8 #include "CollisionBvh.h"
9 #include "CollisionBvhIntersectionQueries.h"
10 #include "RayCasterFirstIntersectionBuilder.h"
20 template <
typename TCollisionBvhContainer,
typename TRayNodeIntersectionResults >
24 typedef typename TCollisionBvhContainer::const_iterator TCollisionBvhIter;
27 TRayNodeIntersectionResults& rayNodeIntersectionsScratch,
28 std::function<
SkewedRay(
const v3&,
const v3&,
const dv3&,
const float)> rayToWorldSpaceTransform,
30 : m_collisionBvhs(collisionBvhs)
31 , m_rayNodeIntersectionsScratch(rayNodeIntersectionsScratch)
32 , m_rayUnflatteningOperation(rayToWorldSpaceTransform)
33 , m_intersectionValidator(intersectionValidator)
39 const TCollisionBvhContainer& m_collisionBvhs;
40 TRayNodeIntersectionResults& m_rayNodeIntersectionsScratch;
41 std::function<SkewedRay(const v3&, const v3&, const dv3&, const float)> m_rayUnflatteningOperation;
49 return _a.IntersectionParam() < _b.IntersectionParam();
52 template <
typename TCollisionBvhIter,
typename TOutIter>
53 TOutIter GatherLeafNodesIntersectingRay(
54 std::function<SkewedRay(
const v3&,
const v3&,
const dv3&,
const float)> rayUnflatteningOperation,
55 const float environmentFlatteningScale,
58 TCollisionBvhIter first,
59 TCollisionBvhIter last,
62 for (TCollisionBvhIter iter = first; iter != last; ++iter)
66 v3 meshRelativeRayOrigin = (rayOrigin - collisionBvh.OriginEcef()).ToSingle();
68 const SkewedRay& skewedRay = rayUnflatteningOperation(meshRelativeRayOrigin, rayDirection, collisionBvh.OriginEcef(), environmentFlatteningScale);
70 GetLeafNodesIntersectingSkewedRay(collisionBvh, skewedRay, outIter);
76 template <
typename TCollisionBvhContainer,
typename TRayNodeIntersectionResults>
77 inline RayCasterResult
78 CollisionBvhRayCaster<TCollisionBvhContainer, TRayNodeIntersectionResults>::FindFirstRayIntersection(
79 const float environmentFlatteningScale,
const Eegeo::dv3& rayOrigin,
const Eegeo::dv3& rayDirection)
const
81 m_rayNodeIntersectionsScratch.clear();
82 GatherLeafNodesIntersectingRay(m_rayUnflatteningOperation, environmentFlatteningScale, rayOrigin, rayDirection.ToSingle(), m_collisionBvhs.begin(), m_collisionBvhs.end(), std::back_inserter(m_rayNodeIntersectionsScratch));
84 std::sort(m_rayNodeIntersectionsScratch.begin(), m_rayNodeIntersectionsScratch.end(), RayNodeIntersectionResultSortByT);
86 RayCasterFirstIntersectionBuilder firstIntersectionBuilder(rayOrigin, rayDirection, environmentFlatteningScale, m_intersectionValidator);
88 for (
const auto& intersection : m_rayNodeIntersectionsScratch)
90 bool shouldContinue = firstIntersectionBuilder(intersection);
97 return firstIntersectionBuilder.BuildResult();