gwnavgeneration/boundary/boundarysimplifier.h Source File

boundarysimplifier.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 
12 
13 namespace Kaim
14 {
15 
16 class BoundaryVertexWithCost
17 {
18  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
19 public:
20  BoundaryVertexWithCost() : m_cost(KyFloat32MAXVAL), m_alreadyRemoved(false) {}
21  BoundaryVertexWithCost(BoundaryVertexListIterator vertex, KyFloat32 cost) : m_cost(cost), m_alreadyRemoved(false)
22  {
23  m_vertexIterator = vertex;
24  }
25 
26  KY_INLINE bool operator<(const BoundaryVertexWithCost& other) const { return m_cost > other.m_cost; }
27 
28 public:
29  BoundaryVertexListIterator m_vertexIterator;
30  KyFloat32 m_cost;
31  bool m_alreadyRemoved;
32 };
33 
34 class BoundarySimplifier
35 {
36  KY_DEFINE_NEW_DELETE_OPERATORS(Stat_Default_Mem)
37 public:
38 
39  BoundarySimplifier(BoundaryGraph* boundaryGraph) : m_boundaryGraph(boundaryGraph)
40  {}
41 
42  //Smooth all the polylines of the input BoundaryGraph with a certain tolerance
43  //Tolerance measures the maximum error allowed between a vertex of BoundaryGraph
44  //and the corresponding simplified output polyline
45  //Units of tolerance depends on Boundary vertex coordinates system.
46  //Tolerance can be interpreted in terms of "percentage" of pixelSize
47  //for example, a tolerance of 0.5f means we accept an error of 0.5f * pixelSize
48  //Vertical tolerance defines the maximum error along the up Axis. Its in meters and
49  //the value is ignored if it is negative.
50  KyResult SimplifyBoundaries();
51 
52  KyResult SimplifyObstaclesAndConnexLinkBoundariesOfPolygon(KyUInt32 polygonIdx);
53 private:
54 
55  //based on Ramer Douglas Peucker approach
56  //see: http://en.wikipedia.org/wiki/Ramer-Douglas-Peucker_algorithm
57  KyResult DoSimplifyPolyline(BoundarySimplifyPolyline* polyline);
58 
59  //recursive algo. Max depth = log(polyline.size())
60  void RecursiveRDP(BoundarySimplifyPolyline* polyline,
61  BoundaryVertexListIterator begin,
62  BoundaryVertexListIterator last);
63 
64  void ComputePolylineBoundingBox(KyUInt32 polylineIdx);
65 
66  KyResult SimplifyAllCellLinkAndFloorLinkBoundaries();
67  void RemoveVerticesIfPossible(BoundarySimplifyPolyline* polyline, BoundaryVertexListIterator begin,
68  BoundaryVertexListIterator last);
69 
70  bool TestSimplificationAgainstOtherPolylinesAndSections(BoundarySimplifyPolyline* polyline,
71  BoundaryVertexListIterator begin, BoundaryVertexListIterator last,
72  BoundaryVertexListIterator previousVertex,BoundaryVertexListIterator currentVertex, BoundaryVertexListIterator nextVertex,
73  const Vec2i& v0, const Vec2i& v1, const Vec2i& v2, KyFloat32 squareDist, KyUInt32 insideVertexCount, bool canCollapse);
74 
75  bool TestAgainstTheOtherSectionsOfPolyline(BoundarySimplifyPolyline* polyline, BoundaryVertexListIterator begin, BoundaryVertexListIterator last,
76  BoundaryVertexListIterator previousVertex, BoundaryVertexListIterator currentVertex, BoundaryVertexListIterator nextVertex,
77  const Vec2i& v0, const Vec2i& v1, const Vec2i& v2, KyFloat32 squareDist);
78 
79  bool TestAgainstTheOtherPolylinesInFloor(BoundarySimplifyPolyline* polyline, BoundaryVertexListIterator previousVertex,
80  BoundaryVertexListIterator currentVertex, BoundaryVertexListIterator nextVertex, const Vec2i& v0, const Vec2i& v1, const Vec2i& v2,
81  KyFloat32 squareDist, KyUInt32 insideVertexCount, bool canCollapse);
82 
83  bool TestAgainstTheOtherPointsOfPolylineSection(BoundarySimplifyPolyline* polyline,
84  BoundaryVertexListIterator begin, BoundaryVertexListIterator last, BoundaryVertexListIterator previousVertex,
85  BoundaryVertexListIterator currentVertex, BoundaryVertexListIterator nextVertex, const Vec2i& v0, const Vec2i& v1, const Vec2i& v2, KyFloat32 squareDist);
86 
87  bool TestAgainsPoint(const Vec2i& v0, const Vec2i& v1, const Vec2i& v2, const Vec2i& otherPos,
88  BoundaryVertexListIterator previousVertex, BoundaryVertexListIterator nextVertex, BoundaryVertexListIterator otherCurrent, KyFloat32 squareDist);
89 
90  void BuildBinaryHeap(BoundarySimplifyPolyline* polyline, BoundaryVertexListIterator begin,
91  BoundaryVertexListIterator last, KyUInt32& insideVertexCount);
92 
93  bool IsAxisAlignedElseGetCCW(const Vec2i& previousPos, const Vec2i& currentPos, const Vec2i& nextPos, Vec2i& v0, Vec2i& v1, Vec2i& v2);
94 
95  void MarkAsRemovedInTheHeap(BoundaryVertexListIterator iter, BoundaryVertexListIterator invalidIter);
96  void GetCyclicNext(BoundaryVertexList& vertices, BoundaryVertexListIterator& iter);
97  void GetCyclicPrev(BoundaryVertexList& vertices, BoundaryVertexListIterator& iter);
98 
99 public:
100  KyFloat32 m_verticalTolerance;
101  KyFloat32 m_exteriorSimplificationTolerance;
102  KyFloat32 m_interiorSimplificationTolerance;
103  KyFloat32 m_toleranceForConnexLink;
104 
105 private:
106  BoundaryGraph* m_boundaryGraph;
107 
108  KyArrayTLS_POD<BoundarySimplifyPolyline*> m_polylinesInPolygon;
109  KyArrayTLS_POD<BoundarySimplifyPolyline*> m_currentBoxIntersectingPolylines;
110  KyArrayTLS_POD<Box2f> m_polylineBoudingBox;
111 
112  KyArrayTLS<BoundaryVertexListIterator> m_staticVertices; // used in cycled polyline to extract p
113  BinaryHeapTLS_CPP<BoundaryVertexWithCost> m_binaryHeap;
114 };
115 
116 KY_INLINE void BoundarySimplifier::GetCyclicNext(BoundaryVertexList& vertices, BoundaryVertexListIterator& iter)
117 {
118  ++iter;
119 
120  if (iter == vertices.End())
121  iter = vertices.Begin();
122 }
123 
124 KY_INLINE void BoundarySimplifier::GetCyclicPrev(BoundaryVertexList& vertices, BoundaryVertexListIterator& iter)
125 {
126  if (iter == vertices.Begin())
127  iter = vertices.End();
128 
129  --iter;
130 }
131 
132 }
133 
134 
135 
#define KyFloat32MAXVAL
KyFloat32 max value
Definition: types.h:71
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
Navigation return code class.
Definition: types.h:108
The Autodesk Navigation namespace.
Definition: gamekitcrowddispersion.cpp:17
float KyFloat32
float
Definition: types.h:32