gwnavruntime/queries/utils/polygonintersector.h Source File

polygonintersector.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 
13 
14 namespace Kaim
15 {
16 
17 /*
18 class PolygonIntersector
19 */
20 class PolygonIntersector
21 {
22  KY_DEFINE_NEW_DELETE_OPERATORS(MemStat_Query)
23 
24 public:
25  PolygonIntersector(const Vec2f* points, KyUInt32 pointCount, KyFloat32 integerPrecision)
26  {
27  if (pointCount > 0)
28  {
29  bool inputPolylineIsClosed = points[0] == points[pointCount-1];
30  m_points.Resize(inputPolylineIsClosed ? pointCount : pointCount+1);
31 
32  for(KyUInt32 i = 0; i < pointCount; ++i)
33  {
34  m_points[i] = points[i];
35  m_box2f.ExpandByPos(points[i]);
36  }
37 
38  if (inputPolylineIsClosed == false)
39  m_points[pointCount] = points[0];
40  }
41 
42  m_integerPrecision = integerPrecision;
43  }
44 
45  bool DoesIntersectEdge(const CoordPos64& startEdgeCoordPos, const CoordPos64& endEdgeCoordPos)
46  {
47  const Vec2f startEdgePos(startEdgeCoordPos.x * m_integerPrecision, startEdgeCoordPos.y * m_integerPrecision);
48  const Vec2f endEdgePos(endEdgeCoordPos.x * m_integerPrecision, endEdgeCoordPos.y * m_integerPrecision);
49 
50  Box2f edgeBox(
51  Kaim::Min(startEdgePos.x, endEdgePos.x), Kaim::Min(startEdgePos.y, endEdgePos.y),
52  Kaim::Max(startEdgePos.x, endEdgePos.x), Kaim::Max(startEdgePos.y, endEdgePos.y));
53 
54  if (Math::IsBoxVsBox2f(m_box2f, edgeBox) == false)
55  return false;
56 
57  if (IsPointInsidePolyline(startEdgePos))
58  return true;
59 
60  if (IsPointInsidePolyline(endEdgePos))
61  return true;
62 
63  return DoesEdgeIntersectOnePolylineBorder(startEdgePos, endEdgePos);
64  }
65 
66  bool IsPointInsidePolyline(const Vec2f& point)
67  {
68  if (m_box2f.DoesContain(point) == false)
69  return false;
70 
71  return ClosedContour::DoesContain2f(point, m_points.GetDataPtr(), m_points.GetCount());
72  }
73 
74  bool DoesEdgeIntersectOnePolylineBorder(const Vec2f& startEdgePos, const Vec2f& endEdgePos)
75  {
76  KyUInt32 pointCount = m_points.GetCount();
77  for(KyUInt32 i = 0; i < pointCount - 1; ++i)
78  {
79  if (Math::IsSegmentVsSegment2f(m_points[i], m_points[i+1], startEdgePos, endEdgePos))
80  return true;
81  }
82 
83  return false;
84  }
85 private:
86  KyArrayPOD<Vec2f> m_points;
87  Box2f m_box2f;
88  KyFloat32 m_integerPrecision;
89 };
90 
91 
92 }
93 
94 
95 
std::uint32_t KyUInt32
uint32_t
Definition: types.h:29
#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