//private double GetOutPointsRatio(string learntCurveName)
        //{
        //    ClassicCurve learntCurve = m_gesturesCollection.GetCurve(learntCurveName);
        //    if (learntCurve != null)
        //    {
        //        Rectangle rect = new Rectangle(0, 0, 20, 20);
        //        int parts = 5;

        //        List<PointF> centerLearnPoints = MyCurve.ScaleToCenter(MyCurve.CreateExactPath(learntCurve.Points, 1, 2), rect);            
        //        int centerCount = centerLearnPoints.Count / parts;

        //        List<PointF> centerPoints = MyCurve.ScaleToCenter(MyCurve.CreateExactPath(m_pathPoints, 1, 2), rect);
        //        int testCount = centerPoints.Count / parts;
                
        //        int outPoints = 0;
        //        for (int i = 0; i < parts; i++)
        //        {
        //            int centerMax = centerCount;
        //            int testMax =testCount;
        //            if (i + 1 == parts)
        //            {
        //                centerMax = centerLearnPoints.Count - i * centerCount;
        //                testMax = centerPoints.Count - i * testCount;
        //            }
                    
        //            System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
        //            List<PointF> center10 = centerLearnPoints.GetRange(i * centerCount, centerMax);
        //            path.AddLines(center10.ToArray());
        //            path.Widen(new Pen(Color.Green, 3));

        //            List<PointF> test10 = centerPoints.GetRange(i * testCount, testMax);
        //            foreach (PointF point in test10)
        //            {
        //                if (!path.IsVisible(point))
        //                    outPoints++;
        //            }
        //            m_gp.DrawPath(new Pen(Brushes.Yellow, 1), path);
        //            m_gp.DrawLines(new Pen(Brushes.Red, 1), test10.ToArray());
        //        }
        //        m_pbDisplay.Invalidate();
                

        //        double ratio = outPoints / (double)centerPoints.Count;
        //        Debug.WriteLine("Ratio of points out of region is: " + ratio);
        //        return ratio;

        //    }
        //    return 1;
        //}
        #endregion Not in use

        public override void Display_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && m_pathPoints.Count > 1)
            {
                m_mouseDown = false;
                MyCurve curve = new MyCurve(m_pathPoints, true);
                if (m_curveLength > CURVE_MIN_LENGTH)
                {
                    //if (curve != null && curve.Points.Count > 1)
                    //{
                    //    List<PointF> rotated = MyCurve.RotateCurve(curve.Points, 10);
                    //    m_gp.DrawLines(new Pen(Brushes.Yellow, 2), rotated.ToArray());
                    //    rotated = MyCurve.RotateCurve(curve.Points, -10);
                    //    m_gp.DrawLines(new Pen(Brushes.Pink, 2), rotated.ToArray());                        
                    //    m_pbDisplay.Invalidate();
                    //}
                    //return;

                    //if (curve != null && curve.Points.Count > 1)
                    //{
                    //    List<PointF> resized = MyCurve.ResizeCurve(curve.Points, 0.25, 0);
                    //    m_gp.DrawLines(new Pen(Brushes.Yellow, 2), resized.ToArray());
                    //    resized = MyCurve.ResizeCurve(curve.Points, -0.25, 0);
                    //    m_gp.DrawLines(new Pen(Brushes.Pink, 2), resized.ToArray());
                    //    m_pbDisplay.Invalidate();
                    //}
                    //return;

                    //m_network.StopLearning();
                    m_newCurveUsed = true;
                    m_networkTrained.StopLearning();
                    m_network = new MyNeuralNetwork(m_networkBackup);
                    m_shortCurve = false;
                    
                    double divergence;
                    string name = m_network.RecognizeCurve(curve.GetScaledInput(), out divergence);
                    m_tooMuchSimilar = (divergence >= 0 && divergence < MAX_DIVERGENCE); 

                    if (name != string.Empty)
                    {
                        if (m_tooMuchSimilar)
                            m_newCurve = m_gesturesCollection.GetCurve(name);
                        else
                            m_newCurve = new ClassicCurve(m_pathPoints, m_gesturesCollection); 
                        rB_newCurve.Enabled = !m_tooMuchSimilar;
                        lv_curvesList.Focus();
                        lv_curvesList.EnsureVisible(lv_curvesList.Items[name].Index);
                        if (!lv_curvesList.Items[name].Selected)
                            lv_curvesList.Items[name].Selected = true;
                        else
                        {
                            if (!rB_suggestedCurve.Checked) rB_suggestedCurve.Checked = true;
                            else
                            {
                                lv_curvesList.Items[name].Selected = false;
                                lv_curvesList.Items[name].Selected = true;
                                //panel_curveDraw.Refresh();
                            }
                        }
                    }
                    else
                    {
                        m_newCurve = new ClassicCurve(m_pathPoints, m_gesturesCollection);
                        rB_newCurve.Enabled = true;
                        if (!rB_newCurve.Checked) rB_newCurve.Checked = true;
                        else
                        {
                            Debug.WriteLine("Drawing - MouseUp (new gesture)");
                            m_newCurve.DrawToPictureBox(m_pbDisplay);                          
                            SetNewGesture();
                        }
                    }
                    OnCanContinue(true);
                    SetInfoValues();
                }
                else
                {                    
                    m_newCurve = null;
                    m_shortCurve = true;
                    //Debug.WriteLine("Drawing - MouseUp (short gesture)");  
                    m_gp = Graphics.FromImage(m_pbDisplay.Image);
                    m_gp.FillRectangle(Brushes.White, 0, 0, m_pbDisplay.Width, m_pbDisplay.Height);
                    m_pbDisplay.Invalidate();
                    m_pbDisplay.Focus();
                    rB_newCurve.Enabled = false;
                    OnCanContinue(false);
                    SetInfoValues();
                }             
                m_gp.Dispose();
            }
        }
 private void lv_curvesList_SelectedIndexChanged(object sender, EventArgs e)
 {
     lV_curvesMatchedGestures.Items.Clear();
     if (lv_curvesList.SelectedItems.Count != 1) return;
     //int count = 0;            
     List<MyGesture> matchedGest = m_gesturesCollection.MatchedGestures(lv_curvesList.SelectedItems[0].Name);
     if (matchedGest != null)
         foreach (MyGesture gest in matchedGest)
         {
             lV_curvesMatchedGestures.Items.Add(new ListViewItem(new string[] { gest.Caption, gest.AppGroup.Caption }));
         }
     m_recognizedCurve = m_gesturesCollection.GetCurve(lv_curvesList.SelectedItems[0].Name);
     rB_suggestedCurve.Enabled = true;
     if (!rB_suggestedCurve.Checked) rB_suggestedCurve.Checked = true;
     else
     {
         Debug.WriteLine("Drawing - SelectedIndexChanged");
         m_recognizedCurve.DrawToPictureBox(m_pbDisplay);
         SetFromRecognizedGesture();
         if (!m_maximumLimitReached)
             SetInfoValues();
     }
 }