gwnavruntime/queries/utils/capsuleintersector.h Source File

capsuleintersector.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 #pragma once
8 
14 
15 namespace Kaim
16 {
17 
18 class CapsuleIntersector
19 {
20  KY_DEFINE_NEW_DELETE_OPERATORS(MemStat_Query)
21 
22 public:
23  CapsuleIntersector(const Vec3f& startPos, const Vec3f& endPos, KyFloat32 radius, KyFloat32 integerPrecision)
24  : m_integerPrecision(integerPrecision)
25  , m_startCenter(startPos)
26  , m_endCenter(endPos)
27  , m_radius(radius)
28  {
29 
30  Vec3f dir(m_endCenter - m_startCenter);
31  dir.z = 0.f;
32  m_dist = dir.Normalize();
33 
34  const Vec3f normalizedDir3D_ortho(-dir.y, dir.x, 0.f);
35  const Vec3f box_a(m_startCenter - (normalizedDir3D_ortho * m_radius));
36 
37  m_orientedBox2d.Set(box_a, Vec2f(dir.x, dir.y), m_dist, m_radius * 2.f, 0.f);
38  }
39 
40  CapsuleIntersector(const Vec3f& startPos, const Vec2f& normalizedDir2d, KyFloat32 dist, KyFloat32 radius, KyFloat32 integerPrecision)
41  : m_integerPrecision(integerPrecision)
42  , m_startCenter(startPos)
43  , m_normalizedDir2d(normalizedDir2d)
44  , m_dist(dist)
45  , m_radius(radius)
46  {
47  const Vec3f normalizedDir3D_ortho(-m_normalizedDir2d.y, m_normalizedDir2d.x, 0.f);
48  const Vec3f box_a(m_startCenter - (normalizedDir3D_ortho * m_radius));
49  m_orientedBox2d.Set(box_a, m_normalizedDir2d, m_dist, m_radius * 2.f, 0.f);
50 
51  const Vec3f normalizedDir3D(m_normalizedDir2d.x, m_normalizedDir2d.y, 0.f);
52  m_endCenter = m_startCenter + (normalizedDir3D * m_dist);
53  }
54 
55  bool DoesIntersectEdge(const CoordPos64& startEdgeCoordPos, const CoordPos64& endEdgeCoordPos)
56  {
57  const Vec3f startEdgePos(startEdgeCoordPos.x * m_integerPrecision, startEdgeCoordPos.y * m_integerPrecision, 0.f);
58  const Vec3f endEdgePos(endEdgeCoordPos.x * m_integerPrecision, endEdgeCoordPos.y * m_integerPrecision, 0.f);
59 
60  return Math::SegmentVsCapsule2f(startEdgePos, endEdgePos, m_orientedBox2d, m_startCenter, m_endCenter, m_radius);
61  }
62 
63  void ComputeCollisionPosOnEdge(const CoordPos64& startEdgeCoordPos, const CoordPos64& endEdgeCoordPos, Vec3f& collisionPos, KyFloat32& squareDistToCollisionPos)
64  {
65  const Vec3f startEdgePos(startEdgeCoordPos.x * m_integerPrecision, startEdgeCoordPos.y * m_integerPrecision, 0.f);
66  const Vec3f endEdgePos(endEdgeCoordPos.x * m_integerPrecision, endEdgeCoordPos.y * m_integerPrecision, 0.f);
67 
68  ClosestPoint::OnSegmentVsDiskCast2d(startEdgePos, endEdgePos, m_startCenter, m_radius, m_normalizedDir2d, m_dist, collisionPos, squareDistToCollisionPos);
69  }
70 
71  void ComputeTriangleCost(const CoordPos64& v0CoordPos, const CoordPos64& v1CoordPos, const CoordPos64& v2CoordPos, KyFloat32& cost)
72  {
73  const Vec3f v0(v0CoordPos.x * m_integerPrecision, v0CoordPos.y * m_integerPrecision, 0.f);
74  const Vec3f v1(v1CoordPos.x * m_integerPrecision, v1CoordPos.y * m_integerPrecision, 0.f);
75  const Vec3f v2(v2CoordPos.x * m_integerPrecision, v2CoordPos.y * m_integerPrecision, 0.f);
76 
77  Vec3f unused;
78  ClosestPoint::OnTriangleVsDiskCast2d(v0, v1, v2, m_startCenter, m_radius, m_normalizedDir2d, m_dist, unused, cost);
79  }
80 
81 private:
82  KyFloat32 m_integerPrecision;
83 
84  Vec3f m_startCenter;
85  Vec3f m_endCenter;
86  Vec2f m_normalizedDir2d;
87  KyFloat32 m_dist;
88  KyFloat32 m_radius; //< the radius of the extremity disks
89 
90  OrientedBox2d m_orientedBox2d;
91 };
92 
93 
94 }
95 
96 
97 
#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