Esempio n. 1
0
 public ClassicCurve(ClassicCurve curve)
 {
     m_id          = curve.ID;
     m_type        = curve.Type;
     m_nnIndex     = curve.NnIndex;
     m_caption     = curve.Caption;
     m_bezierCurve = new List <PointF>(curve.BezierCurve);
     m_points      = new List <PointF>(curve.Points);
 }
 public ClassicCurve(ClassicCurve curve)
 {
     m_id = curve.ID;
     m_type = curve.Type;
     m_nnIndex = curve.NnIndex;
     m_caption = curve.Caption;
     m_bezierCurve = new List<PointF>(curve.BezierCurve);
     m_points = new List<PointF>(curve.Points);
 }
        /// <summary>
        /// Network learns new curve 
        /// (adds curve to collection and runs training )
        /// </summary>
        /// <param name="curve">Curve</param>
        public void LearnNewCurve(ClassicCurve curve)
        {
            int position = FindFreePosition();
            if (position == -1)
            {
                // maximum amount of gestures that can be learnt is reached
                return;
            }
            
            StopLearning(); //just for sure...
            //if (threadTrainNetwork != null && threadTrainNetwork.IsAlive)
            //    threadTrainNetwork.Abort();
            //if (m_newCurveAdded) m_curves.RemoveAt(m_curves.Count - 1);
            
            MyCurve curveToTrain = new MyCurve(curve.ID, curve.Points);            
            curve.NnIndex = position;           
            curveToTrain.CreateTrainingSet();

            // Might happen that curve sets is not compatible with curves 
            // therefore it is required to reinicialize it
            if (m_curveSets.ContainsKey(curve.ID))
                CreateTrainingSet();
            
            m_curveSets.Add(curve.ID, curveToTrain);
            m_curves.Add(curve.ID, curve);

            InitializeNetwork();
            OnNetworkStartTraining();
            threadTrainNetwork = new Thread(new ThreadStart(TrainNeuralNetwork));
            threadTrainNetwork.Start();
        }
        public override void Initialize()           
        {
            base.Initialize();
            if (m_gesturesCollection.GetCurves().Count >= Config.User.NnOutputSize)
                m_maximumLimitReached = true;

            m_pen = new Pen(Color.Blue, 3);
            m_pen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
            m_pen.EndCap = System.Drawing.Drawing2D.LineCap.Round; 

            m_networkBackup = new MyNeuralNetwork(m_network);
            m_networkTrained = new MyNeuralNetwork(m_network);
            foreach (MyGesture gest in m_gesturesCollection.Gestures)
                if (gest.Activator.Type == MouseActivator.Types.ClassicCurve && !imageList1.Images.ContainsKey(gest.Activator.ID))
                {
                    ClassicCurve classicCurve = gest.Activator;
                    imageList1.Images.Add(gest.Activator.ID, gest.Activator.ExtractIcon(imageList1.ImageSize));
                    ListViewItem item = new ListViewItem(new string[] { "", classicCurve.Caption }, gest.Activator.ID);
                    item.Name = gest.Activator.ID;
                    lv_curvesList.Items.Add(item);
                }

            if (m_tempGesture.Activator != null && m_tempGesture.Activator.Type == BaseActivator.Types.ClassicCurve)
            {
                ClassicCurve classicCurve = m_tempGesture.Activator;
                m_newCurve = m_tempGesture.Activator;
                tB_newName.Text = classicCurve.Caption;
                if (lv_curvesList.Items.ContainsKey(m_tempGesture.Activator.ID))
                {
                    //sameCurve = true;
                    lv_curvesList.Focus();
                    lv_curvesList.EnsureVisible(lv_curvesList.Items[m_tempGesture.Activator.ID].Index);
                    lv_curvesList.Items[m_tempGesture.Activator.ID].Selected = true;
                    rB_newCurve.Enabled = false;
                }
                else
                {
                    gB_use.Enabled = true;
                    m_newCurveUsed = false;
                    m_networkTrained = new MyNeuralNetwork(m_networkOriginal);
                    rB_newCurve.Checked = true;
                    //SetNewGesture();
                }
            }
            else
            {
                rB_newCurve.Enabled = false;
                tB_newName.Enabled = false;
                tB_newName.Text = "curve_name";
            }
            gB_alreadyInUse.Text = Translation.GetText("C_CurveG_gB_alreadyInUse");
            cH_curve.Text = Translation.GetText("C_CurveG_cH_curve");
            cH_name.Text = Translation.GetText("C_CurveG_cH_name");
            cH_associatedActions.Text = Translation.GetText("C_Gestures_cH_associatedActions");
            cH_group.Text = Translation.GetText("C_Gestures_cH_group");
            gB_use.Text = Translation.GetText("C_CurveG_gB_useCurve");
            rB_newCurve.Text = Translation.GetText("C_CurveG_rB_newOne");
            rB_suggestedCurve.Text = Translation.GetText("C_CurveG_rB_selected");
            lbl_name.Text = Translation.GetText("C_CurveG_lbl_name");

        }
        //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();
     }
 }