Ejemplo n.º 1
0
        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));
                }
            }
        }
Ejemplo n.º 2
0
        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);
        }
Ejemplo n.º 3
0
        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);
        }
Ejemplo n.º 4
0
        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));
                }
            }
        }
Ejemplo n.º 5
0
        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
            });
        }
Ejemplo n.º 6
0
        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);
        }
Ejemplo n.º 7
0
        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);
            }
        }
Ejemplo n.º 8
0
        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);
        }
Ejemplo n.º 9
0
        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);
        }
Ejemplo n.º 11
0
 public NgonEdgePermutation(Ngon ngon)
 {
     this.modelNgon    = ngon;
     this.permutations = (new NgonEdgePermutations(ngon)).edgePermutations();
     InitializeComponent();
     updateCollection();
 }
Ejemplo n.º 12
0
        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));
        }
Ejemplo n.º 13
0
        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));
        }
Ejemplo n.º 14
0
 public VisualNgon(Ngon ngon)
 {
     this.ngonModel = ngon;
     foreach (Vertex vertex in ngonModel.Verticies)
     {
         Point point = new Point(vertex.coordX, vertex.coordY);
         rawVerticies.Add(point);
     }
 }
Ejemplo n.º 15
0
        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);
        }
Ejemplo n.º 16
0
        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));
                }
            }
        }
Ejemplo n.º 17
0
        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);
        }
Ejemplo n.º 18
0
        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);
        }
Ejemplo n.º 19
0
        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]);
                }
            }
        }
Ejemplo n.º 20
0
        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]);
                }
            }
        }
Ejemplo n.º 21
0
        /// <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);
        }
Ejemplo n.º 22
0
        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);
        }
Ejemplo n.º 23
0
        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);
        }
Ejemplo n.º 24
0
            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);
            }
Ejemplo n.º 25
0
        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);
        }
Ejemplo n.º 26
0
        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);
        }
Ejemplo n.º 27
0
        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);
        }
Ejemplo n.º 28
0
        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);
            }
        }
Ejemplo n.º 29
0
        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);
        }
Ejemplo n.º 30
0
        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);
        }