gwnavruntime/queries/utils/rectangleintersector.h Source File

rectangleintersector.h
Go to the documentation of this file.
1 /*
2 * Copyright 2016 Autodesk, Inc. All rights reserved.
3 * Use of this software is subject to the terms of the Autodesk license agreement and any attachments or Appendices thereto provided at the time of installation or download,
4 * or which otherwise accompanies this software in either electronic or hard copy form, or which is signed by you and accepted by Autodesk.
5 */
6 
7 
8 #pragma once
9 
15 
16 namespace Kaim
17 {
18 
19 /*
20 class RectangleIntersector
21 */
22 class RectangleIntersector
23 {
24  KY_DEFINE_NEW_DELETE_OPERATORS(MemStat_Query)
25 
26 public:
27  RectangleIntersector(BaseSegmentCastQuery& query) : m_maxDist(query.GetMaxDist())
28  {
29  const DatabaseGenMetrics& databaseGenMetrics = query.m_database->GetDatabaseGenMetrics();
30 
31  m_integerPrecision = databaseGenMetrics.m_integerPrecision;
32 
33  Vec2f ortho = query.GetNormalizedDir2d().PerpCCW();
34  m_startA.Set(query.GetStartPos().x + (ortho.x * query.GetRadius()), query.GetStartPos().y + (ortho.y * query.GetRadius()), query.GetStartPos().z);
35  m_startB.Set(query.GetStartPos().x - (ortho.x * query.GetRadius()), query.GetStartPos().y - (ortho.y * query.GetRadius()), query.GetStartPos().z);
36 
37  m_rectangle = OrientedBox2d(m_startB, query.GetNormalizedDir2d(), m_maxDist, 2.f * query.GetRadius(), 0.f);
38 
39  const Vec2f direction2f(query.GetNormalizedDir2d() * m_maxDist);
40  Vec2f destPos2f(query.GetStartPos().x + direction2f.x, query.GetStartPos().y + direction2f.y);
41  const CoordPos64 destCoordPos(databaseGenMetrics.GetCoordPos64FromVec2f(destPos2f));
42 
43  databaseGenMetrics.GetCoordPos64FromVec3f(m_startB, m_o1);
44  const CoordPos64 startCoordPos = databaseGenMetrics.GetCoordPos64FromWorldIntegerPos(query.GetStartIntegerPos());
45  m_lengthVec = destCoordPos - startCoordPos;
46  m_widthVec = (startCoordPos - m_o1) * 2;
47  }
48 
49  RectangleIntersector(BaseSegmentCanGoQuery& query)
50  {
51  const DatabaseGenMetrics& databaseGenMetrics = query.m_database->GetDatabaseGenMetrics();
52 
53  m_integerPrecision = databaseGenMetrics.m_integerPrecision;
54 
55  Vec2f dir(query.GetDestPos().x - query.GetStartPos().x, query.GetDestPos().y - query.GetStartPos().y);
56  dir.Normalize();
57  dir = dir.PerpCCW();
58  Vec3f normalizedDir3D(dir.x, dir.y, 0.f);
59 
60  const Vec3f o1Floats(query.GetStartPos() - (normalizedDir3D * query.GetRadius()));
61 
62  databaseGenMetrics.GetCoordPos64FromVec3f(o1Floats, m_o1);
63  const CoordPos64 startCoordPos = databaseGenMetrics.GetCoordPos64FromWorldIntegerPos(query.GetStartIntegerPos());
64  m_lengthVec = databaseGenMetrics.GetCoordPos64FromWorldIntegerPos(query.GetDestIntegerPos()) - startCoordPos;
65  m_widthVec = (startCoordPos - m_o1) * 2;
66  }
67 
68  bool DoesIntersectEdge(const CoordPos64& startEdgeCoordPos, const CoordPos64& endEdgeCoordPos)
69  {
70  return Math::IsSegmentVsOrientedBox2LL(startEdgeCoordPos, endEdgeCoordPos, m_o1, m_lengthVec, m_widthVec);
71  }
72 
73  void ComputeCollisionPosOnEdge(const CoordPos64& startEdgeCoordPos, const CoordPos64& endEdgeCoordPos, Vec3f& collisionPos, KyFloat32& squareDistToCollisionPos)
74  {
75  const Vec3f startEdgePos(startEdgeCoordPos.x * m_integerPrecision, startEdgeCoordPos.y * m_integerPrecision, 0.f);
76  const Vec3f endEdgePos(endEdgeCoordPos.x * m_integerPrecision, endEdgeCoordPos.y * m_integerPrecision, 0.f);
77 
78  ClosestPoint::OnSegmentVsSegmentCast2d(startEdgePos, endEdgePos, m_startA, m_startB, m_maxDist, collisionPos, squareDistToCollisionPos);
79  }
80 
81  void ComputeTriangleCost(const CoordPos64& v0CoordPos, const CoordPos64& v1CoordPos, const CoordPos64& v2CoordPos, KyFloat32& cost)
82  {
83  const Vec3f v0(v0CoordPos.x * m_integerPrecision, v0CoordPos.y * m_integerPrecision, 0.f);
84  const Vec3f v1(v1CoordPos.x * m_integerPrecision, v1CoordPos.y * m_integerPrecision, 0.f);
85  const Vec3f v2(v2CoordPos.x * m_integerPrecision, v2CoordPos.y * m_integerPrecision, 0.f);
86 
87  Vec3f unused;
88  ClosestPoint::OnTriangleVsSegmentCast2d(v0, v1, v2, m_startA, m_startB, m_maxDist, unused, cost);
89  }
90 
91 private:
92  /*
93  o4 +-----------------------+ o3
94  | |
95  | |
96  | |
97  o1 +---------------------->+
98  m_lengthVec
99  */
100 
101  CoordPos64 m_o1;
102  Vec2LL m_lengthVec;
103  Vec2LL m_widthVec;
104 
105  OrientedBox2d m_rectangle;
106  Vec3f m_startA;
107  Vec3f m_startB;
108  KyFloat32 m_maxDist;
109 
110 
111  KyFloat32 m_integerPrecision;
112 
113 };
114 
115 
116 }
117 
118 
119 
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:132
Vec2LL CoordPos64
A type that represents the position of a point within the 2D integer grid.
Definition: navmeshtypes.h:16
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17
float KyFloat32
float
Definition: types.h:32