My Octopress Blog

A blogging framework for hackers.

The Life Aquatic

A while ago, I saw an xkcd comic about being stranded on a desert island - looking out at the vast, perfectly flat blue infinity, struck by the lack of anything worthwhile.

xkcd's Desert Island

Had I been stranded on a deserted island six months ago, this would have absolutely been me. Well, and without goggles, it would still be me, but that’s neither here nor there.

As I live in Saudi Arabia, activities can be… sparse. There’s cycling and sports, but for the most part it’s too hot to do anything until the evening. In January I first joined some of my friends on a dive trip of theirs - I was only snorkeling, but they assured me there would be plenty to see. Growing up a good 1500km from a large saline body of water, my experience had been rather limited. When I finally got ready, and put my head under water, my head popped back up about half a second later, shouting to my friends, “Holy crap! There’s fish everywhere!”

My friends just sort of looked back at me as if to say, “yeah… about that - that’s why we come.”

From that point on, I’ve been hooked. I have since gotten dive-certified, and gone a few trips. Few things are better than getting to explore a world unseen by most people, sometimes seeing reefs so random and remote that it’s unlikely that people have even been there at all. On every boat trip, we pass uncountably many shallow reef patches, that extend for kilometers - they really are sprawling, massive underwater cities.

A few weeks ago, I got to go on a truly fantastic trip. The Marine Science department at KAUST tags and studies whale sharks, and every so often they take along “tourists.” We saw between 5 and 7 unique sharks, the longest of which was about 6.5m (20 feet), though apparently they can grow to be over 12m (40 feet). Roll clip:

How this worked is we had two boats (one filled with researchers and one filled with us tourists), and if either boat spotted something, they’d sound the horn, and everyone jumped in. As such, we would all sit on the edge of the boat, fins on, masks ready - just in and out of the water all day. Once in, the sharks tended to swim pretty fast - they seem to be barely exerting themselves and we’re swimming as fast as we can. And, if they don’t care to outrun us and want to get away, they dive beyond our reaches.

On this same trip we saw a lonely sea turtle, about the size of a dinner plate. We all hopped in the water and had turns getting our pictures taken with him. Honestly, I felt a little weird about picking him up, but he really did not seem to mind it. It does violate a widely-followed rule among responsible divers - touch nothing (unless you really know what it is and that it’s not going to damage you or it).

We also saw a “pod” of three dolphins; they often appear briefly on these boat trips, but usually scurry off pretty quickly in our experience. On this occasion, though, as we were already set up to be hopping in and out of the water at a moment’s notice, we did try to swim with them. They mostly stayed about 6m below us, and were much more difficult to keep up with than the whale sharks. That said, fortune favored the persistent, and after about 15 minutes of some of my hardest swimming, I managed to spot them and get within about 2m (6 feet) of them as they surfaced. One was badly scarred across his back, but the other two were untouched. They came up for a quick drink of air, descended a few meters, did a somersault underwater and then looked at me as if to say, “Oh, you can’t do that, can you?”

It’s hard to take in the incredible gravity and beauty of the sights, let alone when swimming as fast as you can and perpetually out of breath. I’m glad I got the chance, and I hope to see more aquatic life, but there is definitely a reason it’s remote. Consider this: in hiking, the highest I’ve been is just over 14,000 feet, but the limits of recreational diving is a mere 40m (130 feet) and that requires even more specialized training. Consider, too, the Mariana Trench - which is less accessible: the deep sea or space?

Since the opening of the KAUST beach, we’ve explored much of the reef, sometimes to the dismay of the coast guard. We take precautions - never going alone, bringing a dive flag, and knowing our limits. Floating along in the warm water, seeing thousands of fishes, swimming around is enough to make anyone want to play Jacques Cousteau.

The Pains of Nightly Builds

I’ve been working with WebKit a lot lately. Specifically, WebGL. It’s fantastic, and it has the potential to change a lot of important applications, but working with nightly builds is… well, frustrating.

I’ve been using a revision released in December. Some later revisions have weird behaviors and draw pixelated, but I’ve finally come across a problem with r52426 that I just can’t circumvent - floating-point textures. They just don’t seem to work properly. I had noticed the bug before, but it wasn’t absolutely crucial before. Rather, the real bug is that you can’t initialize floating-point textures from the JavaScript side.

They’ve fixed this in later versions, but many of these that have this bug fixed also have a problem with gl.viewport, which is pretty mission-critical. So now I’m sifting through the changelog, hoping to find a release somewhere in the middle that isn’t completely broken.

Update: Victory! If you’re going to be developing WebGL stuff on Mac, I’d recommend WebKit r53036. It seems to have everything I need (so far) fixed.

Fix Warnings

I would ask of anyone writing a library these days to get rid of compile warnings. I find it a little disconcerting to see a library compiling with typecasting warnings between pointers. I won’t name names here, but it is kind of unprofessional.

So please, compile with -Wall and fix your warnings.

Tech Addiction

Several days ago, Gizmodo posted a quiz about technology addiction. Somewhat nervously / reluctantly looked through the questions, and was pleased to find that I fit into their “Coffee Fiend” category. Well, rather, I was pleased that I didn’t fall in any of their worse categories, despite working with computers for a living. There are even some places I could do with shaving down that would get me into their “Social Drinker” category; I see this as proof that one still can be in a tech field and not be a total tech fiend.

Some of their more amusing / revealing / interesting questions were:

2. Do you sometimes bring your laptop when you sit on the toilet?

Just for the record, I do not. But, I know people who do, and let’s face it - that’s why the iPhone was invented in the first place.

8. Have you ever changed vacation plans based on wi-fi availability?

Well, technically yes, I have done that. But it was deciding between two capsule hotels, and I was going to be there for a week for a tech conference. I think that’s justified.

10. If your house were on fire, would you run in to rescue your laptop?

I have sort of done this already. Fire alarms are a regular feature of KAUST, and one day we saw smoke coming from the building we were in as one went off. Ben and I looked at each other for 2 or 3 seconds, then both immediately grabbed our laptops and dashed out. In all fairness, most of my work is on there (I’m doing more off-site backups for the important stuff), and it’s the only computer I have in this country. What is life without computer?

35. Do you tweet or read blogs while watching movies at home?

Well, I do play Tetris. That’s my tech meditation time.

Delicious Sshfs

It is extremely easy for computer scientists (well, and the rest of humanity) to get entrenched in their ways. You’ve learned and taken the time to become a master of a programming language, or tool, and it’s a serious time investment. As such, new tools generally have to be very compelling in order to get someone to switch. For example, I’m a recent svn-to-git convert, and am often met with horrified looks when I suggest others give it a shot. A visiting professor I spoke to yesterday said that it would be years before they stopped using svn.

Recently, I’ve begun using sshfs. It mounts a volume over ssh and appears as any normal folder on your computer. It behaves that way at the command line, or your text editor, or as far as VLC is concerned. For development, I had always maintained a local copy and rsync’d changes to the remote machine I was using. It worked well enough, and was often more convenient than using a tool like Cyberduck.

Aron recommended sshfs, and though it took me a while to try it out, I’m hooked. No more trying to remember if I’ve synchronized my code with the other machine I’m using, only editing and saving. It’s really easy when you have ssh keys set up as well. To mount your home directory on a remote machine:

1
2
$> mkdir datastore_mount
$> sshfs datastore: datastore_mount

And now, I’ve got access to remote files on datastore as if they lived in the directory datastore_mount. Unmounting is business as usual:

1
$> umount datastore_mount

Try it, use it, live it. You won’t regret it.

Snorkeling

For the past semester, many of my friends here have been enjoying the Red Sea, diving regularly. One even went on a two-week research cruise, making three to four dives a day.

‘What,’ I wondered, ‘could possibly be the fascination?’

Growing up in Colorado, playing around on the beach meant going to Boulder Reservoir, which isn’t much more than an enormous swimming pool. Just not much remarkable about it. The only real beaches I had been to until now were in San Francisco and Japan, and the novelty wore off pretty quickly.

This last weekend, I decided that I ought to give it a try, and not being certified for diving I went snorkeling with my friends. We drove to a private beach (which was a treat in itself - men and women can mix, and music plays over the loudspeakers. These are things I’ve learned to not take for granted), rented gear and got in the water. On the dock, I was excited when I saw a small coral growing in the shallows. It couldn’t have been more than 10 centimeters in diameter but thinking about exploring the beach for more such treasures was enticing.

We waded out and put on our flippers, and then our masks. I dipped my head down, ready to explore, and instantly my head popped right back out of the water. “Guys! Guys!” I shouted to my friends. “There’s a freakin’ million fish here!” They chuckled knowingly, and I realized this is what they came for.

I just could not get in the water fast enough. There were so many fish I had never seen except for in aquariums, brightly colored, quick, and just stunning. Thousands of them from deep blues to iridescent purples, all swarming around beautifully-colored coral and anemones. All told, I spent about four hours tooling around the reef, examining fish at the cleaning station, clownfish in their anemones and predators lurking near the surface.

On leaving I instantly felt like a tree-hugging hippie, and felt a need to protect the ocean. Ok, sure, it was a bit short-lived and I feel like someone who’s hopped on a bandwagon, but it’s an incredible world down there. You see shadows off in the “distance” (though it’s usually only 30 feet away) shimmering and swimming around. Floating in the sea, weightless, above the sheer cliff faces of 20, 30 or 40 feet is a unique sensation. I tried to compare it to hiking and climbing mountains, but it occurred to me that unless I learn to base jump, I will never see that kind of geographical structure from the same relative perspective - hovering above it.

Between dives (well, my friends were diving while I was snorkeling) we’d pull out a book of Red Sea fish and try to find all the ones we’d seen. The camaraderie of stories of dives past and fish seen was nearly as satisfying as the experience of being in the water like that.

I’m exploring getting certified (either through PADI or BSAC - we have both available here) so that I can join my friends on deeper adventures, but in the mean time I’m going snorkeling as often as I can. Forgive the pun, but I think I’m hooked.

Monkey Business

Nagano (well, Yudanaka) is famous for it’s “Monkey Park,” an open space where Japanese Macaques relax in large groups in the natural hot springs. Visitors can walk among them as they graze, groom, and occasionally, fight.

DSC_0023.NEF

The younger monkeys are pretty playful, climbing ropes and poles, and rough-housing with one another. The adult monkeys seem more interested in eating the food provided to them, a mix of barley and other grains. There seemed to be some areas used mostly by families (these were inaccessible to human visitors) and others that seemed to be dominated by younger bachelors. Fights would break out from time to time, but nothing too serious.

DSC_0052.NEF

Japan is known for their natural hot springs, and the monkeys take full advantage. The day I was there, it was about 3 degrees Celsius, and the water was pretty tempting.

You can get pretty close to these brazen animals, but do not touch. Occasionally they will grab onto you, but I only saw one instance of this; a group of us were watching a couple monkeys playing and banging against a sign, when an attention-starved lad came up and tugged on one man’s coat to get his attention. Still, they’re pretty aloof to our presence for the most part, and will walk and eat within a few feet of you.

DSC_0106.NEF

I kept thinking of them as terrestrial Ewoks, or furry seven-year-old human children, but the opposable toes are kind of unnerving. They often appear so human that it’s hard to remember that they’re still very wild.

DSC_0239.NEF

They cry and scream at one another, but these noises serve as low-priority warnings more than anything else.

If you even get a chance to visit, take full advantage. Best US$5 I spent in Japan. More photos on flickr.

Triangle Strip for Grids - a Construction

I’m working with WebGL and as such, I’m discovering some quirks about OpenGL ES 2.0. I have been using display lists as long as I’ve been using OpenGL, but WebGL doesn’t have support for them. So, I’m buckled down and familiarized myself with vertex buffer objects, the (perhaps better) alternative.

At any rate, I need to render a regular 2D grid, and as it doesn’t support quads, either, I was forced to use triangles. In the interest of getting things running, I just provided a wasteful list of discrete triangles. This is wasteful because it references many more vertices than necessary - I ended up declaring vertices when in reality there are only unique vertices. This worked fine, until I wanted to increase the resolution. It turns out, JavaScript doesn’t like large arrays.

That’s fair, because the implementation was pretty wasteful. A triangle strip was the best choice anyway. A triangle strip is a highly compact form of representing a mesh. For triangles, it requires only vertices defined. Well, that’s roughly true. We’ll see another case in a minute. It’s useful when many triangles share vertices, and perhaps I’ll let Wikipedia’s explanation stand.

It wasn’t immediately obvious how to define a grid out of a single triangle strip and so I got out a pen and paper. I kept in mind a neat trick: if in a triangle strip, you need to skip the use of a vertex, a vertex can be introduced twice in a row. That is, if I need triangles (6, 3, 7) and (7, 11, 6) in that order, you can just make your strip with 6, 3, 7, 7, 11, 6. You can think of it as if there are two triangles created (3, 7, 7) and (7, 7, 11), but they have no area and a degenerate case - a line. Furthermore, these lines lie on triangles already defined.

Perhaps the obvious choice doesn’t yield any results, and in fact in this layout, it can’t be easily done (you have to have vertices appear three times in a row):

[caption id=”attachment_792” align=”aligncenter” width=”207” caption=”This is a bad idea for a topology if you want to use a single triangle strip.”][/caption]

To better convince yourself, try to come up with a good way to put this in a triangle strip. I’ll make the case that it is pretty difficult with a claim from graph theory. In order for a triangle mesh to be turned into a triangle strip, each consecutive triangle must share an edge. We can then think of the mesh as a connectivity graph (the dual of the mesh) and then the problem will emerge more clearly:

[caption id=”attachment_794” align=”aligncenter” width=”207” caption=”The dual graph of the bad idea.”][/caption]

To make the triangle strip “nice,” we ought to be able to visit each node once and exactly once. There’s good and bad news in this - it’s the same problem as finding a Hamlitonian path which is NP complete. The good news is that if we find a solution to our small problem, we’ve found a solution to all such grids (with arbitrarily many triangles). Note that we don’t need an Eulerian path.

If you stare long enough at the above connectivity graph, you’ll hopefully convince yourself that there’s no way to traverse it visiting each node once and exactly once. Go ahead and try - it’s pretty infuriating.

Looking at how we would traverse one strip (triangles a, b, c, d, e and f) actually gives us a clue. A triangular strip for that case would be 0, 4, 1, 5, 2, 6, 3, 7, and happiness ensues and we should move onto the next row. Unfortunately, in the context of this new row, we’re starting in a different place (topologically) than we started with the first strip. Vertex 0 has two connected neighbors in its row - 1 and 4. Vertex 7 has three in its row: 6, 10 and 11. It turns out we can change up the topology to remedy this simply:

[caption id=”attachment_795” align=”aligncenter” width=”207” caption=”A much better topology for drawing this with a single triangle strip.”]A much better topology for drawing this with a single triangle strip.[/caption]

We can also see that this is a much better solution by looking at this new graph’s dual:

[caption id=”attachment_796” align=”aligncenter” width=”207” caption=”The dual of the better topological choice.”][/caption]

You can probably easily find a Hamlitonian path in this case. But this still leaves us with how to determine the vertex orderings. We decided that the first row ought to be 0, 4, 1, 5, 2, 6, 3, 7, but moving on from there we need a bit of “glue” to move onto the next row. We insert 7 again, and then continue on from there: 7, 11, 6, 10, 5, 9, 4, 8. A bit more glue for the third row: 8, 12, 9, 13, 10, 14, 11, and 15:

[caption id=”attachment_797” align=”aligncenter” width=”251” caption=”An alternative representation of the vertex ordering”][/caption]

Looking at the indices from the first row, starting at 0, we can get the next index by alternately adding 4 and then subtracting 3. On the next row, we’ll continue to add 4, but then alternately subtract 5. The 4 is derived as being the number of vertices on a side (if there are divisions, then there are vertices), and the 3 and 5 are explained by the fact that we need to change columns in the mesh, by one step at a time.

An clean implementation is not trivial, but not extremely difficult. In terms of results, I can fit more than 4 times as many vertices into the mesh than with a per-triangle implementation. And to boot, it has cut the work of the vertex shader a great deal.

webGLot - a Preview

I’ve mentioned WebGL before, and I think it could be very important. There is a competitor from Google, but like OpenGL and OpenCL, this API is managed by the Khronos Group and that fact appeals to me. Perhaps it’s that I’ve used it fairly extensively, but I really like OpenGL. Despite its quirks, it’s quite powerful.

The big “get” is that it gives programmers access to hardware-accelerated graphics from directly within the browser. There’s a lot of interest in this arena for game development as it would obviate much of the need for separate distributions based on operating system. (Skip to the end for more of an opinion on this subject.)

As such, I’ve been working with WebGL as opposed to the Google-proposed O3D. (I have every intention to explore O3D, time permitting, as there are some jagged edges to the current specification.) The result of this recent toil is a budding WebGL implementation of my OpenGLot project. It’s still in early stages, but in the coming weeks, it should develop even further. To whet appetites, I have a few pictures.

[caption id=”attachment_788” align=”aligncenter” width=”300” caption=”A scalar field, my persistent test function.”]A scalar field, my persistent test function.[/caption]

[caption id=”attachment_789” align=”aligncenter” width=”300” caption=”A 3D surface, again one of my usual test functions.”]A 3D surface, again one of my usual test functions.[/caption]

I seriously doubt that WebGL will every match the performance of OpenGL. Even though JavaScript interpreters are getting faster at a somewhat alarming rate, they won’t match the speed of C or C++. That said, if one can appropriately offload work onto the GPU, it won’t matter as much, but there will always be that overhead.

It won’t so much be a matter of having the same performance, but enough performance. If a person can go to a single webpage and get 60 frames per second performance in a game or tool without having to install software, that’s tremendous. Currently I’ve been getting between 60 and 90 frames per second with WebGLot, and I’m sure I can keep that number up as more features are added.

My hope is that this will be a tool and library that has a wide-enough feature set by the time WebGL is widely adopted that becomes often-used. But that’s just ego. The purer motivation is that if you’re a math teacher, and you want to have interactive demonstrations of Newton’s method, or parametric surfaces, or even flow fields, you can write an application in 20 minutes that does all the heavy lifting of graphing it for you. As long as you can describe the mathematical primitives, you should be able to render it. Of course there will be a general-purpose grapher available for any calculus student who’s having trouble visualizing this or that, too. Or a resourceful PDE student who need to solve his homework (the GPU-based PDE solver will take a little bit more time, but it’s very nearly complete).

In short, the strength of WebGL is that is has respectable performance, and in a year’s time, half the browsers (well maybe not half) on computers will support it, giving the average internet-user access to a wealth of media.

Silly Mistakes

Every programmer has had this, or at least I like to think that every programmer has experienced this - you compile, press “go,” and epic failure. And the joy doesn’t stop there - then the debugging begins. Occasionally, one encounters a bug that gets the better of them, sometimes for hours, sometimes for days, and sometimes for months.

Hairs is pulled, teeth are gnashed, and eyeballs strain, scouring line after line. You try to convince yourself that your algorithm is correct, and that each line of code is justified. And yet, it still gets the better of you.

After perhaps eight hours, you swallow your pride, and ask a trusted friend to take a look. Often the very act of explaining things to another human being is helpful, but sometimes you both have to dig into the code. Maybe a third friend happens upon the two of you, and joins in.

Then, a light bulb goes off. If you’re lucky, it’s a massive structural change that’s required, but sometimes, it’s a single line, or a single word or character, and you suddenly find yourself embarrassed. But do not be. Every programmer I’ve ever met, no matter how qualified has run into these problems. Still, I find it easy to doubt my competence afterwards.

There are rare and beautiful moments when not only does code compile on the first try, but it runs as expected. Few and far between, cherish these when they come.

This is all inspired from a recent bug I tracked down. An embarrassing one. Sure, had I read the 350 or so pages of the OpenGL ES 2.0 specification, I may have caught it earlier, but this was one of those times when it was a single word that had to change. I tell myself that I won’t keep making these kinds of mistakes, and with each conquered bug I gain a tool, an experience point, and that’s what makes one’s craft.

I’ve looked at the time I spend debugging, and I’ve noticed that the time it takes to solve a bug can often be reduced by leaving the problem for a bit. Taking a walk, getting a cup of coffee, or sometimes watching an episode of Arrested Development. The desire to find and fix a bug is a siren’s song - nearly impossible to walk away from, but often a bad idea.