Click here to Skip to main content
15,894,343 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.4K   3.4K   18  
Further promotion of integration ideas
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

using BaseTypes.Interfaces;

using Chart;
using Chart.Utils;
using Chart.Indicators;

using DataPerformer;


namespace DataPerformerUI.UserControls
{
    /// <summary>
    /// Tab series control
    /// </summary>
    public partial class UserControlSeriesTab : UserControl, IObjectOperation,
        FormulaEditor.Interfaces.IVariableDetector, 
        FormulaEditor.Interfaces.IOperationAcceptor, IPowered
    {
        #region Fields

        private Series series;

        private double currentTime;

        private event Action<string> setFormula = (string formula) => { };

        FormulaEditor.ObjectFormulaTree tree;

        private FormulaEditor.Interfaces.IFormulaObjectCreator creator = null;

        #endregion

        #region Ctor

        /// <summary>
        /// Default constructor
        /// </summary>
        public UserControlSeriesTab()
        {
            InitializeComponent();
            userControlSeries.ShowStrip(false);
            SeriesPainterControlPovider sp =
                new SeriesPainterControlPovider(toolStripButtonType, pic,
                    StaticPerformerDataPerformerUI.DefaultSeriesPaintingArray);
            userControlSeries.PainterProvider = sp;
            userControlSeries.Performer.SetMouseIndicator(
               toolStripStatusCoord);
            creator = VariableDetector.GetCreator(this);
            
        }

        #endregion

        #region IObjectOperation Members

        object[] IObjectOperation.InputTypes
        {
            get { return new object[0]; }
        }

        object IObjectOperation.this[object[] x]
        {
            get { return currentTime; }
        }

        object IObjectOperation.ReturnType
        {
            get { return (double)0; }
        }

        #endregion

        #region IVariableDetector Members

        FormulaEditor.Interfaces.IOperationAcceptor 
            FormulaEditor.Interfaces.IVariableDetector.Detect(FormulaEditor.Symbols.MathSymbol sym)
        {
            if (sym.Symbol == 't')
            {
                return this;
            }
            return null;
        }

        #endregion

        #region IOperationAcceptor Members

        IObjectOperation FormulaEditor.Interfaces.IOperationAcceptor.Accept(object type)
        {
            return this;
        }

        #endregion

        #region IPowered Members

        bool IPowered.IsPowered
        {
            get { return true; }
        }

        #endregion

        #region Public Members

        /// <summary>
        /// Series
        /// </summary>
        public Series Series
        {
            get
            {
                return series;
            }
            set
            {
                series = value;
                userControlSeries.Series = value;
                userControlSeriesTable.Series = value;
                SetLabel();
            }
        }


        /// <summary>
        /// Shows chart and all UI
        /// </summary>
        public void ShowAll()
        {
            userFormulaEditor.PreparePoly(new int[] { 15, 11 }, 't', new string[0]);
            userControlSeries.ShowAll();
            userControlSeriesTable.Show = Array.GetShowTable();
            userControlSeriesTable.ShowTable += (bool b) =>
            {
                Array.SetShowTable(b);
            };
        }


        /// <summary>
        /// Loads itself
        /// </summary>
        new public void Load()
        {
            userControlSeries.Post();
        }

        /// <summary>
        /// Set formula event
        /// </summary>
        public event Action<string> SetFormula
        {
            add { setFormula += value; }
            remove { setFormula -= value; }
        }

        #endregion

        #region Private And Internal Members

        internal object[] Array
        {
            get
            {
                return userControlSeries.PainterProvider.Array;
            }
            set
            {
                checkBoxShow.Checked = value.GetShowTable();
                userControlSeries.PainterProvider.Array = value;
            }
        }


        private void SetLabel()
        {
            labelCount.Text = "";
            if (userControlSeriesTable.Series != null)
            {
                labelCount.Text = userControlSeriesTable.Series.Count + "";
            }
            if (series != null)
            {
                if (series.Count >= 2)
                {
                    textBoxStart.Text = series[0, 0] + "";
                    textBoxStep.Text = (series[1, 0] - series[0, 0]) + "";
                    textBoxStepCount.Text = series.Count + "";
                }
            }

        }

        void ExportToXml()
        {
            try
            {
                if (saveFileDialogXml.ShowDialog(this) != DialogResult.OK)
                {
                    return;
                }

                XmlDocument doc = userControlSeriesTable.Series.Xml;
                doc.Save(saveFileDialogXml.FileName);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        void ImportFromXml()
        {
            try
            {
                if (openFileDialogXml.ShowDialog(this) != DialogResult.OK)
                {
                    return;
                }
                XmlDocument doc = new XmlDocument();
                doc.Load(openFileDialogXml.FileName);
                userControlSeriesTable.Series.Xml = doc;
                userControlSeries.ShowAll();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        void CreateTree()
        {
            tree = FormulaEditor.ObjectFormulaTree.CreateTree(
                FormulaEditor.MathFormula.FromString(new int[4], userFormulaEditor.Formula).FullTransform(null), 
                creator);
            Double a = 0;
            if (!tree.ReturnType.Equals(a))
            {
                throw new Exception();
            }
            setFormula(userFormulaEditor.Formula);
        }

        void Generate()
        {
            try
            {
                CreateTree();
                double start = Double.Parse(textBoxStart.Text);
                double step = Double.Parse(textBoxStep.Text);
                int stepCount = Int32.Parse(textBoxStepCount.Text);
                double[,] xy = new double[stepCount, 2];
                for (int i = 0; i < stepCount; i++)
                {
                    currentTime = start + (i * step);
                    xy[i, 0] = currentTime;
                    xy[i, 1] = (double)tree.Result;
                }
                series.Clear();
                for (int i = 0; i < stepCount; i++)
                {
                    series.AddXY(xy[i, 0], xy[i, 1]);
                }
                userControlSeries.ShowAll();
                userControlSeriesTable.FillTable(checkBoxShow.Checked);
                labelCount.Text = series.Count + "";
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }

        internal string Formula
        {
            set
            {
                userFormulaEditor.Formula = value;
            }
        }

        #endregion

        #region Event Handlers

        private void userControlSeriesTable_Update()
        {
            userControlSeries.ShowAll();
            SetLabel();
        }

        private void checkBoxShow_CheckedChanged(object sender, EventArgs e)
        {
            bool ch = checkBoxShow.Checked;
            object[] array = userControlSeries.Array;
            if (ch != array.GetShowTable())
            {
                array.SetShowTable(ch);
                userControlSeriesTable.Show = ch;
            }
        }

        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            userControlSeriesTable.UpdateTable();
        }

        private void openToolStripButton_Click(object sender, EventArgs e)
        {
            userControlSeries.Open();
        }

        private void saveToolStripButton_Click(object sender, EventArgs e)
        {
            userControlSeries.Save();
        }

        private void toolStripButtonRefresh_Click(object sender, EventArgs e)
        {
            userControlSeries.ShowAll();
        }


        private void exportToXmlToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ExportToXml();
        }

        private void importFromXmlToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ImportFromXml();
        }

 
        private void buttonGenerate_Click(object sender, EventArgs e)
        {
            Generate();
        }
        #endregion

    }
}

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