Disk failure (with some data loss)

My primary disk just failed yesterday evening (around 21:30), so I lost at least all data from this week (last backup is from Sunday, 19:00.) However, I think I didn’t miss any unread e-mails, but of course I cannot check as all mails from this week are definitely lost. In case there was something urgent, please send me a mail again.

I ordered a new hard disk which should arrive early next week. I hope to be back up (no pun intended) & running around Thursday again. As soon as the RMA for the faulty disk finishes, I’ll plug it in for daily backups. While I’m at this: What’s your backup strategy?

Here I have one additional internal HDD which I use for backups once a week. I don’t have a “disaster” backup yet at a secondary location; I wonder how well Amazon S3 would be suited for something like this (or SkyDrive.) I’m also planning to install a NAS in my home network and use this for backups, but I haven’t found a nice and free backup software yet. Second problem with the NAS is how to backup the NAS, in case of failure (RAID-5 might be a solution, but how to guarantee that I can rebuild the RAID-5 in 4 years, or, worse, in case the controller fails and can’t be replaced?) I’m curious to hear what backup strategies you employ, and how you coped with disk failures so far.

Posted in General | Tagged , | 4 Comments

Avoid unsigned types by default

For some reason or another, unsigned types in C++ are heavily overused. I know that Java doesn’t have them, and many students who learn programming with Java think this is a really serious deficiency which they have to make up by using unsigned types everywhere … however, it turns out, that you will rarely need unsigned types, and in most cases, they do more harm than good.

First of all, there are some cases where you need unsigned types. Pretty much all of these cases can be grouped into two categories:

  • Interop: Someone somewhere expects an unsigned value, so you have to convert your stuff at some point. You should try to convert it as late as possible, but with some sanity check. For instance, if you call an API which expects an unsigned size, and you have a signed size in your application, convert only after sanity-checking your signed value!
  • I/O: Disk or network I/O often requires the use of unsigned data types, as you typically want to work on the raw bit pattern of the value and not on the actual value. In this case, unsigned types fit and should be used.

Everywhere else, you should really try hard to avoid them. Especially when you design an API, stay away from unsigned types. There’s a very good article by Scott Meyers on this topic which explains the problems with unsigned types and interfaces. The main problem is that an unsigned type makes error/sanity checking impossible. If you have a function like memset(void* t, size_t size) and a pod like

struct pod {
    unsigned char flags;
    Vector3 coords;
};

and you pass on something like memset(&pod, sizeof (pod) - sizeof (Vector4)); because you vaguely remembered that pod has a Vector4 and a flags field in it, you’re going to clear around 18446744073709551613 bytes worth of data if running on a 64-bit system — with signed types, a quick assert(size >= 0); would have failed with size == -3.

This happens far more often than you would expect. Since I’m trying to use signed types as much as possible, I’ve found lots of lots of places where I can sanity-check values using asserts and pre-condition checks than before. Even for typical use cases like I/O routines (fwrite) or memory stuff (malloc), you can get away with one less bit in practically every case — I don’t remember ever having seen a call which would allocate or write more than 231 elements on a 32-bit machine; let alone 263 elements on a 64-bit machine. So, next time before you start typing unsigned type size, think twice if you’re really ever going to need that extra bit, or whether you are willing to trade it off for error checking.

Note: This does not imply that you don’t have to think about valid value ranges. The only help you get from signed types is that overflows are much harder to exploit if you sanitize most values in between. You should however still use things like SafeInt to make sure your arithmetic stays in range.

Posted in Programming | Tagged , | 12 Comments

Triangle rasterization in homogeneous coordinates gotcha

There’s a very difficult to find bug you might encounter when you try to implement triangle rasterization in homogeneous coordinates (as described in “Triangle scan conversion using 2D homogeneous coordinates“.) You’ll face the problem if you transpose the interpolation matrix by chance — either because you forgot the transpose when doing the standard 3×3 matrix inverse, or because you multiply your vectors from the wrong side onto your matrix.

Anyway, what happens is that you suddenly get the same z-Value across your triangle. The weird thing is that the z-Value is actually correct for one of the vertices, so if you have pixel-sized triangles, everything will work out fine. It only breaks down once you interpolate across the triangle, as you always wind up with the same value.Triangle depth interpolation with transposed interpolation matrix, each fragment in a triangle gets the same depth value.

Short explanation why this is difficult to track down: First of all, it’s the correct z-Value after all, and the matrix is far from symmetric,  so you wouldn’t expect that the transpose would end up anywhere near the correct result. Second, the matrix condition is typically not too good, so numerical problems are actually something you’ll face — I spent quite some time to improve the condition of the matrix as well as trying out several inversion methods, just to make sure that numerical problems are not the problem. Finally, you have to use the colums/rows (depending on your layout) of the matrix for the edge half-space tests, so you can be sure that you have computed the entries of the matrix correctly.

With the properly transposed matrix, everything works fine. Small hint: You need to interpolate 1 to get w, for which you can save the matrix multiply and just compute the sum, which saves several multiplications with 1. Here’s the correct result for comparison:

Triangle depth interpolation with correct interpolation matrix, depth varies smoothly accross a triangle

Posted in Graphics | Tagged , | Leave a comment

CSS3/JavaScript for styling: Your opinion matters

I’m planning to switch to a new style which relies heavily on CSS3 and JavaScript. This means for instance that certain parts might absolutely require JavaScript and not work correctly with JavaScript disabled (for instance, tabs might load their content using AJAX without a fallback.) However, before I spend time working on this new style I’d like to get some opinions from you, my dear readers …

  • Do you mind requiring JavaScript?
  • Do you prefer JavaScript linked off a CDN or hosted locally? (For instance, I could pull JQuery from Microsoft’s CDN)
  • Do you plan to keep your browser up-to-date to follow new web-standards or do you update rather casually?

Please comment or drop me a line using the contact form, so I can flesh out a nice and useful redesign plan.

Posted in General | Tagged , , , | 4 Comments