Saturday, 2 February 2013

visit my new blog found at http://www.weaklist.com

Tuesday, 16 October 2012

C# Simple Code Timer



Tried of writing datetimes all over the place to measure performace, do it the noob way!

public class watch : IDisposable
    {
        private Stopwatch iSWatch;
        private string name;

        public watch(string name)
        {
#if DEBUG
            this.name = name;
            iSWatch = new Stopwatch();
            iSWatch.Start();
#endif
        }

        public void Dispose()
        {
#if DEBUG
            iSWatch.Stop();
            Debug.WriteLine(name + ": " + iSWatch.Elapsed);
#endif
        }
    }







Usage:


using (var timer = new watch("Some Name"))
{
        //Code to time here
}

c# BiCubic Interpolation

So today at work i had to implement a bicubic interpolation implementation to interpolate a geoid for height calculation, here is what i came up with, very noobish, For those interested you can download the example project https://www.dropbox.com/s/bk0cf1a32lms0ai/Interp.zip


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Interp
{
    class Program
    {
        static Importer importer = new Importer();
        static void Main(string[] args)
        {
            importer.load_tol("ausgeoid09.tol");
            importer.load_tor("ausgeoid09.tor");

            // E = X = Long = Col
            // N = Y = Lat  = Lines

            double rett = get_height(132.32, -29.92);  // 4.2
            //rett = get_height(-8, 108);
            //rett = get_height(-8.000001, 130.000001);
            double hight = get_geoid(132.32,-29.92); // 77.935
        }
        static double get_height(double X, double Y)
        {
            int xx = 0;
            int yy = 0;
            xx = (int)((X - importer.UP_LEFT_E) / 0.01666667);
            yy = (int)(( importer.UP_LEFT_N - Y) / 0.01666667);

            return importer.tor_data[ yy * importer.COLUMNS + xx];
        }

        static double Interpolate(double z, double[] height)
        {
            return height[1] + 0.5 * z
                * (height[2] - height[0] + z
                * (2.0 * height[0] - 5.0 * height[1] + 4.0 * height[2] - height[3] + z
                * (3.0 * (height[1] - height[2]) + height[3] - height[0])));
        }

        static double get_geoid(double X, double Y)
        {
            int xx = 0;
            int yy = 0;
            xx = (int)Math.Floor((X - importer.UP_LEFT_E) / 0.01666667) -1 ;
            yy = (int)Math.Floor((importer.UP_LEFT_N - Y) / 0.01666667) -1 ;

            double tX = X % 0.01666667;
            double tY = Y % 0.01666667;
            tX /= 0.01666667;
            tY /= -0.01666667;
           
            double[] four_line = new double[4];
            double[] four_col = new double[4];
            for (int m = 0; m < 4; m++)
            {
                for (int k = 0; k < 4; k++)
                {
                    four_line[k] = importer.tor_data[(yy + m) * importer.COLUMNS + xx + k];
                }
                // calclulate the cubic interpolation
                four_col[m] = Interpolate(tY, four_line);
            }
            double result1 = Interpolate(tX, four_col);
          

            return result1;
        }
    }
}

c# Cubic Interpolation

The method you can use for interpolating and bi cubic interpolation is described below (the noob way obviously):

        static double Interpolate(double z, double[] height)
        {
                return height[1] + 0.5 * z * (height[2] - height[0] + z
                * (2.0 * height[0] - 5.0 * height[1] + 4.0 * height[2] - height[3] + z
                * (3.0 * (height[1] - height[2]) + height[3] - height[0])));
        }


Monday, 8 October 2012

c# Window Enumerating the noob way

So today i was faced with a silly noob problem!, At work today i came across a silly problem with the way we were enumerating our windows, So i decided to write my own title enumerator.

You'll see how it works when you run it.



--


using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using Timer = System.Threading.Timer;

namespace Enum
{
    public partial class Form1 : Form
    {   
        public string Window { get; set; } //this window to enumerate

        public Form1()
        {
            InitializeComponent();

            this.Window = this.Text;

            Application.Idle += new EventHandler(Application_Idle);

            new Timer((@trigger) =>
            {
                Application.RaiseIdle(null);
            }).Change(1000, System.Threading.Timeout.Infinite);
        }

        void Application_Idle(object sender, EventArgs e)
        {
            var windows = Enum.GetDesktopWindowsTitles();
            var count = windows.Where(i => i.Contains(Window)).Count();

            if (count > 0)
            {
                var processes = Process.GetProcesses().Where(i => i.MainWindowTitle == Window).ToArray();
                for (var i = 0; i < processes.Length; i++)
                {
                    var str = Window + " " + count;
                    if (windows.Count(j => j == str) == 0)
                    {
                        Enum.SetWindowText(processes[i].MainWindowHandle, str);
                        count++;
                    }
                    else
                    {
                        var titles = windows.Where(z => { return z.Contains(Window + " "); }).Select(k => int.Parse(k.Split(' ')[1])).ToArray();
                        int length = titles.Length,
                               min = titles.Min(),
                               max = titles.Max();

                        var result = Enumerable.Range(1, max).Except(titles);

                        Enum.SetWindowText(processes[i].MainWindowHandle, Window + " " + result.FirstOrDefault());
                        count++;
                    }
                }
            }
        }
    }

    public class Enum
    {
        const int MAXTITLE = 255;
        private static List<string> lstTitles;
        private delegate bool EnumDelegate(IntPtr hWnd, int lParam);

        [DllImport("user32.dll")]
        public static extern bool SetWindowText(IntPtr hWnd, string lpString);

        [DllImport("user32.dll", EntryPoint = "EnumDesktopWindows", ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
        private static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumDelegate lpEnumCallbackFunction, IntPtr lParam);

        [DllImport("user32.dll", EntryPoint = "GetWindowText", ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
        private static extern int _GetWindowText(IntPtr hWnd, StringBuilder lpWindowText, int nMaxCount);

        [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool IsWindowVisible(IntPtr hWnd);

        private static bool EnumWindowsProc(IntPtr hWnd, int lParam)
        {
            var strTitle = GetWindowText(hWnd);
            if (!string.IsNullOrEmpty(strTitle) & IsWindowVisible(hWnd))
                lstTitles.Add(strTitle);

            return true;
        }

        public static string GetWindowText(IntPtr hWnd)
        {
            var strbTitle = new StringBuilder(MAXTITLE);
            var nLength = _GetWindowText(hWnd, strbTitle, strbTitle.Capacity + 1);
            strbTitle.Length = nLength;           
            return strbTitle.ToString();
        }

        public static string[] GetDesktopWindowsTitles()
        {
            lstTitles = new List<string>();
            var delEnumfunc = new EnumDelegate(EnumWindowsProc);
           
            var result = EnumDesktopWindows(IntPtr.Zero, delEnumfunc, IntPtr.Zero);
            if (result)
                return lstTitles.ToArray();

            return null;
        }
    }
}

Thursday, 9 August 2012

Nvidia Opensuse 11.2 Repository mirror

For any of those out there seeking a mirror for the repository http://nvidia.download.com/opensuse/11.2 then you can use this repository found here:

The Opensuse 11.2 NVIDIA repository can be accessed from:
http://atlass.dnsalias.com/nvidia/

It is a complete copy of what was once the original 11.2 opensuse Nvidia repository before nvidia took it down for whatever reason, it makes no real sense why they would take it down.

I guess 11.2 has become deprecated