-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSolutionSpecExt.cs
339 lines (322 loc) · 13.1 KB
/
SolutionSpecExt.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
using System;
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using NUnit.Framework;
using SquareConstructor;
namespace lib
{
public static class SolutionSpecExt
{
public static SolutionSpec Reflect(this SolutionSpec origSpec, Segment segment)
{
var reflectedDestPoints = origSpec.DestPoints.Select(x => x.Reflect(segment)).ToArray();
return new SolutionSpec(origSpec.SourcePoints, origSpec.Facets, reflectedDestPoints);
}
public static SolutionSpec Shift(this SolutionSpec origSpec, Vector vector)
{
var shiftedDestPoints = origSpec.DestPoints.Select(x => x + vector).ToArray();
return new SolutionSpec(origSpec.SourcePoints, origSpec.Facets, shiftedDestPoints);
}
public static SolutionSpec Fold(this SolutionSpec origSpec, Segment segment)
{
var facetsToFold = new HashSet<Facet>();
for (int i = 0; i < origSpec.DestPoints.Length; i++)
{
if (GetDestPointRelativePosition(origSpec.DestPoints[i], segment) == PointRelativePosition.Outside)
facetsToFold.UnionWith(origSpec.GetFacetsWithPoint(i));
}
if (!facetsToFold.Any())
return origSpec;
var newSourcePoints = new List<Vector>(origSpec.SourcePoints);
var srcPointsMap = newSourcePoints.Select((x, i) => new { V = x, Index = i }).ToDictionary(x => x.V, x => x.Index);
var newDestPoints = new List<Vector>(origSpec.DestPoints);
var newFacets = new List<Facet>();
foreach (var facet in origSpec.Facets)
{
if (!facetsToFold.Contains(facet))
newFacets.Add(facet);
else
{
if (facet.Vertices.All(v => GetDestPointRelativePosition(origSpec.DestPoints[v], segment) != PointRelativePosition.Inside))
{
newFacets.Add(facet);
foreach (var vertex in facet.Vertices)
newDestPoints[vertex] = origSpec.DestPoints[vertex].Reflect(segment);
}
else
{
int prevVertex = -1;
var prevRelativePos = default (PointRelativePosition);
var innerFacetVertices = new List<int>();
var outerFacetVertices = new List<int>();
foreach (var vertex in facet.Vertices)
{
var relativePos = GetDestPointRelativePosition(origSpec.DestPoints[vertex], segment);
if (prevVertex == -1)
{
switch (relativePos)
{
case PointRelativePosition.Inside:
innerFacetVertices.Add(vertex);
break;
case PointRelativePosition.Outside:
outerFacetVertices.Add(vertex);
newDestPoints[vertex] = origSpec.DestPoints[vertex].Reflect(segment);
break;
case PointRelativePosition.Boundary:
innerFacetVertices.Add(vertex);
outerFacetVertices.Add(vertex);
break;
default:
throw new ArgumentOutOfRangeException();
}
}
else
{
var destEdge = new Segment(origSpec.DestPoints[prevVertex], origSpec.DestPoints[vertex]);
var srcEdge = new Segment(origSpec.SourcePoints[prevVertex], origSpec.SourcePoints[vertex]);
switch (prevRelativePos)
{
case PointRelativePosition.Inside:
switch (relativePos)
{
case PointRelativePosition.Inside:
innerFacetVertices.Add(vertex);
break;
case PointRelativePosition.Outside:
var intersectionDestPoint = destEdge.GetIntersectionWithLine(segment).Value;
int intersectionVertex;
var intersectionSrcPoint = GetSrcPoint(srcEdge, destEdge, intersectionDestPoint);
if (!srcPointsMap.TryGetValue(intersectionSrcPoint, out intersectionVertex))
{
intersectionVertex = newDestPoints.Count;
newDestPoints.Add(intersectionDestPoint);
newSourcePoints.Add(intersectionSrcPoint);
srcPointsMap.Add(intersectionSrcPoint, intersectionVertex);
}
innerFacetVertices.Add(intersectionVertex);
outerFacetVertices.Add(intersectionVertex);
outerFacetVertices.Add(vertex);
newDestPoints[vertex] = origSpec.DestPoints[vertex].Reflect(segment);
break;
case PointRelativePosition.Boundary:
innerFacetVertices.Add(vertex);
outerFacetVertices.Add(vertex);
break;
default:
throw new ArgumentOutOfRangeException();
}
break;
case PointRelativePosition.Outside:
switch (relativePos)
{
case PointRelativePosition.Inside:
var intersectionDestPoint = destEdge.GetIntersectionWithLine(segment).Value;
int intersectionVertex;
var intersectionSrcPoint = GetSrcPoint(srcEdge, destEdge, intersectionDestPoint);
if (!srcPointsMap.TryGetValue(intersectionSrcPoint, out intersectionVertex))
{
intersectionVertex = newDestPoints.Count;
newDestPoints.Add(intersectionDestPoint);
newSourcePoints.Add(intersectionSrcPoint);
srcPointsMap.Add(intersectionSrcPoint, intersectionVertex);
}
innerFacetVertices.Add(intersectionVertex);
innerFacetVertices.Add(vertex);
outerFacetVertices.Add(intersectionVertex);
break;
case PointRelativePosition.Outside:
outerFacetVertices.Add(vertex);
newDestPoints[vertex] = origSpec.DestPoints[vertex].Reflect(segment);
break;
case PointRelativePosition.Boundary:
innerFacetVertices.Add(vertex);
outerFacetVertices.Add(vertex);
break;
default:
throw new ArgumentOutOfRangeException();
}
break;
case PointRelativePosition.Boundary:
switch (relativePos)
{
case PointRelativePosition.Inside:
innerFacetVertices.Add(vertex);
break;
case PointRelativePosition.Outside:
outerFacetVertices.Add(vertex);
newDestPoints[vertex] = origSpec.DestPoints[vertex].Reflect(segment);
break;
case PointRelativePosition.Boundary:
throw new ArgumentOutOfRangeException("WTF! Переход из Boundary в Boundary");
default:
throw new ArgumentOutOfRangeException();
}
break;
default:
throw new ArgumentOutOfRangeException();
}
}
prevVertex = vertex;
prevRelativePos = relativePos;
}
// last edge
{
var vertex = facet.Vertices[0];
var relativePos = GetDestPointRelativePosition(origSpec.DestPoints[vertex], segment);
var destEdge = new Segment(origSpec.DestPoints[prevVertex], origSpec.DestPoints[vertex]);
var srcEdge = new Segment(origSpec.SourcePoints[prevVertex], origSpec.SourcePoints[vertex]);
switch (prevRelativePos)
{
case PointRelativePosition.Inside:
switch (relativePos)
{
case PointRelativePosition.Inside:
break;
case PointRelativePosition.Outside:
var intersectionDestPoint = destEdge.GetIntersectionWithLine(segment).Value;
int intersectionVertex;
var intersectionSrcPoint = GetSrcPoint(srcEdge, destEdge, intersectionDestPoint);
if (!srcPointsMap.TryGetValue(intersectionSrcPoint, out intersectionVertex))
{
intersectionVertex = newDestPoints.Count;
newDestPoints.Add(intersectionDestPoint);
newSourcePoints.Add(intersectionSrcPoint);
srcPointsMap.Add(intersectionSrcPoint, intersectionVertex);
}
innerFacetVertices.Add(intersectionVertex);
outerFacetVertices.Add(intersectionVertex);
break;
case PointRelativePosition.Boundary:
break;
default:
throw new ArgumentOutOfRangeException();
}
break;
case PointRelativePosition.Outside:
switch (relativePos)
{
case PointRelativePosition.Inside:
var intersectionDestPoint = destEdge.GetIntersectionWithLine(segment).Value;
int intersectionVertex;
var intersectionSrcPoint = GetSrcPoint(srcEdge, destEdge, intersectionDestPoint);
if (!srcPointsMap.TryGetValue(intersectionSrcPoint, out intersectionVertex))
{
intersectionVertex = newDestPoints.Count;
newDestPoints.Add(intersectionDestPoint);
newSourcePoints.Add(intersectionSrcPoint);
srcPointsMap.Add(intersectionSrcPoint, intersectionVertex);
}
innerFacetVertices.Add(intersectionVertex);
outerFacetVertices.Add(intersectionVertex);
break;
case PointRelativePosition.Outside:
break;
case PointRelativePosition.Boundary:
break;
default:
throw new ArgumentOutOfRangeException();
}
break;
case PointRelativePosition.Boundary:
break;
default:
throw new ArgumentOutOfRangeException();
}
}
newFacets.Add(new Facet(innerFacetVertices.ToArray()));
newFacets.Add(new Facet(outerFacetVertices.ToArray()));
}
}
}
return new SolutionSpec(newSourcePoints.ToArray(), newFacets.ToArray(), newDestPoints.ToArray());
}
private static Vector GetSrcPoint(Segment srcEdge, Segment destEdge, Vector destPoint)
{
var k = destEdge.ToVector().ScalarProd(destPoint - destEdge.Start)/destEdge.QuadratOfLength;
var srcPoint = srcEdge.Start + srcEdge.ToVector()*k;
return srcPoint;
}
private static PointRelativePosition GetDestPointRelativePosition(Vector destPoint, Segment segment)
{
var pointVector = destPoint - segment.Start;
var vectorProdLength = pointVector.VectorProdLength(segment.ToVector());
return vectorProdLength > 0 ? PointRelativePosition.Outside : vectorProdLength < 0 ? PointRelativePosition.Inside : PointRelativePosition.Boundary;
}
public static IEnumerable<Facet> GetFacetsWithPoint(this SolutionSpec spec, int vertex)
{
return spec.Facets.Where(f => f.Vertices.Contains(vertex));
}
private enum PointRelativePosition
{
Inside,
Outside,
Boundary
}
}
[TestFixture]
public class SolutionSpecExt_Should
{
[TestCase("0,0 1,0")]
[TestCase("0,-1/2 1,-1/2")]
[TestCase("1/2,2 -2,1/2")]
public void Fold_Nothing(string segment)
{
var origSolution = SolutionSpec.CreateTrivial(x => x);
var result = origSolution.Fold(segment);
result.SourcePoints.Should().Equal(origSolution.SourcePoints);
result.Facets.Should().Equal(origSolution.Facets);
result.DestPoints.Should().Equal(origSolution.DestPoints);
}
[TestCase("1,0 0,0", "0,0|1,0|1,-1|0,-1")]
[TestCase("1,1 1,0", "2,0|1,0|1,1|2,1")]
[TestCase("0,1 1,1", "0,2|1,2|1,1|0,1")]
[TestCase("0,0 0,1", "0,0|-1,0|-1,1|0,1")]
public void Fold_ByBoundary(string segment, string expectedDestPoints)
{
var origSolution = SolutionSpec.CreateTrivial(x => x);
var result = origSolution.Fold(segment);
result.SourcePoints.Should().Equal(origSolution.SourcePoints);
result.Facets.Should().Equal(origSolution.Facets);
result.DestPoints.Should().Equal(expectedDestPoints.Split('|').Select(Vector.Parse).ToArray());
}
[TestCase("0,0 1,1", "0,0|0,1|1,1|0,1", "0 1 2|0 2 3")]
[TestCase("1,1 0,0", "0,0|1,0|1,1|1,0", "0 1 2|0 2 3")]
[TestCase("1,0 0,1", "0,0|1,0|0,0|0,1", "0 1 3|1 2 3")]
[TestCase("0,1 1,0", "1,1|1,0|1,1|0,1", "0 1 3|1 2 3")]
public void Fold_ByDiagonal(string segment, string expectedDestPoints, string expectedFacets)
{
var origSolution = SolutionSpec.CreateTrivial(x => x);
var result = origSolution.Fold(segment);
result.SourcePoints.Should().Equal(origSolution.SourcePoints);
result.Facets.Select(FacetToString).ToArray().Should().BeEquivalentTo(expectedFacets.Split('|'));
result.DestPoints.Should().Equal(expectedDestPoints.Split('|').Select(Vector.Parse).ToArray());
}
[TestCase("0,1/2 1,1/2", "0,0|1,0|1,1|0,1|1,1/2|0,1/2", "0,1|1,1|1,1|0,1|1,1/2|0,1/2", "0 1 4 5|4 2 3 5")]
[TestCase("1,1/2 0,1/2", "0,0|1,0|1,1|0,1|1,1/2|0,1/2", "0,0|1,0|1,0|0,0|1,1/2|0,1/2", "0 1 4 5|4 2 3 5")]
[TestCase("1/2,0 1/2,1", "0,0|1,0|1,1|0,1|1/2,0|1/2,1", "0,0|0,0|0,1|0,1|1/2,0|1/2,1", "0 4 5 3|4 1 2 5")]
[TestCase("1/2,1 1/2,0", "0,0|1,0|1,1|0,1|1/2,0|1/2,1", "1,0|1,0|1,1|1,1|1/2,0|1/2,1", "0 4 5 3|4 1 2 5")]
public void Fold_ByMiddleLine(string segment, string expectedSrcPoints, string expectedDestPoints, string expectedFacets)
{
var origSolution = SolutionSpec.CreateTrivial(x => x);
var result = origSolution.Fold(segment);
result.SourcePoints.Should().Equal(expectedSrcPoints.Split('|').Select(Vector.Parse).ToArray());
result.Facets.Select(FacetToString).ToArray().Should().BeEquivalentTo(expectedFacets.Split('|'));
result.DestPoints.Should().Equal(expectedDestPoints.Split('|').Select(Vector.Parse).ToArray());
}
[Test]
[Explicit]
public void Fold_Demo()
{
var origSolution = SolutionSpec.CreateTrivial(x => x);
var result = origSolution.Fold("1,3/4 0,1/4");
result = result.Fold("1,1 1/4,0");
result.CreateVisualizerForm().ShowDialog();
}
private object FacetToString(Facet arg)
{
return string.Join(" ", arg.Vertices);
}
}
}