forked from MatterHackers/MatterSlice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpathOrderOptimizer.cs
171 lines (154 loc) · 7.07 KB
/
pathOrderOptimizer.cs
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
/*
This file is part of MatterSlice. A commandline utility for
generating 3D printing GCode.
Copyright (C) 2013 David Braam
Copyright (c) 2014, Lars Brubaker
MatterSlice is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.IO;
using System.Collections.Generic;
using MatterSlice.ClipperLib;
namespace MatterHackers.MatterSlice
{
using Polygon = List<IntPoint>;
using Polygons = List<List<IntPoint>>;
public class PathOrderOptimizer
{
IntPoint startPosition;
List<Polygon> polygons = new List<Polygon>();
public List<int> startIndexInPolygon = new List<int>();
public List<int> bestPolygonOrderIndex = new List<int>();
public PathOrderOptimizer(IntPoint startPoint)
{
this.startPosition = startPoint;
}
public void AddPolygon(Polygon polygon)
{
this.polygons.Add(polygon);
}
public void AddPolygons(Polygons polygons)
{
for (int i = 0; i < polygons.Count; i++)
{
this.polygons.Add(polygons[i]);
}
}
public void Optimize()
{
// Find the point that is closest to our current position (start position)
bool[] polygonHasBeenAdded = new bool[polygons.Count];
for (int polygonIndex = 0; polygonIndex < polygons.Count; polygonIndex++)
{
int bestPointIndex = -1;
double closestDist = double.MaxValue;
Polygon currentPolygon = polygons[polygonIndex];
for (int pointIndex = 0; pointIndex < currentPolygon.Count; pointIndex++)
{
double dist = (currentPolygon[pointIndex] - startPosition).LengthSquared();
if (dist < closestDist)
{
bestPointIndex = pointIndex;
closestDist = dist;
}
}
startIndexInPolygon.Add(bestPointIndex);
}
//IntPoint incommingPerpendicularNormal = new IntPoint(0, 0);
IntPoint currentPosition = startPosition;
// We loop over the polygon list twice, as each inner loop we only pick one polygon.
for (int polygonIndexOuterLoop = 0; polygonIndexOuterLoop < polygons.Count; polygonIndexOuterLoop++)
{
int bestPolygonIndex = -1;
double bestDist = double.MaxValue;
for (int polygonIndex = 0; polygonIndex < polygons.Count; polygonIndex++)
{
if (polygonHasBeenAdded[polygonIndex] || polygons[polygonIndex].Count < 1)
{
continue;
}
// If there are only 2 points (a single line) we are willing to start from the start or the end.
if (polygons[polygonIndex].Count == 2)
{
double distToSart = (polygons[polygonIndex][0] - currentPosition).LengthSquared();
//dist += Math.Abs(incommingPerpendicularNormal.Dot(polygons[polygonIndex][1] - polygons[polygonIndex][0].normal(1000))) * 0.0001f;
if (distToSart < bestDist)
{
bestPolygonIndex = polygonIndex;
bestDist = distToSart;
startIndexInPolygon[polygonIndex] = 0;
}
double distToEnd = (polygons[polygonIndex][1] - currentPosition).LengthSquared();
//dist += Math.Abs(incommingPerpendicularNormal.Dot(polygons[polygonIndex][0] - polygons[polygonIndex][1].normal(1000))) * 0.0001f;
if (distToEnd < bestDist)
{
bestPolygonIndex = polygonIndex;
bestDist = distToEnd;
startIndexInPolygon[polygonIndex] = 1;
}
}
else
{
double dist = (polygons[polygonIndex][startIndexInPolygon[polygonIndex]] - currentPosition).LengthSquared();
if (dist < bestDist)
{
bestPolygonIndex = polygonIndex;
bestDist = dist;
}
}
}
if (bestPolygonIndex > -1)
{
if (polygons[bestPolygonIndex].Count == 2)
{
// get the point that is opposite from the one we started on
int startIndex = startIndexInPolygon[bestPolygonIndex];
int endIndex = (startIndex + 1) % 2;
currentPosition = polygons[bestPolygonIndex][endIndex];
//incommingPerpendicularNormal = (polygons[bestIndex][endIndex] - polygons[bestIndex][polyStart[bestIndex]]).normal(1000).CrossZ();
}
else
{
currentPosition = polygons[bestPolygonIndex][startIndexInPolygon[bestPolygonIndex]];
//incommingPerpendicularNormal = new IntPoint(0, 0);
}
polygonHasBeenAdded[bestPolygonIndex] = true;
bestPolygonOrderIndex.Add(bestPolygonIndex);
}
}
currentPosition = startPosition;
foreach(int bestPolygonIndex in bestPolygonOrderIndex)
{
int bestStartPoint = -1;
double bestDist = double.MaxValue;
for (int pointIndex = 0; pointIndex < polygons[bestPolygonIndex].Count; pointIndex++)
{
double dist = (polygons[bestPolygonIndex][pointIndex] - currentPosition).LengthSquared();
if (dist < bestDist)
{
bestStartPoint = pointIndex;
bestDist = dist;
}
}
startIndexInPolygon[bestPolygonIndex] = bestStartPoint;
if (polygons[bestPolygonIndex].Count == 2)
{
currentPosition = polygons[bestPolygonIndex][(bestStartPoint + 1) % 2];
}
else
{
currentPosition = polygons[bestPolygonIndex][bestStartPoint];
}
}
}
}
}