14 #ifndef Navigation_EarClippingTriangulator_H
15 #define Navigation_EarClippingTriangulator_H
24 class DynamicTriangulation;
27 class EarClippingTriangulatorInputPolygon
31 EarClippingTriangulatorInputPolygon(MemoryHeap* heap =
KY_NULL) : m_pointIndices(heap), m_edgeUserData(heap) {}
33 KyArrayDH_POD<KyUInt32> m_pointIndices;
34 KyArrayDH_POD<void*> m_edgeUserData;
37 class EarClippingTriangulatorInputPolygonWithHoles
41 EarClippingTriangulatorInputPolygonWithHoles(MemoryHeap* heap = Memory::GetGlobalHeap()) : m_points(heap), m_exterior(heap), m_holes(heap) {}
46 m_exterior.m_pointIndices.Clear();
47 m_exterior.m_edgeUserData.Clear();
50 KyArrayDH_POD<Vec2i> m_points;
51 EarClippingTriangulatorInputPolygon m_exterior;
52 KyArrayDH<EarClippingTriangulatorInputPolygon> m_holes;
55 class EarClippingTriangulator
60 static const PolygonVertexIdx PolygonVertexIdx_Invalid =
KyUInt16MAXVAL;
74 m_posIdx(KyUInt16MAXVAL),
75 m_vertexType((
KyUInt16)TYPE_UNKNOWN),
76 m_nextPolygonVertexIdx(PolygonVertexIdx_Invalid),
77 m_prevPolygonVertexIdx(PolygonVertexIdx_Invalid),
78 m_duplicateVertexIdx(PolygonVertexIdx_Invalid),
79 m_edgeGoingFromNextToMe(
KY_NULL)
82 Type GetVertexType()
const {
return (Type)m_vertexType; }
83 void SetVertexType(Type vertexType) { m_vertexType =
KyUInt16(vertexType); }
86 PolygonVertexIdx m_nextPolygonVertexIdx;
87 PolygonVertexIdx m_prevPolygonVertexIdx;
88 PolygonVertexIdx m_duplicateVertexIdx;
89 void* m_edgeGoingFromNextToMe;
94 EarClippingTriangulator(
const EarClippingTriangulatorInputPolygonWithHoles& input, KyArrayDH<PolygonVertex>& workingArray,
95 DynamicTriangulation& output, MemoryHeap* heap = Memory::GetGlobalHeap()) :
99 m_polygonVertices(&workingArray) {}
101 KyResult BuildPolygonAndLinkHolesToExterior();
104 enum TriangulateStepResult { TriangulateStepResult_Finish, TriangulateStepResult_GoOn };
105 KyResult TriangulateStep(TriangulateStepResult& triangulateStepResult,
KyUInt16& currentVertexCountInPolygon,
106 PolygonVertexIdx& firstValidVertexIdx,
KyUInt32 maxTestCount);
109 class HoleIdxWithMaxXVertexIdx
112 HoleIdxWithMaxXVertexIdx() : m_toConnectIdx(PolygonVertexIdx_Invalid) {}
113 Vec2i m_posToConnect;
114 PolygonVertexIdx m_toConnectIdx;
117 class FindVertexIdxResult
120 enum ResultType { NOTFOUND, INTERSECTION_ON_VERTEX, INTERSECTION_ON_EDGE };
122 FindVertexIdxResult () : m_resultType(NOTFOUND), m_intersectionPolygonVertexIdx(PolygonVertexIdx_Invalid), m_xIntersection(
KyInt32MAXVAL) {}
124 void SetVertexIntersection(
KyInt32 xIntersection, PolygonVertexIdx intersectionPolygonVertexIdx)
126 m_resultType = INTERSECTION_ON_VERTEX;
127 m_xIntersection = xIntersection;
128 m_intersectionPolygonVertexIdx = intersectionPolygonVertexIdx;
131 void SetEdgeIntersection(
const Vec2i& edgeStart,
const Vec2i& edgeEnd, PolygonVertexIdx intersectionPolygonVertexIdx)
133 m_resultType = INTERSECTION_ON_EDGE;
134 m_edgeStart = edgeStart;
136 m_intersectionPolygonVertexIdx = intersectionPolygonVertexIdx;
139 ResultType m_resultType;
140 PolygonVertexIdx m_intersectionPolygonVertexIdx;
146 KY_INLINE
bool HasDuplicate(PolygonVertexIdx polygonVertexIdx)
const {
return (*m_polygonVertices)[polygonVertexIdx].m_duplicateVertexIdx != polygonVertexIdx; }
147 KY_INLINE PolygonVertex::Type GetPolygonVertexType(
const Vec2i& prevPos,
const Vec2i& currentPos,
const Vec2i& nextPos)
const
149 const KyInt32 crossProduct = (nextPos - currentPos) ^ (prevPos - currentPos);
150 return crossProduct > 0 ? PolygonVertex::TYPE_CONVEX : PolygonVertex::TYPE_REFLEX;
153 void FillPolygonVerticesForExterior(PolygonVertexIdx& currentNewPolygonVertexIdx);
154 void FillPolygonVerticesForHole(
const EarClippingTriangulatorInputPolygon& holePolygon, HoleIdxWithMaxXVertexIdx& currentHoleMinX, PolygonVertexIdx& currentNewPolygonVertexIdx);
156 KyResult LinkHoleToExterior(HoleIdxWithMaxXVertexIdx& currentHoleMinX, PolygonVertexIdx& currentNewPolygonVertexIdx);
157 KyUInt32 FindAndClipEar(PolygonVertexIdx& firstValidVertexIdx,
KyUInt32& vertexTestedCount);
159 void FindPolygonVertexIdxToLinkTo(
KyUInt32 posToConnectIdx, FindVertexIdxResult& result);
160 void FindPolygonVertexIdxToLinkToForEdgeIntersection(
KyUInt32 posToConnectIdx, FindVertexIdxResult& result);
161 PolygonVertexIdx FindBestCandidateForHoleConnection(
KyUInt32 posToConnectIdx);
162 bool IsInAngularSectorOfVertex(
const Vec2i& posToConnect,
const PolygonVertex* polygonVertex)
const;
165 bool IsPolygonVertexAnEar(
const PolygonVertex* polygonVertex,
KyUInt32& vertexTestedCount)
const;
166 void UpdateVertexConvexStatus(PolygonVertex* polygonVertex);
168 void PrintInputPolygonForDebug();
170 static bool DoesPreviousEdgeIntersectsXAxisBeforeCurrentEdge(
const Vec2i& prevEdgeStart,
const Vec2i& prevEdgeEnd,
const Vec2i& currEdgeStart,
const Vec2i& currEdgeEnd);
173 const EarClippingTriangulatorInputPolygonWithHoles* m_input;
174 DynamicTriangulation* m_output;
175 KyArrayDH<PolygonVertex>* m_polygonVertices;
180 #endif //Navigation_EarClippingTriangulator_H
KyInt32 KyResult
Defines a type that can be returned by methods or functions in the Gameware Navigation SDK to indicat...
Definition: types.h:254
int KyInt32
Type used internally to represent a 32-bit integer.
Definition: types.h:35
#define KY_NULL
Null value.
Definition: types.h:247
#define KyInt32MAXVAL
The maximum value that can be stored in the KyInt32 variable type.
Definition: types.h:224
Definition: gamekitcrowddispersion.h:20
#define KY_DEFINE_NEW_DELETE_OPERATORS(MemStat)
This macro defines new and delete operators.
Definition: memory.h:137
#define KyUInt16MAXVAL
The maximum value that can be stored in the KyUInt16 variable type.
Definition: types.h:230
unsigned short KyUInt16
Type used internally to represent an unsigned 16-bit integer.
Definition: types.h:40
unsigned int KyUInt32
Type used internally to represent an unsigned 32-bit integer.
Definition: types.h:36