Пример #1
0
        public static double MainFunctionFakeData(int[] translation, double phi, double[] axis, string macro)
        {
            //----------------------------------------PARAMETERS ---------------------------------------------
            double threshold           = 100; // percentage
            int    numberOfPointsMicro = 10_000;
            int    numberOfPointsMacro = 10_000;

            string       finalName    = "d:\\ALPHAS/output_test" + phi + ".txt";
            FileStream   fileStream   = new FileStream(finalName, FileMode.OpenOrCreate);
            StreamWriter streamWriter = new StreamWriter(fileStream);
            Stopwatch    stopwatchL   = new Stopwatch();

            stopwatchL.Start();

            streamWriter.WriteLine("Number of points in micro data: " + numberOfPointsMicro);
            streamWriter.WriteLine("Number of points in macro data: " + numberOfPointsMacro);
            TransData td = SettingFakeData(macro, translation, phi, axis);

            Console.WriteLine("Artificial data created succesfully.");

            //----------------------------------------DATA ---------------------------------------------------
            IFeatureComputer fc = new FeatureComputer();
            ISampler         s  = new Sampler(configuration);

            Console.WriteLine("Sampling.");
            Point3D[] pointsMicro = s.Sample(iDataMicro, numberOfPointsMicro);
            //Point3D[] pointsMacro = s.Sample(iDataMacro, numberOfPointsMacro);
            Point3D[] pointsMacro = td.SampleShifted(pointsMicro);
            //Point3D[] pointsMacro = td.Sample(pointsMicro);

            FeatureVector[] featureVectorsMicro = new FeatureVector[pointsMicro.Length];
            FeatureVector[] featureVectorsMacro = new FeatureVector[pointsMacro.Length];

            Console.WriteLine("Computing feature vectors.");
            for (int i = 0; i < pointsMicro.Length; i++)
            {
                featureVectorsMicro[i] = fc.ComputeFeatureVector(iDataMicro, pointsMicro[i]);
            }

            for (int i = 0; i < pointsMacro.Length; i++)
            {
                featureVectorsMacro[i] = fc.ComputeFeatureVector(iDataMacro, pointsMacro[i]);
            }

            //----------------------------------------MATCHES-------------------------------------------------
            IMatcher matcher = new Matcher();

            //Matcher matcherFake = new Matcher();
            Console.WriteLine("\nMatching.");
            Match[] matches = matcher.Match(featureVectorsMicro, featureVectorsMacro, threshold);
            //Match[] matches2 = matcher.Match(featureVectorsMicro2, featureVectorsMacro2, threshold);
            //Match[] matches3 = matcher.Match(featureVectorsMicro3, featureVectorsMacro3, threshold);
            //Match[] matchesFake = matcherFake.FakeMatch(featureVectorsMicro, featureVectorsMacro, threshold);

            Console.WriteLine(axis[0] + " " + axis[1] + " " + axis[2] + "; " + phi + " .......................... AXIS & PHI  ..............................");
            streamWriter.WriteLine(axis[0] + " " + axis[1] + " " + axis[2] + "; " + phi + " .......................... AXIS & PHI  ..............................");
            Console.WriteLine("Count of matches: " + matches.Length);
            streamWriter.WriteLine("Count of matches: " + matches.Length);
            streamWriter.WriteLine();

            //------------------------------------GET TRANSFORMATION -----------------------------------------
            ITransformer transformer = new Transformer3D();

            Console.WriteLine("Computing transformations.\n");
            List <Transform3D> transformations = new List <Transform3D>();

            //List<Transform3D> transformations2 = new List<Transform3D>();
            //List<Transform3D> transformations3 = new List<Transform3D>();
            //List<Transform3D> transformationsFake = new List<Transform3D>();

            for (int i = 0; i < matches.Length; i++)
            {
                //transformations.Add(transformer.GetTransformation(matches[i], iDataMicro, iDataMacro));
                transformations.Add(transformer.GetTransformation(matches[i], iDataMicro, iDataMacro));
                //transformations2.Add(transformer.GetTransformation(matches2[i], iDataMicro, iDataMacro));
                //transformations3.Add(transformer.GetTransformation(matches3[i], iDataMicro, iDataMacro));
            }

            Console.WriteLine("Looking for optimal transformation.\n");
            Candidate.initSums(iDataMicro.Measures[0] / iDataMicro.XSpacing, iDataMicro.Measures[1] / iDataMicro.YSpacing, iDataMicro.Measures[2] / iDataMicro.ZSpacing); // micro
            Density     d        = new Density();                                                                                                                         // finder, we need an instance for certain complicated reason
            Transform3D solution = d.Find(transformations.ToArray());

            stopwatchL.Stop();

            double alpha = td.GetAlpha(solution.RotationMatrix);

            Console.WriteLine("Solution found.");
            Console.WriteLine(solution);
            Console.WriteLine("Expected rotation and translation.");
            Console.WriteLine(td.RotationM);
            Console.WriteLine(translation[0] * iDataMicro.XSpacing + " " + translation[1] * iDataMicro.YSpacing + " " + translation[2] * iDataMicro.ZSpacing);
            Console.WriteLine();
            Console.WriteLine("Alpha: " + alpha);

            double[] computedAxis = td.GetAxis(solution.RotationMatrix);
            double   computedPhi  = td.GetAngle(solution.RotationMatrix);

            Console.WriteLine(Math.Round(computedAxis[0], 2) + " " + Math.Round(computedAxis[1], 2) + " " + Math.Round(computedAxis[2], 2) + "; " + Math.Round(computedPhi, 2) + " .......................... AXIS & PHI  ..............................");

            streamWriter.WriteLine("Solution:");
            streamWriter.WriteLine(solution);
            streamWriter.WriteLine("Expected rotation and translation:");
            streamWriter.WriteLine(td.RotationM);
            streamWriter.WriteLine(translation[0] * iDataMicro.XSpacing + " " + translation[1] * iDataMicro.YSpacing + " " + translation[2] * iDataMicro.ZSpacing);
            streamWriter.WriteLine("Alpha: " + alpha);
            streamWriter.WriteLine(Math.Round(computedAxis[0], 2) + " " + Math.Round(computedAxis[1], 2) + " " + Math.Round(computedAxis[2], 2) + "; " + Math.Round(computedPhi, 2) + " .......................... AXIS & PHI  ..............................");
            streamWriter.WriteLine("Elapsed Time is {0} s", stopwatchL.ElapsedMilliseconds / 1000.0);
            streamWriter.Close();
            fileStream.Close();

            List <double> a1       = new List <double>();
            List <Test>   testik   = new List <Test>();
            List <Test>   testik2  = new List <Test>();
            List <Test>   testikF1 = new List <Test>();

            for (int i = 0; i < matches.Length; i++)
            {
                a1.Add(td.GetAlpha(transformations[i].RotationMatrix));
                testik.Add(new Test(transformations[i], td.GetAlpha(transformations[i].RotationMatrix)));
                testik2.Add(new Test(transformations[i], td.GetAlpha(transformations[i].RotationMatrix), matches[i].F1.Point));
                testikF1.Add(new Test(transformations[i], td.GetAlpha(transformations[i].RotationMatrix), matches[i].F1.Point, matches[i].F2.Point, matches[i].Similarity));
            }
            double[] alphasNonSorted = a1.ToArray();
            a1.Sort();
            //a2.Sort();
            double[] alphas = a1.ToArray();
            //double[] alphasFake = a2.ToArray();
            testik.Sort((x, y) => x.alpha.CompareTo(y.alpha));
            testik2.Sort((x, y) => x.alpha.CompareTo(y.alpha));
            testikF1.Sort((x, y) => x.alpha.CompareTo(y.alpha));

            WriteCSVdouble(alphasNonSorted, "d:\\ALPHAS/nonSortedAlphas" + phi + "_001_hFAKE" + ".csv");
            WriteCSVdouble(alphas, "d:\\ALPHAS/alphas" + phi + "_001_hFAKE" + ".csv");

            //__________________________________________________________________________TEST_____________________________________________________________
            //Chart ch = MakeChart(alphas);
            //Form1 formik = new Form1();
            //formik.AddChart(ch);
            //formik.ShowDialog();

            FileStream   fs2 = new FileStream("d:\\ALPHAS/testAlpha" + phi + ".txt", FileMode.OpenOrCreate);
            StreamWriter sw2 = new StreamWriter(fs2);

            sw2.WriteLine("Expected rotation: ");
            sw2.WriteLine(td.RotationM);
            sw2.WriteLine();
            foreach (Test t in testik)
            {
                sw2.WriteLine("Alpha: " + t.alpha);
                sw2.WriteLine("Rotation: ");
                sw2.WriteLine(t.t.RotationMatrix);
                sw2.WriteLine();
            }
            sw2.Close();
            fs2.Close();

            FileStream   fs4 = new FileStream("d:\\ALPHAS/testMatcherFC2" + phi + ".txt", FileMode.OpenOrCreate);
            StreamWriter sw4 = new StreamWriter(fs4);

            sw4.WriteLine("Alpha: " + alpha);
            foreach (Test t in testikF1)
            {
                sw4.WriteLine("Alpha: " + t.alpha);
                sw4.Write("p micro: " + t.pointMicro.ToString() + ", p macro: " + t.pointMacro.ToString());
                sw4.WriteLine();
                sw4.WriteLine("Similarity: " + t.similarity);
                sw4.WriteLine();
            }
            sw4.Close();
            fs4.Close();
            //______________________________________________________________________END TEST_____________________________________________________________

            return(alpha);
        }
Пример #2
0
        public static void MainFunction(string micro, string macro, Point3D[] pointsMicro, Point3D[] pointsMacro) // the truly main function
        {
            //----------------------------------------PARAMETERS----------------------------------------------
            int              numberOfPointsMicro = 1_000;
            int              numberOfPointsMacro = 1_000;
            double           threshold           = 10; //percentage
            ISampler         s           = new Sampler(configuration);
            IFeatureComputer fc          = new FeatureComputer();
            IMatcher         matcher     = new Matcher();
            ITransformer     transformer = new Transformer3D();

            //----------------------------------------MICRO CT------------------------------------------------
            Console.WriteLine("Reading micro data.");
            VolumetricData vDataMicro = new VolumetricData(micro);

            iDataMicro = vDataMicro;
            Console.WriteLine("Data read succesfully.");
            Console.WriteLine("Sampling.");
            //Point3D[] pointsMicro = s.Sample(iDataMicro, numberOfPointsMicro);

            FeatureVector[] featureVectorsMicro = new FeatureVector[pointsMicro.Length];

            Console.WriteLine("Computing feature vectors.");
            for (int i = 0; i < pointsMicro.Length; i++)
            {
                featureVectorsMicro[i] = fc.ComputeFeatureVector(iDataMicro, pointsMicro[i]);
            }

            //----------------------------------------MACRO CT------------------------------------------------
            Console.WriteLine("\nReading macro data.");
            VolumetricData vDataMacro = new VolumetricData(macro);

            iDataMacro = vDataMacro;
            Console.WriteLine("Data read succesfully.");
            Console.WriteLine("Sampling.");
            //Point3D[] pointsMacro = s.Sample(iDataMacro, numberOfPointsMacro);

            FeatureVector[] featureVectorsMacro = new FeatureVector[pointsMacro.Length];

            Console.WriteLine("Computing feature vectors.");
            for (int i = 0; i < pointsMacro.Length; i++)
            {
                featureVectorsMacro[i] = fc.ComputeFeatureVector(iDataMacro, pointsMacro[i]);
            }

            //----------------------------------------MATCHES-------------------------------------------------
            Console.WriteLine("\nMatching.");
            Match[] matches = matcher.Match(featureVectorsMicro, featureVectorsMacro, threshold);
            Console.WriteLine("Count of matches: " + matches.Length);

            //------------------------------------GET TRANSFORMATION -----------------------------------------
            Console.WriteLine("Computing transformations.\n");

            List <Transform3D> transformations = new List <Transform3D>();

            for (int i = 0; i < matches.Length; i++)
            {
                transformations.Add(transformer.GetTransformation(matches[i], iDataMicro, iDataMacro));
                //transformations.Add(transformer.GetTransformation(matches[i], vData, vData2, configuration));
            }

            Candidate.initSums(iDataMicro.Measures[0] / iDataMicro.XSpacing, iDataMicro.Measures[1] / iDataMicro.YSpacing, iDataMicro.Measures[2] / iDataMicro.ZSpacing);
            Density     d        = new Density(); // finder, we need an instance for certain complicated reason
            Transform3D solution = d.Find(transformations.ToArray());

            Console.WriteLine("Solution found.");
            Console.WriteLine(solution);

            Cut(solution.RotationMatrix, solution.TranslationVector);
        }