Tangible Level Editor with Arduino

The Tangible Level Editor is a proof of concept input device that uses an Arduino microcontroller, resistor networks and a multiplexer.  A user stacks blocks in the real world to complete a circuit that’s connected to the Arduino.  At each interval, the amount of voltage going through each circuit is read and the number of blocks can be determined. This data is used to construct a virtual game world. Each block contains a resistor that is comparably equal to those in every other block. Here is a schematic:

We know from Ohm’s Law that V = I * R. As the Arduino runs on 5V, an input value to the Arduino pin at 5V is 1023, and at 0V is 0. It is therefore possible to determine the approximate voltage going into the pin, since the current is constant. The relationship is inversely proportional, so as the number of blocks goes up, the voltage will go down. This means that with one resistor, the voltage is one half. With two resistors, the voltage is one third. With three, the voltage is one fourth, and so on. Here’s how it looks all assembled:

On the left, you’ll notice a sweet little controller I made out of a single PlayStation joystick mounted in a RadioShack project box. Here’s a close up of that:

And the moment you’ve been waiting for, the virtual world, created live, by the block configuration shown above:

If you want a more detailed explanation and a live demo, here’s a video we made showing the virtual world built in Unity3D:

Posted in Arduino, C#, Virtual Reality | Tagged , , , , , | Leave a comment

Windows 7: Open command window here

Maybe I’m the last one to find out, but apparently Windows 7 has had an Open command window here button hiding this whole time! Most non-Windows operating systems will have a context menu item in their desktop environments that allows you to open a command prompt or shell with the working directory set to the folder you’re viewing or selecting. Anyways, the trick is: Shift+Right Click and boom! Now there’s the Open command window here option.

Update: I created a .reg file that will automatically enable this (so you don’t have to hold Shift anymore!)  You can get it here.

Posted in Windows | Tagged , , , , , | Leave a comment

PointCloudNI

I felt like playing around with the Kinect some more this evening, so I put together a point cloud in Unity3D. This takes the base code from TrackingNI and with a little help from Marshal, it can pull the depth data directly from a Unity3D script. This project does not use the OpenNI Unity Wrapper. Anyways, here’s a quick demo:

I may decide to build my own wrapper library at some point. We’ll just have to see.

Update: I’ve uploaded the source to this demo.  Feel free to download it.  If you make something cool with it, be sure to let me know, enjoy!

Posted in C#, Kinect, Virtual Reality | Tagged , , , , | 15 Comments

TrackingNI: Depth Correction

If you’ve been following my MouseNI or TrackingNI demos, then this one is somewhat more interesting.

I had a little more free time today and thought I’d try my hand at filling in the IR shadow in the Kinect’s depth data. While it’s not perfect, there’s far less noise and the shadow of my hand is often completely unnoticeable. Overall, I would say it was a success. The left side is the original depth data, the right side is my corrected version:



This currently works by directly manipulating a WriteableBitmap, however I will probably adapt the code to correct the actual DepthMetaData object instead. Stay tuned!

Update: I still haven’t converted the code to do anything besides modify the WriteableBitmap, but it’s committed as-is right now. Feel free to check out the project from http://code.google.com/p/trackingni/. If anybody updates it, let me know!

Posted in C#, Kinect, Virtual Reality | Tagged , , , | 5 Comments

TrackingNI: Simple Skeleton, Hand and Head Tracking

This is a follow-up to my MouseNI post. We got some more time to work with the Kinect yesterday and finally got the skeletal tracking working in C# with WPF.  Based solely on the skeletal data, it also draws circles for the head and hands.  Here’s Ramsey giving a demo:



Now that we’ve got it working, we can start actually building some cool projects. More to come soon.

Posted in C#, Kinect, Virtual Reality | Tagged , , , , , | 14 Comments

C# Object Cloner

I recently came across the need to replicate large numbers of objects, so I will share this nice piece of code. The following snippet is a class for deep cloning of objects (usually instances of classes) in C# posted by Stephen Inglish:

/// <summary>
/// Reference Article http://www.codeproject.com/KB/tips/SerializedObjectCloner.aspx
///
/// Provides a method for performing a deep copy of an object.
/// Binary Serialization is used to perform the copy.
/// </summary>

public static class ObjectCopier
{
    /// <summary>
    /// Perform a deep Copy of the object.
    /// </summary>
    /// <typeparam name="T">The type of object being copied.</typeparam>
    /// <param name="source">The object instance to copy.</param>
    /// <returns>The copied object.</returns>
    public static T Clone<T>(T source)
    {
        if (!typeof(T).IsSerializable)
        {
            throw new ArgumentException("The type must be serializable.", "source");
        }

        // Don't serialize a null object, simply return the default for that object
        if (Object.ReferenceEquals(source, null))
        {
            return default(T);
        }

        IFormatter formatter = new BinaryFormatter();
        Stream stream = new MemoryStream();
        using (stream)
        {
            formatter.Serialize(stream, source);
            stream.Seek(0, SeekOrigin.Begin);
            return (T)formatter.Deserialize(stream);
        }
    }
}

The code essentially serializes the object and all serializable objects to which it contains references.  It then deserializes the result into a new reference, effectively cloning the entire object.  I have put together a basic example that uses this cloner.  First, a couple classes:

[Serializable]
public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }

    public Person(string name, int age)
    {
        Name = name;
        Age = age;
    }
}

[Serializable]
public class Group
{
    public List<Person> Members
    {
        get;
        private set;
    }

    public Group()
    {
        Members = new List<Person>();
    }
}

A group simply contains a list of people.  Note that these must be given the [Serializable] attribute to be cloned.  Then we can create a couple of groups and clone one:

// make some people
Person jack = new Person("Jack", 24);
Person bruce = new Person("Bruce", 22);
Person mary = new Person("Mary", 25);
Person jill = new Person("Jill", 23);

// create the boys
Group boys = new Group();
boys.Members.Add(jack);
boys.Members.Add(bruce);

// create the girls
Group girls = new Group();
girls.Members.Add(mary);
girls.Members.Add(jill);

// make a couple
Group dating = new Group();
dating.Members.Add(jack);
dating.Members.Add(jill);

// clone this couple
Group couple = ObjectCopier.Clone<Group>(dating);

// alter the original people dating
jack.Age = 100;
jill.Age = 100;

// check the clones' ages
foreach (Person person in couple.Members)
{
    Console.WriteLine(person.Name + " is " + person.Age);
}

This will first output Jack is 24 and then Jill is 23, demonstrating that the objects are cloned and retain their own copies of the objects the “parent” objects originally referenced.

Posted in C# | Tagged , , , | Leave a comment

MouseNI

I managed to get enough free time today to play around with the Kinect. Using the OpenNI drivers and NITE middleware, I made this very simple mouse controller with hand tracking in C#:



More to come soon, hopefully!

Update: I have uploaded the source to this demo here. Enjoy!

Posted in C#, Kinect, Virtual Reality | Tagged , , , , , | 3 Comments

Extending C# Enumerators with Methods

Although I personally find C# enumerators to be atrocious, they still have several advantages over constant integers.  One of these advantages is the ability to extend the object with additional logic.  This is accomplished by binding extension methods at compile time.  Consider this common enumerator as an example:

public enum Month
{
    January,
    February,
    March,
    April,
    May,
    June,
    July,
    August,
    September,
    October,
    November,
    December
}

Now say we want to be able to find out the number of days in a month and be able to produce an abbreviation of the month.  Rather than creating an additional class with helper functions that must be remembered and referenced later on, new methods can be added directly to the enumerator like so:

public static class MonthExt
{
    // assumes no leap year
    private static int[] MonthDayCount = new int[] {
        31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

    public static int DayCount(this Month month)
    {
        return MonthDayCount[(int)month];
    }

    public static string Abbreviation(this Month month)
    {
        return month.ToString().Substring(0, 3);
    }
}

The operant piece of code here is the this preceding the type (the enumerator to be extended) of the first argument in each method.  That’s how the compiler knows which method to bind to which type.  Using it is as simple as can be:

Console.WriteLine(Month.April.DayCount());
Console.WriteLine(Month.April.Abbreviation());

This will first print 30 and then Apr to the console.

Posted in C# | Tagged , , | Leave a comment

Generic Singleton Pattern in C# with Reflection

Reusable and proven design patterns are crucial to building robust and scalable applications.  I loathe copying and pasting boilerplate code; naturally, I always prefer abstract classes and interfaces when possible.  And frequently, I end up employing a singleton pattern when designing software.  For those who don’t know, a singleton is a class that can only be instantiated once.  This design is especially safe when collaborating with other developers and even on large solo projects, because a class may contain logic or state that should only exist once.  Numerous articles have been written describing a generic implementation in C#, but they almost always fall short in my opinion.  The typical implementation that you can find will appear similar to this (credit to davojc):

*note: do not use this code

public class SingletonProvider<T> where T : new()
{
    SingletonProvider() { }

    public static T Instance
    {
        get { return SingletonCreator.instance; }
    }

    class SingletonCreator
    {
        static SingletonCreator() { }

        internal static readonly T instance = new T();
    }
}

There have been many variations on this implementation, whether striving to more deeply encapsulate the instantiation or to protect the class from threading mishaps.  However, the most glaring problem exists in the fact that the SingletonProvider class applies the new() constraint.   Rather helpfully, this enables the code to instantiate whatever type T is with a blank constructor.

However, this blank constructor must be public to actually work.  This entirely breaks the concept of a singleton pattern because you can then instantiate the class to your heart’s content.  In essence, this implementation not only creates more work for the programmer, but does not provide any of the intended protection!

Fear not, for .NET has reflection!  When there’s no way to do what you want within the bounds of C#, you can always hack it together with reflection.  Thanks to Martin Lapierre for this solution:

*note: you can use any code beyond this point

public static class Singleton<T>
       where T : class
{
  static volatile T _instance;
  static object _lock = new object();

  static Singleton()
  {
  }

  public static T Instance
  {
    get
    {
      if (_instance == null)
        lock (_lock)
        {
          if (_instance == null)
          {
            ConstructorInfo constructor = null;

            try
            {
              // Binding flags exclude public constructors.
              constructor = typeof(T).GetConstructor(BindingFlags.Instance |
                            BindingFlags.NonPublic, null, new Type[0], null);
            }
            catch (Exception exception)
            {
              throw new SingletonException(exception);
            }

            if (constructor == null || constructor.IsAssembly)
            // Also exclude internal constructors.
              throw new SingletonException(string.Format("A private or " +
                    "protected constructor is missing for '{0}'.", typeof(T).Name));

            _instance = (T)constructor.Invoke(null);
          }
        }

      return _instance;
    }
  }
}

Before I explain it, this is how you use it (taken from Martin’s article):

public static SingleInstanceClass Instance
{
  get
  {
    return Singleton<SingleInstanceClass>.Instance;
  }
}

This static property would belong in a SingleInstanceClass class.  Accessing it would make a call to the Instance property of Singleton<T>, which will instantiate (by calling the private constructor using reflection) a class T, store it internally if it has not already done so and of course return the instance. All subsequent calls to Singleton<T>.Instance using this type T will return the original instance. Naturally, the implementation also includes the double-lock check pattern for thread safety. This is preferred over a nested solution to ensure that the constructor may recover in the case that it threw an exception the first time.

The only two caveats I’d like to mention–which are relatively minor–is that this does not ensure that no public constructors exist within the class; and it should be noted that other code that is also using reflection can instantiate this multiple times. However, someone would have to go well out of their way to produce either of these.

Posted in C# | Tagged , , , | 1 Comment

CSS Browser Selector

One of the most time consuming activities during web development is ensuring cross-browser compatibility.   However rare, it is sometimes necessary to write CSS rules that target only a specific browser.  Not to mention, there are any number of other reasons one might wish to display something differently in an alternative browser.   I recently encountered a similar case and came across this nice little piece of code by Rafael Lima:

/*
CSS Browser Selector v0.4.0 (Nov 02, 2010)
Rafael Lima (http://rafael.adm.br)

http://rafael.adm.br/css_browser_selector

License: http://creativecommons.org/licenses/by/2.5/
Contributors: http://rafael.adm.br/css_browser_selector#contributors
*/
function css_browser_selector(u){var ua=u.toLowerCase(),is=function(t){return ua.indexOf(t)>-1},g='gecko',w='webkit',s='safari',o='opera',m='mobile',h=document.documentElement,b=[(!(/opera|webtv/i.test(ua))&&/msie\s(\d)/.test(ua))?('ie ie'+RegExp.$1):is('firefox/2')?g+' ff2':is('firefox/3.5')?g+' ff3 ff3_5':is('firefox/3.6')?g+' ff3 ff3_6':is('firefox/3')?g+' ff3':is('gecko/')?g:is('opera')?o+(/version\/(\d+)/.test(ua)?' '+o+RegExp.$1:(/opera(\s|\/)(\d+)/.test(ua)?' '+o+RegExp.$2:'')):is('konqueror')?'konqueror':is('blackberry')?m+' blackberry':is('android')?m+' android':is('chrome')?w+' chrome':is('iron')?w+' iron':is('applewebkit/')?w+' '+s+(/version\/(\d+)/.test(ua)?' '+s+RegExp.$1:''):is('mozilla/')?g:'',is('j2me')?m+' j2me':is('iphone')?m+' iphone':is('ipod')?m+' ipod':is('ipad')?m+' ipad':is('mac')?'mac':is('darwin')?'mac':is('webtv')?'webtv':is('win')?'win'+(is('windows nt 6.0')?' vista':''):is('freebsd')?'freebsd':(is('x11')||is('linux'))?'linux':'','js']; c = b.join(' '); h.className += ' '+c; return c;}; css_browser_selector(navigator.userAgent);

The latest version can be found here.

The code adds a class to the body tag of your page based on the user’s browser.  You can then write style rules to be used only by specific browsers like so:

.ie .sample {
	/* Internet Explorer */
}

.gecko .sample,
.chrome .sample {
	/* All Firefox versions and Chrome */
}

.opera .sample {
	/* Opera */
}

Depending on the user’s browser, the sample class will hold the corresponding attributes of one (or possibly none–if the browser is say, Safari) of the above rules.  The full list of available browser codes:

ie - Internet Explorer (All versions)
ie8 - Internet Explorer 8.x
ie7 - Internet Explorer 7.x
ie6 - Internet Explorer 6.x
ie5 - Internet Explorer 5.x
gecko - Mozilla, Firefox (all versions), Camino
ff2 - Firefox 2
ff3 - Firefox 3
ff3_5 - Firefox 3.5
ff3_6 - Firefox 3.6
opera - Opera (All versions)
opera8 - Opera 8.x
opera9 - Opera 9.x
opera10 - Opera 10.x
konqueror - Konqueror
webkit or safari - Safari, NetNewsWire, OmniWeb, Shiira, Google Chrome
safari3 - Safari 3.x
chrome - Google Chrome
iron - SRWare Iron

Incidentally, this useful little snippet was made into a WordPress plugin.

Posted in CSS, Javascript | Tagged , , , | 2 Comments