public void SetupSolve(CalibrationInfo ci, bool useConstraints, bool radialDistortion)
        {
            foreach (ProjectorEntry pe in ci.Projectors)
            {
                SolveProjector sp = new SolveProjector(pe, ci.DomeSize, ci.ScreenType == ScreenTypes.FishEye ? ProjectionType.FishEye : ProjectionType.Projector, ScreenTypes.Spherical, ci.ScreenType == ScreenTypes.FishEye ? SolveParameters.FishEye : (SolveParameters)ci.SolveParameters);
                sp.RadialDistorion = radialDistortion;
                projectors.Add(sp);

                if (useConstraints)
                {
                    foreach (GroundTruthPoint gt in pe.Constraints)
                    {
                        SolveList.Add(new Constraint(sp, gt));
                    }
                }

            }

            foreach (Edge edge in ci.Edges)
            {
                foreach (EdgePoint pnt in edge.Points)
                {
                    SolveList.Add( new Coorespondence(projectors[edge.Left-1],projectors[edge.Right-1],pnt));
                }
            }

            foreach (SolveProjector sp in projectors)
            {
                regressionParameters.AddRange(sp.Parameters);
            }

            int count = SolveList.Count;
            double[,] data = new double[2, count];
            for (int i = 0; i < count; i++)
            {
                data[0, i] = i;
                data[1, i] = 0;
            }
            Parameter[] observed = new Parameter[] { ParmeterIndex };

            lm = new LevenbergMarquardt(new functionDelegate(SolveFunction), regressionParameters.ToArray(), observed, data);
        }
        public void SetupSolve(CalibrationInfo ci, bool useConstraints, bool radialDistortion)
        {
            foreach (var pe in ci.Projectors)
            {
                var sp = new SolveProjector(pe, ci.DomeSize, ci.ScreenType == ScreenTypes.FishEye ? ProjectionType.FishEye : ProjectionType.Projector, ScreenTypes.Spherical, ci.ScreenType == ScreenTypes.FishEye ? SolveParameters.FishEye : (SolveParameters)ci.SolveParameters)
                {
                    RadialDistorion = radialDistortion
                };
                projectors.Add(sp);

                if (useConstraints)
                {
                    foreach (var gt in pe.Constraints)
                    {
                        SolveList.Add(new Constraint(sp, gt));
                    }
                }

            }

            foreach (var edge in ci.Edges)
            {
                foreach (var pnt in edge.Points)
                {
                    SolveList.Add( new Coorespondence(projectors[edge.Left-1],projectors[edge.Right-1],pnt));
                }
            }

            foreach (var sp in projectors)
            {
                regressionParameters.AddRange(sp.Parameters);
            }

            var count = SolveList.Count;
            var data = new double[2, count];
            for (var i = 0; i < count; i++)
            {
                data[0, i] = i;
                data[1, i] = 0;
            }
            var observed = new[] { ParmeterIndex };

            lm = new LevenbergMarquardt(SolveFunction, regressionParameters.ToArray(), observed, data);
        }
        private WcsSolution SolveLM()
        {
            Vector2d temp = Points[0].Image - Points[1].Image;
            double imageLength = temp.Length;
            double angularSperation = CAAAngularSeparation.Separation(Points[0].Celestial.RA, Points[0].Celestial.Dec, Points[1].Celestial.RA, Points[1].Celestial.Dec);

            // Degrees per pixel
            double scale = angularSperation / imageLength;

            WcsSolution sinit = Solve(Points[0], Points[1]);

            TanSolver ts = new TanSolver(sinit.CenterX, sinit.CenterY, sinit.Rotation-180, sinit.Scale);

            foreach (CorresponencePoint cp in Points)
            {
                SolveList.Add(new CoorespondenceSolver(ts, cp, width, height));
            }

            regressionParameters.AddRange(ts.Parameters);

            int count = SolveList.Count;
            double[,] data = new double[2, count];
            for (int i = 0; i < count; i++)
            {
                data[0, i] = i;
                data[1, i] = 0;
            }
            Parameter[] observed = new Parameter[] { ParmeterIndex };

            lm = new LevenbergMarquardt(new functionDelegate(SolveFunction), regressionParameters.ToArray(), observed, data);

            for (int d = 0; d < 50; d++)
            {
                lm.Iterate();
            }

            WcsSolution s = ts.GetSolution();
            s.OffsetX = width / 2;
            s.OfsetY = height / 2;

            return s;
        }