public ProjectionSignMatrixDistribution(long sampleSize, int dimensions) { for (long i = sampleSize; i > 0; i--) { Ngon ngon = Program.generateRandomNgon(dimensions); ProjectionMatrix projetion = new ProjectionMatrix(ngon); SignMatrix signMatrix = new SignMatrix(projetion); SignMatrix fullKey = similarFullMatrix.Keys.FirstOrDefault(m => m.Equals(signMatrix)); if (fullKey == null) { similarFullMatrix.Add(signMatrix, new List <Ngon>()); fullKey = signMatrix; } similarFullMatrix[fullKey].Add(ngon); ReducedSignMatrix reducedKey = similarReducedMatrix.Keys.FirstOrDefault(m => m.Equals(signMatrix.getReduced())); if (reducedKey == null) { similarReducedMatrix.Add(signMatrix.getReduced(), new List <Ngon>()); reducedKey = signMatrix.getReduced(); } similarReducedMatrix[reducedKey].Add(ngon); if (i % (sampleSize / 100) == 0) { Console.Write("\rCreating sign matrix table: {0}% ", i / (sampleSize / 100)); } } }
public static Ngons MinkowskiSumSegment(Ngon pattern, IntPoint p1, IntPoint p2, bool flip_pattern) { Clipper clipper = new Clipper(); Ngon p1_c = pattern.Clone(p1.X, p1.Y, flip_pattern); if (p1 == p2) { return new Ngons() { p1_c } } ; Ngon p2_c = pattern.Clone(p2.X, p2.Y, flip_pattern); Ngons full = new Ngons(); clipper.AddPath(p1_c, PolyType.ptSubject, true); clipper.AddPath(p2_c, PolyType.ptSubject, true); clipper.AddPaths(Clipper.MinkowskiSum(pattern.Clone(0, 0, flip_pattern), new Ngon() { p1, p2 }, false), PolyType.ptSubject, true); clipper.Execute(ClipType.ctUnion, full, PolyFillType.pftNonZero); return(full); }
private static Ngons MSumFull(Ngon pattern, Ngons subject, bool flip_pattern) { Clipper clipper = new Clipper(); Ngons full = new Ngons(); long scale = flip_pattern ? -1 : 1; for (int i = 0; i < pattern.Count; i++) { clipper.AddPaths(subject.Clone(scale * pattern[i].X, scale * pattern[i].Y), PolyType.ptSubject, true); } clipper.Execute(ClipType.ctUnion, full, PolyFillType.pftNonZero); clipper.Clear(); clipper.AddPaths(full, PolyType.ptSubject, true); clipper.AddPaths(MinkowskiSumBoundary(pattern, subject, flip_pattern), PolyType.ptSubject, true); Ngons res = new Ngons(); clipper.Execute(ClipType.ctUnion, res, PolyFillType.pftNonZero); return(res); }
public PluckerSignMatrixDistribution(long sampleSize, int dimensions) { Matrix <double> mask = Matrix <double> .Build.Dense(5, 5, 1); mask[2, 0] = 0; mask[3, 0] = 0; mask[3, 1] = 0; for (long i = sampleSize; i > 0; i--) { Ngon ngon = Program.generateRandomNgon(dimensions); PluckerMatrix plucker = new PluckerMatrix(ngon); SignMatrix signMatrix = new SignMatrix(plucker, mask); SignMatrix fullKey = similarFullMatrix.Keys.FirstOrDefault(m => m.Equals(signMatrix)); if (fullKey == null) { similarFullMatrix.Add(signMatrix, new List <Ngon>()); fullKey = signMatrix; } similarFullMatrix[fullKey].Add(ngon); ReducedSignMatrix2 reducedKey = similarReducedMatrix.Keys.FirstOrDefault(m => m.Equals(signMatrix.getReduced2(mask))); if (reducedKey == null) { similarReducedMatrix.Add(signMatrix.getReduced2(mask), new List <Ngon>()); reducedKey = signMatrix.getReduced2(mask); } similarReducedMatrix[reducedKey].Add(ngon); if (i % (sampleSize / 100) == 0) { Console.Write("\rCreating sign matrix table: {0}% ", i / (sampleSize / 100)); } } }
private static Ngons MSumSimple(Ngon pattern, Ngons subject, bool flip_pattern) { IntRect pB = GetBounds(pattern); IntRect sB = GetBounds(subject[0]); if (flip_pattern) { pB = new IntRect(-pB.right, -pB.bottom, -pB.left, -pB.top); } long l = pB.left + sB.left; long r = pB.right + sB.right; long t = pB.top + sB.top; long b = pB.bottom + sB.bottom; Ngon p = new Ngon() { new IntPoint(l, b), new IntPoint(r, b), new IntPoint(r, t), new IntPoint(l, t) }; return(new Ngons() { p }); }
public void getTypeTest() { double[][] edgevectors = { new double[] { 1, 0 }, new double[] { 0, 1 }, new double[] { -1, 0 }, new double[] { 0, -1 } }; Ngon ngon = new Ngon(edgevectors); Assert.AreEqual(ngon.Type, NgonType.Convex); edgevectors = new double[][] { new double[] { 0, 2 }, new double[] { 4, -2 }, new double[] { 0, 2 }, new double[] { -4, -2 } }; ngon = new Ngon(edgevectors); Assert.AreEqual(ngon.Type, NgonType.Self_Intersecting); edgevectors = new double[][] { new double[] { 1, 0 }, new double[] { (double)-3 / 4, (double)1 / 4 }, new double[] { (double)-1 / 4, (double)3 / 4 }, new double[] { 0, -1 } }; ngon = new Ngon(edgevectors); Assert.AreEqual(ngon.Type, NgonType.Reflex); }
public static Ngons MinkowskiSum(Ngon pattern, Ngons subject, NFPQUALITY quality, bool flip_pattern) { switch (quality) { case NFPQUALITY.Simple: return(MSumSimple(pattern, subject, flip_pattern)); case NFPQUALITY.Convex: return(MSumConvex(pattern, subject, flip_pattern)); case NFPQUALITY.ConcaveLight: return(MSumConcave(pattern, subject, flip_pattern, 0.25)); case NFPQUALITY.ConcaveMedium: return(MSumConcave(pattern, subject, flip_pattern, 0.55)); case NFPQUALITY.ConcaveHigh: return(MSumConcave(pattern, subject, flip_pattern, 0.85)); case NFPQUALITY.ConcaveFull: return(MSumConcave(pattern, subject, flip_pattern, 1.0)); case NFPQUALITY.Full: return(MSumFull(pattern, subject, flip_pattern)); default: return(null); } }
public static bool AlmostRectangle(Ngon target, double percent_diff = 0.05) { IntRect bounds = GetBounds(target); double area = Math.Abs(Clipper.Area(target)); return(1.0 - area / bounds.Area() < percent_diff); }
public void fourgonTypeDistribution() { long sampleSize = 1000000; double precision = 0.1; //4gons: 1/3 convex, 1/3 reflex, 1/3 self-intersecting long convexNum = 0; long reflexNum = 0; long selfintersectingNum = 0; for (long i = sampleSize; i > 0; i--) { Ngon randomNgon = Program.generateRandomNgon(4); if (randomNgon.Type == NgonType.Convex) { convexNum++; } if (randomNgon.Type == NgonType.Reflex) { reflexNum++; } if (randomNgon.Type == NgonType.Self_Intersecting) { selfintersectingNum++; } } double convexPercentage = (double)convexNum / sampleSize * 100; double reflexPercentage = (double)convexNum / sampleSize * 100; double selfintersectingPercentage = (double)convexNum / sampleSize * 100; Assert.AreEqual(convexPercentage, 33.3333333, precision); Assert.AreEqual(reflexPercentage, 33.3333333, precision); Assert.AreEqual(selfintersectingPercentage, 33.3333333, precision); }
public ICollection <Ngon> edgePermutations() { ICollection <Ngon> result = new List <Ngon>(); var permutations = GetPermutations(edgeVectors); foreach (var ngonVectors in permutations) { double[][] evect = ngonVectors.ToArray(); Ngon n = new Ngon(evect); result.Add(n); if (n.Type == NgonType.Convex) { this.convex++; } if (n.Type == NgonType.Reflex) { this.reflex++; } if (n.Type == NgonType.Self_Intersecting) { this.self_intersecting++; } } return(result); }
public NgonEdgePermutation(Ngon ngon) { this.modelNgon = ngon; this.permutations = (new NgonEdgePermutations(ngon)).edgePermutations(); InitializeComponent(); updateCollection(); }
public void SignMatrixEqualsTest() { Ngon ngon = Program.generateRandomNgon(6); SignMatrix a = new SignMatrix(new PluckerMatrix(ngon)); SignMatrix b = new SignMatrix(new PluckerMatrix(ngon)); Assert.IsTrue(a.Equals(b)); }
public static double AlignToEdgeRotation(Ngon target, int edge_start) { edge_start %= target.Count; int next_pt = (edge_start + 1) % target.Count; IntPoint best_edge = new IntPoint(target[next_pt].X - target[edge_start].X, target[next_pt].Y - target[edge_start].Y); return(-Math.Atan2(best_edge.Y, best_edge.X)); }
public VisualNgon(Ngon ngon) { this.ngonModel = ngon; foreach (Vertex vertex in ngonModel.Verticies) { Point point = new Point(vertex.coordX, vertex.coordY); rawVerticies.Add(point); } }
public static Ngon Clone(this Ngon poly, Mat3x3 T) { Ngon clone = new Ngon(poly.Count); for (int i = 0; i < poly.Count; i++) { clone.Add(T * poly[i]); } return(clone); }
public PluckerAndProjectionMatricesDistribution(long sampleSize, int dimensions) { for (long i = sampleSize; i > 0; i--) { Ngon ngon = Program.generateRandomNgon(dimensions); #region Plucker PluckerMatrix plucker = new PluckerMatrix(ngon); SignMatrix pluckerSign = new SignMatrix(plucker); SignMatrix fullPluckerKey = similarPluckerFullMatrix.Keys.FirstOrDefault(m => m.Equals(pluckerSign)); if (fullPluckerKey == null) { similarPluckerFullMatrix.Add(pluckerSign, new List <Ngon>()); fullPluckerKey = pluckerSign; } similarPluckerFullMatrix[fullPluckerKey].Add(ngon); ReducedSignMatrix reducedPluckerKey = similarReducedMatrix.Keys.FirstOrDefault(m => m.Equals(pluckerSign.getReduced())); if (reducedPluckerKey == null) { similarReducedMatrix.Add(pluckerSign.getReduced(), new List <Ngon>()); reducedPluckerKey = pluckerSign.getReduced(); } similarReducedMatrix[reducedPluckerKey].Add(ngon); #endregion #region Projection ProjectionMatrix projection = new ProjectionMatrix(ngon); SignMatrix projectionSign = new SignMatrix(projection); SignMatrix fullProjectionKey = similarProjectionFullMatrix.Keys.FirstOrDefault(m => m.Equals(projectionSign)); if (fullProjectionKey == null) { similarProjectionFullMatrix.Add(projectionSign, new List <Ngon>()); fullProjectionKey = projectionSign; } similarProjectionFullMatrix[fullProjectionKey].Add(ngon); #endregion SignMatrix fullMatrixKey = matrixMap.Keys.FirstOrDefault(m => m.Equals(pluckerSign)); if (fullMatrixKey == null) { matrixMap.Add(pluckerSign, new Dictionary <SignMatrix, int>()); fullMatrixKey = pluckerSign; } SignMatrix projectionResult = matrixMap[fullMatrixKey].Keys.FirstOrDefault(m => m.Equals(projectionSign)); if (projectionResult == null) { matrixMap[fullMatrixKey].Add(projectionSign, 0); projectionResult = projectionSign; } matrixMap[fullMatrixKey][projectionResult]++; if (i % (sampleSize / 100) == 0) { Console.Write("\rCreating sign matrix table: {0}% ", i / (sampleSize / 100)); } } }
public void getEdgeVectorsTest() { double[][] edgevectors = new double[][] { new double[] { 0, 2 }, new double[] { 4, -2 }, new double[] { 0, 2 }, new double[] { -4, -2 } }; Ngon ngon = new Ngon(edgevectors); compareArrays(ngon.getEdgeVectors(), edgevectors); }
public static Ngon Clone(this Ngon poly, long shift_x, long shift_y, bool flip_first = false) { long scale = flip_first ? -1 : 1; Ngon clone = new Ngon(poly.Count); for (int i = 0; i < poly.Count; i++) { clone.Add(new IntPoint(scale * poly[i].X + shift_x, scale * poly[i].Y + shift_y)); } return(clone); }
public void PluckerMatrixTest() { Ngon n = Program.generateRandomNgon(10); PluckerMatrix D = new PluckerMatrix(n); for (int i = 0; i < D.columnVectors.Count(); i++) { for (int j = 0; j < D.columnVectors[i].Count(); j++) { Assert.AreEqual(-D.columnVectors[i][j], D.columnVectors[j][i]); } } }
public void ProjectionMatrixTest() { Ngon n = Program.generateRandomNgon(10); double[][] P = new ProjectionMatrix(n).columnVectors; for (int i = 0; i < P.Count(); i++) { for (int j = 0; j < P[i].Count(); j++) { Assert.AreEqual(P[i][j], P[j][i]); } } }
/// <summary> /// Get the optimal quality for tradeoff between speed and precision of NFP /// </summary> /// <param name="subj_handle"></param> /// <param name="pattern_handle"></param> /// <returns></returns> private NFPQUALITY GetNFPQuality(int subj_handle, int pattern_handle, double max_area_bounds) { Ngon S = polygon_lib[subj_handle].GetTransformedPoly()[0]; Ngon P = polygon_lib[pattern_handle].GetTransformedPoly()[0]; if (GeomUtility.AlmostRectangle(S) && GeomUtility.AlmostRectangle(P)) { return(NFPQUALITY.Simple); } double s_A = GeomUtility.GetBounds(S).Area(); double p_A = GeomUtility.GetBounds(P).Area(); if (p_A / s_A > 1000) { return(NFPQUALITY.Simple); } if (s_A / max_area_bounds < 0.05) { return(NFPQUALITY.Simple); } if (p_A / s_A > 100) { return(NFPQUALITY.Convex); } if (p_A / s_A > 50) { return(NFPQUALITY.ConcaveLight); } if (p_A / s_A > 10) { return(NFPQUALITY.ConcaveMedium); } if (p_A / s_A > 2) { return(NFPQUALITY.ConcaveHigh); } if (p_A / s_A > 0.25) { return(NFPQUALITY.ConcaveFull); } return(NFPQUALITY.Full); }
public PluckerMatrix PluckerMatrixTest(Ngon ngon) { PluckerMatrix D = new PluckerMatrix(ngon); for (int j = 0; j < ngon.getOrthonormal().Length; j++) { for (int i = 0; i < ngon.getOrthonormal()[j].Length; i++) { //double fromEdgeVectors = ngon.getOrthonormal()[i][0] * ngon.getOrthonormal()[j][1] - ngon.getOrthonormal()[j][0] * ngon.getOrthonormal()[i][1]; //Assert.AreEqual(fromEdgeVectors, D.columnVectors[i][j]); } } return(D); }
public ProjectionMatrix ProjectionMatrixTest(Ngon ngon) { ProjectionMatrix P = new ProjectionMatrix(ngon); for (int j = 0; j < ngon.getOrthonormal().Length; j++) { for (int i = 0; i < ngon.getOrthonormal()[j].Length; i++) { //double fromEdgeVectors = ngon.getOrthonormal()[i][0] * ngon.getOrthonormal()[j][0] + ngon.getOrthonormal()[i][1] * ngon.getOrthonormal()[j][1]; //Assert.AreEqual(fromEdgeVectors, P.columnVectors[i][j]); } } return(P); }
public Ngons GetTransformedPoly() { Ngons n = new Ngons(poly.Count); for (int i = 0; i < poly.Count; i++) { Ngon nn = new Ngon(poly[i].Count); for (int j = 0; j < poly[i].Count; j++) { nn.Add(GetTransformedPoint(i, j)); } n.Add(nn); } return(n); }
public int AddCanvasFitPolygon(IntRect canvas, int pattern_handle) { Ngon B = polygon_lib[pattern_handle].GetTransformedPoly()[0]; Ngon C = GeomUtility.CanFitInsidePolygon(canvas, B); polygon_lib.Add(new PolyRef() { poly = new Ngons() { C }, trans = Mat3x3.Eye() }); return(polygon_lib.Count - 1); }
private void but_randomNgon_Click(object sender, RoutedEventArgs e) { frame.Children.Clear(); int dimensions = Int32.Parse(text_dimensions.Text); currentNgon = Program.generateRandomNgon(dimensions); but_ngonPermutations.IsEnabled = true; VisualNgon n = new VisualNgon(currentNgon); lab_PluckerMatrix.Content = (new SignMatrix(new PluckerMatrix(currentNgon))).ToString(); n.draw(frame); displayAngleSum(currentNgon); displayType(currentNgon); }
public void angleBetweenEdgesDegreesTest() { Edge a = new Edge(new Vertex(0, 0), new double[] { 0, 1 }); Edge b = new Edge(new Vertex(0, 1), new double[] { 1, 0 }); Ngon ngon = new Ngon(new double[][] { new double[] { 0, 1 }, new double[] { 0, -1 }, }); Assert.AreEqual(ngon.angleBetweenEdgesDegrees(a, b), 90, 0.0001); a = new Edge(new Vertex(0, 0), new double[] { 1, 1 }); b = new Edge(new Vertex(1, 1), new double[] { 2, 2 }); Assert.AreEqual(ngon.angleBetweenEdgesDegrees(a, b), 180, 0.0001); a = new Edge(new Vertex(0, 0), new double[] { 0, 3 }); b = new Edge(new Vertex(0, 3), new double[] { 3, -3 }); Assert.AreEqual(ngon.angleBetweenEdgesDegrees(a, b), 45, 0.0001); }
public void NgonTest() { double[][] edgeVectors = new double[][] { new double[] { 0, 4 }, new double[] { 4, 0 }, new double[] { 0, -4 }, new double[] { -4, 0 }, }; Ngon ngon = new Ngon(edgeVectors); CollectionAssert.AreEquivalent(ngon.Verticies, new Vertex[] { new Vertex(0, 4), new Vertex(4, 4), new Vertex(4, 0), new Vertex(0, 0) }); foreach (Edge edge in ngon.Edges) { Assert.AreEqual(edge.length, 4, 0.001); } }
public void PluckerAndProjectionMatrixEquality() { Ngon ngon = Program.generateRandomNgon(4); PluckerMatrix plucker = PluckerMatrixTest(ngon); ProjectionMatrix projection = ProjectionMatrixTest(ngon); Matrix <double> D = Matrix <double> .Build.DenseOfColumnArrays(plucker.columnVectors); string d = D.ToString(); Matrix <double> Dnsq = (D.Multiply(D)).Negate(); string dnsq = Dnsq.ToString(); Matrix <double> P = Matrix <double> .Build.DenseOfColumnArrays(projection.columnVectors); string p = P.ToString(); compareMatricies(Dnsq, P); }
public void TriangleTest() { long sampleSize = 1000000; long areObtuse = 0; for (long i = sampleSize; i > 0; i--) { Ngon ngon = Program.generateRandomNgon(3); Triangle t = new Triangle(ngon); if (t.isObtuse) { areObtuse++; } } double percentObtuse = (double)areObtuse / sampleSize * 100; Assert.AreEqual(percentObtuse, 83.8093, 0.1); }