Click here to Skip to main content
15,894,343 members
Articles / Programming Languages / C#

Universal Framework for Science and Engineering - Part 2: Regression

Rate me:
Please Sign up or sign in to vote.
4.77/5 (19 votes)
11 Jul 20067 min read 51.2K   5K   76  
An article on universal scalable engineering framework applications.
using System;
using System.Drawing;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

using CategoryTheory;
using DiagramUI;
using FormulaEditor;
using FormulaEditorUI;
using DataPerformer;
using DataPerformerUI;
using BitmapConsumer;


namespace ImageTransformations
{
	/// <summary>
	/// Summary description for FormColorTransformer.
	/// </summary>
	public class FormColorTransformer : System.Windows.Forms.Form, IUpdatableForm
	{
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private static readonly string[] head = new string[]{"r ", "g ","b "};
		private IObjectLabel label;
		private System.Data.DataSet dataSetAliases;
		private System.Windows.Forms.Label labelProt;
		private System.Windows.Forms.PictureBox pictureBoxPrototype;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Panel panelComments;
		private System.Windows.Forms.Panel panelBitmap;
		private System.Windows.Forms.TabControl tabControlMain;
		private System.Windows.Forms.TabPage tabPageBitmap;
		private System.Windows.Forms.TabPage tabPageParameters;
		private System.Windows.Forms.Panel panelFormula;
		private System.Windows.Forms.DataGrid dataGridConstants;
		private System.Data.DataTable dataTableConstants;
		private System.Data.DataColumn dataColumnParameter;
		private System.Data.DataColumn dataColumnValue;
		private System.Windows.Forms.Button buttonAcceptForm;
		private System.Windows.Forms.Button buttonAcceptPar;
		private System.Windows.Forms.Button buttonRefresh;
		private System.Windows.Forms.Panel panelBmp;
		private System.Windows.Forms.Label labelX;
		private System.Windows.Forms.Label labelY;
		private System.Windows.Forms.Label labelR;
		private System.Windows.Forms.Label labelG;
		private System.Windows.Forms.Label labelB;
		private ColorTransformer transformer;

		public FormColorTransformer()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		public FormColorTransformer(IObjectLabel label)
		{
			InitializeComponent();
			panelBmp.Left = 0;
			panelBmp.Top = 0;
			ResourceService.Resources.LoadControlResources(this);
			this.label = label;
			transformer = label.Object as ColorTransformer;
			UpdateFormUI();
			createFormulaControls();
			fillConstantTable();
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			//NamedComponent.RemoveForm(label);
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(FormColorTransformer));
			this.dataSetAliases = new System.Data.DataSet();
			this.dataTableConstants = new System.Data.DataTable();
			this.dataColumnParameter = new System.Data.DataColumn();
			this.dataColumnValue = new System.Data.DataColumn();
			this.tabControlMain = new System.Windows.Forms.TabControl();
			this.tabPageBitmap = new System.Windows.Forms.TabPage();
			this.buttonRefresh = new System.Windows.Forms.Button();
			this.labelProt = new System.Windows.Forms.Label();
			this.pictureBoxPrototype = new System.Windows.Forms.PictureBox();
			this.label1 = new System.Windows.Forms.Label();
			this.panelComments = new System.Windows.Forms.Panel();
			this.panelBitmap = new System.Windows.Forms.Panel();
			this.panelBmp = new System.Windows.Forms.Panel();
			this.tabPageParameters = new System.Windows.Forms.TabPage();
			this.buttonAcceptPar = new System.Windows.Forms.Button();
			this.buttonAcceptForm = new System.Windows.Forms.Button();
			this.dataGridConstants = new System.Windows.Forms.DataGrid();
			this.panelFormula = new System.Windows.Forms.Panel();
			this.labelX = new System.Windows.Forms.Label();
			this.labelY = new System.Windows.Forms.Label();
			this.labelR = new System.Windows.Forms.Label();
			this.labelG = new System.Windows.Forms.Label();
			this.labelB = new System.Windows.Forms.Label();
			((System.ComponentModel.ISupportInitialize)(this.dataSetAliases)).BeginInit();
			((System.ComponentModel.ISupportInitialize)(this.dataTableConstants)).BeginInit();
			this.tabControlMain.SuspendLayout();
			this.tabPageBitmap.SuspendLayout();
			this.panelBitmap.SuspendLayout();
			this.tabPageParameters.SuspendLayout();
			((System.ComponentModel.ISupportInitialize)(this.dataGridConstants)).BeginInit();
			this.SuspendLayout();
			// 
			// dataSetAliases
			// 
			this.dataSetAliases.DataSetName = "NewDataSet";
			this.dataSetAliases.Locale = new System.Globalization.CultureInfo("ru-RU");
			this.dataSetAliases.Tables.AddRange(new System.Data.DataTable[] {
																				this.dataTableConstants});
			// 
			// dataTableConstants
			// 
			this.dataTableConstants.Columns.AddRange(new System.Data.DataColumn[] {
																					  this.dataColumnParameter,
																					  this.dataColumnValue});
			this.dataTableConstants.TableName = "TableConstants";
			// 
			// dataColumnParameter
			// 
			this.dataColumnParameter.ColumnName = "Parameter";
			this.dataColumnParameter.ReadOnly = true;
			// 
			// dataColumnValue
			// 
			this.dataColumnValue.ColumnName = "Value";
			this.dataColumnValue.DataType = typeof(System.Double);
			// 
			// tabControlMain
			// 
			this.tabControlMain.Controls.Add(this.tabPageBitmap);
			this.tabControlMain.Controls.Add(this.tabPageParameters);
			this.tabControlMain.Location = new System.Drawing.Point(48, 32);
			this.tabControlMain.Name = "tabControlMain";
			this.tabControlMain.SelectedIndex = 0;
			this.tabControlMain.Size = new System.Drawing.Size(1024, 688);
			this.tabControlMain.TabIndex = 0;
			// 
			// tabPageBitmap
			// 
			this.tabPageBitmap.Controls.Add(this.labelB);
			this.tabPageBitmap.Controls.Add(this.labelG);
			this.tabPageBitmap.Controls.Add(this.labelR);
			this.tabPageBitmap.Controls.Add(this.labelY);
			this.tabPageBitmap.Controls.Add(this.labelX);
			this.tabPageBitmap.Controls.Add(this.buttonRefresh);
			this.tabPageBitmap.Controls.Add(this.labelProt);
			this.tabPageBitmap.Controls.Add(this.pictureBoxPrototype);
			this.tabPageBitmap.Controls.Add(this.label1);
			this.tabPageBitmap.Controls.Add(this.panelComments);
			this.tabPageBitmap.Controls.Add(this.panelBitmap);
			this.tabPageBitmap.Location = new System.Drawing.Point(4, 22);
			this.tabPageBitmap.Name = "tabPageBitmap";
			this.tabPageBitmap.Size = new System.Drawing.Size(1016, 662);
			this.tabPageBitmap.TabIndex = 0;
			this.tabPageBitmap.Text = "Bitmap";
			// 
			// buttonRefresh
			// 
			this.buttonRefresh.Location = new System.Drawing.Point(64, 104);
			this.buttonRefresh.Name = "buttonRefresh";
			this.buttonRefresh.TabIndex = 10;
			this.buttonRefresh.Text = "Refresh";
			this.buttonRefresh.Click += new System.EventHandler(this.buttonRefresh_Click);
			// 
			// labelProt
			// 
			this.labelProt.Location = new System.Drawing.Point(80, 47);
			this.labelProt.Name = "labelProt";
			this.labelProt.Size = new System.Drawing.Size(72, 23);
			this.labelProt.TabIndex = 9;
			// 
			// pictureBoxPrototype
			// 
			this.pictureBoxPrototype.Location = new System.Drawing.Point(32, 47);
			this.pictureBoxPrototype.Name = "pictureBoxPrototype";
			this.pictureBoxPrototype.Size = new System.Drawing.Size(32, 32);
			this.pictureBoxPrototype.TabIndex = 8;
			this.pictureBoxPrototype.TabStop = false;
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(56, 15);
			this.label1.Name = "label1";
			this.label1.TabIndex = 7;
			this.label1.Text = "Prototype";
			// 
			// panelComments
			// 
			this.panelComments.BackColor = System.Drawing.SystemColors.ControlLightLight;
			this.panelComments.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
			this.panelComments.Location = new System.Drawing.Point(176, 7);
			this.panelComments.Name = "panelComments";
			this.panelComments.Size = new System.Drawing.Size(784, 112);
			this.panelComments.TabIndex = 6;
			// 
			// panelBitmap
			// 
			this.panelBitmap.AutoScroll = true;
			this.panelBitmap.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.panelBitmap.Controls.Add(this.panelBmp);
			this.panelBitmap.Location = new System.Drawing.Point(72, 143);
			this.panelBitmap.Name = "panelBitmap";
			this.panelBitmap.Size = new System.Drawing.Size(824, 512);
			this.panelBitmap.TabIndex = 5;
			// 
			// panelBmp
			// 
			this.panelBmp.Cursor = System.Windows.Forms.Cursors.Cross;
			this.panelBmp.Location = new System.Drawing.Point(88, 32);
			this.panelBmp.Name = "panelBmp";
			this.panelBmp.TabIndex = 0;
			this.panelBmp.Paint += new System.Windows.Forms.PaintEventHandler(this.panelBmp_Paint);
			this.panelBmp.MouseMove += new System.Windows.Forms.MouseEventHandler(this.panelBmp_MouseMove);
			// 
			// tabPageParameters
			// 
			this.tabPageParameters.Controls.Add(this.buttonAcceptPar);
			this.tabPageParameters.Controls.Add(this.buttonAcceptForm);
			this.tabPageParameters.Controls.Add(this.dataGridConstants);
			this.tabPageParameters.Controls.Add(this.panelFormula);
			this.tabPageParameters.Location = new System.Drawing.Point(4, 22);
			this.tabPageParameters.Name = "tabPageParameters";
			this.tabPageParameters.Size = new System.Drawing.Size(1016, 662);
			this.tabPageParameters.TabIndex = 1;
			this.tabPageParameters.Text = "Parameters";
			// 
			// buttonAcceptPar
			// 
			this.buttonAcceptPar.Location = new System.Drawing.Point(736, 608);
			this.buttonAcceptPar.Name = "buttonAcceptPar";
			this.buttonAcceptPar.Size = new System.Drawing.Size(264, 23);
			this.buttonAcceptPar.TabIndex = 14;
			this.buttonAcceptPar.Text = "Accept input parameters";
			this.buttonAcceptPar.Click += new System.EventHandler(this.buttonAcceptPar_Click);
			// 
			// buttonAcceptForm
			// 
			this.buttonAcceptForm.Location = new System.Drawing.Point(48, 608);
			this.buttonAcceptForm.Name = "buttonAcceptForm";
			this.buttonAcceptForm.Size = new System.Drawing.Size(216, 23);
			this.buttonAcceptForm.TabIndex = 13;
			this.buttonAcceptForm.Text = "Accept formulas";
			this.buttonAcceptForm.Click += new System.EventHandler(this.buttonAcceptForm_Click);
			// 
			// dataGridConstants
			// 
			this.dataGridConstants.DataMember = "";
			this.dataGridConstants.DataSource = this.dataTableConstants;
			this.dataGridConstants.HeaderForeColor = System.Drawing.SystemColors.ControlText;
			this.dataGridConstants.Location = new System.Drawing.Point(800, 376);
			this.dataGridConstants.Name = "dataGridConstants";
			this.dataGridConstants.Size = new System.Drawing.Size(208, 136);
			this.dataGridConstants.TabIndex = 1;
			// 
			// panelFormula
			// 
			this.panelFormula.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.panelFormula.Location = new System.Drawing.Point(96, 40);
			this.panelFormula.Name = "panelFormula";
			this.panelFormula.Size = new System.Drawing.Size(688, 544);
			this.panelFormula.TabIndex = 0;
			// 
			// labelX
			// 
			this.labelX.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(204)));
			this.labelX.Location = new System.Drawing.Point(904, 216);
			this.labelX.Name = "labelX";
			this.labelX.TabIndex = 11;
			this.labelX.Text = "X = ";
			// 
			// labelY
			// 
			this.labelY.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(204)));
			this.labelY.Location = new System.Drawing.Point(904, 264);
			this.labelY.Name = "labelY";
			this.labelY.TabIndex = 12;
			this.labelY.Text = "Y =";
			// 
			// labelR
			// 
			this.labelR.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(204)));
			this.labelR.ForeColor = System.Drawing.Color.Crimson;
			this.labelR.Location = new System.Drawing.Point(904, 312);
			this.labelR.Name = "labelR";
			this.labelR.TabIndex = 13;
			this.labelR.Text = "R = ";
			// 
			// labelG
			// 
			this.labelG.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(204)));
			this.labelG.ForeColor = System.Drawing.Color.Green;
			this.labelG.Location = new System.Drawing.Point(904, 360);
			this.labelG.Name = "labelG";
			this.labelG.TabIndex = 14;
			this.labelG.Text = "G = ";
			// 
			// labelB
			// 
			this.labelB.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(204)));
			this.labelB.ForeColor = System.Drawing.Color.Navy;
			this.labelB.Location = new System.Drawing.Point(904, 416);
			this.labelB.Name = "labelB";
			this.labelB.TabIndex = 15;
			this.labelB.Text = "B =";
			// 
			// FormColorTransformer
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(1088, 733);
			this.Controls.Add(this.tabControlMain);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.Name = "FormColorTransformer";
			this.Text = "FormColorTransformer";
			((System.ComponentModel.ISupportInitialize)(this.dataSetAliases)).EndInit();
			((System.ComponentModel.ISupportInitialize)(this.dataTableConstants)).EndInit();
			this.tabControlMain.ResumeLayout(false);
			this.tabPageBitmap.ResumeLayout(false);
			this.panelBitmap.ResumeLayout(false);
			this.tabPageParameters.ResumeLayout(false);
			((System.ComponentModel.ISupportInitialize)(this.dataGridConstants)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		#region IUpdatableForm Members

		public void UpdateFormUI()
		{
			Text = label.RootName;//NamedComponent.GetText(label);
			ICategoryObject p = transformer.Provider as ICategoryObject;
			if (p == null)
			{
				return;
			}
			IObjectLabel lab = p.Object as IObjectLabel;
			pictureBoxPrototype.Image = NamedComponent.GetImage(lab);
			labelProt.Text = lab.RootName;//NamedComponent.GetText(lab);
		}

		#endregion

		private void createFormulaControls()
		{
			int w = panelFormula.Width;
			int y = 0;
			string[] str = transformer.Formulas;
			int n = str.Length;
			for (int i = 0; i < n; i++)
			{
				PanelFormula p = new PanelFormula(head[i], this, panelFormula.Width, 200, 
					"rgbxyacdfhijklmnopqstuvwz", false, null, null);
				p.Left = 0;
				p.Top = y;
				y += p.Height;
				p.Formula = str[i];
				panelFormula.Controls.Add(p);
			}
		}

		private void refreshConstants()
		{
			Hashtable old = new Hashtable();
			foreach (DataRow row in dataTableConstants.Rows)
			{
				string s = row[0] as string;
				double a = (double) row[1];
				old[s] = a;
			}
			dataTableConstants.Rows.Clear();
			List<string> names = transformer.AliasNames;
			foreach (string n in names)
			{
				double a = 0;
				if (old.ContainsKey(n))
				{
					a = (double) old[n];
				}
				transformer[n] = a;
				dataTableConstants.Rows.Add(new object[]{n, a});
			}
		}

		private void fillConstantTable()
		{
			List<string> names = transformer.AliasNames;
			foreach (string n in names)
			{
				double a = (double) transformer[n];
				dataTableConstants.Rows.Add(new object[]{n, a});
			}
		}

		private void buttonAcceptForm_Click(object sender, System.EventArgs e)
		{
			try
			{
				string[] form = new string[3];
				int i = 0;
				foreach (Control c in panelFormula.Controls)
				{
					if (!(c is PanelFormula))
					{
						continue;
					}
					PanelFormula p = c as PanelFormula;
					form[i] = p.Formula;
					++i;
				}
				transformer.Formulas = form;
				refreshConstants();
			}
			catch (Exception ex)
			{
				DefaultForm.ShowError(this, ex);
			}
		}


		private void buttonRefresh_Click(object sender, System.EventArgs e)
		{
			try
			{
				BitmapConsumerLink.Update(transformer);
				Refresh();
			}
			catch (Exception ex)
			{
				ex = ex;
			}
		}


		private void panelBmp_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
		{
			Bitmap bmp = transformer.Bitmap;
			if (bmp == null)
			{
				return;
			}
			if ((panelBmp.Width != bmp.Width) | (panelBmp.Height != bmp.Height))
			{
				panelBmp.Width = bmp.Width;
				panelBmp.Height = bmp.Height;
			}
			e.Graphics.DrawImage(bmp, 0, 0);
		}

		private void buttonAcceptPar_Click(object sender, System.EventArgs e)
		{
			foreach (DataRow row in dataTableConstants.Rows)
			{
				string s = (string) row[0];
				double a = (double) row[1];
				transformer[s] = a;
			}
		}

		private void panelBmp_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			int x = e.X;
			int y = e.Y;
			Bitmap bmp = transformer.Bitmap;
			if (bmp == null)
			{
				return;
			}
			if ((x >= bmp.Width) | (y >= bmp.Height))
			{
				return;
			}
			labelX.Text = "X = " + x;
			labelY.Text = "Y = " + y;
			Color c = bmp.GetPixel(x, y);
			labelR.Text = "R = " + c.R;
			labelG.Text = "G = " + c.G;
			labelB.Text = "B = " + c.B;
		}
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


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