using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Text;
using System.IO;
// using Microsoft.SqlServer;
using System.Data.SqlClient;
using System.Data.SqlTypes;

// using Microsoft.DirectX;
// using Microsoft.DirectX.Direct3D;

using Spherical.HTM;
using Spherical;


namespace hcalc
{
	/// <summary>
	/// Summary description for Form1.
	/// With each keypress in all fields: RA, DEC, x,y,z
	/// and hid, htmname
	/// the current field is updated and so are the others
	/// If keypressed in ra or dec --> updateXYZ --> cartesiantoname ...
	/// if keypressed in x,y,z --> updatradec --> cartesiantoname ...
	/// Keep a SpatialVector and a HtmTtrixel about for this
	/// 
	/// </summary>
	public class HtmCalcForm : System.Windows.Forms.Form
	{
		private static HtmCalcForm form = new HtmCalcForm();
		private static Htmcanvas canvas = new Htmcanvas();
        private static WDB _wdb = null;
		private  Cartesian _formFector;
		private  bool updating = false;
		private  int _level = 3;
		private bool _initialized = false;

		//private static int rx = 40;
		//private static int ry = 42;
		//private static int rw = 0;
		//private static int rh = 0;
		//private static int rdx = -1;
		//private static int rdy = -1;
		//private static int rddy = 1; // Down is +y

		// private static bool rrunning = true;

		private System.Windows.Forms.Button Quit;
		private System.Windows.Forms.Label labelRA;
		private System.Windows.Forms.Label labelDEC;
		private System.Windows.Forms.TextBox textBoxRA;
		private System.Windows.Forms.TextBox textBoxDEC;
		private System.Windows.Forms.TextBox textBoxHTMname;
		private System.Windows.Forms.TextBox textBoxHTMID;
		private System.Windows.Forms.Label labelHtmName;
		private System.Windows.Forms.Label labelHtmID;
		private System.Windows.Forms.Label labelX;
		private System.Windows.Forms.Label labelY;
		private System.Windows.Forms.Label labelZ;
		private System.Windows.Forms.TextBox textBoxX;
		private System.Windows.Forms.TextBox textBoxY;
		private System.Windows.Forms.TextBox textBoxZ;
		private System.Windows.Forms.Label b;
		private System.Windows.Forms.Button update;
		private System.Windows.Forms.Label labelLevel;
		private System.Windows.Forms.TextBox textBoxLevel;
		private System.Windows.Forms.PictureBox picture;
		private System.Timers.Timer timer1;
		private System.Windows.Forms.TextBox textBoxHTMdirectID;
		private System.Windows.Forms.TextBox textBoxHTMhex;
		private System.Windows.Forms.MainMenu mainMenu1;
		private System.Windows.Forms.MenuItem menuItem1;
		private System.Windows.Forms.MenuItem NewMenuItem;
		private System.Windows.Forms.TextBox textBoxTruncatedHID;
		private System.Windows.Forms.TextBox textBoxTruncatedHIGH;
		private System.Windows.Forms.Label labelAnglesLabel;
		private System.Windows.Forms.Label labelAnglesValues;
		private Button centerhid;
		private Button button_direct3D;
		private Button button_3dfw;
		private ErrorProvider errorProvider1;
		private TrackBar twist_trackBar;
		private Label coneangle_label;
		private TrackBar depth_trackBar;
		private Label depth_label;
		private TrackBar zoom_trackBar;
		private Label zoom_label;
		private MenuItem menuItem2;
		private Button rewind;
		private CheckBox enableAlgoTrace;
		private Button step;
		private Label labelAlgTrace;
		private CheckBox checkBoxTrixels;
		private CheckBox checkBoxConvexes;
		private CheckBox checkBoxGrid;
		private Button back;
		private MenuItem menuItem3;
		private MenuItem menuItem4;
		private Button Px;
		private CheckBox checkBoxDuper;
		private CheckBox checkBoxAxes;
        private CheckBox checkBoxAnnotate;
        private Button wdbButton;
        private Button DoPov;
        private Button rndButton;
        private CheckBox checkBoxCurvetrixel;
		private IContainer components;

		private enum ChangedField
		{
			Radec,
			XYZ,
			Htmname,
			HtmID,
			Level,
			Unkown
		}
		public HtmCalcForm() {
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			this.Text += ApplicationDescription.version;
			//
			// TODO: Add any constructor code after InitializeComponent call
			//


		}
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose(bool disposing) {
			if (disposing) {
				if (components != null) {
					components.Dispose();
				}
			}
			base.Dispose(disposing);
		}
		private void updateHTM() {
			char[] name = new char[Trixel.eMaxNameSize];
			string Sname;
			double x, y, z;
			Int64 htmid;
			Int64 htmdirect;
			if (_formFector.Norm < 1.0 - Const.Epsilon) {
				this.textBoxHTMname.Text = "Vector Bogus- ";
				return;
			} else if (_formFector.Norm > 1.0 + Const.Epsilon) {
				this.textBoxHTMname.Text = "Vector Bogus+ ";
				return;
			}
			x = _formFector.X;
			y = _formFector.Y;
			z = _formFector.Z;
            //Trixel.CartesianToName(x, y, z, _level + 1, name);
            //Sname = new string(name, 0, _level + 2);
            Sname = Trixel.CartesianToString(x, y, z, _level);
			this.textBoxHTMname.Text = Sname;
			htmid = Trixel.NameToHid(name, _level + 2);
			htmdirect = Trixel.CartesianToHid(x, y, z, _level);
			////
			////
			this.textBoxHTMID.Text = htmid.ToString();
			this.textBoxHTMdirectID.Text = htmdirect.ToString();
			this.textBoxHTMhex.Text = htmdirect.ToString("x");
			////
			Int64Pair lohi;
			lohi = Trixel.Extend(htmdirect, 20);
			this.textBoxTruncatedHID.Text = lohi.lo.ToString("D");
			this.textBoxTruncatedHIGH.Text = lohi.hi.ToString("D");
			// this.textBoxLevel.Text = trixel.levelOfHid(htmid).ToString();
			return;
		}
		/// <summary>
		/// Updates fiels like angles and area
		/// </summary>
		/// <param name="hid"></param>
		private void updateTrivia(Int64 hid) {
			double A = 0.0, B = 0.0, C = 0.0;
			double Area;
			StringBuilder sb;
			sb = new StringBuilder();
			double[] v0 = new double[3];
			double[] v1 = new double[3];
			double[] v2 = new double[3];

			StringWriter sw = new StringWriter(sb);
			//
			// Get the triangle for the trixel
			char[] name = new char[Trixel.eMaxNameSize];
			canvas.sethid(hid);
			canvas.setXYZ(_formFector.X, _formFector.Y, _formFector.Z);
			picture.Invalidate();
			if (hid < 8) {
				this.labelAnglesValues.Text = "Undefined trixel";
			} else {

				//
				// compute internal angle along great circles from v0 to v1, and
				// v1 to v2 and v2 to v0
				// A is angle between plane v0 x v1 and v2 x v0
				// B is angle between plane v1 x v2 and v0 x v1
				// C is angle between plane v2 x v0 and v1 x v2

				Trixel.GetAngles(hid, out A, out B, out C);
				// trixel.getAngles(hid, out A, out B, out C);
				Area = A + B + C - Math.PI;
				Area *= (Const.Radian2Degree * Const.Radian2Degree);
				A *= Const.Radian2Degree;
				B *= Const.Radian2Degree;
				C *= Const.Radian2Degree;
				if (Area > 0.1) {
					sw.WriteLine("{0,8:0.00}, {1,8:0.00}, {2,8:0.00} (A = {3,8:0.000} deg^2)",
						A, B, C, Area);
				} else {
					Area *= 3600.0;
					if (Area > 0.5) {
						sw.WriteLine("{0,8:0.00}, {1,8:0.00}, {2,8:0.00} (A = {3,8:0.000} arcmin^2)",
							A, B, C, Area);
					} else {
						Area *= 3600.0;
						sw.WriteLine("{0,8:0.00}, {1,8:0.00}, {2,8:0.00} (A = {3,8:0.000} arcsec^2)",
							A, B, C, Area);
					}
				}

				this.labelAnglesValues.Text = sb.ToString();
			}
			return;
		}
		private void updateAllbutone(ChangedField theOne) {
			double x, y, z;
			int level;
			int len;
			Int64 htmid = 1;
			Int64Pair lohi;
			char[] name = new char[Trixel.eMaxNameSize];
			Cartesian v0, v1, v2;
			string htmname;


			// Updates all parts of the gui, except the one that changed
			// when user typed something...
			// RA/DEC, XYX, Htmname, HtmID, 
			// HtmHex, lo and hi limits are read-only.
			updating = true;
			switch (theOne) {
				case ChangedField.Htmname:
					//
					// compute new level, compute triangle (centroid) to that
					// new level, and update other forms of HTMs

					htmname = this.textBoxHTMname.Text;
					len = htmname.Length;
					htmname.CopyTo(0, name, 0, len); // name2id uses char[].
					htmid = Trixel.NameToHid(name, len); // TODO:need a string arg function

					if (Trixel.NameToTriangle(name, out v0, out v1, out v2)) {
						double ra, dec;
						_formFector.Set(
							v0.X + v1.X + v2.X,
							v0.Y + v1.Y + v2.Y,
							v0.Z + v1.Z + v2.Z, true);

						this.textBoxX.Text = _formFector.X.ToString();
						this.textBoxY.Text = _formFector.Y.ToString();
						this.textBoxZ.Text = _formFector.Z.ToString();
						Cartesian.Xyz2Radec(_formFector.X, _formFector.Y, _formFector.Z, out ra, out dec);
						this.textBoxRA.Text = ra.ToString();
						this.textBoxDEC.Text = dec.ToString();


						//
						// change other gui components: (1) htmid, and (2) level
						//
						this.textBoxHTMID.Text = htmid.ToString();
						_level = Trixel.LevelOfHid(htmid);
						this.textBoxLevel.Text = Trixel.LevelOfHid(htmid).ToString();
						this.textBoxHTMhex.Text = htmid.ToString("x");
						lohi = Trixel.Extend(htmid, 20);
						this.textBoxTruncatedHID.Text = lohi.lo.ToString("D");
						this.textBoxTruncatedHIGH.Text = lohi.hi.ToString("D");
					}
					this.textBoxHTMdirectID.Text = "ok";
					break;
				case ChangedField.HtmID:
					//
					// compute new level, compute triangle (centroid) to that
					// new level, and update other forms of HTMs.
					// almost the same as above..
					try {
						htmid = Int64.Parse(this.textBoxHTMID.Text);
						if (Trixel.IsValid(htmid)) {
							Trixel.ToName(name, htmid);
							htmname = new string(name, 0, 24);
							if (Trixel.NameToTriangle(name, out v0, out v1, out v2)) {
								_formFector.Set(
									v0.X + v1.X + v2.X,
									v0.Y + v1.Y + v2.Y,
									v0.Z + v1.Z + v2.Z, true);

								double ra, dec;

								this.textBoxX.Text = _formFector.X.ToString();
								this.textBoxY.Text = _formFector.Y.ToString();
								this.textBoxZ.Text = _formFector.Z.ToString();
								Cartesian.Xyz2Radec(_formFector.X, _formFector.Y, _formFector.Z, out ra, out dec);
								this.textBoxRA.Text = ra.ToString();
								this.textBoxDEC.Text = dec.ToString();


								//
								// change other gui components: (1) htmid, and (2) level
								//
								Trixel.ToName(name, htmid);
								htmname = new string(name, 0, 24);
								_level = Trixel.LevelOfHid(htmid);
								this.textBoxLevel.Text = Trixel.LevelOfHid(htmid).ToString();
								this.textBoxHTMhex.Text = htmid.ToString("x");
								this.textBoxHTMname.Text = htmname;
								lohi = Trixel.Extend(htmid, 20);
								this.textBoxTruncatedHID.Text = lohi.lo.ToString("D");
								this.textBoxTruncatedHIGH.Text = lohi.hi.ToString("D");
								this.textBoxHTMdirectID.Text = "HtmID ok";
							} else {
								this.textBoxHTMdirectID.Text = "HtmID error";
							}
						} else {
							this.textBoxHTMdirectID.Text = "Invalid HtmID (new)";
						}
					} catch {
						this.textBoxHTMdirectID.Text = "HtmID format error";
					}
					break;
				case ChangedField.Level:
					try {
						level = int.Parse(this.textBoxLevel.Text);
						if (level > 24) {
							level = 24;
							this.textBoxLevel.Text = "24";
						}
						if (level < 0) {
							this.textBoxLevel.Text = "0";
							level = 0;
						}
						_level = level;

						//
						// Only the level changed, not the XYZ or RADEC
						// which are in sync. vector is valid
						// Do as if ra/dec and xyz changed, do not update thise fields
						// but do everything else. The value of level is current and good.
						x = _formFector.X;
						y = _formFector.Y;
						z = _formFector.Z;
						htmid = Trixel.CartesianToHid(x, y, z, level);
						Trixel.ToName(name, htmid);
						htmname = new string(name, 0, 24);

						this.textBoxHTMID.Text = htmid.ToString();
						this.textBoxHTMname.Text = htmname;
						//
						this.textBoxHTMdirectID.Text = "level ok";
						this.textBoxHTMhex.Text = htmid.ToString("x");
						lohi = Trixel.Extend(htmid, 20);
						this.textBoxTruncatedHID.Text = lohi.lo.ToString("D");
						this.textBoxTruncatedHIGH.Text = lohi.hi.ToString("D");
					} catch {
						this.textBoxHTMdirectID.Text = "Level incorrect";
					}
					break;
				case ChangedField.Radec:
					try {
						double ra, dec;
						ra = double.Parse(this.textBoxRA.Text);
						dec = double.Parse(this.textBoxDEC.Text);

						Cartesian.Radec2Xyz(ra, dec, out x, out y, out z);
						_formFector.Set(x, y, z, false);

						this.textBoxX.Text = _formFector.X.ToString();
						this.textBoxY.Text = _formFector.Y.ToString();
						this.textBoxZ.Text = _formFector.Z.ToString();
						// 
						// Leave level alone, and compute HTMname to level.
						//
						level = int.Parse(this.textBoxLevel.Text);
						htmid = Trixel.CartesianToHid(x, y, z, level);
						Trixel.ToName(name, htmid);
						htmname = new string(name, 0, 24);

						this.textBoxHTMID.Text = htmid.ToString();
						this.textBoxHTMname.Text = htmname;
						//
						this.textBoxHTMdirectID.Text = "ra/dec ok";
						this.textBoxHTMhex.Text = htmid.ToString("x");
						lohi = Trixel.Extend(htmid, 20);
						this.textBoxTruncatedHID.Text = lohi.lo.ToString("D");
						this.textBoxTruncatedHIGH.Text = lohi.hi.ToString("D");

					} catch {
						this.textBoxHTMdirectID.Text = "ra/dec error";
					}
					break;
				case ChangedField.XYZ:
					//
					// Change ra, dec, htmid, leave level alone
					//
					try {
						x = double.Parse(this.textBoxX.Text);
						y = double.Parse(this.textBoxY.Text);
						z = double.Parse(this.textBoxZ.Text);
						_formFector.Set(x, y, z, true);

						double ra, dec;
						Cartesian.Xyz2Radec(_formFector.X, _formFector.Y, _formFector.Z, out ra, out dec);
						this.textBoxRA.Text = ra.ToString();
						this.textBoxDEC.Text = dec.ToString();
						// 
						// Leave level alone, and compute HTMname to level.
						//
						level = int.Parse(this.textBoxLevel.Text);
						htmid = Trixel.CartesianToHid(x, y, z, level);
						Trixel.ToName(name, htmid);
						htmname = new string(name, 0, 24);

						this.textBoxHTMID.Text = htmid.ToString();
						this.textBoxHTMname.Text = htmname;
						//
						//
						this.textBoxHTMdirectID.Text = "XYZ ok";
						this.textBoxHTMhex.Text = htmid.ToString("x");
						lohi = Trixel.Extend(htmid, 20);
						this.textBoxTruncatedHID.Text = lohi.lo.ToString("D");
						this.textBoxTruncatedHIGH.Text = lohi.hi.ToString("D");
					} catch {
						this.textBoxHTMdirectID.Text = "XYZ error";
					}
					break;
			}
			updateTrivia(htmid);
			updating = false;
		}

		#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() {
            this.components = new System.ComponentModel.Container();
            this.Quit = new System.Windows.Forms.Button();
            this.labelRA = new System.Windows.Forms.Label();
            this.labelDEC = new System.Windows.Forms.Label();
            this.textBoxRA = new System.Windows.Forms.TextBox();
            this.textBoxDEC = new System.Windows.Forms.TextBox();
            this.textBoxHTMname = new System.Windows.Forms.TextBox();
            this.textBoxHTMID = new System.Windows.Forms.TextBox();
            this.labelHtmName = new System.Windows.Forms.Label();
            this.labelHtmID = new System.Windows.Forms.Label();
            this.labelX = new System.Windows.Forms.Label();
            this.labelY = new System.Windows.Forms.Label();
            this.labelZ = new System.Windows.Forms.Label();
            this.textBoxX = new System.Windows.Forms.TextBox();
            this.textBoxY = new System.Windows.Forms.TextBox();
            this.textBoxZ = new System.Windows.Forms.TextBox();
            this.update = new System.Windows.Forms.Button();
            this.b = new System.Windows.Forms.Label();
            this.labelLevel = new System.Windows.Forms.Label();
            this.textBoxLevel = new System.Windows.Forms.TextBox();
            this.picture = new System.Windows.Forms.PictureBox();
            this.timer1 = new System.Timers.Timer();
            this.textBoxHTMdirectID = new System.Windows.Forms.TextBox();
            this.textBoxHTMhex = new System.Windows.Forms.TextBox();
            this.mainMenu1 = new System.Windows.Forms.MainMenu(this.components);
            this.menuItem1 = new System.Windows.Forms.MenuItem();
            this.NewMenuItem = new System.Windows.Forms.MenuItem();
            this.menuItem2 = new System.Windows.Forms.MenuItem();
            this.menuItem3 = new System.Windows.Forms.MenuItem();
            this.menuItem4 = new System.Windows.Forms.MenuItem();
            this.textBoxTruncatedHID = new System.Windows.Forms.TextBox();
            this.textBoxTruncatedHIGH = new System.Windows.Forms.TextBox();
            this.labelAnglesLabel = new System.Windows.Forms.Label();
            this.labelAnglesValues = new System.Windows.Forms.Label();
            this.centerhid = new System.Windows.Forms.Button();
            this.button_direct3D = new System.Windows.Forms.Button();
            this.button_3dfw = new System.Windows.Forms.Button();
            this.errorProvider1 = new System.Windows.Forms.ErrorProvider(this.components);
            this.twist_trackBar = new System.Windows.Forms.TrackBar();
            this.coneangle_label = new System.Windows.Forms.Label();
            this.depth_trackBar = new System.Windows.Forms.TrackBar();
            this.depth_label = new System.Windows.Forms.Label();
            this.zoom_trackBar = new System.Windows.Forms.TrackBar();
            this.zoom_label = new System.Windows.Forms.Label();
            this.rewind = new System.Windows.Forms.Button();
            this.enableAlgoTrace = new System.Windows.Forms.CheckBox();
            this.step = new System.Windows.Forms.Button();
            this.labelAlgTrace = new System.Windows.Forms.Label();
            this.checkBoxGrid = new System.Windows.Forms.CheckBox();
            this.checkBoxConvexes = new System.Windows.Forms.CheckBox();
            this.checkBoxTrixels = new System.Windows.Forms.CheckBox();
            this.back = new System.Windows.Forms.Button();
            this.Px = new System.Windows.Forms.Button();
            this.checkBoxDuper = new System.Windows.Forms.CheckBox();
            this.checkBoxAxes = new System.Windows.Forms.CheckBox();
            this.checkBoxAnnotate = new System.Windows.Forms.CheckBox();
            this.wdbButton = new System.Windows.Forms.Button();
            this.DoPov = new System.Windows.Forms.Button();
            this.rndButton = new System.Windows.Forms.Button();
            this.checkBoxCurvetrixel = new System.Windows.Forms.CheckBox();
            ((System.ComponentModel.ISupportInitialize)(this.picture)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.errorProvider1)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.twist_trackBar)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.depth_trackBar)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.zoom_trackBar)).BeginInit();
            this.SuspendLayout();
            // 
            // Quit
            // 
            this.Quit.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.Quit.Location = new System.Drawing.Point(544, 449);
            this.Quit.Name = "Quit";
            this.Quit.Size = new System.Drawing.Size(80, 24);
            this.Quit.TabIndex = 0;
            this.Quit.Text = "Quit";
            this.Quit.Click += new System.EventHandler(this.Quit_Click);
            // 
            // labelRA
            // 
            this.labelRA.Location = new System.Drawing.Point(16, 16);
            this.labelRA.Name = "labelRA";
            this.labelRA.Size = new System.Drawing.Size(32, 24);
            this.labelRA.TabIndex = 1;
            this.labelRA.Text = "RA";
            this.labelRA.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // labelDEC
            // 
            this.labelDEC.Location = new System.Drawing.Point(16, 40);
            this.labelDEC.Name = "labelDEC";
            this.labelDEC.Size = new System.Drawing.Size(32, 24);
            this.labelDEC.TabIndex = 2;
            this.labelDEC.Text = "DEC";
            this.labelDEC.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // textBoxRA
            // 
            this.textBoxRA.Location = new System.Drawing.Point(56, 16);
            this.textBoxRA.Name = "textBoxRA";
            this.textBoxRA.Size = new System.Drawing.Size(136, 20);
            this.textBoxRA.TabIndex = 3;
            this.textBoxRA.Text = "-76";
            this.textBoxRA.TextChanged += new System.EventHandler(this.textBoxRADEC_TextChanged);
            // 
            // textBoxDEC
            // 
            this.textBoxDEC.Location = new System.Drawing.Point(56, 40);
            this.textBoxDEC.Name = "textBoxDEC";
            this.textBoxDEC.Size = new System.Drawing.Size(136, 20);
            this.textBoxDEC.TabIndex = 4;
            this.textBoxDEC.Text = "38";
            this.textBoxDEC.TextChanged += new System.EventHandler(this.textBoxRADEC_TextChanged);
            // 
            // textBoxHTMname
            // 
            this.textBoxHTMname.Location = new System.Drawing.Point(203, 14);
            this.textBoxHTMname.Name = "textBoxHTMname";
            this.textBoxHTMname.Size = new System.Drawing.Size(176, 20);
            this.textBoxHTMname.TabIndex = 5;
            this.textBoxHTMname.Text = "XY10000000001000000000";
            this.textBoxHTMname.TextChanged += new System.EventHandler(this.textBoxHTMname_TextChanged);
            // 
            // textBoxHTMID
            // 
            this.textBoxHTMID.Location = new System.Drawing.Point(203, 38);
            this.textBoxHTMID.Name = "textBoxHTMID";
            this.textBoxHTMID.Size = new System.Drawing.Size(176, 20);
            this.textBoxHTMID.TabIndex = 6;
            this.textBoxHTMID.Text = "n/a";
            this.textBoxHTMID.TextChanged += new System.EventHandler(this.textBoxHTMID_TextChanged);
            // 
            // labelHtmName
            // 
            this.labelHtmName.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.labelHtmName.Location = new System.Drawing.Point(385, 15);
            this.labelHtmName.Name = "labelHtmName";
            this.labelHtmName.Size = new System.Drawing.Size(56, 23);
            this.labelHtmName.TabIndex = 7;
            this.labelHtmName.Text = "Htm name";
            this.labelHtmName.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.labelHtmName.Click += new System.EventHandler(this.labelHtmName_Click);
            // 
            // labelHtmID
            // 
            this.labelHtmID.Location = new System.Drawing.Point(387, 39);
            this.labelHtmID.Name = "labelHtmID";
            this.labelHtmID.Size = new System.Drawing.Size(55, 23);
            this.labelHtmID.TabIndex = 8;
            this.labelHtmID.Text = "HTMID";
            this.labelHtmID.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.labelHtmID.Click += new System.EventHandler(this.labelHtmID_Click);
            // 
            // labelX
            // 
            this.labelX.Location = new System.Drawing.Point(32, 72);
            this.labelX.Name = "labelX";
            this.labelX.Size = new System.Drawing.Size(16, 24);
            this.labelX.TabIndex = 9;
            this.labelX.Text = "X";
            this.labelX.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // labelY
            // 
            this.labelY.Location = new System.Drawing.Point(32, 96);
            this.labelY.Name = "labelY";
            this.labelY.Size = new System.Drawing.Size(16, 24);
            this.labelY.TabIndex = 10;
            this.labelY.Text = "Y";
            this.labelY.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // labelZ
            // 
            this.labelZ.Location = new System.Drawing.Point(32, 120);
            this.labelZ.Name = "labelZ";
            this.labelZ.Size = new System.Drawing.Size(16, 24);
            this.labelZ.TabIndex = 3;
            this.labelZ.Text = "Z";
            this.labelZ.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // textBoxX
            // 
            this.textBoxX.Location = new System.Drawing.Point(56, 72);
            this.textBoxX.Name = "textBoxX";
            this.textBoxX.Size = new System.Drawing.Size(136, 20);
            this.textBoxX.TabIndex = 12;
            this.textBoxX.Text = "0.00";
            this.textBoxX.TextChanged += new System.EventHandler(this.textBoxXYZ_TextChanged);
            // 
            // textBoxY
            // 
            this.textBoxY.Location = new System.Drawing.Point(56, 96);
            this.textBoxY.Name = "textBoxY";
            this.textBoxY.Size = new System.Drawing.Size(136, 20);
            this.textBoxY.TabIndex = 13;
            this.textBoxY.Text = "0.00";
            this.textBoxY.TextChanged += new System.EventHandler(this.textBoxXYZ_TextChanged);
            // 
            // textBoxZ
            // 
            this.textBoxZ.Location = new System.Drawing.Point(56, 120);
            this.textBoxZ.Name = "textBoxZ";
            this.textBoxZ.Size = new System.Drawing.Size(136, 20);
            this.textBoxZ.TabIndex = 14;
            this.textBoxZ.Text = "0.00";
            this.textBoxZ.TextChanged += new System.EventHandler(this.textBoxXYZ_TextChanged);
            // 
            // update
            // 
            this.update.Location = new System.Drawing.Point(203, 116);
            this.update.Name = "update";
            this.update.Size = new System.Drawing.Size(51, 24);
            this.update.TabIndex = 16;
            this.update.Text = "update";
            this.update.Click += new System.EventHandler(this.updateall_Click);
            // 
            // b
            // 
            this.b.Location = new System.Drawing.Point(260, 120);
            this.b.Name = "b";
            this.b.Size = new System.Drawing.Size(117, 16);
            this.b.TabIndex = 17;
            this.b.Text = " ???";
            // 
            // labelLevel
            // 
            this.labelLevel.Location = new System.Drawing.Point(385, 60);
            this.labelLevel.Name = "labelLevel";
            this.labelLevel.Size = new System.Drawing.Size(56, 23);
            this.labelLevel.TabIndex = 19;
            this.labelLevel.Text = "Level";
            this.labelLevel.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
            // 
            // textBoxLevel
            // 
            this.textBoxLevel.Location = new System.Drawing.Point(339, 62);
            this.textBoxLevel.Name = "textBoxLevel";
            this.textBoxLevel.Size = new System.Drawing.Size(40, 20);
            this.textBoxLevel.TabIndex = 20;
            this.textBoxLevel.Text = "3";
            this.textBoxLevel.TextChanged += new System.EventHandler(this.textBoxLevel_TextChanged);
            // 
            // picture
            // 
            this.picture.Cursor = System.Windows.Forms.Cursors.Cross;
            this.picture.Location = new System.Drawing.Point(8, 154);
            this.picture.Name = "picture";
            this.picture.Size = new System.Drawing.Size(320, 320);
            this.picture.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
            this.picture.TabIndex = 23;
            this.picture.TabStop = false;
            this.picture.Click += new System.EventHandler(this.picture_Click);
            this.picture.MouseMove += new System.Windows.Forms.MouseEventHandler(this.picture_MouseMove);
            this.picture.Paint += new System.Windows.Forms.PaintEventHandler(this.picture_Paint);
            this.picture.MouseEnter += new System.EventHandler(this.picture_MouseEnter);
            // 
            // timer1
            // 
            this.timer1.Enabled = true;
            this.timer1.SynchronizingObject = this;
            this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed);
            // 
            // textBoxHTMdirectID
            // 
            this.textBoxHTMdirectID.Font = new System.Drawing.Font("Century Gothic", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.textBoxHTMdirectID.Location = new System.Drawing.Point(440, 136);
            this.textBoxHTMdirectID.Name = "textBoxHTMdirectID";
            this.textBoxHTMdirectID.Size = new System.Drawing.Size(174, 27);
            this.textBoxHTMdirectID.TabIndex = 25;
            this.textBoxHTMdirectID.Text = "n/a";
            this.textBoxHTMdirectID.TextChanged += new System.EventHandler(this.textBoxHTMdirectID_TextChanged);
            // 
            // textBoxHTMhex
            // 
            this.textBoxHTMhex.BackColor = System.Drawing.SystemColors.GradientActiveCaption;
            this.textBoxHTMhex.Font = new System.Drawing.Font("Courier New", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.textBoxHTMhex.Location = new System.Drawing.Point(448, 16);
            this.textBoxHTMhex.Name = "textBoxHTMhex";
            this.textBoxHTMhex.ReadOnly = true;
            this.textBoxHTMhex.Size = new System.Drawing.Size(176, 26);
            this.textBoxHTMhex.TabIndex = 26;
            this.textBoxHTMhex.Text = "n/a";
            // 
            // mainMenu1
            // 
            this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuItem1,
            this.menuItem3});
            // 
            // menuItem1
            // 
            this.menuItem1.Index = 0;
            this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.NewMenuItem,
            this.menuItem2});
            this.menuItem1.Text = "File";
            // 
            // NewMenuItem
            // 
            this.NewMenuItem.Index = 0;
            this.NewMenuItem.Text = "Legacy REGION";
            this.NewMenuItem.Click += new System.EventHandler(this.menuNewClick);
            // 
            // menuItem2
            // 
            this.menuItem2.Index = 1;
            this.menuItem2.Text = "Read mhg";
            this.menuItem2.Click += new System.EventHandler(this.menuMhgClick);
            // 
            // menuItem3
            // 
            this.menuItem3.Index = 1;
            this.menuItem3.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
            this.menuItem4});
            this.menuItem3.Text = "Test";
            // 
            // menuItem4
            // 
            this.menuItem4.Index = 0;
            this.menuItem4.Text = "CosBug";
            this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
            // 
            // textBoxTruncatedHID
            // 
            this.textBoxTruncatedHID.BackColor = System.Drawing.Color.GreenYellow;
            this.textBoxTruncatedHID.Font = new System.Drawing.Font("Courier New", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.textBoxTruncatedHID.Location = new System.Drawing.Point(447, 56);
            this.textBoxTruncatedHID.Name = "textBoxTruncatedHID";
            this.textBoxTruncatedHID.ReadOnly = true;
            this.textBoxTruncatedHID.Size = new System.Drawing.Size(176, 26);
            this.textBoxTruncatedHID.TabIndex = 27;
            this.textBoxTruncatedHID.Text = "n/a";
            // 
            // textBoxTruncatedHIGH
            // 
            this.textBoxTruncatedHIGH.BackColor = System.Drawing.Color.LightSalmon;
            this.textBoxTruncatedHIGH.Font = new System.Drawing.Font("Courier New", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.textBoxTruncatedHIGH.Location = new System.Drawing.Point(448, 88);
            this.textBoxTruncatedHIGH.Name = "textBoxTruncatedHIGH";
            this.textBoxTruncatedHIGH.ReadOnly = true;
            this.textBoxTruncatedHIGH.Size = new System.Drawing.Size(176, 26);
            this.textBoxTruncatedHIGH.TabIndex = 28;
            this.textBoxTruncatedHIGH.Text = "n/a";
            this.textBoxTruncatedHIGH.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
            // 
            // labelAnglesLabel
            // 
            this.labelAnglesLabel.Location = new System.Drawing.Point(339, 154);
            this.labelAnglesLabel.Name = "labelAnglesLabel";
            this.labelAnglesLabel.Size = new System.Drawing.Size(40, 24);
            this.labelAnglesLabel.TabIndex = 32;
            this.labelAnglesLabel.Text = "Angles";
            this.labelAnglesLabel.Click += new System.EventHandler(this.labelAnglesLabel_Click);
            // 
            // labelAnglesValues
            // 
            this.labelAnglesValues.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.labelAnglesValues.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.labelAnglesValues.ForeColor = System.Drawing.SystemColors.Highlight;
            this.labelAnglesValues.Location = new System.Drawing.Point(334, 178);
            this.labelAnglesValues.Name = "labelAnglesValues";
            this.labelAnglesValues.Size = new System.Drawing.Size(289, 51);
            this.labelAnglesValues.TabIndex = 33;
            this.labelAnglesValues.Text = "n/a";
            this.labelAnglesValues.Click += new System.EventHandler(this.labelAnglesValues_Click);
            // 
            // centerhid
            // 
            this.centerhid.Location = new System.Drawing.Point(203, 64);
            this.centerhid.Name = "centerhid";
            this.centerhid.Size = new System.Drawing.Size(75, 23);
            this.centerhid.TabIndex = 34;
            this.centerhid.Text = "centerhid";
            this.centerhid.Click += new System.EventHandler(this.centerhid_Click);
            // 
            // button_direct3D
            // 
            this.button_direct3D.Enabled = false;
            this.button_direct3D.Location = new System.Drawing.Point(402, 450);
            this.button_direct3D.Name = "button_direct3D";
            this.button_direct3D.Size = new System.Drawing.Size(62, 23);
            this.button_direct3D.TabIndex = 35;
            this.button_direct3D.Text = "future 2..";
            this.button_direct3D.Click += new System.EventHandler(this.direct3D_click);
            // 
            // button_3dfw
            // 
            this.button_3dfw.Enabled = false;
            this.button_3dfw.Location = new System.Drawing.Point(334, 450);
            this.button_3dfw.Name = "button_3dfw";
            this.button_3dfw.Size = new System.Drawing.Size(62, 23);
            this.button_3dfw.TabIndex = 36;
            this.button_3dfw.Text = "future...";
            this.button_3dfw.Click += new System.EventHandler(this.button_3dfw_Click);
            // 
            // errorProvider1
            // 
            this.errorProvider1.ContainerControl = this;
            // 
            // twist_trackBar
            // 
            this.twist_trackBar.Cursor = System.Windows.Forms.Cursors.Default;
            this.twist_trackBar.LargeChange = 10;
            this.twist_trackBar.Location = new System.Drawing.Point(334, 402);
            this.twist_trackBar.Maximum = 180;
            this.twist_trackBar.Minimum = -180;
            this.twist_trackBar.Name = "twist_trackBar";
            this.twist_trackBar.Size = new System.Drawing.Size(289, 42);
            this.twist_trackBar.TabIndex = 37;
            this.twist_trackBar.TickFrequency = 30;
            this.twist_trackBar.TickStyle = System.Windows.Forms.TickStyle.TopLeft;
            this.twist_trackBar.Scroll += new System.EventHandler(this.twist_trackBar_Scroll);
            // 
            // coneangle_label
            // 
            this.coneangle_label.AutoSize = true;
            this.coneangle_label.Location = new System.Drawing.Point(339, 371);
            this.coneangle_label.Name = "coneangle_label";
            this.coneangle_label.Size = new System.Drawing.Size(58, 13);
            this.coneangle_label.TabIndex = 38;
            this.coneangle_label.Text = "For Later...";
            // 
            // depth_trackBar
            // 
            this.depth_trackBar.LargeChange = 1;
            this.depth_trackBar.Location = new System.Drawing.Point(334, 233);
            this.depth_trackBar.Maximum = 4;
            this.depth_trackBar.Name = "depth_trackBar";
            this.depth_trackBar.Orientation = System.Windows.Forms.Orientation.Vertical;
            this.depth_trackBar.Size = new System.Drawing.Size(42, 104);
            this.depth_trackBar.TabIndex = 39;
            this.depth_trackBar.Value = 3;
            this.depth_trackBar.Scroll += new System.EventHandler(this.depth_trackBar_Scroll);
            // 
            // depth_label
            // 
            this.depth_label.AutoSize = true;
            this.depth_label.Location = new System.Drawing.Point(367, 324);
            this.depth_label.Name = "depth_label";
            this.depth_label.Size = new System.Drawing.Size(13, 13);
            this.depth_label.TabIndex = 40;
            this.depth_label.Text = "3";
            // 
            // zoom_trackBar
            // 
            this.zoom_trackBar.LargeChange = 1;
            this.zoom_trackBar.Location = new System.Drawing.Point(334, 342);
            this.zoom_trackBar.Maximum = 23;
            this.zoom_trackBar.Name = "zoom_trackBar";
            this.zoom_trackBar.RightToLeftLayout = true;
            this.zoom_trackBar.Size = new System.Drawing.Size(280, 42);
            this.zoom_trackBar.TabIndex = 41;
            this.zoom_trackBar.Scroll += new System.EventHandler(this.zoom_trackBar_Scroll);
            // 
            // zoom_label
            // 
            this.zoom_label.AutoSize = true;
            this.zoom_label.Location = new System.Drawing.Point(599, 342);
            this.zoom_label.Name = "zoom_label";
            this.zoom_label.Size = new System.Drawing.Size(19, 13);
            this.zoom_label.TabIndex = 42;
            this.zoom_label.Text = "10";
            // 
            // rewind
            // 
            this.rewind.Location = new System.Drawing.Point(8, 520);
            this.rewind.Name = "rewind";
            this.rewind.Size = new System.Drawing.Size(49, 27);
            this.rewind.TabIndex = 43;
            this.rewind.Text = "rewind";
            this.rewind.Click += new System.EventHandler(this.rewind_Click);
            // 
            // enableAlgoTrace
            // 
            this.enableAlgoTrace.AutoSize = true;
            this.enableAlgoTrace.Checked = true;
            this.enableAlgoTrace.CheckState = System.Windows.Forms.CheckState.Checked;
            this.enableAlgoTrace.Location = new System.Drawing.Point(21, 494);
            this.enableAlgoTrace.Name = "enableAlgoTrace";
            this.enableAlgoTrace.Size = new System.Drawing.Size(99, 17);
            this.enableAlgoTrace.TabIndex = 44;
            this.enableAlgoTrace.Text = "enable alg trac.";
            this.enableAlgoTrace.CheckedChanged += new System.EventHandler(this.enableAlgoTrace_CheckedChanged);
            // 
            // step
            // 
            this.step.Location = new System.Drawing.Point(118, 520);
            this.step.Name = "step";
            this.step.Size = new System.Drawing.Size(49, 27);
            this.step.TabIndex = 45;
            this.step.Text = "step";
            this.step.Click += new System.EventHandler(this.step_Click);
            // 
            // labelAlgTrace
            // 
            this.labelAlgTrace.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.labelAlgTrace.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.labelAlgTrace.ForeColor = System.Drawing.SystemColors.Highlight;
            this.labelAlgTrace.Location = new System.Drawing.Point(188, 479);
            this.labelAlgTrace.Name = "labelAlgTrace";
            this.labelAlgTrace.Size = new System.Drawing.Size(449, 68);
            this.labelAlgTrace.TabIndex = 46;
            this.labelAlgTrace.Text = "none";
            // 
            // checkBoxGrid
            // 
            this.checkBoxGrid.AutoSize = true;
            this.checkBoxGrid.Checked = true;
            this.checkBoxGrid.CheckState = System.Windows.Forms.CheckState.Checked;
            this.checkBoxGrid.Location = new System.Drawing.Point(399, 250);
            this.checkBoxGrid.Name = "checkBoxGrid";
            this.checkBoxGrid.Size = new System.Drawing.Size(71, 17);
            this.checkBoxGrid.TabIndex = 47;
            this.checkBoxGrid.Text = "show grid";
            this.checkBoxGrid.CheckedChanged += new System.EventHandler(this.checkBoxGrid_CheckedChanged);
            // 
            // checkBoxConvexes
            // 
            this.checkBoxConvexes.AutoSize = true;
            this.checkBoxConvexes.Checked = true;
            this.checkBoxConvexes.CheckState = System.Windows.Forms.CheckState.Checked;
            this.checkBoxConvexes.Location = new System.Drawing.Point(399, 273);
            this.checkBoxConvexes.Name = "checkBoxConvexes";
            this.checkBoxConvexes.Size = new System.Drawing.Size(100, 17);
            this.checkBoxConvexes.TabIndex = 48;
            this.checkBoxConvexes.Text = "show convexes";
            this.checkBoxConvexes.CheckedChanged += new System.EventHandler(this.checkBoxConvexes_CheckedChanged);
            // 
            // checkBoxTrixels
            // 
            this.checkBoxTrixels.AutoSize = true;
            this.checkBoxTrixels.Checked = true;
            this.checkBoxTrixels.CheckState = System.Windows.Forms.CheckState.Checked;
            this.checkBoxTrixels.Location = new System.Drawing.Point(399, 296);
            this.checkBoxTrixels.Name = "checkBoxTrixels";
            this.checkBoxTrixels.Size = new System.Drawing.Size(80, 17);
            this.checkBoxTrixels.TabIndex = 49;
            this.checkBoxTrixels.Text = "show trixels";
            this.checkBoxTrixels.CheckedChanged += new System.EventHandler(this.checkBoxTrixels_CheckedChanged);
            // 
            // back
            // 
            this.back.Location = new System.Drawing.Point(63, 520);
            this.back.Name = "back";
            this.back.Size = new System.Drawing.Size(49, 27);
            this.back.TabIndex = 50;
            this.back.Text = "back";
            this.back.Click += new System.EventHandler(this.step_Click);
            // 
            // Px
            // 
            this.Px.Location = new System.Drawing.Point(203, 90);
            this.Px.Name = "Px";
            this.Px.Size = new System.Drawing.Size(75, 24);
            this.Px.TabIndex = 51;
            this.Px.Text = "Debug Sql";
            this.Px.Click += new System.EventHandler(this.Px_Click);
            // 
            // checkBoxDuper
            // 
            this.checkBoxDuper.AutoSize = true;
            this.checkBoxDuper.Location = new System.Drawing.Point(503, 250);
            this.checkBoxDuper.Name = "checkBoxDuper";
            this.checkBoxDuper.Size = new System.Drawing.Size(83, 17);
            this.checkBoxDuper.TabIndex = 52;
            this.checkBoxDuper.Text = "show Duper";
            this.checkBoxDuper.CheckedChanged += new System.EventHandler(this.checkBoxDuper_CheckedChanged);
            // 
            // checkBoxAxes
            // 
            this.checkBoxAxes.AutoSize = true;
            this.checkBoxAxes.Checked = true;
            this.checkBoxAxes.CheckState = System.Windows.Forms.CheckState.Checked;
            this.checkBoxAxes.Location = new System.Drawing.Point(399, 319);
            this.checkBoxAxes.Name = "checkBoxAxes";
            this.checkBoxAxes.Size = new System.Drawing.Size(77, 17);
            this.checkBoxAxes.TabIndex = 53;
            this.checkBoxAxes.Text = "show Axes";
            this.checkBoxAxes.CheckedChanged += new System.EventHandler(this.checkBoxAxes_CheckedChanged);
            // 
            // checkBoxAnnotate
            // 
            this.checkBoxAnnotate.AutoSize = true;
            this.checkBoxAnnotate.Location = new System.Drawing.Point(503, 273);
            this.checkBoxAnnotate.Name = "checkBoxAnnotate";
            this.checkBoxAnnotate.Size = new System.Drawing.Size(97, 17);
            this.checkBoxAnnotate.TabIndex = 54;
            this.checkBoxAnnotate.Text = "show Annotate";
            this.checkBoxAnnotate.CheckedChanged += new System.EventHandler(this.checkBoxAnnotate_CheckedChanged);
            // 
            // wdbButton
            // 
            this.wdbButton.Location = new System.Drawing.Point(284, 90);
            this.wdbButton.Name = "wdbButton";
            this.wdbButton.Size = new System.Drawing.Size(75, 24);
            this.wdbButton.TabIndex = 55;
            this.wdbButton.Text = "wdbButton";
            this.wdbButton.Click += new System.EventHandler(this.WDB_Click);
            // 
            // DoPov
            // 
            this.DoPov.Location = new System.Drawing.Point(366, 93);
            this.DoPov.Name = "DoPov";
            this.DoPov.Size = new System.Drawing.Size(61, 20);
            this.DoPov.TabIndex = 56;
            this.DoPov.Text = "Pov";
            this.DoPov.UseVisualStyleBackColor = true;
            this.DoPov.Click += new System.EventHandler(this.DoPov_Click);
            // 
            // rndButton
            // 
            this.rndButton.BackColor = System.Drawing.SystemColors.GrayText;
            this.rndButton.Location = new System.Drawing.Point(366, 116);
            this.rndButton.Name = "rndButton";
            this.rndButton.Size = new System.Drawing.Size(45, 23);
            this.rndButton.TabIndex = 57;
            this.rndButton.Text = "rnd";
            this.rndButton.UseVisualStyleBackColor = false;
            this.rndButton.Click += new System.EventHandler(this.rndButton_Click);
            // 
            // checkBoxCurvetrixel
            // 
            this.checkBoxCurvetrixel.AutoSize = true;
            this.checkBoxCurvetrixel.Location = new System.Drawing.Point(503, 296);
            this.checkBoxCurvetrixel.Name = "checkBoxCurvetrixel";
            this.checkBoxCurvetrixel.Size = new System.Drawing.Size(86, 17);
            this.checkBoxCurvetrixel.TabIndex = 58;
            this.checkBoxCurvetrixel.Text = "curve Trixels";
            this.checkBoxCurvetrixel.CheckedChanged += new System.EventHandler(this.checkBoxCurvetrixel_CheckedChanged);
            // 
            // HtmCalcForm
            // 
            this.ClientSize = new System.Drawing.Size(638, 550);
            this.Controls.Add(this.checkBoxCurvetrixel);
            this.Controls.Add(this.rndButton);
            this.Controls.Add(this.DoPov);
            this.Controls.Add(this.wdbButton);
            this.Controls.Add(this.checkBoxAnnotate);
            this.Controls.Add(this.checkBoxAxes);
            this.Controls.Add(this.checkBoxDuper);
            this.Controls.Add(this.Px);
            this.Controls.Add(this.back);
            this.Controls.Add(this.checkBoxTrixels);
            this.Controls.Add(this.checkBoxConvexes);
            this.Controls.Add(this.checkBoxGrid);
            this.Controls.Add(this.labelAlgTrace);
            this.Controls.Add(this.step);
            this.Controls.Add(this.enableAlgoTrace);
            this.Controls.Add(this.rewind);
            this.Controls.Add(this.zoom_label);
            this.Controls.Add(this.zoom_trackBar);
            this.Controls.Add(this.depth_label);
            this.Controls.Add(this.depth_trackBar);
            this.Controls.Add(this.coneangle_label);
            this.Controls.Add(this.twist_trackBar);
            this.Controls.Add(this.button_3dfw);
            this.Controls.Add(this.button_direct3D);
            this.Controls.Add(this.centerhid);
            this.Controls.Add(this.labelAnglesValues);
            this.Controls.Add(this.labelAnglesLabel);
            this.Controls.Add(this.textBoxTruncatedHIGH);
            this.Controls.Add(this.textBoxTruncatedHID);
            this.Controls.Add(this.textBoxHTMhex);
            this.Controls.Add(this.textBoxHTMdirectID);
            this.Controls.Add(this.textBoxLevel);
            this.Controls.Add(this.textBoxZ);
            this.Controls.Add(this.textBoxY);
            this.Controls.Add(this.textBoxX);
            this.Controls.Add(this.textBoxHTMID);
            this.Controls.Add(this.textBoxHTMname);
            this.Controls.Add(this.textBoxDEC);
            this.Controls.Add(this.textBoxRA);
            this.Controls.Add(this.picture);
            this.Controls.Add(this.labelLevel);
            this.Controls.Add(this.b);
            this.Controls.Add(this.update);
            this.Controls.Add(this.labelZ);
            this.Controls.Add(this.labelY);
            this.Controls.Add(this.labelX);
            this.Controls.Add(this.labelHtmID);
            this.Controls.Add(this.labelHtmName);
            this.Controls.Add(this.labelDEC);
            this.Controls.Add(this.labelRA);
            this.Controls.Add(this.Quit);
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.ForeColor = System.Drawing.SystemColors.WindowText;
            this.Menu = this.mainMenu1;
            this.Name = "HtmCalcForm";
            this.Text = "Htm Converter V:";
            this.Activated += new System.EventHandler(this.HtmCalcForm_Activated);
            this.Load += new System.EventHandler(this.HtmCalcForm_Load);
            ((System.ComponentModel.ISupportInitialize)(this.picture)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.errorProvider1)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.twist_trackBar)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.depth_trackBar)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.zoom_trackBar)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() {

			Application.Run(form);
		}
		private void Quit_Click(object sender, System.EventArgs e) {
			canvas.purge();
			form.Close();
			Application.Exit();
		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void textBoxHTMname_TextChanged(object sender, System.EventArgs e) {
			if (updating) {
				return;
			}
			updateAllbutone(ChangedField.Htmname);
			updating = false;
			return;
		}
		private void textBoxHTMID_TextChanged(object sender, System.EventArgs e) {
			// ID changed, change level and htmname
			if (updating) {
				return;
			}
			this.updateAllbutone(ChangedField.HtmID);
			updating = false;
			return;
		}
		private void labelHtmName_Click(object sender, System.EventArgs e) {

		}
		private void labelHtmID_Click(object sender, System.EventArgs e) {

		}
		private void textBoxRA_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e) {
			//
			// try to parse the RA,DEC and then update XYZ, or say
			// format error
			// doesn't matter who sent it, parsing both...
			//
		}
		private void HtmCalcForm_Activated(object sender, System.EventArgs e) {
			if (!_initialized) {
				_formFector = new Cartesian();
				updating = false;
				this.textBoxRA.Text = "-76.0";
				this.textBoxDEC.Text = "38.00";
				this.textBoxLevel.Text = "4";
				// _level = 0;
				_initialized = true;
			}
		}
		private void textBoxRADEC_TextChanged(object sender, System.EventArgs e) {
			if (updating)
				return;

			this.updateAllbutone(ChangedField.Radec);
			return;
		}
		private void textBoxXYZ_TextChanged(object sender, System.EventArgs e) {
				if (updating)
				return;

			this.updateAllbutone(ChangedField.XYZ);
			return;
		}

		private void updateall_Click(object sender, System.EventArgs e) {
			double norm;
			double x, y, z;

			//if (updating)
			//    return;

			this.updateAllbutone(ChangedField.Radec);

			//if (rrunning) {
			//    timer1.Stop();
			//    rrunning = false;
			//} else {
			//    timer1.Start();
			//    rrunning = true;
			//}
			try {
				x = double.Parse(this.textBoxX.Text);
				y = double.Parse(this.textBoxY.Text);
				z = double.Parse(this.textBoxZ.Text);
				norm = x * x + y * y + z * z;
				b.Text = norm.ToString();
			} catch {
				b.Text = "nan";
			}
			picture.Invalidate();
		}

		private void HtmCalcForm_Load(object sender, System.EventArgs e) {

		}

		private void labelHTMExample_Click(object sender, System.EventArgs e) {

		}

		private void canvas_Paint(object sender, System.Windows.Forms.PaintEventArgs e) {

		}
		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void picture_Paint(object sender, System.Windows.Forms.PaintEventArgs e) {
			Graphics g = e.Graphics;
			g.SmoothingMode = SmoothingMode.HighQuality;
			g.PixelOffsetMode = PixelOffsetMode.HighQuality;
			canvas.showgrid(checkBoxGrid.Checked);
			canvas.showtrixels(checkBoxTrixels.Checked);
			canvas.showconvexes(checkBoxConvexes.Checked);
			canvas.contents(g, picture, _wdb);
		}
		private void textBoxLevel_TextChanged(object sender, System.EventArgs e) {
			if (updating)
				return;
			this.updateAllbutone(ChangedField.Level);
			return;

		}
		private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e) {

			picture.Invalidate();
            if(canvas != null) {
                canvas.tick++;
            }
			timer1.Interval = 500.0;
		}
		private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e) {

		}

		private void textBoxHTMdirectID_TextChanged(object sender, System.EventArgs e) {

		}

		private void menuNewClick(object sender, System.EventArgs e) {
			//
			// Clicked File->New
			//
			RegionForm form2 = new RegionForm(this);
			form2.Show();
		}

		private void textBox1_TextChanged(object sender, System.EventArgs e) {
		}

		//private void NormalFormButton_Click(object sender, System.EventArgs e) {
		//    //
		//    // read richtextbox1, get normal form and copy into richtextbox2
		//    //
		//    String s = richTextBox1.Text;
		//    String d = HtmCover.NormalForm(s);
		//    richTextBox2.Text = d;
		//}

		private void labelAnglesLabel_Click(object sender, System.EventArgs e) {

		}

		private void labelAnglesValues_Click(object sender, System.EventArgs e) {

		}

		private void rotateTest_click(object sender, EventArgs e) {
			// Using with debugger
			Camera3D t3 = new Camera3D();
			double ra, dec;
			Cartesian pout = new Cartesian();
			try {
				ra = double.Parse(this.textBoxRA.Text);
				dec = double.Parse(this.textBoxDEC.Text);
				//x = double.Parse(this.textBoxX.Text);
				//y = double.Parse(this.textBoxY.Text);
				//z = double.Parse(this.textBoxZ.Text);
				t3.reset();
				t3.set_camera(ra, dec);
				t3.rotate(1.0, 0.0, 0.0, ref pout);
				double norm = pout.X * pout.X +
					pout.Y * pout.Y + pout.Z * pout.Z;

				t3.reset();
				t3.set_camera(ra, dec);
				t3.rotate(0.0, 1.0, 0.0, ref pout);

				t3.reset();
				t3.set_camera(ra, dec);
				t3.rotate(0.0, 0.0, 1.0, ref pout);


				b.Text = "rot";
			} catch {
				b.Text = "BAD";
			}

		}
		private void picture_Click(object sender, System.EventArgs e) {
			// Right click disabled
			//if (((MouseEventArgs) e).Button == MouseButtons.Right){
			//    MouseEventArgs ma = (MouseEventArgs) e;
			//    int x = ma.X, y = ma.Y;
			//    Int64 hid = canvas.mouse2HID(x, y);
			//    canvas.footnote("RC({0})", hid);
			//    updateTrivia(hid);
			//    // Change the form's hid, as if...
			//}
		}
		private void picture_MouseMove(object sender, MouseEventArgs e) {
			int x = e.X;
			int y = e.Y;
			// canvas.caption2("M {0} {1}", x, y);
			canvas.mousemove(x, y, e.Button, _level);
            picture.Invalidate();
			// this.updateTrivia(hid);
			// RIGHT BUTTON DISABLED
			//if (e.Button == MouseButtons.Right) { // Add trixel to displayable one
			//    Int64 hid = canvas.mouse2HID(x, y);
			//    canvas.footnote("rD({0},{1})", x, y);

			//}
		}

		private void picture_MouseEnter(object sender, EventArgs e) {
			// this.picture.Cursor = 
		}

		private void centerhid_Click(object sender, EventArgs e) {
			canvas.center_formhid();
		}
		

		private void direct3D_click(object sender, EventArgs e) {
			//GlobeDevice.mainGlobe();
		}
		private void make_groundtruth(){

			Int64[] hids = { 1L, 2L, 3L, };
			double[,] foo = { { 1.0, 2.0, 3.0 },
			{22.0, 4.0, 6.0},
			};
			Int64 hid;
			double x, y, z;
			double ra, dec;

			StreamWriter f = new StreamWriter("C:\\htmtmp\\mhg\\gt.cs", false);
			//
			for (ra = 0.0; ra < 360; ra += 12.0) {
				for (dec = -90.0; dec <= 90.0; dec += 15) {
					Cartesian.Radec2Xyz(ra, dec, out x, out y, out z);
					hid = Trixel.CartesianToHid(x, y, z, 20);
					f.WriteLine("{0}, ", hid);
				}
			}
			for (ra = 0.0; ra < 360; ra += 12.0) {
				for (dec = -90.0; dec <= 90.0; dec += 15) {
					Cartesian.Radec2Xyz(ra, dec, out x, out y, out z);
					hid = Trixel.CartesianToHid(x, y, z, 20);
					f.WriteLine("{{{0}, {1}, {2}}}, ", x, y, z);
				}
			}
			f.Close();
		}

		private void button_3dfw_Click(object sender, EventArgs e) {
			//D3HtmForm.create();
			// use it to make a ground truth set for HTMIDs
			//

		}

		private void trackBar1_Scroll(object sender, EventArgs e) {
			int v;
			v = twist_trackBar.Value;
			double angle;
			double D;
			// Interface has a finite resolution
			//
			// Change the sense of scrollbar
			// target angle range is function of zom level;
			double lowAngle = 0.0;
			double rangeAngle = Math.PI;
			int level = canvas.getZoomLevel();
			while (level-- > 0) {
				rangeAngle /= 2.0;
			}
			angle = v;
			angle = (v - twist_trackBar.Minimum);
			angle *= rangeAngle;
			angle /= (twist_trackBar.Maximum - twist_trackBar.Minimum);
			angle += lowAngle;
			D = Math.Cos(angle);
			//
			//if (D > 1.0) D = 1.0;
			//else if (D < -1.0) D = -1.0;
			//
			canvas.setHalfspaceD(D);


		}

		private void depth_trackBar_Scroll(object sender, EventArgs e) {
			int v;
			v = depth_trackBar.Value;
			canvas.setGridlevel(v);
			depth_label.Text = v.ToString();
		}

		private void zoom_trackBar_Scroll(object sender, EventArgs e) {
			int v;
			v = zoom_trackBar.Value;
			// v is grid level in this case
			// limited to 0 - 24 inclusive
			canvas.setZoom(v);
			zoom_label.Text = v.ToString();

		}
		//
		// 
		// This will read files created by HtmCover.RegionTighter()
		// When tracing is enabled, the files are written into the hardwired directory
		// in c:\htmtmp\mhg\ and these files are:
		// ranges (e)
		// saved trixels (i)
		// halfspaces (c)
		// and tracing trixel algorithm (ix.txt)
		private void menuMhgClick(object sender, EventArgs e) {
			renderTraces();
		}
		public void renderTraces() {
			// Read the three files from c:\htmtmp\mhg\
			CalcParser cpar = new CalcParser();
			StreamReader rs;
			String s;
			char type;
			double x, y, z, D;
			Int64 hid;
			bool full;
			char color;

			// (1) parse halfspace "c"
			// (2) parse saved trixels "i"
			// 
			#region [READHAFSPACES_C_]
			// (1) ////////////////////////////////////////////////////////////////////
			// Parse a file with halfspaces in it 
			// Each line is in the form "1 0 0 0.999999957692025 R"
			// where the last character is a hint of color to use to render.
			// This is a hack, but works for now

			try {
				rs = new StreamReader("C:\\htmtmp\\mhg\\c");
			} catch {
				rs = null;
			}
			if (rs == null)
				return;
			canvas.clearHalfspaces();
			while (!rs.EndOfStream) {
				s = rs.ReadLine();
				cpar.input = s; //
				if (cpar.parseHalfspace(out x, out y, out z, out D, out color)) {
					canvas.addHalfspace(x, y, z, D);
					// canvas.displayHalfpaces(boolean) 
					// Same for trixels (2) below
				}
			}

			// rs is not null, it would have returned
			// rs.DiscardBufferedData();
			rs.Close();
			#endregion
			#region [READSAVEDTRIXELS_I_]
			// (2) ////////////////////////////////////////////////////////////////////
			// Parse a file with saved trixels in it 
			// Each line is in the form "            50331648"
			// This is a hack, but works for now

			try {
				rs = new StreamReader("C:\\htmtmp\\mhg\\i");
			} catch {
				rs = null;
			}
			if (rs == null)
				return;
			canvas.clearTracedHIDs();
			while (!rs.EndOfStream) {
				s = rs.ReadLine();
				cpar.input = s; //
				if (cpar.parseHid(out hid, out type)) {
                    
					canvas.addHID(hid, type == 'F');
					//
					// Put it away somewhere
					//
				}
			}
			// rs is not null, it would have returned by now
			// rs.DiscardBufferedData();
			rs.Close();
			#endregion
			#region [READINFOTRIXELS_IX_]
			// (3) ////////////////////////////////////////////////////////////////////
			// Parse a file with algorithm trace  
			// This is a hack, but works for now

			try {
				rs = new StreamReader("C:\\htmtmp\\mhg\\ix.txt");
			} catch {
				rs = null;
			}
			if (rs == null)
				return;
			canvas.clearSteps();
			while (!rs.EndOfStream) {
				s = rs.ReadLine();
				string reason;
				cpar.input = s; //
				if ((reason = cpar.parseStep(out hid, out full)) != null) {
					canvas.addStep(hid, reason);
				}
			}
			#endregion
			#region [READPATCHES_P_]
			// (2) ////////////////////////////////////////////////////////////////////
			// Parse a file with patches (arcs) in it
			// Each line is in the form "x y z  x y z  x y z x y z C"
			// for: p1 pmiddle p2 Halfspace
			//
			// where the last character is a hint of color to use to render.
			// This is a hack, but works for now

			//try {
			//    rs = new StreamReader("C:\\htmtmp\\mhg\\p");
			//} catch {
			//    rs = null;
			//}
			//if (rs == null)
			//    return;
			//canvas.clearHalfspaces();
			//while (!rs.EndOfStream) {
			//    s = rs.ReadLine();
			//    cpar.input = s; //
			//    if (cpar.parseHalfspace(out x, out y, out z, out D, out color)) {
			//        canvas.addHalfspace(x, y, z, D);
			//        // canvas.displayHalfpaces(boolean) 
			//        // Same for trixels (2) below
			//    }
			//}

			#endregion

		}

		private void enableAlgoTrace_CheckedChanged(object sender, EventArgs e) {
			CheckBox cb = (CheckBox)sender;
			canvas.alg_enable(cb.Checked);
			rewind.Enabled = cb.Checked;
			step.Enabled = cb.Checked;
			back.Enabled = cb.Checked;
		}

		private void rewind_Click(object sender, EventArgs e) {
			canvas.alg_rewind();
			canvas.alg_setDisplay();
			labelAlgTrace.Text = canvas.alg_getText();
		}

		private void step_Click(object sender, EventArgs e) {
			if (sender.Equals(step)) {
				canvas.alg_step(1);
			} else if (sender.Equals(back)) {
				canvas.alg_step(-1);
			}
			canvas.alg_setDisplay();
			labelAlgTrace.Text = canvas.alg_getText();

			//int v = 3;
			//zoom_trackBar.Value = v;
			//zoom_label.Text = v.ToString();
			//canvas.setZoom(v);
		}


		private void menuItem4_Click(object sender, EventArgs e) {
			double x, y;
			x = Math.PI / 2.0;
			y = Math.Cos(x);
			double z = y;
			return;
		}

		private void foo(double x0, double y0, double z0, double x1, double y1, double z1) {
			Cartesian X = new Cartesian(x0, y0, z0, true);
			Cartesian Y = new Cartesian(x1, y1, z1, true);
			Cartesian axis = X.Cross(Y, true);



		}
        private void emptyregion()
        {
            Spherical.Region reg = new Spherical.Region(new Halfspace(45.0, 45.0, 0.0));
            // reg.ConvexList[0].Add(new Halfspace(-45.0, -45.0, 0.0));
            reg.Simplify(Const.Tolerance);
            String s = reg.Format();
            return;
        }
        //private void twospheres() {
        //    List<Halfspace> edges = new List<Halfspace>();
        //    Markup mark;
        //    Spherical.Region reg;

        //    reg = Parser.compile("REGION CONVEX J2000 -2 -2 0.99 CONVEX J2000 2 2 0.99");
        //    reg.Simplify(true, Const.Tolerance, Const.MaximumIteration, false);
        //    //SuperRegion sreg = new SuperRegion(reg, true, false);

        //    Int64 hid = 3085L;
        //    Cartesian a, b, c;
        //    Trixel.ToTriangle(hid, out a, out b, out c);
        //    Cover duper = new Cover(reg, false, null);

        //    //mark = duper.testNode(a, b, c, hid);
        //    //canvas.sreg = reg;
        //}
		private void cube() {
			List<Halfspace> edges = new List<Halfspace>();
			Spherical.Region reg;
			Convex con;
			// CONVEX CARTESIAN 1 0 0 -0.6 -1 0 0 -0.6 0 1 0 -0.6 0 -1 0 -0.6 0 0 1 -0.6 0 0 -1 -0.6";
			edges.Add(new Halfspace(1.0, 0.0, 0.0, true, -0.6));
			edges.Add(new Halfspace(-1.0, 0.0, 0.0, true, -0.6));
			edges.Add(new Halfspace(0.0, 1.0, 0.0, true, -0.6));
			edges.Add(new Halfspace(0.0, -1.0, 0.0, true, -0.6));
			edges.Add(new Halfspace(0.0, 0.0, 1.0, true, -0.6));
			edges.Add(new Halfspace(0.0, 0.0, -1.0, true, -0.6));
			con = new Convex(edges);
			reg = new Spherical.Region(con, false);
			reg.Simplify(true, Const.Tolerance, Const.MaximumIteration, false);

		}
		private void colorado() {

			List<Halfspace> edges = new List<Halfspace>();

			edges.Add(new Halfspace(0.164086335552035, 0.579868816326304, 0.798014931149435, true, 0.0));
			edges.Add(new Halfspace(-0.977965791127952, 0.208765206352684, 1.98946234850113E-16, true, 0.0));
			edges.Add(new Halfspace(-0.178849926450745, -0.63204224045915, -0.75401280498672, true, 0.0));
			edges.Add(new Halfspace(0.942349830759932, -0.334629341909413, 0.0, true, 0.0));


		}
		private
			string
			conn = "Data Source=SDSSDB001; Initial Catalog=BESTDR4; User=test; Password=riuzg54; Timeout=50;";
        // conn = ("Initial Catalog=Spatial; Data Source=localhost; Integrated Security=SSPI;");

		private void Px_Click(object sender, EventArgs e) {

            emptyregion();

			String s = Sql.fHtmVersion().ToString();
			SqlConnection cx = new SqlConnection(conn);
			String version;
			SqlDataReader reader = null;
			try {
				cx.Open();

                // SqlCommand command = new SqlCommand("SELECT @@version", cx);
                SqlCommand command = new SqlCommand("SELECT dbo.fHtmVersion()", cx);
                version = command.ExecuteScalar().ToString();

                Type type = null;
				SqlCommand tabs = new SqlCommand("SELECT * from Place", cx);
				reader = tabs.ExecuteReader();
				int fc = reader.FieldCount;
				for (int i = 0; i < fc; i++) {
					type = reader.GetFieldType(i);
					if (type == typeof(String)) {
						String foo = type.ToString();
					}					
				}
				while (reader.Read()) {
					string place = reader.GetString(0);

				}
			} catch (Exception ex) {
				version = ex.Message;
			} finally {
				if (reader != null)
					reader.Close();
				if (cx != null)
					cx.Close();
			}

			// twospheres(); // colorado();

			//foo(1.0, 0.0, 0.0, 0.0, 1.0, 0.0);
			//foo(1.0, 1.0, 0.0, 0.0, 1.0, 0.0); // diagonal to Y axis
			//foo(0.0, 1.0, 0.0, 1.0, 1.0, 0.0); // y axis to diagonal

			//foo(1.0, 0.0, 0.0, 0.5, Math.Sqrt(3.0) / 2.0, 0.0);
			//foo(1.0, 0.0, 0.0, 0.5, -Math.Sqrt(3.0) / 2.0, 0.0);
			//foo(1.0, 0.0, 0.0, -0.5, Math.Sqrt(3.0) / 2.0, 0.0);


			//foo(0.0, 0.0, 1.0, 0.0, Math.Sqrt(3.0) / 2.0, 0.5);



		}
		private void checkBoxGrid_CheckedChanged(object sender, EventArgs e) {
			CheckBox cb = (CheckBox)sender;
			canvas.showgrid(cb.Checked);
		}

		private void checkBoxConvexes_CheckedChanged(object sender, EventArgs e) {
			CheckBox cb = (CheckBox)sender;
			canvas.showconvexes(cb.Checked);

		}

		private void checkBoxTrixels_CheckedChanged(object sender, EventArgs e) {
			CheckBox cb = (CheckBox)sender;
			canvas.showtrixels(cb.Checked);

		}
		private void checkBoxDuper_CheckedChanged(object sender, EventArgs e) {
			CheckBox cb = sender as CheckBox;
			canvas.showduper(cb.Checked);
		}

		private void checkBoxAxes_CheckedChanged(object sender, EventArgs e) {
			CheckBox cb = sender as CheckBox;
			canvas.showaxes = cb.Checked;

		}

        private void checkBoxAnnotate_CheckedChanged(object sender, EventArgs e) {
            CheckBox cb = sender as CheckBox;
            canvas.showannotate = cb.Checked;
        }
        private void checkBoxCurvetrixel_CheckedChanged(object sender, EventArgs e) {
            CheckBox cb = sender as CheckBox;
            canvas.curvetrixels = cb.Checked;

        }
        private void twist_trackBar_Scroll(object sender, EventArgs e) {
            int v = twist_trackBar.Value;
            double angle;
            angle = v;
            canvas.twistAngle = angle * Const.Degree2Radian;
        }

        private void WDB_Click(object sender, EventArgs e) {
            _wdb = new WDB();
            _wdb.makeLowRes();
            wdbButton.Text = "Initied";
            wdbButton.Enabled = false;
        }

        private void DoPov_Click(object sender, EventArgs e) {
            Spherical.Region sreg = new Spherical.Region();
            PovWriter pover = new PovWriter("C:\\htmtmp\\lofasz.pov");
            Convex con = new Convex();
            con.Add(new Halfspace( 1.0,  0.0,  0.0, false, -0.6));
            con.Add(new Halfspace(-1.0,  0.0,  0.0, false, -0.6));
            con.Add(new Halfspace( 0.0,  1.0,  0.0, false, -0.6));
            con.Add(new Halfspace( 0.0, -1.0,  0.0, false, -0.6));
            con.Add(new Halfspace( 0.0,  0.0,  1.0, false, -0.6));
            con.Add(new Halfspace( 0.0,  0.0, -1.0, false, -0.6));
            sreg.Add(con);

            sreg.Add(new Convex(new Halfspace(0.0, 0.0, 1200.0)));
            sreg.Add(new Convex(new Halfspace(10.0, 3.0, 1200.0)));
            pover.Write(sreg, new Cartesian(20.0, 0.0, 1.0, false),  9.0);
            pover.Close();
            sreg.Clear();


        }

        private void rndButton_Click(object sender, EventArgs e) {
            Random r = new Random();
            Int64 hid;
            String name;
            StreamWriter sw = new StreamWriter("C:\\htmtmp\\mhg\\RandomC.dat", false);
            List<Int64> list = new List<Int64>(100000);
            double lat, lon, x, y, z;
            int i = 100000;
            while(i > 0) {
                lon = (double)r.Next(-11000, -8000);
                lat = (double)r.Next(3500, 5000);
                lon /= 100.0;
                lat /= 100.0;
                Cartesian.Radec2Xyz(lon, lat, out x, out y, out z);
                hid = Trixel.CartesianToHid(x, y, z, 20);
                if(!list.Contains(hid)) { // Skip it
                    list.Add(hid);
                    name = Trixel.ToString(hid);
                    // hid  name ST k1 k2 k3 k4 lat, lon 
                    sw.WriteLine("{0}\t{1}\tXX\t1\t2\t3\t4\t{2}\t{3}",
                        hid, name, lat, lon);
                    i--;
                } else {
                    // Do nothing;
                }
            }
            // sw.WriteLine("Stuff here later");
            sw.Close();
        }

        

	}
}
/*
private void textBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e) {
int kval = e.KeyValue;
switch (kval){
case 27:
this.richTextBox1.Text = "";
this.textBox1.Text = "";
break;
default:
this.richTextBox1.Text = this.textBox1.Text;
break;
}		
}
***/
