Exemple #1
0
        public void CI_InsertPairTest_04()
        {
            string testObject = "InsertPair";

            // Prepare data structures used in the test.
            int width = 1000, height = 1;

            List <int>[] objectXs, contourXs, borderXs;
            SWA_Ariadne_Gui_Mazes_ContourImageAccessor.InitializeScanLines(width, height, out objectXs, out contourXs, out borderXs);
            List <int> borderX = borderXs[0];

            SWA_Ariadne_Gui_Mazes_ContourImageAccessor.InsertPair(borderX, 100, 300);
            SWA_Ariadne_Gui_Mazes_ContourImageAccessor.InsertPair(borderX, 500, 700);
            SWA_Ariadne_Gui_Mazes_ContourImageAccessor.InsertPair(borderX, 700, 900);
            SWA_Ariadne_Gui_Mazes_ContourImageAccessor.InsertPair(borderX, 300, 500);

            SWA_Ariadne_Gui_Mazes_ContourImageAccessor.InsertPair(borderX, 100, 900);
            SWA_Ariadne_Gui_Mazes_ContourImageAccessor.InsertPair(borderX, 400, 600);
            SWA_Ariadne_Gui_Mazes_ContourImageAccessor.InsertPair(borderX, 600, 800);
            SWA_Ariadne_Gui_Mazes_ContourImageAccessor.InsertPair(borderX, 400, 800);
            SWA_Ariadne_Gui_Mazes_ContourImageAccessor.InsertPair(borderX, 550, 850);

            int[] expectedPoints = { 100, 900 };
            TestInsertPair(testObject, borderXs, expectedPoints);
        }
Exemple #2
0
        private static void TestNoInfluence(string testObject, int nbL, int nbR, int x, int y)
        {
            testObject += string.Format("<{0},{1}>: [{2},{3}]", nbL, nbR, x, y);
            int d2 = SWA_Ariadne_Gui_Mazes_ContourImageAccessor.InfluenceD2(nbL, nbR, x, y);

            Assert.IsFalse(d2 < int.MaxValue, testObject + " should not be influence");
        }
Exemple #3
0
        public void CI_PrepareInfluenceRegionsTest_05_BorderLimits()
        {
            string testObject     = "borderLimits";
            int    radius         = SWA_Ariadne_Gui_Mazes_ContourImageAccessor.ContourDistance;
            int    influenceRange = radius + 1;

            SWA_Ariadne_Gui_Mazes_ContourImageAccessor.PrepareInfluenceRegions(influenceRange);

            // Verify that an application of the borderLimits gives the same scan lines as applying leftBorderLimits and rightBorderLimits.
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    #region Prepare required data structures.

                    int          width = 2 * influenceRange + 1, height = 2 * influenceRange + 1;
                    int          x = (width + 1) / 2, y = (height + 1) / 2;
                    List <int>[] objectXs, contourXs, borderXs;
                    List <int>[] objectXsLR, contourXsLR, borderXsLR;
                    SWA_Ariadne_Gui_Mazes_ContourImageAccessor.InitializeScanLines(width, height, out objectXs, out contourXs, out borderXs);
                    SWA_Ariadne_Gui_Mazes_ContourImageAccessor.InitializeScanLines(width, height, out objectXsLR, out contourXsLR, out borderXsLR);

                    #endregion

                    // Apply the (single) list to borderXs.
                    List <ContourImage.RelativePoint> list = SWA_Ariadne_Gui_Mazes_ContourImageAccessor.borderLimits[i, j];
                    foreach (ContourImage.RelativePoint rp in list)
                    {
                        SWA_Ariadne_Gui_Mazes_ContourImageAccessor.InsertPair(borderXs[y + rp.ry], x - rp.rx, x + rp.rx);
                    }

                    // Apply the left and right lists to borderXsLR.
                    list = SWA_Ariadne_Gui_Mazes_ContourImageAccessor.leftBorderLimits[i, j];
                    foreach (ContourImage.RelativePoint rp in list)
                    {
                        SWA_Ariadne_Gui_Mazes_ContourImageAccessor.InsertPair(borderXsLR[y + rp.ry], x + rp.rx, x - rp.rx);
                    }
                    list = SWA_Ariadne_Gui_Mazes_ContourImageAccessor.rightBorderLimits[i, j];
                    foreach (ContourImage.RelativePoint rp in list)
                    {
                        SWA_Ariadne_Gui_Mazes_ContourImageAccessor.InsertPair(borderXsLR[y + rp.ry], x - rp.rx, x + rp.rx);
                    }

                    // Compare the resulting scan lines.
                    for (y = 0; y < height; y++)
                    {
                        string     testLine = testObject + string.Format("[{0},{1}][{2}]", i, j, y);
                        List <int> expected = borderXsLR[y], actual = borderXs[y];
                        Assert.AreEqual(expected.Count, actual.Count, testLine + " different count");

                        for (int p = 0; p < expected.Count; p++)
                        {
                            Assert.AreEqual(expected[p], actual[p], testLine + " different entry");
                        }
                    }
                }
            }
        }
Exemple #4
0
        private static void TestInfluence(string testObject, int nbL, int nbR, int x, int y, int btExpected)
        {
            testObject += string.Format("<{0},{1}>: [{2},{3}]", nbL, nbR, x, y);
            int d2 = SWA_Ariadne_Gui_Mazes_ContourImageAccessor.InfluenceD2(nbL, nbR, x, y);

            Assert.IsTrue(d2 < int.MaxValue, testObject + " should be influenced");
            Assert.AreEqual(x * x + y * y, d2, testObject + " is not correct");

#if false
            int btActual = SWA_Ariadne_Gui_Mazes_ContourImageAccessor.BorderLimitType(nbL, nbR, x, y);
            Assert.AreEqual(btExpected, btActual, testObject + " has the wrong border type");
#endif
        }
Exemple #5
0
        public void CI_EliminateInsideRegionsTest_03()
        {
            string testObject = "EliminateInsideRegions";

            int width = 11, height = 10, sy = 1, y0 = sy - 1;

            List <int>[] objectXs, contourXs, borderXs;
            SWA_Ariadne_Gui_Mazes_ContourImageAccessor.InitializeScanLines(width, height, out objectXs, out contourXs, out borderXs);

            int y = y0;

            // nothing

            y += sy;
            SWA_Ariadne_Gui_Mazes_ContourImageAccessor.InsertPair(borderXs[y], 4, 6);

            y += sy;
            SWA_Ariadne_Gui_Mazes_ContourImageAccessor.InsertPair(borderXs[y], 2, 7);

            y += sy;
            SWA_Ariadne_Gui_Mazes_ContourImageAccessor.InsertPair(borderXs[y], 1, 2);
            SWA_Ariadne_Gui_Mazes_ContourImageAccessor.InsertPair(borderXs[y], 4, 5);
            SWA_Ariadne_Gui_Mazes_ContourImageAccessor.InsertPair(borderXs[y], 7, 8);

            y += sy;
            SWA_Ariadne_Gui_Mazes_ContourImageAccessor.InsertPair(borderXs[y], 1, 2);
            SWA_Ariadne_Gui_Mazes_ContourImageAccessor.InsertPair(borderXs[y], 4, 5);
            SWA_Ariadne_Gui_Mazes_ContourImageAccessor.InsertPair(borderXs[y], 8, 9);

            y += sy;
            SWA_Ariadne_Gui_Mazes_ContourImageAccessor.InsertPair(borderXs[y], 1, 2);
            SWA_Ariadne_Gui_Mazes_ContourImageAccessor.InsertPair(borderXs[y], 4, 4);
            SWA_Ariadne_Gui_Mazes_ContourImageAccessor.InsertPair(borderXs[y], 7, 8);

            y += sy;
            SWA_Ariadne_Gui_Mazes_ContourImageAccessor.InsertPair(borderXs[y], 2, 7);

            int insideRegionsExpected = 2;
            int insideRegionsActual   = SWA_Ariadne_Gui_Mazes_ContourImageAccessor.EliminateInsideRegions(borderXs, y0, sy);

            Assert.AreEqual(insideRegionsExpected, insideRegionsActual, testObject + " returned wrong number of inside regions");
        }
Exemple #6
0
        public void CI_PrepareInfluenceRegionsTest_02()
        {
            string testObject     = "Distance from object pixel";
            int    radius         = SWA_Ariadne_Gui_Mazes_ContourImageAccessor.ContourDistance;
            int    influenceRange = radius + 1;

            SWA_Ariadne_Gui_Mazes_ContourImageAccessor.PrepareInfluenceRegions(influenceRange);

            int dyN = SWA_Ariadne_Gui_Mazes_ContourImageAccessor.NbDY[SWA_Ariadne_Gui_Mazes_ContourImageAccessor.NbN];
            int dyS = -dyN;
            int dxW = SWA_Ariadne_Gui_Mazes_ContourImageAccessor.NbDX[SWA_Ariadne_Gui_Mazes_ContourImageAccessor.NbW];
            int dxE = -dxW;

            int nbL = SWA_Ariadne_Gui_Mazes_ContourImageAccessor.NbW;
            int nbR = SWA_Ariadne_Gui_Mazes_ContourImageAccessor.NbE;

            TestInfluence(testObject, nbL, nbR, 0, radius * dyN, 0);    // N
            TestNoInfluence(testObject, nbL, nbR, dxW, radius * dyN);   // N + 1px W
            TestNoInfluence(testObject, nbL, nbR, dxE, radius * dyN);   // N + 1px E
        }
Exemple #7
0
        public void CI_PrepareInfluenceRegionsTest_04_ContourLimits()
        {
            string testObject     = "contourLimits";
            int    radius         = SWA_Ariadne_Gui_Mazes_ContourImageAccessor.ContourDistance;
            int    influenceRange = radius + 1;

            SWA_Ariadne_Gui_Mazes_ContourImageAccessor.PrepareInfluenceRegions(influenceRange);

            // Verify that rx is always >= 0.
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    List <ContourImage.RelativePoint> list = SWA_Ariadne_Gui_Mazes_ContourImageAccessor.contourLimits[i, j];
                    foreach (ContourImage.RelativePoint pt in list)
                    {
                        Assert.IsTrue(pt.rx >= 0, testObject + " rx must be positive");
                    }
                }
            }
        }
Exemple #8
0
        public void CI_InsertPairTest_08_ThickLines()
        {
            string testObject = "ContourImage.ScanObject";

            Color backgroundColor = Color.White;
            Color foregroundColor = Color.Black;
            Brush fgBrush         = new SolidBrush(foregroundColor);
            Pen   fgPen           = new Pen(fgBrush, 8);

            fgPen.StartCap = fgPen.EndCap = System.Drawing.Drawing2D.LineCap.Round;

            // When painting into the image, leave an outer frame of 16 pixels free.
            // Only use the range 50..250.
            int      width = 300, height = 300;
            Bitmap   image = new Bitmap(width, height);
            Graphics g     = Graphics.FromImage(image);

            int contourDist = SWA_Ariadne_Gui_Mazes_ContourImageAccessor.ContourDistance;
            int blurDist    = SWA_Ariadne_Gui_Mazes_ContourImageAccessor.BlurDistanceMax;

            SWA_Ariadne_Gui_Mazes_ContourImageAccessor.PrepareInfluenceRegions(contourDist + blurDist);

            int[,] alpha = new int[width, height];
            List <int>[] objectXs, contourXs, borderXs;
            SWA_Ariadne_Gui_Mazes_ContourImageAccessor.InitializeScanLines(width, height, out objectXs, out contourXs, out borderXs);

            #region Create a complicated pattern of border scan lines.

            Point[] points =
            {
                new Point(50,   50), new Point(250, 250),
                new Point(200, 100), new Point(100, 200),
            };

            g.FillRectangle(new SolidBrush(backgroundColor), 0, 0, image.Width, image.Height);

            for (int i = 0, j = i + 1; j < points.Length; i++, j++)
            {
                g.DrawLine(fgPen, points[i], points[j]);
            }

            #endregion

            ContourImage target = new ContourImage(image);
            SWA_Ariadne_Gui_Mazes_ContourImageAccessor accessor = new SWA_Ariadne_Gui_Mazes_ContourImageAccessor(target);
            accessor.image = image;

            int y0 = image.Height / 2;
            #region Find the leftmost object pixel on the scan line at y0.

            int x0        = 0;
            int fuzziness = (int)(0.1F * SWA_Ariadne_Gui_Mazes_ContourImageAccessor.MaxColorDistance);
            while (accessor.ColorDistance(image.GetPixel(x0, y0)) <= fuzziness)
            {
                x0++;
            }

            #endregion

            accessor.ScanObject(x0, y0, fuzziness, alpha, objectXs, contourXs, borderXs);

            TestBorderScanlines(testObject, borderXs);
        }
Exemple #9
0
 private static void InsertBorderLR(List <int> borderX, List <bool> borderLR, int left, int right)
 {
     SWA_Ariadne_Gui_Mazes_ContourImageAccessor.InsertPair(borderX, left, right);
 }
Exemple #10
0
        private static void TestScan(string testObject, Bitmap image, Color backgroundColor, int y0, int maxContourScanRegions)
        {
            testObject += " @ " + y0.ToString();
            int fuzziness = (int)(0.1F * SWA_Ariadne_Gui_Mazes_ContourImageAccessor.MaxColorDistance);

            int frameWidth = SWA_Ariadne_Gui_Mazes_ContourImageAccessor.ContourDistance;

            SWA_Ariadne_Gui_Mazes_ContourImageAccessor.PrepareInfluenceRegions(frameWidth);

            ContourImage target = new ContourImage(image);
            SWA_Ariadne_Gui_Mazes_ContourImageAccessor accessor = new SWA_Ariadne_Gui_Mazes_ContourImageAccessor(target);

            accessor.image = image;

            #region Find the leftmost object pixel on the scan line at y0.

            int x0 = 0;
            while (accessor.ColorDistance(image.GetPixel(x0, y0)) <= fuzziness)
            {
                x0++;
                if (x0 >= image.Width)
                {
                    return;
                }
            }

            #endregion

            #region Prepare required data structures.

            int width = image.Width, height = image.Height;
            int[,] alpha = new int[width, height];
            List <int>[] objectXs, contourXs, borderXs;
            SWA_Ariadne_Gui_Mazes_ContourImageAccessor.InitializeScanLines(width, height, out objectXs, out contourXs, out borderXs);

            #endregion

            accessor.ScanObject(x0, y0, fuzziness, alpha, objectXs, contourXs, borderXs);

            #region Test if the object map is well formed.
            for (int i = 0; i < height; i++)
            {
                int nEntries = objectXs[i].Count;
                int m        = nEntries % 2;
                Assert.AreEqual(1, m, testObject + string.Format(" - objectXs[{0}] must be an odd number: {1}", i, nEntries));

                int nRegions = (nEntries - 1) / 2;
                Assert.IsTrue(nRegions <= maxContourScanRegions, testObject + string.Format(" - objectXs[{0}] regions = {1} must be less than {2}", i, nRegions, maxContourScanRegions));
            }
            #endregion

            #region Test if the object map is complete.
            int imageArea  = ImageArea(image, backgroundColor, (float)fuzziness / SWA_Ariadne_Gui_Mazes_ContourImageAccessor.MaxColorDistance);
            int objectArea = ObjectArea(objectXs);
            Assert.AreEqual(imageArea, objectArea, testObject + string.Format(" - object area and image area must be equal"));
            #endregion

            // Test if the contour map is well formed.
            TestBorderScanlines(testObject + " - contourXs", contourXs);

            // Test if the border map is well formed.
            TestBorderScanlines(testObject + " - borderXs", borderXs);
        }