6 #include "VectorMath.h"
7 #include "CollisionBvh.h"
8 #include "RayNodeIntersectionResult.h"
9 #include "RayMeshIntersectionResult.h"
10 #include "CollisionBvhNodeIntersectionTests.h"
11 #include "ArrayAllocator.h"
12 #include "SkewedRay.h"
23 template <
typename TOutputIter>
24 inline TOutputIter GetLeafNodesIntersectingRay(
28 TOutputIter rayNodeIntersectionResult);
30 template <
typename TOutputIter>
34 float lineSegmentLength,
35 TOutputIter rayNodeIntersectionResult);
37 template <
typename TOutputIter>
38 inline TOutputIter GetLeafNodesIntersectingSkewedRay(
40 const SkewedRay& skewedRay,
41 TOutputIter rayNodeIntersectionResult);
43 template <
typename TOutputIter>
45 const SkewedRay& skewedRay,
46 TOutputIter rayNodeIntersectionResult);
48 template <
typename TOutputIter>
49 inline TOutputIter GetTransformedLeafNodesIntersectingRay(
51 const m44& inverseModelTransform,
54 TOutputIter rayNodeIntersectionResults);
62 const float skewedParamScale);
64 SkewedRay CalculateModelSpaceRay(
65 const dv3& rayOriginEcef,
66 const dv3& rayDirectionEcef,
67 const dv3& modelOriginEcef,
68 const m44& inverseModelTransform
72 SkewedRay CalcEnvironmentFlattenedSkewedRay(
const v3& rayOrigin,
73 const v3& rayDirection,
74 const dv3& collisionBvhOriginEcef,
75 const float environmentFlatteningScale);
77 SkewedRay CorrectInteriorRayForEnvironmentFlattening(
const v3& rayOrigin,
78 const v3& rayDirection,
79 const dv3& collisionBvhOriginEcef,
80 const float environmentFlatteningScale,
81 const float terrainHeight);
83 m33 CalcEnvironmentFlatteningTransform(
const dv3& meshOrigin,
const float environmentFlatteningScale);
88 template <
typename T,
int MaxStackSize>
89 struct TStackAllocatedStack
92 #if defined(EEGEO_WIN) && !defined(NDEBUG)
93 typedef std::allocator<T> IntArrayStackAllocator;
97 typedef std::vector<T, IntArrayStackAllocator> StackAllocatedVector;
99 typedef std::stack<T, StackAllocatedVector> Type;
102 typedef TStackAllocatedStack<int, 1024>::Type WorkspaceStack;
104 template <
typename TIntegerStack,
typename TIntersectionTest,
typename TResultPredicate,
typename TOutIter>
105 inline TOutIter GetCollisionBvhLeafNodes(TIntegerStack& stack,
107 TIntersectionTest intersectionTest,
108 TResultPredicate resultPredicate,
109 TOutIter outIterFirst)
111 Eegeo_ASSERT(stack.empty());
112 TOutIter outIter = outIterFirst;
113 if (collisionBvh.Nodes().empty())
116 const int RootNodeIndex = 0;
117 stack.push(RootNodeIndex);
119 const std::vector<Eegeo::Collision::CollisionBvhNode>& nodes = collisionBvh.Nodes();
121 while (!stack.empty())
123 int candidateNodeIndex = stack.top();
128 const typename TIntersectionTest::result_type& result = intersectionTest(candidate);
129 if ( resultPredicate(result))
131 if (candidate.IsLeafNode())
137 const int indexOffset = candidate.FirstChildIndex();
138 const int end = indexOffset + candidate.ChildCount();
139 for (
int i = indexOffset ; i < end; ++i)
151 template <
typename TOutputIter>
152 inline TOutputIter GetLeafNodesIntersectingRay(
156 TOutputIter rayNodeIntersectionResult)
158 const float skewedParamScale = 1.f;
159 BvhNodeRayIntersectionTest intersectionTest(collisionBvh, rayOrigin, rayDirection, skewedParamScale);
160 WorkspaceStack workspaceStack;
162 return GetCollisionBvhLeafNodes(workspaceStack, collisionBvh, intersectionTest, BvhNodeIntersectionTestPredicate(), rayNodeIntersectionResult);
165 template <
typename TOutputIter>
169 float lineSegmentLength,
170 TOutputIter rayNodeIntersectionResult)
172 const float skewedParamScale = 1.f;
173 BvhNodeLineSegmentIntersectionTest intersectionTest(collisionBvh, lineSegmentStart, lineSegmentDirection, lineSegmentLength, skewedParamScale);
174 WorkspaceStack workspaceStack;
176 return GetCollisionBvhLeafNodes(workspaceStack, collisionBvh, intersectionTest, BvhNodeIntersectionTestPredicate(), rayNodeIntersectionResult);
179 template <
typename TOutputIter>
180 inline TOutputIter GetLeafNodesIntersectingSkewedRay(
182 const SkewedRay& skewedRay,
183 TOutputIter rayNodeIntersectionResult)
185 BvhNodeRayIntersectionTest intersectionTest(collisionBvh, skewedRay.Origin, skewedRay.Direction, skewedRay.SkewedParamScale);
186 WorkspaceStack workspaceStack;
188 return GetCollisionBvhLeafNodes(workspaceStack, collisionBvh, intersectionTest, BvhNodeIntersectionTestPredicate(), rayNodeIntersectionResult);
191 template <
typename TOutputIter>
193 const SkewedRay& skewedRay,
194 TOutputIter rayNodeIntersectionResult)
196 TOutputIter outIter = rayNodeIntersectionResult;
197 const std::vector<Eegeo::Collision::CollisionBvhNode>& nodes = collisionBvh.Nodes();
198 for (
int i = 0; i < nodes.size(); ++i)
201 if (node.IsLeafNode())
204 const Geometry::SingleSphere& sphere = collisionBvh.GetSphere(node);
205 if ( Geometry::IntersectionTests::SphereIntersectsWithRay(sphere, skewedRay.Origin, skewedRay.Direction, t))
207 t *= skewedRay.SkewedParamScale;
215 template <
typename TOutputIter>
216 TOutputIter GetTransformedLeafNodesIntersectingRay(
218 const m44& inverseModelTransform,
221 TOutputIter rayNodeIntersectionResults)
223 const SkewedRay& modelSpaceRay = CalculateModelSpaceRay(rayOriginEcef, rayDirectionEcef, collisionBvh.OriginEcef(), inverseModelTransform);
225 return GetLeafNodesIntersectingSkewedRay(collisionBvh, modelSpaceRay, rayNodeIntersectionResults);