Esempio n. 1
0
 /// <summary>
 /// creates a circle shape
 /// </summary>
 /// <param name="centre_x">
 /// centre x coordinate of the circle <see cref="System.Single"/>
 /// </param>
 /// <param name="centre_y">
 /// centre y coordinate of the circle <see cref="System.Single"/>
 /// </param>
 /// <param name="radius">
 /// radius of teh circle <see cref="System.Single"/>
 /// </param>
 /// <param name="circumference_steps">
 /// number of steps to use when drawing the circle <see cref="System.Int32"/>
 /// </param>
 /// <returns>
 /// polygon representing a circle <see cref="polygon2D"/>
 /// </returns>
 public static polygon2D CreateCircle(float centre_x, float centre_y,
                                      float radius, int circumference_steps)
 {
     polygon2D circle = new polygon2D();
     for (int i = 0; i < circumference_steps; i++)
     {
         float angle = i * (float)Math.PI * 2 / circumference_steps;
         float x = centre_x + (radius * (float)Math.Sin(angle));
         float y = centre_y + (radius * (float)Math.Cos(angle));
         circle.Add(x, y);
     }
     return(circle);
 }
Esempio n. 2
0
        /// <summary>
        /// returns a copy of the polygon
        /// </summary>
        /// <returns></returns>
        public polygon2D Copy()
        {
            polygon2D new_poly = new polygon2D();
            new_poly.name = name;
            new_poly.type = type;
            new_poly.occupied = occupied;

            if (x_points != null)
            {
                for (int i = 0; i < x_points.Count; i++)
                {
                    float x = x_points[i];
                    float y = y_points[i];
                    new_poly.Add(x, y);
                }
            }
            return (new_poly);
        }
Esempio n. 3
0
        /// <summary>
        /// locates the centre position of a spot
        /// </summary>
        /// <param name="raw_image_mono">mono image data</param>
        /// <param name="raw_image_width">image width</param>
        /// <param name="raw_image_height">image height</param>
        /// <param name="search_region">region within which to search</param>
        /// <param name="black_on_white">whether this image contains dark markings on a lighter background</param>
        /// <param name="centre_x">returned x centre position</param>
        /// <param name="centre_y">returned y centre position</param>
        private static unsafe void LocateSpotCentre(byte* raw_image_mono,
                                                    int raw_image_width, int raw_image_height,
                                                    polygon2D search_region,
                                                    bool black_on_white,
                                                    ref float centre_x, ref float centre_y)
        {
            centre_x = 0;
            centre_y = 0;

            // get dimensions of the region to be searched
            int tx = (int)search_region.left();
            int ty = (int)search_region.top();
            int bx = (int)search_region.right();
            int by = (int)search_region.bottom();

            int r = (bx - tx) / 3;
            if (r < 1) r = 1;
            int search_r = (bx - tx) / 4;
            if (search_r < 1) search_r = 1;

            // centre of the search region
            float search_centre_x = 0;
            float search_centre_y = 0;
            search_region.GetSquareCentre(ref search_centre_x, ref search_centre_y);
            //search_region.getCentreOfGravity(ref search_centre_x, ref search_centre_y);

            int image_pixels = raw_image_width * raw_image_height;
            float max = 0;
            float v;
            for (float offset_y = search_centre_y - search_r; offset_y <= search_centre_y + search_r; offset_y += 0.5f)
            {
                for (float offset_x = search_centre_x - search_r; offset_x <= search_centre_x + search_r; offset_x += 0.5f)
                {
                    float tot = 0;
                    for (int yy = (int)(offset_y - r); yy <= (int)(offset_y + r); yy += 2)
                    {
                        int n0 = yy * raw_image_width;
                        for (int xx = (int)(offset_x - r); xx <= (int)(offset_x + r); xx += 2)
                        {
                            int n1 = n0 + xx;
                            if ((n1 > -1) && (n1 < image_pixels))
                            {
                                if (black_on_white)
                                    v = 255 - raw_image_mono[n1];
                                else
                                    v = raw_image_mono[n1];
                                tot += v * v;
                            }
                        }
                    }
                    if (tot > max)
                    {
                        max = tot;
                        centre_x = offset_x;
                        centre_y = offset_y;
                    }
                }
            }
        }
Esempio n. 4
0
        /// <summary>
        /// initialise the grid
        /// </summary>
        /// <param name="dimension_x"></param>
        /// <param name="dimension_y"></param>
        /// <param name="perimeter"></param>
        /// <param name="border_cells"></param>
        /// <param name="simple_orientation"></param>
        public void init(int dimension_x, int dimension_y,
                         polygon2D perimeter, int border_cells, 
                         bool simple_orientation)
        {
            this.border_cells = border_cells;
            this.perimeter = perimeter;

            cell = new grid2Dcell[dimension_x][];
            for (int x = 0; x < dimension_x; x++)
                cell[x] = new grid2Dcell[dimension_y];

            line = new ArrayList[2];

            float length1, length2, length3, length4;
            int index = 0;
            for (int i = 0; i < 2; i++)
            {
                line[i] = new ArrayList();

                int idx1 = index + i;
                if (idx1 >= 4) idx1 -= 4;
                int idx2 = index + i + 1;
                if (idx2 >= 4) idx2 -= 4;
                float x0 = (float)perimeter.x_points[idx1];
                float y0 = (float)perimeter.y_points[idx1];
                float x1 = (float)perimeter.x_points[idx2];
                float y1 = (float)perimeter.y_points[idx2];
                length1 = perimeter.getSideLength(idx1);

                int next_idx1 = idx1 + 1;
                if (next_idx1 >= 4) next_idx1 -= 4;
                length3 = perimeter.getSideLength(next_idx1);

                float w0 = Math.Abs(x1 - x0);
                float h0 = Math.Abs(y1 - y0);

                int idx3 = index + i + 2;
                if (idx3 >= 4) idx3 -= 4;
                int idx4 = index + i + 3;
                if (idx4 >= 4) idx4 -= 4;
                float x2 = (float)perimeter.x_points[idx3];
                float y2 = (float)perimeter.y_points[idx3];
                float x3 = (float)perimeter.x_points[idx4];
                float y3 = (float)perimeter.y_points[idx4];
                length2 = perimeter.getSideLength(idx3);

                int next_idx3 = next_idx1 + 2;
                if (next_idx3 >= 4) next_idx3 -= 4;
                length4 = perimeter.getSideLength(next_idx3);

                float w1 = Math.Abs(x3 - x2);
                float h1 = Math.Abs(y3 - y2);

                int dimension = dimension_x;
                if (!simple_orientation)
                {
                    if (i > 0)
                        dimension = dimension_y;
                }
                else
                {
                    if (h0 > w0) dimension = dimension_y;
                }

                // how much shorter is one line than the other on the opposite axis?
                float shortening = 0;
                //if (h0 > w0)
                {
                    if (length3 > length4)
                        shortening = (length3 - length4) / length3;
                    else
                        shortening = (length4 - length3) / length4;
                }


                for (int j = -border_cells; j <= dimension + border_cells; j++)
                {
                    // locate the position along the first line
                    float xx0, yy0;  // position along the first line
                    
                    float fraction = j / (float)dimension;

                    // modify for foreshortening
                    //if ((h0 > w0) && (shortening > 0))
                    if (shortening > 0)
                    {
                        fraction = (fraction * (1.0f - shortening)) + ((float)Math.Sin(fraction * (float)Math.PI / 2) * shortening);
                        if (length3 > length4) fraction = 1.0f - fraction;
                    }

                    if (w0 > h0)
                    {
                        float grad = (y1 - y0) / (x1 - x0);
                        if (x1 > x0)
                            xx0 = x0 + (w0 * fraction);
                        else
                            xx0 = x0 - (w0 * fraction);
                        yy0 = y0 + ((xx0 - x0) * grad);
                    }
                    else
                    {
                        float grad = (x1 - x0) / (y1 - y0);
                        if (y1 > y0)
                            yy0 = y0 + (h0 * fraction);
                        else
                            yy0 = y0 - (h0 * fraction);
                        xx0 = x0 + ((yy0 - y0) * grad);
                    }

                    // locate the position along the second line
                    float xx1, yy1;  // position along the second line

                    if (w1 > h1)
                    {
                        float grad = (y2 - y3) / (x2 - x3);
                        if (x2 > x3)
                            xx1 = x3 + (w1 * fraction);
                        else
                            xx1 = x3 - (w1 * fraction);
                        yy1 = y3 + ((xx1 - x3) * grad);
                    }
                    else
                    {
                        float grad = (x2 - x3) / (y2 - y3);
                        if (y2 > y3)
                            yy1 = y3 + (h1 * fraction);
                        else
                            yy1 = y3 - (h1 * fraction);
                        xx1 = x3 + ((yy1 - y3) * grad);
                    }

                    // add the line to the list
                    line[i].Add(xx0);
                    line[i].Add(yy0);
                    line[i].Add(xx1);
                    line[i].Add(yy1);
                }
            }

            // find interceptions between lines
            poltLineIntercepts();

            // create grid cells
            initialiseCells();
        }
Esempio n. 5
0
        /// <summary>
        /// returns a grey scale histogram for the given image within the given perimeter region
        /// </summary>
        /// <param name="bmp">image data</param>
        /// <param name="wdth">width of the image</param>
        /// <param name="hght">height of the image</param>
        /// <param name="bytes_per_pixel">number of bytes per pixel</param>
        /// <param name="levels">histogram levels</param>
        /// <param name="perimeter">perimeter region inside which to calculate the histogram</param>
        /// <returns></returns>
        public static float[] GetGreyHistogram(byte[] bmp,
                                int wdth, int hght,
                                int bytes_per_pixel,
                                int levels,
                                polygon2D perimeter)
        {
            float[] hist = new float[levels];

            int tx = (int)perimeter.left();
            int ty = (int)perimeter.top();
            int bx = (int)perimeter.right();
            int by = (int)perimeter.bottom();

            for (int y = ty; y <= by; y++)
            {
                if ((y > -1) && (y < hght))
                {
                    for (int x = tx; x <= bx; x++)
                    {
                        if ((x > -1) && (x < wdth))
                        {
                            if (perimeter.isInside(x, y))
                            {
                                int n = ((y * wdth) + x) * bytes_per_pixel;
                                float intensity = 0;
                                for (int col = 0; col < bytes_per_pixel; col++)
                                    intensity += bmp[n + col];
                                intensity /= bytes_per_pixel;

                                int bucket = (int)Math.Round(intensity * levels / 255);
                                if (bucket >= levels) bucket = levels - 1;
                                hist[bucket]++;
                            }
                        }
                    }
                }
            }

            // normalise the histogram
            float max = 1;
            for (int level = 0; level < levels; level++)
                if (hist[level] > max) max = hist[level];

            for (int level = 0; level < levels; level++)
                hist[level] = hist[level] / max;

            return (hist);
        }
Esempio n. 6
0
        /// <summary>
        /// return true if this polygon overlaps with another
        /// </summary>
        /// <param name="other"></param>
        /// <returns></returns>
        public bool overlaps(polygon2D other)
        {
            int i;
            bool retval = false;

            i = 0;
            while ((i < x_points.Count) && (retval == false))
            {
                if (other.isInside(x_points[i],y_points[i])) retval=true;
                i++;
            }

            i = 0;
            while ((i < other.x_points.Count) && (retval == false))
            {
                if (isInside((float)other.x_points[i], (float)other.y_points[i])) retval = true;
                i++;
            }
            return (retval);
Esempio n. 7
0
        /// <summary>
        /// shows the detected centre square
        /// </summary>
        /// <param name="filename">raw image filename</param>
        /// <param name="square">square to be displayed</param>
        /// <param name="output_filename">filename to save as</param>
        private static void ShowSquare(string filename, polygon2D square,
                                       string output_filename)
        {
            Bitmap bmp = (Bitmap)Bitmap.FromFile(filename);
            byte[] img = new byte[bmp.Width * bmp.Height * 3];
            BitmapArrayConversions.updatebitmap(bmp, img);
            square.show(img, bmp.Width, bmp.Height, 0, 255, 0, 0);

            Bitmap output_bmp = new Bitmap(bmp.Width, bmp.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            BitmapArrayConversions.updatebitmap_unsafe(img, output_bmp);
            if (output_filename.ToLower().EndsWith("jpg"))
                output_bmp.Save(output_filename, System.Drawing.Imaging.ImageFormat.Jpeg);
            if (output_filename.ToLower().EndsWith("bmp"))
                output_bmp.Save(output_filename, System.Drawing.Imaging.ImageFormat.Bmp);
        }
Esempio n. 8
0
        /// <summary>
        /// finds the interception points between grid lines
        /// </summary>
        private void poltLineIntercepts()
        {
            if (line != null)
            {
                // create an array to store the line intercepts
                int intercepts_x = line[0].Count/4;
                int intercepts_y = line[1].Count/4;
                line_intercepts = new float[intercepts_x, intercepts_y, 2];

                for (int i = 0; i < line[0].Count; i += 4)
                {
                    // get the first line coordinates
                    ArrayList lines0 = line[0];
                    float x0 = (float)lines0[i];
                    float y0 = (float)lines0[i + 1];
                    float x1 = (float)lines0[i + 2];
                    float y1 = (float)lines0[i + 3];

                    for (int j = 0; j < line[1].Count; j += 4)
                    {
                        // get the second line coordinates
                        ArrayList lines1 = line[1];
                        float x2 = (float)lines1[j];
                        float y2 = (float)lines1[j + 1];
                        float x3 = (float)lines1[j + 2];
                        float y3 = (float)lines1[j + 3];

                        // find the interception between the two lines
                        float ix = 0, iy = 0;
                        geometry.intersection(x0, y0, x1, y1, x2, y2, x3, y3, ref ix, ref iy);

                        // store the intercept position
                        line_intercepts[i/4, j/4, 0] = ix;
                        line_intercepts[i/4, j/4, 1] = iy;
                    }
                }

                // update the perimeter
                border_perimeter = new polygon2D();
                float xx = line_intercepts[0, 0, 0];
                float yy = line_intercepts[0, 0, 1];
                border_perimeter.Add(xx, yy);
                xx = line_intercepts[intercepts_x - 1, 0, 0];
                yy = line_intercepts[intercepts_x - 1, 0, 1];
                border_perimeter.Add(xx, yy);
                xx = line_intercepts[intercepts_x - 1, intercepts_y - 1, 0];
                yy = line_intercepts[intercepts_x - 1, intercepts_y - 1, 1];
                border_perimeter.Add(xx, yy);
                xx = line_intercepts[0, intercepts_y - 1, 0];
                yy = line_intercepts[0, intercepts_y - 1, 1];
                border_perimeter.Add(xx, yy);
            }
        }
Esempio n. 9
0
        /// <summary>
        /// constructor
        /// </summary>
        /// <param name="dimension_x">number of cells across the grid</param>
        /// <param name="dimension_y">number of cells down the grid</param>
        /// <param name="cx">centre of the grid</param>
        /// <param name="cy">centre of the grid</param>
        /// <param name="orientation">orientation of the grid in radians</param>
        /// <param name="average_spacing_x">grid spacing in the x axis</param>
        /// <param name="average_spacing_y">grid spacing in the y axis</param>
        /// <param name="phase_offset_x">phase offset in the x axis</param>
        /// <param name="phase_offset_y">phase offset in the y axis</param>
        /// <param name="border_cells">number of cells to use as a border around the grid</param>
        public grid2D(int dimension_x, int dimension_y,
                      float cx, float cy,
                      float orientation,
                      float average_spacing_x, float average_spacing_y,
                      float phase_offset_x, float phase_offset_y,
                      int border_cells,
                      bool orientation_simple)
        {
            // create a perimeter region
            polygon2D perimeter = new polygon2D();

            float length_x = average_spacing_x * dimension_x;
            float length_y = average_spacing_y * dimension_y;
            float half_length_x = length_x / 2;
            float half_length_y = length_y / 2;

            // adjust for phase
            cx += (average_spacing_x * phase_offset_x / (2 * (float)Math.PI)) * (float)Math.Sin(orientation);
            cy -= (average_spacing_y * phase_offset_y / (2 * (float)Math.PI)) * (float)Math.Cos(orientation);

            // find the mid point of the top line
            float px1 = cx + (half_length_y * (float)Math.Sin(orientation));
            float py1 = cy + (half_length_y * (float)Math.Cos(orientation));

            // find the top left vertex
            float x0 = px1 + (half_length_x * (float)Math.Sin(orientation - (float)(Math.PI / 2)));
            float y0 = py1 + (half_length_x * (float)Math.Cos(orientation - (float)(Math.PI / 2)));

            // find the top right vertex
            float x1 = px1 + (half_length_x * (float)Math.Sin(orientation + (float)(Math.PI / 2)));
            float y1 = py1 + (half_length_x * (float)Math.Cos(orientation + (float)(Math.PI / 2)));

            // find the bottom vertices by mirroring around the centre
            float x2 = cx + (cx - x0);
            float y2 = cy + (cy - y0);
            float x3 = cx - (x1 - cx);
            float y3 = cy - (y1 - cy);

            // update polygon with the perimeter vertices
            perimeter.Add(x0, y0);
            perimeter.Add(x1, y1);
            perimeter.Add(x2, y2);
            perimeter.Add(x3, y3);

            int dim_x = dimension_x;
            int dim_y = dimension_y;
            float first_side_length = perimeter.getSideLength(0);
            float second_side_length = perimeter.getSideLength(1);
            if (((dimension_x > dimension_y + 2) &&
                 (second_side_length > first_side_length)) ||
                 ((dimension_y > dimension_x + 2) &&
                 (first_side_length > second_side_length)))
            {
                dim_x = dimension_y;
                dim_y = dimension_x;
            }

            // initialise using this perimeter
            init(dim_x, dim_y, perimeter, border_cells, orientation_simple);
        }
Esempio n. 10
0
        /// <summary>
        /// scale the polygon to a new image size
        /// </summary>
        /// <param name="original_image_width"></param>
        /// <param name="original_image_height"></param>
        /// <param name="new_image_width"></param>
        /// <param name="new_image_height"></param>
        /// <returns></returns>
        public polygon2D Scale(int original_image_width, int original_image_height,
                               int new_image_width, int new_image_height)
        {
            polygon2D rescaled = new polygon2D();

            for (int i = 0; i < x_points.Count; i++)
            {
                float x = x_points[i] * new_image_width / original_image_width;
                float y = y_points[i] * new_image_height / original_image_height;
                rescaled.Add(x, y);
            }
            return (rescaled);
        }
Esempio n. 11
0
        /// <summary>
        /// return a scaled version of the polygon
        /// </summary>
        /// <param name="factor"></param>
        /// <returns></returns>
        public polygon2D Scale(float factor)
        {
            polygon2D rescaled = new polygon2D();

            float centre_x = 0, centre_y = 0;
            getCentreOfGravity(ref centre_x, ref centre_y);

            for (int i = 0; i < x_points.Count; i++)
            {
                float dx = x_points[i] - centre_x;
                float dy = y_points[i] - centre_y;
                float x = (float)(centre_x + (dx * factor));
                float y = (float)(centre_y + (dy * factor));
                rescaled.Add(x, y);
            }
            return (rescaled);
        }
Esempio n. 12
0
        /// <summary>
        /// snaps the vertices of the polygon to a grid having the given spacing
        /// </summary>
        /// <param name="grid_spacing">spacing of the grid</param>
        /// <returns>fitted polygon</returns>
        public polygon2D SnapToGrid(float grid_spacing)
        {
            polygon2D result = new polygon2D();
            if (x_points != null)
            {
                for (int i = 0; i < x_points.Count; i++)
                {
                    float x = x_points[i] / grid_spacing;
                    x = (float)Math.Round(x) * grid_spacing;
                    float y = y_points[i] / grid_spacing;
                    y = (float)Math.Round(y) * grid_spacing;
                    result.Add(x, y);
                }
            }
            return (result);
Esempio n. 13
0
        /// <summary>
        /// returns the difference in the positions of vertices
        /// compared to another polygon
        /// </summary>
        /// <param name="other">the other polygon</param>
        /// <returns></returns>
        public float Compare(polygon2D other)
        {
            float difference = 0;

            // vertices exist
            if ((x_points != null) && (other.x_points != null))
            {
                // same number of vertices
                if (x_points.Count == other.x_points.Count)
                {
                    for (int i = 0; i < x_points.Count; i++)
                    {
                        float dx = Math.Abs(x_points[i] - other.x_points[i]);
                        float dy = Math.Abs(y_points[i] - other.y_points[i]);
                        difference += dx + dy;
                    }
                }
            }

            return (difference);
Esempio n. 14
0
        /// <summary>
        /// does this polygon overlap with the other, within the given screen dimensions
        /// </summary>
        /// <param name="other">other polygon object</param>
        /// <param name="image_width">image width</param>
        /// <param name="image_height">image height</param>
        /// <returns></returns>
        public bool overlaps(polygon2D other, int image_width, int image_height)
        {
            int i;
            bool retval = false;

            i = 0;
            while ((i < x_points.Count) && (retval == false))
            {
                if (other.isInside(x_points[i] * 1000 / image_width, y_points[i] * 1000 / image_height)) retval = true;
                i++;
            }

            i = 0;
            while ((i < other.x_points.Count) && (retval == false))
            {
                if (isInside((float)other.x_points[i] * image_width / 1000, (float)other.y_points[i] * image_height / 1000)) retval = true;
                i++;
            }
            return (retval);
Esempio n. 15
0
        /// <summary>
        /// simple function used to test grid creation
        /// </summary>
        /// <param name="filename">filename of the test image to be created</param>
        public static void Test(string filename)
        {
            int image_width = 640;
            int image_height = 480;
            int dimension_x = 10;
            int dimension_y = 10;

            Bitmap bmp = new Bitmap(image_width, image_height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
            byte[] img = new byte[image_width * image_height * 3];

            polygon2D test_poly;
            grid2D grid;

            test_poly = new polygon2D();
            test_poly.Add(image_width * 15 / 100, image_height * 20 / 100);
            test_poly.Add(image_width * 40 / 100, image_height * 20 / 100);
            test_poly.Add(image_width * 40 / 100, image_height * 40 / 100);
            test_poly.Add(image_width * 15 / 100, image_height * 40 / 100);
            grid = new grid2D(dimension_x, dimension_y, test_poly, 0, false);
            grid.ShowLines(img, image_width, image_height, 0, 255, 0, 0);

            int half_width = image_width / 2;
            test_poly = new polygon2D();
            test_poly.Add(half_width + (image_width * 20 / 100), image_height * 20 / 100);
            test_poly.Add(half_width + (image_width * 35 / 100), image_height * 20 / 100);
            test_poly.Add(half_width + (image_width * 40 / 100), image_height * 40 / 100);
            test_poly.Add(half_width + (image_width * 15 / 100), image_height * 40 / 100);
            grid = new grid2D(dimension_x, dimension_y, test_poly, 0, false);
            grid.ShowLines(img, image_width, image_height, 0, 255, 0, 0);
            
            int half_height = image_height / 2;
            test_poly = new polygon2D();
            test_poly.Add(image_width * 15 / 100, half_height + (image_height * 24 / 100));
            test_poly.Add(image_width * 40 / 100, half_height + (image_height * 20 / 100));
            test_poly.Add(image_width * 40 / 100, half_height + (image_height * 40 / 100));
            test_poly.Add(image_width * 15 / 100, half_height + (image_height * 36 / 100));
            grid = new grid2D(dimension_x, dimension_y, test_poly, 0, false);
            grid.ShowLines(img, image_width, image_height, 0, 255, 0, 0);

            test_poly = new polygon2D();
            test_poly.Add(half_width + (image_width * 20 / 100), half_height + (image_height * 24 / 100));
            test_poly.Add(half_width + (image_width * 35 / 100), half_height + (image_height * 20 / 100));
            test_poly.Add(half_width + (image_width * 40 / 100), half_height + (image_height * 40 / 100));
            test_poly.Add(half_width + (image_width * 15 / 100), half_height + (image_height * 36 / 100));
            grid = new grid2D(dimension_x, dimension_y, test_poly, 0, false);
            grid.ShowLines(img, image_width, image_height, 0, 255, 0, 0);            

            BitmapArrayConversions.updatebitmap_unsafe(img, bmp);
            bmp.Save(filename);
        }
Esempio n. 16
0
 /// <summary>
 /// constructor
 /// </summary>
 /// <param name="dimension_x">number of cells across the grid</param>
 /// <param name="dimension_y">number of cells down the grid</param>
 /// <param name="perimeter">perimeter of the grid</param>
 /// <param name="border_cells">number of cells to use as a border around the grid</param>
 /// <param name="orientation_simple"></param>
 public grid2D(int dimension_x, int dimension_y,
               polygon2D perimeter, int border_cells,
               bool orientation_simple)
 {
     init(dimension_x, dimension_y, perimeter, border_cells, orientation_simple);
 }
Esempio n. 17
0
        /// <summary>
        /// returns a value indicating how square the given region is
        /// </summary>
        /// <param name="square">
        /// region to be processed <see cref="polygon2D"/>
        /// </param>
        /// <returns>
        /// squareness value <see cref="System.Single"/>
        /// </returns>
        private static float Squareness(polygon2D square)
        {
            float measure1 = square.getSideLength(0) / square.getSideLength(1);
            measure1 = Math.Abs(1.0f - measure1);
            
            float measure2 = square.getSideLength(0) / square.getSideLength(2);
            measure2 = Math.Abs(1.0f - measure2);

            float measure3 = square.getSideLength(1) / square.getSideLength(3);
            measure3 = Math.Abs(1.0f - measure3);
            
            float result = 1.0f / (1.0f + (measure1 + measure2 + measure3));
            return(result);
        }
Esempio n. 18
0
        /// <summary>
        /// detect a grid within the given image using the given perimeter polygon
        /// </summary>
        /// <param name="img">image data</param>
        /// <param name="img_width">width of the image</param>
        /// <param name="img_height">height of the image</param>
        /// <param name="bytes_per_pixel">number of bytes per pixel</param>
        /// <param name="perimeter">bounding perimeter within which the grid exists</param>
        /// <param name="spot_centres">previously detected spot centres</param>
        /// <param name="minimum_dimension_horizontal">minimum number of cells across</param>
        /// <param name="maximum_dimension_horizontal">maximum number of cells across</param>
        /// <param name="minimum_dimension_vertical">minimum number of cells down</param>
        /// <param name="maximum_dimension_vertical">maximum number of cells down</param>
        /// <param name="known_grid_spacing">known grid spacing (cell diameter) value in pixels</param>
        /// <param name="known_even_dimension">set to true if it is known that the number of cells in horizontal and vertical axes is even</param>
        /// <param name="border_cells">extra cells to add as a buffer zone around the grid</param>
        /// <param name="horizontal_scale_spacings">description used on the spacings diagram</param>
        /// <param name="vertical_scale_spacings">description used on the spacings diagram</param>
        /// <param name="average_spacing_horizontal"></param>
        /// <param name="average_spacing_vertical"></param>
        /// <param name="output_img"></param>
        /// <param name="output_img_width"></param>
        /// <param name="output_img_height"></param>
        /// <param name="output_img_type"></param>
        /// <param name="ideal_grid_colour">colour used to draw the ideal grid</param>
        /// <param name="detected_grid_colour">colour used to draw the detected grid</param>
        /// <param name="simple_orientation"></param>
        /// <returns>2D grid</returns>
        public static grid2D DetectGrid(byte[] img, int img_width, int img_height, int bytes_per_pixel,
                                        polygon2D perimeter, ArrayList spot_centres,
                                        int minimum_dimension_horizontal, int maximum_dimension_horizontal,
                                        int minimum_dimension_vertical, int maximum_dimension_vertical,
                                        float known_grid_spacing, bool known_even_dimension,
                                        int border_cells,
                                        String horizontal_scale_spacings, String vertical_scale_spacings,
                                        ref float average_spacing_horizontal, 
                                        ref float average_spacing_vertical,
                                        ref byte[] output_img, int output_img_width, int output_img_height,
                                        int output_img_type,
                                        byte[] ideal_grid_colour, byte[] detected_grid_colour,
                                        bool simple_orientation)
        {
            int tx = (int)perimeter.left();
            int ty = (int)perimeter.top();
            int bx = (int)perimeter.right();
            int by = (int)perimeter.bottom();

            // adjust spot centre positions so that they're relative to the perimeter
            // top left position
            if (spot_centres != null)
            {
                for (int i = 0; i < spot_centres.Count; i += 2)
                {
                    spot_centres[i] = (float)spot_centres[i] - tx;
                    spot_centres[i + 1] = (float)spot_centres[i + 1] - ty;
                }
            }

            int wdth = bx - tx;
            int hght = by - ty;

            // create an image of the grid area
            byte[] grid_img = image.createSubImage(img, img_width, img_height, bytes_per_pixel,
                                                   tx, ty, bx, by);

            // get the orientation of the perimeter
            float dominant_orientation = perimeter.GetSquareOrientation();

            // find the horizontal and vertical dimensions of the grid perimeter
            float grid_horizontal = perimeter.GetSquareHorizontal();
            float grid_vertical = perimeter.GetSquareVertical();

            // detect grid within the perimeter
            int cells_horizontal = 0, cells_vertical = 0;
            float horizontal_phase_offset = 0;
            float vertical_phase_offset = 0;

            if (spot_centres == null)
                DetectGrid(grid_img, wdth, hght, bytes_per_pixel, 
                           dominant_orientation, known_grid_spacing,
                           grid_horizontal, grid_vertical,
                           minimum_dimension_horizontal, maximum_dimension_horizontal,
                           minimum_dimension_vertical, maximum_dimension_vertical,
                           horizontal_scale_spacings, vertical_scale_spacings,
                           ref average_spacing_horizontal, ref average_spacing_vertical,
                           ref horizontal_phase_offset, ref vertical_phase_offset,
                           ref cells_horizontal, ref cells_vertical,
                           ref output_img, output_img_width, output_img_height, output_img_type,
                           ideal_grid_colour, detected_grid_colour);
            else
                DetectGrid(spot_centres, grid_img, wdth, hght, bytes_per_pixel, 
                           dominant_orientation, known_grid_spacing,
                           grid_horizontal, grid_vertical,
                           minimum_dimension_horizontal, maximum_dimension_horizontal,
                           minimum_dimension_vertical, maximum_dimension_vertical,
                           horizontal_scale_spacings, vertical_scale_spacings,
                           ref average_spacing_horizontal, ref average_spacing_vertical,
                           ref horizontal_phase_offset, ref vertical_phase_offset,
                           ref cells_horizontal, ref cells_vertical,
                           ref output_img, output_img_width, output_img_height, output_img_type,
                           ideal_grid_colour, detected_grid_colour);

            
            grid2D detectedGrid = null;

            // apply some range limits
            bool range_limited = false;
            if (cells_horizontal < 3)
            {
                cells_horizontal = 3;
                range_limited = true;
            }
            if (cells_vertical < 3)
            {
                cells_vertical = 3;
                range_limited = true;
            }
            if (cells_horizontal > maximum_dimension_horizontal)
            {
                cells_horizontal = maximum_dimension_horizontal;
                range_limited = true;
            }
            if (cells_vertical > maximum_dimension_vertical)
            {
                cells_vertical = maximum_dimension_vertical;
                range_limited = true;
            }
            if (range_limited)
            {
                Console.WriteLine("WARNING: When detecting the grid the matrix dimension had to be artificially restricted.");
                Console.WriteLine("         This probably means that there is a problem with the original image");
            }
            
            // if we know the number of cells should be even correct any inaccuracies
            if (known_even_dimension)
            {
                cells_horizontal = (int)(cells_horizontal / 2) * 2;
                cells_vertical = (int)(cells_vertical / 2) * 2;
            }

            // get the centre of the region
            float cx = tx + ((bx - tx) / 2);
            float cy = ty + ((by - ty) / 2);
            
            detectedGrid = new grid2D(cells_horizontal, cells_vertical,
                                      cx, cy, dominant_orientation,
                                      average_spacing_horizontal, average_spacing_vertical,
                                      horizontal_phase_offset, vertical_phase_offset,
                                      border_cells, simple_orientation);

            return (detectedGrid);
        }
Esempio n. 19
0
 public static byte[] ShowRawImagePerimeter(byte[] img, int img_width, int img_height,
                                            polygon2D perim,
                                            int r, int g, int b,
                                            int line_width)
 {
     byte[] new_img = (byte[])img.Clone();
     perim.show(new_img, img_width, img_height,
                r, g, b, line_width);
     return(new_img);
 }
Esempio n. 20
0
        /// <summary>
        /// returns an ideally spaced grid over the actual detected spots
        /// </summary>
        /// <param name="grid"></param>
        /// <returns></returns>
        private static grid2D OverlayIdealGrid(calibrationDot[,] grid,
                                               List<calibrationDot> corners,
                                               ref int grid_offset_x, ref int grid_offset_y,
                                               int random_seed)
        {
            grid2D overlay_grid = null;
            int grid_tx = -1;
            int grid_ty = -1;
            int grid_bx = -1;
            int grid_by = -1;

            int offset_x = 0;
            int offset_y = 0;

            bool found = false;
            int max_region_area = 0;

            // try searching horizontally and vertically
            // then pick the result with the greatest area
            for (int test_orientation = 0; test_orientation < 2; test_orientation++)
            {
                bool temp_found = false;

                int temp_grid_tx = -1;
                int temp_grid_ty = -1;
                int temp_grid_bx = -1;
                int temp_grid_by = -1;

                int temp_offset_x = 0;
                int temp_offset_y = 0;

                switch (test_orientation)
                {
                    case 0:
                        {
                            while ((temp_offset_y < 5) && (!temp_found))
                            {
                                temp_offset_x = 0;
                                while ((temp_offset_x < 3) && (!temp_found))
                                {
                                    temp_grid_tx = temp_offset_x;
                                    temp_grid_ty = temp_offset_y;
                                    temp_grid_bx = grid.GetLength(0) - 1 - temp_offset_x;
                                    temp_grid_by = grid.GetLength(1) - 1 - temp_offset_y;

                                    if ((temp_grid_bx < grid.GetLength(0)) &&
                                        (temp_grid_tx < grid.GetLength(0)) &&
                                        (temp_grid_by < grid.GetLength(1)) &&
                                        (temp_grid_ty < grid.GetLength(1)) &&
                                        (temp_grid_ty >= 0) &&
                                        (temp_grid_by >= 0) &&
                                        (temp_grid_tx >= 0) &&
                                        (temp_grid_bx >= 0))
                                    {
                                        if ((grid[temp_grid_tx, temp_grid_ty] != null) &&
                                            (grid[temp_grid_bx, temp_grid_ty] != null) &&
                                            (grid[temp_grid_bx, temp_grid_by] != null) &&
                                            (grid[temp_grid_tx, temp_grid_by] != null))
                                        {
                                            temp_found = true;
                                        }
                                    }

                                    temp_offset_x++;
                                }
                                temp_offset_y++;
                            }
                            break;
                        }
                    case 1:
                        {
                            while ((temp_offset_x < 3) && (!temp_found))
                            {
                                temp_offset_y = 0;
                                while ((temp_offset_y < 5) && (!temp_found))
                                {
                                    temp_grid_tx = temp_offset_x;
                                    temp_grid_ty = temp_offset_y;
                                    temp_grid_bx = grid.GetLength(0) - 1 - temp_offset_x;
                                    temp_grid_by = grid.GetLength(1) - 1 - temp_offset_y;

                                    if ((temp_grid_bx < grid.GetLength(0)) &&
                                        (temp_grid_tx < grid.GetLength(0)) &&
                                        (temp_grid_by < grid.GetLength(1)) &&
                                        (temp_grid_ty < grid.GetLength(1)) &&
                                        (temp_grid_ty >= 0) &&
                                        (temp_grid_by >= 0) &&
                                        (temp_grid_tx >= 0) &&
                                        (temp_grid_bx >= 0))
                                    {
                                        if ((grid[temp_grid_tx, temp_grid_ty] != null) &&
                                            (grid[temp_grid_bx, temp_grid_ty] != null) &&
                                            (grid[temp_grid_bx, temp_grid_by] != null) &&
                                            (grid[temp_grid_tx, temp_grid_by] != null))
                                        {
                                            temp_found = true;
                                        }
                                    }

                                    temp_offset_y++;
                                }
                                temp_offset_x++;
                            }
                            break;
                        }
                }

                temp_offset_y = temp_grid_ty - 1;
                while (temp_offset_y >= 0)
                {
                    if ((temp_offset_y < grid.GetLength(1)) &&
                        (temp_offset_y >= 0))
                    {
                        if ((grid[temp_grid_tx, temp_offset_y] != null) &&
                            (grid[temp_grid_bx, temp_offset_y] != null))
                        {
                            temp_grid_ty = temp_offset_y;
                            temp_offset_y--;
                        }
                        else break;
                    }
                    else break;
                }

                temp_offset_y = temp_grid_by + 1;
                while (temp_offset_y < grid.GetLength(1))
                {
                    if ((temp_offset_y < grid.GetLength(1)) &&
                        (temp_offset_y >= 0))
                    {
                        if ((grid[temp_grid_tx, temp_offset_y] != null) &&
                            (grid[temp_grid_bx, temp_offset_y] != null))
                        {
                            temp_grid_by = temp_offset_y;
                            temp_offset_y++;
                        }
                        else break;
                    }
                    else break;
                }

                if (temp_found)
                {
                    int region_area = (temp_grid_bx - temp_grid_tx) * (temp_grid_by - temp_grid_ty);
                    if (region_area > max_region_area)
                    {
                        max_region_area = region_area;
                        found = true;

                        grid_tx = temp_grid_tx;
                        grid_ty = temp_grid_ty;
                        grid_bx = temp_grid_bx;
                        grid_by = temp_grid_by;

                        offset_x = temp_offset_x;
                        offset_y = temp_offset_y;
                    }
                }
            }

            if (found)
            {
                // record the positions of the corners
                corners.Add(grid[grid_tx, grid_ty]);
                corners.Add(grid[grid_bx, grid_ty]);
                corners.Add(grid[grid_bx, grid_by]);
                corners.Add(grid[grid_tx, grid_by]);

                double dx, dy;

                double x0 = grid[grid_tx, grid_ty].x;
                double y0 = grid[grid_tx, grid_ty].y;
                double x1 = grid[grid_bx, grid_ty].x;
                double y1 = grid[grid_bx, grid_ty].y;
                double x2 = grid[grid_tx, grid_by].x;
                double y2 = grid[grid_tx, grid_by].y;
                double x3 = grid[grid_bx, grid_by].x;
                double y3 = grid[grid_bx, grid_by].y;

                polygon2D perimeter = new polygon2D();
                perimeter.Add((float)x0, (float)y0);
                perimeter.Add((float)x1, (float)y1);
                perimeter.Add((float)x3, (float)y3);
                perimeter.Add((float)x2, (float)y2);

                int grid_width = grid_bx - grid_tx;
                int grid_height = grid_by - grid_ty;

                int min_hits = 0;
                double min_dx = 0, min_dy = 0;

                // try various perimeter sizes
                double min_dist = double.MaxValue;
                int max_perim_size_tries = 100;
                polygon2D best_perimeter = perimeter;
                MersenneTwister rnd = new MersenneTwister(random_seed);
                for (int perim_size = 0; perim_size < max_perim_size_tries; perim_size++)
                {
                    // try a small range of translations
                    for (int nudge_x = -10; nudge_x <= 10; nudge_x++)
                    {
                        for (int nudge_y = -5; nudge_y <= 5; nudge_y++)
                        {
                            // create a perimeter at this scale and translation
                            polygon2D temp_perimeter = perimeter.Scale(1.0f + (perim_size * 0.1f / max_perim_size_tries));
                            temp_perimeter = temp_perimeter.ScaleSideLength(0, 0.95f + ((float)rnd.NextDouble() * 0.1f));
                            temp_perimeter = temp_perimeter.ScaleSideLength(2, 0.95f + ((float)rnd.NextDouble() * 0.1f));
                            for (int i = 0; i < temp_perimeter.x_points.Count; i++)
                            {
                                temp_perimeter.x_points[i] += nudge_x;
                                temp_perimeter.y_points[i] += nudge_y;
                            }

                            // create a grid based upon the perimeter
                            grid2D temp_overlay_grid = new grid2D(grid_width, grid_height, temp_perimeter, 0, false);

                            // how closely does the grid fit the actual observations ?
                            double temp_min_dist = min_dist;
                            BestFit(grid_tx, grid_ty, grid,
                                    temp_overlay_grid, ref min_dist,
                                    ref min_dx, ref min_dy, ref min_hits,
                                    ref grid_offset_x, ref grid_offset_y);

                            // record the closest fit
                            if (temp_min_dist < min_dist)
                            {
                                best_perimeter = temp_perimeter;
                                overlay_grid = temp_overlay_grid;
                            }
                        }
                    }
                }

                if (min_hits > 0)
                {
                    dx = min_dx;
                    dy = min_dy;

                    Console.WriteLine("dx: " + dx.ToString());
                    Console.WriteLine("dy: " + dy.ToString());

                    x0 += dx;
                    y0 += dy;
                    x1 += dx;
                    y1 += dy;
                    x2 += dx;
                    y2 += dy;
                    x3 += dx;
                    y3 += dy;

                    perimeter = new polygon2D();
                    perimeter.Add((float)x0, (float)y0);
                    perimeter.Add((float)x1, (float)y1);
                    perimeter.Add((float)x3, (float)y3);
                    perimeter.Add((float)x2, (float)y2);
                    overlay_grid = new grid2D(grid_width, grid_height, perimeter, 0, false);
                }
            }

            return (overlay_grid);
        }
Esempio n. 21
0
        /// <summary>
        /// returns a polygon representing the periphery of a square region
        /// </summary>
        /// <param name="edges">
        /// edges within the square region <see cref="List`1"/>
        /// </param>
        /// <param name="erode_dilate">
        /// erosion or dilation level <see cref="System.Int32"/>
        /// </param>
        /// <param name="perim">
        /// returned periphery <see cref="polygon2D"/>
        /// </param>
        /// <returns>
        /// set of edges around the periphery <see cref="List`1"/>
        /// </returns>
        private static List<List<int>> GetSquarePeriphery(List<int> edges,
                                                          int erode_dilate,
                                                          ref polygon2D perim)
        {
            List<List<int>> result = new List<List<int>>();
            perim = new polygon2D();
            
            // find the bounding box for all edges
            int tx = 99999;
            int ty = 99999;
            int bx = -99999;
            int by = -99999;            
            for (int i = edges.Count-2; i >= 0; i -= 2)
            {
                int x = edges[i];
                int y = edges[i+1];
                if (x < tx) tx = x;
                if (y < ty) ty = y;
                if (x > bx) bx = x;
                if (y > by) by = y;
            }

            int w = bx - tx;
            int h = by - ty;
            
            if ((w > 0) && (h > 0))
            {                
                int[] left = new int[h+1];
                int[] right = new int[h+1];
                int[] top = new int[w+1];
                int[] bottom = new int[w+1];
                for (int i = edges.Count - 2; i >= 0; i -= 2)
                {
                    int x = edges[i];
                    int x2 = x - tx;
                    int y = edges[i+1];
                    int y2 = y - ty;
                    
                    // left side
                    if ((left[y2] == 0) ||
                        (x < left[y2]))
                        left[y2] = x;

                    // right side
                    if ((right[y2] == 0) ||
                        (x > right[y2]))
                        right[y2] = x;
                    
                    // top
                    if ((top[x2] == 0) ||
                        (y < top[x2]))
                        top[x2] = y;

                    // bottom
                    if ((bottom[x2] == 0) ||
                        (y > bottom[x2]))
                        bottom[x2] = y;
                }

#if SHOW_TIMINGS
                stopwatch timer_best_fit = new stopwatch();
                timer_best_fit.Start();
#endif

                // find a best fit line for the left side
                int best_start = 0;
                int best_end = 0;
                int hits = BestFitLine(left, ref best_start, ref best_end);
                float left_x0 = left[best_start];
                float left_y0 = ty + best_start;
                float left_x1 = left[best_end];
                float left_y1 = ty + best_end;
                /*
                BestFitLineAverage(left, 
                                   ref left_x0, ref left_y0, 
                                   ref left_x1, ref left_y1);
                left_y0 += ty;
                left_y1 += ty;
                */

                // find a best fit line for the right side
                best_start = 0;
                best_end = 0;
                hits = BestFitLine(right, ref best_start, ref best_end);
                float right_x0 = right[best_start];
                float right_y0 = ty + best_start;
                float right_x1 = right[best_end];
                float right_y1 = ty + best_end;

                /*
                BestFitLineAverage(right,
                                   ref right_x0, ref right_y0,
                                   ref right_x1, ref right_y1);
                right_y0 += ty;
                right_y1 += ty;
                 */

                // find a best fit line for the top side
                best_start = 0;
                best_end = 0;
                hits = BestFitLine(top, ref best_start, ref best_end);
                float top_x0 = tx + best_start;
                float top_y0 = top[best_start];
                float top_x1 = tx + best_end;
                float top_y1 = top[best_end];

                /*
                BestFitLineAverage(top,
                                   ref top_x0, ref top_y0,
                                   ref top_x1, ref top_y1);
                top_x0 += tx;
                top_x1 += tx;
                */

                // find a best fit line for the bottom side
                best_start = 0;
                best_end = 0;
                hits = BestFitLine(bottom, ref best_start, ref best_end);
                float bottom_x0 = tx + best_start;
                float bottom_y0 = bottom[best_start];
                float bottom_x1 = tx + best_end;
                float bottom_y1 = bottom[best_end];

                /*
                BestFitLineAverage(bottom,
                                   ref bottom_x0, ref bottom_y0,
                                   ref bottom_x1, ref bottom_y1);
                bottom_x0 += tx;
                bottom_x1 += tx;
                 */

#if SHOW_TIMINGS
                timer_best_fit.Stop();
                if (timer_best_fit.time_elapsed_mS > 20)
                    Console.WriteLine("GetSquarePeriphery: best fit  " + timer_best_fit.time_elapsed_mS.ToString() );
#endif                
                
                // find the intersection between the left side and the top side
                float ix=0;
                float iy = 0;
                geometry.intersection(left_x1, left_y1, left_x0, left_y0,
                                      top_x1, top_y1, top_x0, top_y0,
                                      ref ix, ref iy);
                perim.Add(ix, iy);

                // find the intersection between the right side and the top side
                ix = 0;
                iy = 0;
                geometry.intersection(right_x1, right_y1, right_x0, right_y0,
                                      top_x0, top_y0, top_x1, top_y1,
                                      ref ix, ref iy);
                perim.Add(ix, iy);

                // find the intersection between the right side and the bottom side
                ix = 0;
                iy = 0;
                geometry.intersection(right_x1, right_y1, right_x0, right_y0,
                                      bottom_x0, bottom_y0, bottom_x1, bottom_y1,
                                      ref ix, ref iy);
                perim.Add(ix, iy);

                // find the intersection between the left side and the bottom side
                ix = 0;
                iy = 0;
                geometry.intersection(left_x1, left_y1, left_x0, left_y0,
                                      bottom_x0, bottom_y0, bottom_x1, bottom_y1,
                                      ref ix, ref iy);
                perim.Add(ix, iy);
                                
                // left and right
                List<int> left_edges = new List<int>();
                List<int> right_edges = new List<int>();
                for (int y = h; y >= 0; y--)
                {
                    if (left[y] != 0)
                    {
                        left_edges.Add(left[y]);
                        left_edges.Add(ty + y);
                    }
                    if (right[y] != 0)
                    {
                        right_edges.Add(right[y]);
                        right_edges.Add(ty + y);
                    }
                }
                
                // top and bottom
                List<int> top_edges = new List<int>();
                List<int> bottom_edges = new List<int>();
                for (int x = w; x >= 0; x--)
                {
                    if (top[x] != 0)
                    {
                        top_edges.Add(tx + x);
                        top_edges.Add(top[x]);
                    }
                    if (bottom[x] != 0)
                    {
                        bottom_edges.Add(tx + x);
                        bottom_edges.Add(bottom[x]);
                    }
                }
                
                float aspect_check = perim.getShortestSide() / perim.getLongestSide();
                if (aspect_check > 0.2f)
                {                
                    result.Add(left_edges);
                    result.Add(right_edges);
                    result.Add(top_edges);
                    result.Add(bottom_edges);
                }
                else perim = null;
            }

            // shrink the perimeter according to the erosion/dilation value
            if ((perim != null) && (erode_dilate != 0))
            {
                if (perim.x_points != null)
                {
                    float shrink_percent = (erode_dilate*2) / (perim.getPerimeterLength()/4.0f);
                    perim = perim.Scale(1.0f - shrink_percent);
                }
                else perim = null;
            }
            
            return(result);
        }
Esempio n. 22
0
        /// <summary>
        /// detects a square around the centre dot on the calibration pattern
        /// </summary>
        /// <param name="dots">detected dots</param>
        /// <param name="centredots">returned dots belonging to the centre square</param>
        /// <returns>centre square</returns>
        private static polygon2D GetCentreSquare(hypergraph dots,
                                                 ref List<calibrationDot> centredots)
        {
            centredots = new List<calibrationDot>();

            // find the centre dot
            calibrationDot centre = null;
            int i = 0;
            while ((i < dots.Nodes.Count) && (centre == null))
            {
                calibrationDot dot = (calibrationDot)dots.Nodes[i];
                if (dot.centre) centre = dot;
                i++;
            }

            // look for the four surrounding dots
            List<calibrationDot> centre_dots = new List<calibrationDot>();
            List<double> distances = new List<double>();
            i = 0;
            for (i = 0; i < dots.Nodes.Count; i++)
            {
                calibrationDot dot = (calibrationDot)dots.Nodes[i];
                if (!dot.centre)
                {
                    double dx = dot.x - centre.x;
                    double dy = dot.y - centre.y;
                    double dist = Math.Sqrt(dx * dx + dy * dy);
                    if (distances.Count == 4)
                    {
                        int index = -1;
                        double max_dist = 0;
                        for (int j = 0; j < 4; j++)
                        {
                            if (distances[j] > max_dist)
                            {
                                index = j;
                                max_dist = distances[j];
                            }
                        }
                        if (dist < max_dist)
                        {
                            distances[index] = dist;
                            centre_dots[index] = dot;
                        }
                    }
                    else
                    {
                        distances.Add(dist);
                        centre_dots.Add(dot);
                    }
                }
            }

            polygon2D centre_square = null;
            if (centre_dots.Count == 4)
            {
                centre_square = new polygon2D();
                for (i = 0; i < 4; i++)
                    centre_square.Add(0, 0);

                double xx = centre.x;
                double yy = centre.y;
                int index = 0;
                for (i = 0; i < 4; i++)
                {
                    if ((centre_dots[i].x < xx) &&
                        (centre_dots[i].y < yy))
                    {
                        xx = centre_dots[i].x;
                        yy = centre_dots[i].y;
                        centre_square.x_points[0] = (float)xx;
                        centre_square.y_points[0] = (float)yy;
                        index = i;
                    }
                }
                centredots.Add(centre_dots[index]);

                xx = centre.x;
                yy = centre.y;
                for (i = 0; i < 4; i++)
                {
                    if ((centre_dots[i].x > xx) &&
                        (centre_dots[i].y < yy))
                    {
                        xx = centre_dots[i].x;
                        yy = centre_dots[i].y;
                        centre_square.x_points[1] = (float)xx;
                        centre_square.y_points[1] = (float)yy;
                        index = i;
                    }
                }
                centredots.Add(centre_dots[index]);

                xx = centre.x;
                yy = centre.y;
                for (i = 0; i < 4; i++)
                {
                    if ((centre_dots[i].x > xx) &&
                        (centre_dots[i].y > yy))
                    {
                        xx = centre_dots[i].x;
                        yy = centre_dots[i].y;
                        centre_square.x_points[2] = (float)xx;
                        centre_square.y_points[2] = (float)yy;
                        index = i;
                    }
                }
                centredots.Add(centre_dots[index]);

                xx = centre.x;
                yy = centre.y;
                for (i = 0; i < 4; i++)
                {
                    if ((centre_dots[i].x < xx) &&
                        (centre_dots[i].y > yy))
                    {
                        xx = centre_dots[i].x;
                        yy = centre_dots[i].y;
                        centre_square.x_points[3] = (float)xx;
                        centre_square.y_points[3] = (float)yy;
                        index = i;
                    }
                }
                centredots.Add(centre_dots[index]);
            }
            return (centre_square);
        }
Esempio n. 23
0
 /// <summary>
 /// returns a copy of this polygon with each vetex randomly perturbed
 /// </summary>
 /// <param name="max_displacement">
 /// maximum perturbation <see cref="System.Single"/>
 /// </param>
 /// <param name="rnd">
 /// random number generator <see cref="Random"/>
 /// </param>
 /// <returns>
 /// polygon object <see cref="polygon2D"/>
 /// </returns>
 public polygon2D Jiggle(float max_displacement, Random rnd)
 {
     polygon2D result = null;
     
     if (x_points != null)
     {
         result = new polygon2D();
         float max = max_displacement*2;
         for (int i = 0; i < x_points.Count; i++)
         {
             float jiggled_x = x_points[i] + ((float)rnd.NextDouble() * max) - max_displacement;
             float jiggled_y = x_points[i] + ((float)rnd.NextDouble() * max) - max_displacement;
             result.Add(jiggled_x, jiggled_y);
         }
     }
     return(result);
 }