Click here to Skip to main content
15,897,371 members
Articles / Desktop Programming / WPF

Integration: Kinematics + Digital Image Processing + 3D Graphics

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
9 Sep 2012CPOL12 min read 25.5K   3.4K   18  
Further promotion of integration ideas
using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


using CategoryTheory;
using DiagramUI;
using Diagram.UI.Interfaces;
using DiagramUI.Interfaces;
using DiagramUI.Labels;
using DiagramUI.Utils;

using Regression;
using DataPerformer;
using GeneralLinearMethod;
using DataPerformerUI.UserControls;
using DataPerformer.Interfaces;

namespace DataPerformerUI.Forms
{
    /// <summary>
    /// Editor for alias regression component
    /// </summary>
    public partial class FormAliasRegression : Form, IUpdatableForm
    {
        private IObjectLabel label;
        private AliasRegression regression;

        private Stack<Dictionary<IAliasName, double>> stack = new Stack<Dictionary<IAliasName, double>>();

        private Stack<string> texts = new Stack<string>();
        
        private FormAliasRegression()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="label">Label of object</param>
        public FormAliasRegression(IObjectLabel label)
            : this()
        {
            this.LoadResources();
            this.label = label;
            regression = label.Object as AliasRegression;
            UpdateFormUI();
            createAliasPanels(regression.Aliases.Count);
            fillAliasPanels();
            fillSelections();
            createMeasurementsPanel();
            measurements = regression.MeasuresNames;
            Dictionary<int, object[]> t = regression.Aliases;
            if (t != null)
            {
                numericUpDownAliases.Value = t.Count;
            }
        }

        #region IUpdatableForm Members

        /// <summary>
        /// Updates form UI
        /// </summary>
        public void UpdateFormUI()
        {
            Text = label.Name;
        }

        #endregion


        private void createAliasPanels(int count)
        {
            List<string> al = new List<string>();
            Double a = 0;
            regression.GetAllAliases(al, a);
            panelAliases.Controls.Clear();
            int y = 0;
            for (int i = 0; i < count; i++)
            {
                RegressionAliasUserControl panel = new RegressionAliasUserControl(al);
                panel.Top = y;
                panel.Left = 0;
                panel.Width = panelAliases.Width;
                y += panel.Height;
                panelAliases.Controls.Add(panel);
                Panel pan = new Panel();
                pan.Height = 3;
                pan.BackColor = Color.Black;
                pan.Top = y;
                pan.Width = panelAliases.Width;
                pan.Left = 0;
                panelAliases.Controls.Add(pan);
                y += pan.Height;
            }
        }

        private void fillAliasPanels()
        {
            int i = 0;
            panelAliases.Resize += panelAliases_Resize;
            Dictionary<int, object[]> t = regression.Aliases;
            foreach (Control c in panelAliases.Controls)
            {
                if (!(c is RegressionAliasUserControl))
                {
                    continue;
                }
                RegressionAliasUserControl p = c as RegressionAliasUserControl;
                p.Object = t[i];
                ++i;
            }
        }

        private void panelAliases_Resize(object sender, EventArgs e)
        {
            foreach (Control c in panelAliases.Controls)
            {
                if (!(c is RegressionAliasUserControl))
                {
                    continue;
                }
                RegressionAliasUserControl p = c as RegressionAliasUserControl;
                p.Width = panelAliases.Width - panelAliases.Left - 10;
            }
        }


        private Dictionary<int, object[]> aliases
        {
            get
            {
                int i = 0;
                Dictionary<int, object[]> t = new Dictionary<int, object[]>();
                foreach (Control c in panelAliases.Controls)
                {
                    if (!(c is RegressionAliasUserControl))
                    {
                        continue;
                    }
                    RegressionAliasUserControl p = c as RegressionAliasUserControl;
                    t[i] = p.Object;
                    ++i;
                }
                return t;
            }
        }

        private void createMeasurementsPanel()
        {
            int y = 0;
            Hashtable t = regression.MeasuresNames;
            IDataConsumer cons = regression as IDataConsumer;
            for (int i = 0; i < cons.Count; i++)
            {
                IMeasurements m = regression[i];
                RegessionAliasMeasureUserControl panel = new RegessionAliasMeasureUserControl(regression, m);
                panel.Table = t;
                panel.Top = y;
                panel.Left = 0;
                panel.Width = panelMeasurements.Width;
                panel.Table = t;
                panelMeasurements.Controls.Add(panel);
                y += panel.Height;
                Panel pan = new Panel();
                pan.Height = 3;
                pan.BackColor = Color.Black;
                pan.Top = y;
                pan.Width = panelMeasurements.Width;
                pan.Left = 0;
                panelMeasurements.Controls.Add(pan);
                y += pan.Height;
            }
        }

        private Hashtable measurements
        {
            get
            {
                Hashtable table = new Hashtable();
                foreach (Control c in panelMeasurements.Controls)
                {
                    if (!(c is RegessionAliasMeasureUserControl))
                    {
                        continue;
                    }
                    RegessionAliasMeasureUserControl p = c as RegessionAliasMeasureUserControl;
                    Hashtable t = p.Table;
                    foreach (int i in t.Keys)
                    {
                        table[i] = t[i];
                    }
                }
                return table;
            }
            set
            {
                foreach (Control c in panelMeasurements.Controls)
                {
                    if (!(c is RegessionAliasMeasureUserControl))
                    {
                        continue;
                    }
                    RegessionAliasMeasureUserControl p = c as RegessionAliasMeasureUserControl;
                    p.Table = value;
                }
            }
        }

        private void fillSelections()
        {
            List<IStructuredSelectionCollection> sel = regression.Selections;
            Hashtable t = regression.SelectionsNames;
            int y = 0;
            foreach (IStructuredSelectionCollection s in sel)
            {
                RegressionSelectionUserControl panel = new RegressionSelectionUserControl(regression, s);
                panel.Width = panelSelections.Width;
                panel.Left = 0;
                panel.Top = y;
                panelSelections.Controls.Add(panel);
                panel.Table = t;
                y += panel.Height;
                Panel pan = new Panel();
                pan.Height = 3;
                pan.BackColor = Color.Black;
                pan.Top = y;
                pan.Width = panelSelections.Width;
                pan.Left = 0;
                panelSelections.Controls.Add(pan);
                y += pan.Height;
            }
        }

        private void Back()
        {
            Dictionary<IAliasName, double> d = stack.Pop();
            foreach (IAliasName an in d.Keys)
            {
                an.Value = d[an];
            }
            if (texts.Count > 0)
            {
                string s = texts.Pop();
                labelSigma0.Text = s;
            }
        }

        private Hashtable selections
        {
            get
            {
                Hashtable t = new Hashtable();
                foreach (Control c in panelSelections.Controls)
                {
                    if (!(c is RegressionSelectionUserControl))
                    {
                        continue;
                    }
                    RegressionSelectionUserControl p = c as RegressionSelectionUserControl;
                    p.Fill(t);
                }
                return t;
            }
        }

        private void buttonAcceptAliasesNumber_Click(object sender, EventArgs e)
        {
            int n = (int)numericUpDownAliases.Value;
            createAliasPanels(n);

        }

        private void buttonAccept_Click(object sender, EventArgs e)
        {
            try
            {
                regression.Aliases = aliases;
                regression.MeasuresNames = measurements;
                regression.SelectionsNames = selections;
                regression.Init();
            }
            catch (Exception ex)
            {
                ex.ShowError(10);
                this.ShowError(ex);
            }

        }

        private void buttonIterate_Click(object sender, EventArgs e)
        {
            try
            {
                Dictionary<IAliasName, double> dict = regression.Backup;
                texts.Push(labelSigma0.Text);
                stack.Push(dict);
                buttonBack.Enabled = true;
                regression.FullIterate();
            }
            catch (Exception exc)
            {
                exc.ShowError(1);
                return;
            }
            try
            {
                labelSigma0.Text = Math.Sqrt(regression.SquareResidual / regression.DataDimension) + "";
                IDesktop r = label.Root.Desktop;
                if (r is PanelDesktop)
                {
                    PanelDesktop d = r as PanelDesktop;
                    foreach (Control c in d.Controls)
                    {
                        update(c);
                    }
                    d.Refresh();
                }
            }
            catch (Exception ex)
            {
                ex.ShowError(1);
            }
        }

        void update(Control control)
        {
            IUpdatableSelection upd = control.GetSimpleObject<IUpdatableSelection>();
            if (upd != null)
            {
                upd.UpdateSelection();
            }
            foreach (Control c in control.Controls)
            {
                update(c);
            }
        }

        private void buttonBack_Click(object sender, EventArgs e)
        {
            Back();
            if (stack.Count == 0)
            {
                buttonBack.Enabled = false;
            }
        }

    }
}

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