Click here to Skip to main content
15,892,927 members
Articles / Programming Languages / C#

Reconstruction of Charts from Images

Rate me:
Please Sign up or sign in to vote.
4.92/5 (13 votes)
18 Mar 2010CPOL7 min read 38K   4.1K   42  
Usage of universal framework for chart reconstruction
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using DiagramUI;
using DataPerformer;
using Regression;
using DiagramUI.Labels;
using DiagramUI.Utils;
using DataPerformerUI.UserControls;
using Diagram.UI.Interfaces;

namespace DataPerformerUI
{
    /// <summary>
    /// Editor of propetries of General Linear Method iterator
    /// </summary>
    public partial class FormIterateGLM : Form, IUpdatableForm
    {
        private IObjectLabel comp;
        private IteratorGLM iterator;
        private List<ComboBox> left = new List<ComboBox>();
        private List<ComboBox> right = new List<ComboBox>();
        private List<ComboBox> alias = new List<ComboBox>();
        List<TextBox> disp = new List<TextBox>();
        private List<Label> labs = new List<Label>();
        Control cLeftPart;
        Control cRightPart;
        Control cPar;
        Control cSigma;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="comp">Component label</param>
        public FormIterateGLM(IObjectLabel comp)
        {
            InitializeComponent();
            this.LoadResources();
            this.comp = comp;
            iterator = comp.Object as IteratorGLM;
            cLeftPart = panelLeftPart;
            cRightPart = panelRightPart;
            cPar = panelParameter;
            cSigma = panelSigma;
            toolStripButtonNumberAli.FillCombo(1, 100);
            toolStripComboBoxOut.FillCombo(1, 100);
            toolStripButtonNumberAli.SelectedIndex = iterator.AliasesCount - 1;
            toolStripComboBoxOut.SelectedIndex = iterator.DataCount - 1;
            fillOut();
            fillAli();
            selectCombo();
            Text = comp.Name;
        }

        private FormIterateGLM()
        {
            InitializeComponent();
        }

        #region IUpdatableForm Members

        void IUpdatableForm.UpdateFormUI()
        {
            Text = comp.Name;
        }

        #endregion


 
        private void removeData()
        {
            foreach (ComboBox cb in left)
            {
                Control p = cb.Parent;
                p.Controls.Remove(cb);
            }
            left.Clear();
            foreach (ComboBox cb in right)
            {
                Control p = cb.Parent;
                p.Controls.Remove(cb);
            }
            right.Clear();
            foreach (Label l in labs)
            {
                Control p = l.Parent;
                p.Controls.Remove(l);
            }
            labs.Clear();
        }

        private void removeAli()
        {
            foreach (ComboBox box in alias)
            {
                Control p = box.Parent;
                p.Controls.Remove(box);
            }
            alias.Clear();
            foreach (TextBox tb in disp)
            {
                Control p = tb.Parent;
                p.Controls.Remove(tb);
            }
        }

        void fillOut()
        {
            int y = 10;
            removeData();
            int n = (int)toolStripComboBoxOut.SelectedIndex + 1;
            for (int i = 0; i < n; i++)
            {
                ComboBox comboLeft = new ComboBox();
                left.Add(comboLeft);
                fillData(comboLeft);
                comboLeft.Top = y;
                comboLeft.Left = 5;
                comboLeft.Width = cLeftPart.Width - 10;
                cLeftPart.Controls.Add(comboLeft);
               /* Label l = new Label();
                l.Text = "=";
                l.Left = comboLeft.Left + comboLeft.Width + 5;
                l.Top = comboLeft.Top;
                l.Width = 20;
                labs.Add(l);*/
                //cRightPart.Controls.Add(l);
                ComboBox comboRight = new ComboBox();
                right.Add(comboRight);
                fillData(comboRight);
                comboRight.Top = y;
                comboRight.Left = 5;// l.Right + 5;
                comboRight.Width = cRightPart.Width - 10;
                cRightPart.Controls.Add(comboRight);
                y = comboLeft.Top + comboLeft.Height + 10;
            }
        }

        void fillAli()
        {
            int y =  10;
            removeAli();
            int n = toolStripButtonNumberAli.SelectedIndex + 1;
            for (int i = 0; i < n; i++)
            {
                ComboBox box = new ComboBox();
                box.Top = y;
                alias.Add(box);
                fillAlias(box);
                box.Left = 10;
                box.Width = cPar.Width - 10;
                cPar.Controls.Add(box);
                y = box.Top + box.Height + 10;
                TextBox tb = new TextBox();
                tb.Top = box.Top;
                tb.Left = 5;
                tb.Text = 0 + "";
                disp.Add(tb);
                cSigma.Controls.Add(tb);
            }
        }

        void selectCombo()
        {
            for (int i = 0; i < left.Count; i++)
            {
                ComboBox l = left[i];
                selectCombo(l, iterator.GetLeftName(i));
                ComboBox r = right[i];
                selectCombo(r, iterator.GetRightName(i));
            }
            for (int i = 0; i < alias.Count; i++)
            {
                selectCombo(alias[i], iterator.GetAliasName(i));
            }
            double[,] d = iterator.CorrectionMatrix;
            if (d != null)
            {
                for (int i = 0; i < disp.Count; i++)
                {
                    if (i >= d.GetLength(0))
                    {
                        continue;
                    }
                    if (i >= d.GetLength(1))
                    {
                        continue;
                    }
                    TextBox tb = disp[i];
                    tb.Text = d[i, i] + "";
                }
            }
        }

        void selectCombo(ComboBox box, string item)
        {
            for (int i = 0; i < box.Items.Count; i++)
            {
                string s = box.Items[i] + "";
                if (s.Equals(item))
                {
                    box.SelectedIndex = i;
                    return;
                }
            }
        }

        

 
        void fillData(ComboBox box)
        {
            List<string> l = iterator.AllMeasurements;
            foreach (string s in l)
            {
                box.Items.Add(s);
            }
        }

        void fillAlias(ComboBox box)
        {
            List<string> l = iterator.AllAliases;
            foreach (string s in l)
            {
                box.Items.Add(s);
            }
        }

        void accept()
        {
            try
            {
                List<string> l = getComboList(left);
                List<string> ri = getComboList(right);
                List<string> ali = getComboList(alias);
                List<List<string>> r = new List<List<string>>();
                int n = ali.Count;
                double[,] d = new double[n, n];
                double[] dx = new double[n];
                for (int i = 0; i < dx.Length; i++)
                {
                    dx[i] = 0.00000001;
                }
                for (int i = 0; i < left.Count; i++)
                {
                    List<string> list = new List<string>();
                    r.Add(list);
                    for (int j = 0; j <= i; j++)
                    {
                        list.Add("");
                        d[i, j] = d[j, i] = 0;
                    }
                }
                for (int i = 0; i < disp.Count; i++)
                {
                    d[i, i] = Double.Parse(disp[i].Text);
                }
                iterator.Set(ali, l, ri, r, dx, d);
            }
            catch (Exception ex)
            {
                ex.Log();
                this.ShowError(ex);
            }
        }

        private List<string> getComboList(List<ComboBox> l)
        {
            List<string> list = new List<string>();
            foreach (ComboBox box in l)
            {
                list.Add(box.SelectedItem + "");
            }
            return list;
        }

 
        private void buttonAcceptAll_Click(object sender, EventArgs e)
        {
            accept();
        }

        private void buttonIterate_Click(object sender, EventArgs e)
        {
            double s = iterator.Iterate();
            labelSigma.Text = "Sigma = " + s + "";
        }

        private void fill()
        {
            fillAli();
            fillOut();
        }

        private void toolStripButtonRefresh_Click(object sender, EventArgs e)
        {
            fill();
        }

        private void buttonAcceptNumber_Click(object sender, EventArgs e)
        {
            fill();
        }

 
     }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
Russian Federation Russian Federation
Ph. D. Petr Ivankov worked as scientific researcher at Russian Mission Control Centre since 1978 up to 2000. Now he is engaged by Aviation training simulators http://dinamika-avia.com/ . His additional interests are:

1) Noncommutative geometry

http://front.math.ucdavis.edu/author/P.Ivankov

2) Literary work (Russian only)

http://zhurnal.lib.ru/editors/3/3d_m/

3) Scientific articles
http://arxiv.org/find/all/1/au:+Ivankov_Petr/0/1/0/all/0/1

Comments and Discussions