-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPath.cpp
194 lines (144 loc) · 5.86 KB
/
Path.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "StdAfx.h"
#include "Path.h"
#include <fstream>
Path::Path(void)
{
}
Path::~Path(void)
{
ClearData();
}
void Path::SavePathVectors(const char* FileName) const {
std::ofstream OutputFile(FileName);
//each of these is a vector
for (std::list< std::pair<POINT, POINT> >::const_iterator VectIt = LastPathVectors.begin(); VectIt != LastPathVectors.end(); ++VectIt) {
OutputFile << GeometricObject::Range(VectIt->first, VectIt->second) << " " << atan2((double)VectIt->first.y - VectIt->second.y, (double)VectIt->second.x - VectIt->first.x) << std::endl;
}
OutputFile.close();
}
void Path::ReadData(FileStream& File) {
ClearData();
}
void Path::WriteData(FileStream& File) const {
}
bool Path::PointIntersect(POINT ThePoint, RECT TheRect) {
return (ThePoint.x >= TheRect.left && ThePoint.x <= TheRect.right) && (ThePoint.y >= TheRect.top && ThePoint.y <= TheRect.bottom);
}
void Path::DrawPath(const PointAr& PathToDraw, const HDC DestDC, const COLORREF Color, const int DrawSpeed) const {
GeometricObject::ResetDrawCounter();
for (PointArCItr VectIt = PathToDraw.begin(); VectIt != PathToDraw.end(); ++VectIt) {
GeometricObject::DrawFilibuster();
SetPixel(DestDC, VectIt->x, VectIt->y, Color);
}
}
PointAr Path::GeneratePath(POINT StartLocation, POINT EndLocation) {
PointAr ReturnPath;
ReturnPath.clear();
LastPathVectors.clear();
ShortestPath.ResetData();
//prevent a 0-length path from being returned
if (StartLocation == EndLocation) {
ReturnPath.push_back(StartLocation);
PathsGenerated.push_back(ReturnPath);
return ReturnPath;
}
GetArea()->MakeProbeOK();
//update the class records
StartPoint = StartLocation;
EndPoint = EndLocation;
MappedLine OrgLine(StartLocation, EndLocation);
for (MappedPointPtrArItr VertexLoop = GetArea()->Vertices.begin(); VertexLoop != GetArea()->Vertices.end(); ++VertexLoop) {
(*VertexLoop)->Range = OrgLine.PerpDist((*VertexLoop)->PointPart);
}
//the dummy path
PathDescriptor DummyPath;
DummyPath.TotalDistance = 0;
//add in the ending point
MappedPoint DummyEnd;
DummyEnd.PointPart = EndLocation;
DummyEnd.IsVertex = true;
//this mutilates the vertices for path-finding purposes
GetArea()->FillInLegalNodes(DummyEnd, GetArea()->Vertices, false, false, true);
//add in the starting point
MappedPoint DummyStart;
DummyStart.PointPart = StartLocation;
DummyStart.IsVertex = true; //hack-ish
GetArea()->FillInLegalNodes(DummyStart, GetArea()->Vertices, true, false, false);
DummyPath.Points.push_back(&DummyStart); //add it to the dummy path
//now add the ending point as a node to the starting point (if it's legal)
if (!GetArea()->GetFirstInvalidLinePoint(StartLocation, EndLocation)) {
DummyPath.Points.front()->PossibleNodes().push_back(&DummyEnd);
DummyPath.Points.clear();
DummyPath.Points.push_back(&DummyStart);
DummyPath.Points.push_back(&DummyEnd);
DummyPath.TotalDistance = Range(DummyStart, DummyEnd);
ShortestPath = DummyPath;
}
else {
for (MappedPointPtrArItr VertexLoop = GetArea()->Vertices.begin(); VertexLoop != GetArea()->Vertices.end(); ++VertexLoop) {
(*VertexLoop)->PossibleNodes().sort();
}
DevelopPathsRecursive(&DummyPath);
}
//now search for the shortest path and return it
if (ShortestPath.TotalDistance) {
MappedPointPtrArCItr cur_it = ShortestPath.Points.begin();
for (MappedPointPtrArCItr prev_it = cur_it++; cur_it != ShortestPath.Points.end(); prev_it = cur_it++) {
GetArea()->AddSTLArrays(ReturnPath, LinePath((*prev_it)->PointPart, (*cur_it)->PointPart, false, false), true);
LastPathVectors.push_back( std::pair<POINT, POINT>((*prev_it)->PointPart, (*cur_it)->PointPart));
}
PathsGenerated.push_back(ReturnPath);
}
else {
OutputDebugString(L"Pathfinding has failed.\n");
}
for (MappedPointPtrArCItr VertLoop = GetArea()->Vertices.begin(); VertLoop != GetArea()->Vertices.end(); ++VertLoop) {
(*VertLoop)->ClearPointStorage(true, true, true, true, true);
}
GetArea()->VertexNodeFind(); //restore all vertices nodes
return ReturnPath;
}
void Path::DevelopPathsRecursive(PathDescriptor* PathToDevelop) {
//NOTE: This procedure uses MappedPoint::IsConcaveVertex as a "hit" indicator
MappedPoint* DataBuffer;
for (MappedPointPtrArCItr PathsItr = PathToDevelop->Points.back()->PossibleNodes().begin(); PathsItr != PathToDevelop->Points.back()->PossibleNodes().end(); ++PathsItr) {
DataBuffer = *PathsItr;
//look for the current point in the current path - if found, do nothing (continue the loop), otherwise continue the operation
if (!DataBuffer->IsConcaveVertex) {
//change the transferbuffer, then undo the changes to speed up the procedure
double RangeBuffer = Range(*PathToDevelop->Points.back(), DataBuffer->PointPart);
PathToDevelop->TotalDistance += RangeBuffer;
if (!(PathToDevelop->TotalDistance > ShortestPath.TotalDistance && ShortestPath.TotalDistance)) {
PathToDevelop->Points.push_back(*PathsItr);
DataBuffer->IsConcaveVertex = true;
if (**PathsItr == EndPoint) {
if (!ShortestPath.TotalDistance || (ShortestPath.TotalDistance > PathToDevelop->TotalDistance)) {
ShortestPath = *PathToDevelop;
}
}
else {
DevelopPathsRecursive(PathToDevelop);
}
PathToDevelop->Points.pop_back();
DataBuffer->IsConcaveVertex = false;
}
PathToDevelop->TotalDistance -= RangeBuffer;
}
}
}
void Path::Draw(HDC DrawDC) {
HPEN DrawPen = CreatePen(PS_SOLID, 1, RGB(0, 255, 0));
HPEN OldPen = SelectPen(DrawDC, DrawPen);
for (std::list< PointAr >::const_iterator VectIt = PathsGenerated.begin(); VectIt != PathsGenerated.end(); ++VectIt) {
::MoveToEx(DrawDC, VectIt->front().x, VectIt->front().y, NULL);
::LineTo(DrawDC, VectIt->back().x, VectIt->back().y);
DrawPath(*VectIt, DrawDC, RGB(0, 0, 255));
}
SelectPen(DrawDC, OldPen);
DeletePen(DrawPen);
}
void Path::ClearData() { //does not clear obstacles
PathsGenerated.clear();
LastPathVectors.clear();
ShortestPath.ResetData();
}