public static ICPSolution SetRandomIndices(int myNumberPoints, int maxNumber, List <ICPSolution> solutionList)
        {
            int        i;
            List <int> randomIndices;

            try
            {
                //set trial points
                for (i = 0; i < 1000; i++)
                {
                    try
                    {
                        randomIndices = ICPSolution.GetRandomIndices(maxNumber, myNumberPoints);
                        if (ICPSolution.IndicesAreNew(randomIndices, solutionList))
                        {
                            ICPSolution res = new ICPSolution();
                            res.RandomIndices = randomIndices;
                            return(res);
                        }
                    }
                    catch (Exception err)
                    {
                        MessageBox.Show("Error in SetRandomIndices " + err.Message);
                        return(null);
                    }
                }
                // MessageBox.Show("SetRandomIndices: No indices could be found!!");
                // 1000 trials
                return(null);
            }
            catch (Exception err)
            {
                MessageBox.Show("Error in SetRandomIndices " + err.Message);
                return(null);
            }
        }
        private bool Helper_ICP_Iteration_SA(IList <Point3D> PT, IList <Point3D> PS, KDTreePoint3D kdTree, int keepOnlyPoints)
        {
            try
            {
                //first iteration
                if (solutionList == null)
                {
                    solutionList = new List <ICPSolution>();


                    if (NumberOfStartTrialPoints > PS.Count)
                    {
                        NumberOfStartTrialPoints = PS.Count;
                    }
                    if (NumberOfStartTrialPoints == PS.Count)
                    {
                        NumberOfStartTrialPoints = PS.Count * 80 / 100;
                    }
                    if (NumberOfStartTrialPoints < 3)
                    {
                        NumberOfStartTrialPoints = 3;
                    }



                    for (int i = 0; i < MaxNumberSolutions; i++)
                    {
                        ICPSolution myTrial = ICPSolution.SetRandomIndices(NumberOfStartTrialPoints, PS.Count, solutionList);

                        if (myTrial != null)
                        {
                            myTrial.PointsSource = ExtractPoints(PS, myTrial.RandomIndices);
                            solutionList.Add(myTrial);
                        }
                    }
                    ////test....
                    ////maxNumberSolutions = 1;
                    //ICPSolution myTrial1 = new ICPSolution();
                    //for (int i = 0; i < NumberPointsSolution; i++)
                    //{
                    //    myTrial1.RandomIndices.Add(i);
                    //}
                    //myTrial1.PointsSource = RandomUtils.ExtractPoints(PS, myTrial1.RandomIndices);
                    //solutionList[0] = myTrial1;
                }


                for (int i = 0; i < solutionList.Count; i++)
                {
                    IList <Point3D> transformedPoints = null;

                    ICPSolution myTrial = solutionList[i];
                    Helper_ICP_Iteration(ref myTrial.PointsTarget, ref myTrial.PointsSource, PT, PS, kdTree, keepOnlyPoints);
                    myTrial.Matrix             = Matrix4d.Mult(myTrial.Matrix, this.Matrix);
                    myTrial.MeanDistanceSubset = this.MeanDistance;

                    myTrial.MeanDistance = TransformPoints(ref transformedPoints, PT, PS, myTrial.Matrix);

                    // solutionList[i] = myTrial;
                }
                if (solutionList.Count > 0)
                {
                    solutionList.Sort(new ICPSolutionComparer());
                    RemoveSolutionIfMatrixContainsNaN(solutionList);
                    if (solutionList.Count == 0)
                    {
                        System.Windows.Forms.MessageBox.Show("No solution could be found !");
                    }

                    this.Matrix       = solutionList[0].Matrix;
                    this.MeanDistance = solutionList[0].MeanDistance;

                    if (solutionList[0].MeanDistance < this.MaximumMeanDistance)
                    {
                        return(true);
                    }
                }
            }
            catch (Exception err)
            {
                MessageBox.Show("Error in Helper_ICP_Iteration_SA: " + err.Message);
                return(false);
            }

            return(false);
        }