/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        private void buildButton_Click(object sender, EventArgs e)
        {
            Bitmap areasImage = (Bitmap) rightImage.Image;
            Bitmap intensityImage = (Bitmap) phaseMapImage.Image;

            BitmapData areasImageData = ImageProcessor.getBitmapData(areasImage);
            BitmapData intensityImageData = ImageProcessor.getBitmapData(intensityImage);

            List<Point3D> pointsList = new List<Point3D>();
            int maxColorIntensity = 0;
            int minColorIntensity = 100000;

            for (int x = 0; x < areasImage.Width; x++)
            {
                for (int y = 0; y < areasImage.Height; y++)
                {
                    Color currentColor = ImageProcessor.getPixel(x, y, areasImageData);

                    int currentLineNumber = 0;
                    foreach (int key in numberAndColorCorrespondence.Keys)
                    {
                        if (numberAndColorCorrespondence[key].ToArgb() == currentColor.ToArgb())
                        {
                            currentLineNumber = key;
                            break;
                        }
                    }

                    Color currentIntensity = ImageProcessor.getPixel(x, y, intensityImageData);
                    double additionalZcomponent = 255*currentLineNumber;

                    double currentColorIntencity = currentIntensity.R;

                    currentColorIntencity = 255 - currentColorIntencity + additionalZcomponent;

                    if (maxColorIntensity < currentColorIntencity)
                    {
                        maxColorIntensity = (int) currentColorIntencity;
                    }

                    if ((minColorIntensity > currentColorIntencity) && (currentColorIntencity != 0))
                    {
                        minColorIntensity = (int) currentColorIntencity;
                    }

                    if ((int) (currentColorIntencity) != 0)
                    {
                        Point3D newPoint = new Point3D(y, x, (int) (currentColorIntencity));
                        pointsList.Add(newPoint);
                    }
                }
            }

            var somePlane = getPlaneParams(pointsList);
            /*var somePlane = new Pi_Class1.Plane();
            somePlane.a = 0.04;
            somePlane.b = 0.001369;
            somePlane.c = -0.116;
            somePlane.d = 1;*/

            OpenGLForm newForm = new OpenGLForm();
            List<Point3D> result = new List<Point3D>();

            StreamWriter outfile = new StreamWriter("AllTxtFiles.txt");

            foreach (Point3D currentPoint in pointsList)
            {
                double planeZ = (somePlane.a*currentPoint.x) + (somePlane.b*currentPoint.y) + somePlane.c;

                //newForm.addPoint(new Point3D(currentPoint.x, currentPoint.y, -(int)planeZ));

                //newForm.addPoint(currentPoint);

                newForm.addPoint(new Point3D(currentPoint.x, currentPoint.y,
                                             (int) Math.Abs(Math.Abs(currentPoint.z) - Math.Abs(planeZ)),
                                             Color.RoyalBlue));

                StringBuilder stringForFile = new StringBuilder("");
                stringForFile.Append("v " + currentPoint.x + " " + currentPoint.y + " " +
                                     (int) Math.Abs(Math.Abs(currentPoint.z) - Math.Abs(planeZ)));

                outfile.Write(stringForFile.ToString());
                outfile.WriteLine();
            }

            int i = 0;

            foreach (Point3D firstPoint in pointsList)
            {
                i++;

                int minDistance = Int32.MaxValue;

                int firstClosestPoint = 0;
                int secondClosestPoint = 0;

                int j = 0;

                foreach (Point3D secondPoint in pointsList)
                {
                    j++;

                    if (i == j)
                    {
                        continue;
                    }

                    int currentDistance = (int) Math.Sqrt(Math.Pow((firstPoint.x - secondPoint.x), 2) + Math.Pow((firstPoint.y - secondPoint.y), 2) + Math.Pow((firstPoint.z - secondPoint.z), 2));

                    if (currentDistance < minDistance)
                    {
                        minDistance = currentDistance;
                        if (firstClosestPoint != null)
                        {
                            secondClosestPoint = firstClosestPoint;
                        }

                        firstClosestPoint = j;
                    }
                }

                StringBuilder stringForFile = new StringBuilder("");
                stringForFile.Append("f " + i + " " + firstClosestPoint + " " + secondClosestPoint);

                outfile.Write(stringForFile.ToString());
                outfile.WriteLine();

                stringForFile = new StringBuilder("");
                stringForFile.Append("f " + i + " " + secondClosestPoint + " " + firstClosestPoint);

                outfile.Write(stringForFile.ToString());
                outfile.WriteLine();
            }

            newForm.Show();

            ((Bitmap) rightImage.Image).UnlockBits(areasImageData);
            ((Bitmap) phaseMapImage.Image).UnlockBits(intensityImageData);

            Bitmap resultBitmap = new Bitmap(phaseMapImage.Image.Width, phaseMapImage.Image.Height);

            BitmapData imageData = ImageProcessor.getBitmapData(resultBitmap);

            foreach (Point3D currentPoint in pointsList)
            {
                if (maxColorIntensity < currentPoint.z)
                {
                    maxColorIntensity = currentPoint.z;
                }

                if ((minColorIntensity > currentPoint.z) && (currentPoint.z != 0))
                {
                    minColorIntensity = currentPoint.z;
                }
            }

            int abs = maxColorIntensity - minColorIntensity;
            double ratio = abs/255.0;

            foreach (Point3D currentPoint in pointsList)
            {
                var scaledValue = (int) (Math.Abs(currentPoint.z)/ratio) - 1;

                if (scaledValue > 255)
                {
                    scaledValue = 255;
                }

                if (scaledValue < 0)
                {
                    scaledValue = 0;
                }

                ImageProcessor.setPixel(imageData, currentPoint.y, currentPoint.x,
                                        Color.FromArgb(scaledValue, scaledValue, scaledValue));
            }

            resultBitmap.UnlockBits(imageData);
            phaseMapBuilded(resultBitmap, ratio);

            resultBitmap = new Bitmap(phaseMapImage.Image.Width, phaseMapImage.Image.Height);
            imageData = ImageProcessor.getBitmapData(resultBitmap);

            foreach (Point3D currentPoint in result)
            {
                if (maxColorIntensity < currentPoint.z)
                {
                    maxColorIntensity = currentPoint.z;
                }

                if ((minColorIntensity > currentPoint.z) && (currentPoint.z != 0))
                {
                    minColorIntensity = currentPoint.z;
                }
            }

            abs = maxColorIntensity - minColorIntensity;
            ratio = abs/255.0;
            foreach (Point3D currentPoint in result)
            {
                int scaledValue = (int) (Math.Abs(currentPoint.z)/ratio);
                ImageProcessor.setPixel(imageData, currentPoint.y, currentPoint.x,
                                        Color.FromArgb(scaledValue, scaledValue, scaledValue));
            }

            resultBitmap.UnlockBits(imageData);
            imageRestored(resultBitmap, ratio);
        }
        /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        private void subPlane(Pi_Class1.Plane somePlane)
        {
            Image targetImage = pictureBox9.Image;
            List<Point3D> pointsList = new List<Point3D>();

            for (int i = 0; i < targetImage.Width; i++)
            {
                for (int j = 0; j < targetImage.Height; j++)
                {
                    Color currentColor = ((Bitmap)(targetImage)).GetPixel(i, j);
                    int currentIntencity = currentColor.R;
                    Point3D newPoint = new Point3D(i, j, currentIntencity);

                    if (newPoint.z != 0)
                    {
                        pointsList.Add(newPoint);
                    }
                }
            }

            OpenGLForm newForm = new OpenGLForm();

            foreach (Point3D currentPoint in pointsList)
            {
                double planeZ = (somePlane.a * currentPoint.x) + (somePlane.b * currentPoint.y) + somePlane.c;
                //newForm.addPoint(new Point3D(currentPoint.x, currentPoint.y, (int)(currentPoint.z - planeZ), Color.RoyalBlue));
                newForm.addPoint(new Point3D(currentPoint.x, currentPoint.y, (int)planeZ, Color.Red));
                newForm.addPoint(new Point3D(currentPoint.x, currentPoint.y, currentPoint.z, Color.Green));

                Color planeColor;

                if ((int)(currentPoint.z - planeZ) > 255)
                {
                    planeColor = Color.White;
                }
                else if ((int)(currentPoint.z - planeZ) < 0)
                {
                    planeColor = Color.Black;
                }
                else
                {
                    planeColor = Color.FromArgb((int)(currentPoint.z - planeZ), (int)(currentPoint.z - planeZ),
                    (int)(currentPoint.z - planeZ));
                }

                ((Bitmap)(mainPictureBox.Image)).SetPixel(currentPoint.x, currentPoint.y, planeColor);
            }

            mainPictureBox.Invalidate();

            newForm.Show();

            PlaneParamsForm planeParamsForm = new PlaneParamsForm(somePlane);
            planeParamsForm.planeParamsChoosed+=PlaneParamsFormOnPlaneParamsChoosed;
            planeParamsForm.Show();
        }