Wednesday, 26 October 2011

WP7 A better InkPresenter using XNA

  Last week I was at SMAU Italia  together with Matteo Pagani giving a hand at Windows Phone 7 labs and having a good time with my friends at Microsoft. I also got the pleasure to meet Ben Riga. One of the attendees at the laboratory raised a really good question about the performance of the InkPresenter in WP7. He is using the control to capture a signature, but if you are pretty fast (usually people when they do their signature are fast) the result is "ugly" and not really usable. You won't get a smooth curve, but something like this:


 This screenshot is taken in the emulator, but on the real device the curve looks worst and it's easy to reproduce this behavior. I took this "problem" as a challenge (two nights of work and now the third to write the post) convinced that I can get better results with a mix between XNA and Silverlight, mix that in Mango is possible. T
 The main problem is the number of points returned by the event MouseMove of the InkPresenter control which is not enough points to draw a decent curve.
  The first thing I've tried in the XNA version was to use the TouchPanel.GetState() but I was surprised to see that I get the same number of sampling points as the MouseMove in InkPresenter. The things improved a lot when I've used TouchPanel.ReadGesture() with GestureType.FreeDrag. Using these sampling points as StylusPoints for the InkPresenter the situation improved a little:


 The black line is the one obtained with MouseMove and the red one is the one obtained with ReadGesture.The result is better, but not satisfying. The only way to further improve the result was to manually draw the curve without InkPresenter. I've have used BezierSegment to draw the curve. Without any processing this would be the result:


The new curve is the blue one which is better than the others, but it is still not smooth in some points. This is because a Bézier path is smooth if each endpoint and its two surrounding control points lie in a straight line. In other words, the two tangents at each Bézier endpoint are parallel.



There is more than one approach to solve this behavior and I've implemented two (with all the mathematics I felt back at the University http://en.wikipedia.org/wiki/B%C3%A9zier_curve). 

The first is an algorithm for Automatically Fitting Digitized Curves with the Douglas Peucker algorithm to reduce the number of points (http://stackoverflow.com/questions/5525665/smoothing-a-hand-drawn-curve).

The second one uses Bézier splines which creates the First and Second control points for each Bézier segment http://www.codeproject.com/KB/graphics/BezierSpline.aspx . 

With both algorithms the results are pretty good:


If you are interested to play with the sample you can change the following parameters:

double PhilipSchneiderTolerance = 0;
double PhilipSchneiderError = 4;
-used when the fitting algorithm is PhilipSchneider (run this sample http://cid-c27e99281f78a67a.office.live.com/self.aspx/Public/Simplify.zip on the desktop to understand what changes when you change the Tolerance and the Error)

 private bool _showPoints = false;
-shows or hides the points read by ReadGesture
        
private bool _showOtherCurves = false;
-shows or hides the InkPresenter curve using MouseMove and ReadGesture

private FitCurveAlgorithm _algorithm = FitCurveAlgorithm.PhilipSchneider;
-changes the fitting algorithm: None, BezierSpline, PhilipSchneider




P.S. The project still needs some working/polishing in order to be used in production, but that is the simple part so... HAVE FUN

NAMASTE

Thursday, 20 October 2011

Skydrive Library for Windows Phone v1

   I've started this project before Build. At that time I didn't know what Microsoft was preparing for the skydrive REST Api so there was no way to fully access your skydrive folders and files. The idea was to be able to get the modified files (word, excel) from Skydrive back to the dropbox account (a lot of people requested that feature and, as you probably know, in Mango you can save the files directly to your skydrive account). So I've started by contacting the people that already had developed applications that use Skydrive for Windows Phone 7 ( it doesn't make sense to reinvent the wheel if you can borrow or buy one) . I've found 2 applications: Sky Wallet and the other Skydrive Player and the answers from the developers were not what I was expected: the first one pointed me to Skydrive.Net which is(was) unusable for Windows Phone and then didn't replied to my other emails (so I understood it was disturbing for him to share his work) and the other said that the source code was not really clean (so more or less I don't want to share it). The only option was to start digging up and find out how they did it (that day I've also promised to myself that i will publish the code even if this will mean that the application I have in the marketplace will loose some value) It took me more than I expected  to understand and implement the mess (for me it's a mess) behind the WebDAV, but the results are not bad ... it works. The source code is quite a mess but it's free and you can use it (if I wait to have time to "clean" the code I will never publish the library). When the REST api will be out of beta (you cannot publish an app with the beta sdk) this library will be obsolete, but till then you can use it in your applications and they will pass certification (at least mine did ... till now ). The list root folder method is slow because I have to call two methods in order to have all the files and folders. You will also find a test project that will show you how to browse your skydrive. Creating folders and uploading files are not implemented in this version and it doesn't make sense to implement them because it will be much easier to do it with the REST api. If you need this functionality contact me and I will point you in the right direction.
  The project is published on Codeplex: http://wp7skydrive.codeplex.com/
  
  If you have questions, need support, or want to improve the library please let me know. If you wanna see a better integration of the library than the simple test project you should buy my dropbox client Boxfiles :) .

NAMASTE

DropBox library for Windows Phone 7 v1.1

 I have just published an updated version of my Dropbox client on CodePlex. You can download it here. I have rewritten the library from scratch (took out all dependencies - Json.Net and Hammock, added the download progress event and a lot of other things that I cannot even remember). It's faster, better and it's the exact same library I am using in my Dropbox application . I really hope that it will be useful for your projects and hope your projects will not be something similar to Boxfiles.
   That's all folks!

P.S. It doesn't have a test project, but you can use the one in the first release. It should be easy to use and pretty intuitive. If you find bugs or want to improve the library please let me know


NAMASTE!