Looking for Quote

17. December, 2007

I’m looking for a quote which goes along these lines: “If we are ever visited by aliens, we’ll have a lot of trouble explaining how a race smart enough to design the bomb is dumb enough to actually build it”. Does anyone know who said this?


Mixins in Java

14. November, 2007

Mixins are powerful programming concept in dynamic languages because they allow you to implements aspects of classes in different places and then “plug” them together. For example, the “tree” aspect of a data structure (something having parents and children) is well understood. A lot of data can be arranged in hierarchic trees. Yet, in many languages, you cannot say:

   class FileTreeNode extends File mixin TreeNode

to get a class which gives you access to all file operations and allows to arrange the items in a tree at the same time. This means you can’t directly attach it to a tree viewer. In some languages, like Python, this is trivial since you can add methods to a class any time you want. Other languages like C++ have multiple inheritance which allows to do something like this. Alas, not at runtime.

For Java, the Eclipse guys came up with a solution: adapters. It looks like this:

    public  T getAdapter (Class desiredType)
    {
        ... create an adapter which makes "this" behave like "desiredType" ...
    }

where “desiredType” is usually an interface of some kind (note: The Eclipse API itself is still Java 1.4, this is the generics version to avoid the otherwise necessary cast).

How can you use this?

In the most simple case, you can just make the class implement the interface and “return this” in the adapter. Not very impressive.

The next step is to create a factory which gets two bits of information: The object you want to wrap and the desired API. On top of that, you can use org.eclipse.core.internal.runtime.AdapterManager which allows to register any number of factories for adapters. Now, we’re getting somewhere and the getAdapter() method could look like this:

    @SuppressWarnings("unchecked")
    public  T getAdapter (Class desiredType)
    {
        return (T)AdapterManager.getDefault ().getAdapter (this, desiredType);
    }

This allows me to modify the behavior of my class at runtime more cheaply and safely than using the Reflection API. Best of all, the compiler will complain if you try to call a method that doesn’t exist:

    ITreeNode node = file.getAdapter(ITreeNode.class);
    ITreeNode parent = node.getParent(); // okay
    node.lastModification(); // Sorry, this is a file method

Try this with reflection: Lots of strings and no help. To implement the above example with an object that has no idea about trees but which you want to manage in a tree-like structure, you need this:

  • A factory which creates a tree adapter for the object in question.
  • The tree adapter is the actual tree data structure. The objects still have no idea they are in a tree. So adding/removing objects will happen in the tree adapter. Things get complicated quickly if you have some of the information you need for the tree in the objects themselves. Think files: You can use listFiles() to get the children. This is nice until you want to notify either side that a file has been created or deleted (and it gets horrible when you must spy on the actual filesystem for changes).
  • The factory must interact with the tree adapter in such a way that it can return existing nodes if you ask twice for an adapter for object X. This usually means that you need to have a map to lookup existing nodes.

A very simple example how to use this is to allow to override equals() at runtime. You need an interface:

interface IEquals {
    public boolean equals (Object other);
    public int hashCode ();
}

Now, you can define one or more adapter classes which implement this interface for your objects. If you register a default implementation for your object class, then you can use this code in your object to compare itself in the way you need at runtime:

    public boolean equals (Object obj)
    {
        return getAdapter (IEquals.class).equals (obj);
    }

Note: I suggest to cache the adapter if you don’t plan to change it at runtime. This allows you to switch it once at startup and equals() will still be fast. And you should not try to change this adapter when the object is stored as a key in a hashmap … you will have really strange problems like set.put(obj); ... set.contains(obj) -> false etc.

Or you can define an adapter which looks up a nice icon depending on the class or file type. The best part is that you don’t have API cross-pollution. If you have a file, then getParent() will return the parent directory while if you look at the object from the tree API, it will be a tree node. Neither API can “see” the other, so you will never have to rename methods because of name collisions. ITreeNode node = file.getAdapter(ITreeNode.class) also clearly expresses how you look at the object from now on: as a tree. This makes it much more simple to write reliable, reusable code.


Hollow Sphere in Blender

12. November, 2007

For a scene in one of my books (a public bath on the TAURUS), I need a ball of water suspended around the center of a large sphere. The sphere outside is the “ground” (you can walk around in it; it’s like the Hollow Earth theory but my version is just the product of careful alien design using magic a.k.a hi-tech to control gravity). I decided to render this in Blender to get a feeling how such a thing might look. What would my character actually see when they step into the bath?

Boolean operators are traditionally a weak spot of Blender (they are a major strength of POV-Ray, if you like text-file driven modeling). I had some trouble to get it to work and if you want to achieve a similar effect, here is how I pulled it off.

Inside

First add the inner object (this makes selecting more simple). In my case that would be an Icosphere (Press “Space”, Select “Add” -> “Mesh” -> “Icosphere”) with 3 subdivisions (to make it appear somewhat round even at the edges) and a radius of “4.000”. This should look roughly like this:

Since this is supposed to be the inner volume of the object, there is a problem: Blender thinks it defines the outside. The key information are the “face normals”. Open the mesh tools (Press “F9”) and select “Draw Normals” on the far right (in the “Mesh Tools1” tab; use the middle mouse button to drag the tab into view if you have to – it’s on the far right). Now the sphere sprouts little cyan pimples. Zoom in and rotate the sphere and you’ll see that they start on the center of each face and extend outwards. This is how Blender knows “outside” from “inside”: The direction in which the face normals point is “outside”.

To turn this into the inner volume, all you have to do is to click on “Flip Normals” (“Mesh Tools” Tab, third line, last button). If you have “Solid” rendering active, the face normals will become tiny dots because the triangle faces now hide the rest of them. The object will still look the same but now, you’re “inside” of it. Since all objects in Blender are hollow, it doesn’t mean much … for now.

I want a ball of water and water doesn’t have edges, so I also smooth the surface (“Set Smooth” at the bottom in the “Link and Materials” tab). This doesn’t change the actual geometry; it just draws the object smoothly. In my version of Blender, the object suddenly went all black at this point. Probably because I haven’t assigned a material, yet. Selecting “Solid” rendering helps.

Connecting Hole

I needed a connection between the two sides (there is a “hole” in the hollow water ball where you can swim from one side to the other if you don’t want to dive through it or use the walking spires or elevators in the restaurants), so I cut a hole in the inner sphere by selecting one vertice and deleting it (press “X” and select “Vertices”). In the image, you can see the lighter inside of the sphere shine through the hole:

Outside

Before I can create the outside, I must make sure that nothing is selected (or Blender would add the new points to the active object): Enter “Object Mode” (“Tab”) and select nothing (Press “A” until nothing is highlighted anymore).

For the outside, I create another sphere. Make sure the cursor hasn’t moved, so the centers of both objects are the same. If it isn’t, select the sphere, press “Shift+S” (Snap) and then “Cursor -> Selection”. When everything is ready, add the second icosphere: “Space” -> Add -> Mesh -> Icosphere, 3 subdivisions, Size “5.00”. I also make that smooth but I leave the face normals alone (this is the outside after all).

Again, I delete the face where the connecting hole is supposed to be: Select a point (in “Edit Mode”) and press “X” -> “Vertices”. Now, you might face two problems: a) the hole in the inner sphere is somewhere else and b) the hole might be below the one you just cut but it’s not perfectly aligned. If that is the case, you were in the wrong view.

When creating an icosphere (a sphere made of triangles instead of rectangles), the triangles don’t have all the same size. If you rotate the sphere, you can see that they are uneven somewhat. I found that the triangles touching the horizontal axis are very even. The solution: Create the spheres in one view (for example YZ) and cut the holes in another (for example XZ). So after doing everything again and cutting in the right views, it should look like this:

As you can see, I did erase the vertice on the Y axis. Next, shift select both objects (use the outliner if you have problems with the inner sphere) and join the objects (use the menu or “Ctrl+J”).

Smoothing Out the Wrinkles

After joining, it’s simple to close the whole: Switch to “Edit Mode”, select all vertices (six on the inner sphere, six on the outer, sequence doesn’t matter) and “fill” them with faces (in the menu Edit -> Faces -> Fill or Shift+F). If you rotate the scene, you’ll see that new triangles have been created but they look ugly in the otherwise smooth surface of the ball. Even using “Set Smooth” doesn’t help; the angles between the hole and the rest of the surface is just too big (mostly perpendicular). To fix this, use “Subdivide” (“Mesh Tools” tab) and “Smooth” (same tab). This halves the selected faces, creating new ones and the smooth step evens the angles. For me, it now looked like this:

Holy ugly! What’s wrong? I’ve left a hint … can you find it?

It’s the face normals again. For some reason, they point into the wrong direction around the hole. After a few undo steps (press “U”), I’m back at the point where the faces have just been created (before the smooth/subdivide steps). One “Flip Normals” later, the color transitions around the hole look much smoother. Back to another round of subdividing and smoothing. After the hole was as I wanted it, I noticed that the “horizon” of the ball still looked rough, so I selected all vertices, did another subdivide and several smooth to end with this result:

Pretty smooth, eh?

Rendering … With POV-Ray

After fumbling with water that looks at least a bit realistic, I created the same scene with KPovModeler (with water but without the hole *sigh*) to give you an idea what someone, standing on the “ground” would see:

Each piece of the red-white checker pattern on the walking spires is 10x10m, the ball hovers 1250m above the observer, has a diameter of 500m and the water is 50m thick/deep. The two blue cubes are both 100m big, one is standing on the opposite side on the “ground”, the other floats on the water. Anyone wants to add the water-slides, diving platforms (1250m jump!), etc.? With that much water and these dimensions, we’ll probably also have clouds, too. The spires don’t hold the water there, by the way, they are just a means of transport (if you don’t want to jump or use the slides).


Heroes (TV Show)

29. October, 2007

*gasp* (Sound after emerging from a two day Heroes Season 1 marathon). If you haven’t seen this, yet, you should.

As an author and SciFi fan, I’m always looking for good movies and TV shows. Here is my summary of season 1 (with a few spoilers further down below).

Overall, I’m very impressed. The show delivers depth and atmosphere like few I’ve seen before. It’s as smart and logical as CSI or Dr. House but the cast is much more complex and the story is a beautiful example of an interwoven stream of events which happen independently but influence each other in a very special way. Nothing in that series is set into stone; events happen, the viewer feels he knows what is going on just to stumble over another small piece of information which turns everything around. The same happens to the characters which often find themselves having to make hard decisions they feel they aren’t prepared for. Babylon 5 showed a glimpse of what can be done in this regard, Heroes goes the whole nine yards: Storytelling at it’s best, rich, believable characters, super-human action without losing a grip on the special effects.

Spoiler Warning: The following text is only safe to read after seeing all of season 1.

There are a few dark spots, though, and they show a few of the problems an author/storyteller faces. Let’s start with the “perfect prison”. The prison itself contains almost nothing except for a few pipes which one of the heroes uses later to make an escape. I didn’t notice them when Sylar was in that cell, so I’m giving the author the benefit of doubt and assume that Sylar was in a similar cell but one without the pipes. Alas, if you have ever seen a real prison, you’ll know that surveillance is ubiquitous. Furthermore, with dangerous criminals (especially ones with special abilities), guards never visit the inmate alone. Not so in Sylars case; no one seems to care who visits him and when and what they take along. When Jessica Sanders is imprisoned, the authors don’t make this “mistake”: Guards never handle her alone; they are even afraid to come close to her in rather large groups!

I’m calling this a “mistake” because actually, it is quite easy to create a prison that no one can escape without help. Unfortunately for the show, Sylar has to escape which renders the whole “perfect prison” idea into a death trap for the writer. Authors: If you ever feel you have written yourself into a corner, take a step back and check where you came from. If you can, try to find a real instead of a cheap solution, because when Sylar escaped, I thought: “Oh, that’s so silly.” I didn’t believe the show anymore for some time. When you write a story, the reader trusts that you produce a logical, believable world. Whenever you betray that trust, the reader will feel that your work is not worth the money she paid for it and this not what you want.

In the Sylar case, a possible solution would have been to rewrite story to make the attack on Claire happen far away from any “Company” location. Sylar could then have escaped much more believable from a make-shift prison. Or how about having more people around? It’s unlike Sylar to just slaughter anyone in his path but he could have just rendered the “normal” guards unconscious and then go after the persuading girl (so she can have her grand moment).

The ending of season 1 is something else entirely. At first, I thought is was impossible for Sylar to be alive. Mr. Bennet knows how dangerous he is and would surely have put a few more bullets through his head if he had had any doubt that Sylar was dead. Some of that is solved in season 2 where the writers come up why the heroes didn’t notice Sylar … “escaped”.

Just to round this up, here are a few more blunders which probably only happened because the writers had written themselves into a corner or vital information had to be cut away to fit the time slots of the show:

  • In the scene in the future when the guards smash in the door and shoot “Future Hiro”: Why doesn’t he stop time when he hears the door give in? Why doesn’t he stop time as soon as the Haitian is taken out to tell Hiro everything he knows just to be safe? There is no apparent reason to wait until the last moment (except to allow for a dramatic and tragic (a.k.a stupid) death). Or why doesn’t he stop time as soon as the Haitian is down to take out the guards trying to smash down the door?
  • When abducted in Las Vegas, Nathan Petrelly can fly away despite the Haitian being close by. Oh, and if that was a sonic book we’re hearing, Nathan ought to be dead but maybe his ability turns his skin into something more durable than steel while he flies. That only leaves the question how his clothes make it …
  • Again in the future: In all these years, Matt Parkman never noticed that Nathan Petrelli was in fact someone else? Never? In five years? Okay, again the benefit of doubt: Maybe the ability to create illusions can fool a telepath, too. Still, it seems uncomfortably odd.
  • After Claire ran the car into a wall, her father Noah has the brain of the quarterback erased so he “can’t make her life even more complicated that it already is”. Later, the whole school knows that Claire is somehow involved in the event. Having his brain erased just makes everything worse for her. Seems like an unlikely mistake for someone like Mr. Bennet.

All this might give you the impression that the crew around Tim Kring did a sloppy job. Well, think again. If you have seen Star Wars, you probably noticed the 264 mistakes in the first movie. For a TV show with a budget that is probably close to what Goerge Lucas spent for rubber stamps during the shooting, they did an incredible job.

Conclusion: Well done.

Lesson for authors out there: Strive for perfection and try to eliminate all logical mistakes and “easy ways out”. Otherwise, your readers will spend their money on the authors that try harder than you do, the next time they buy a book.


Five Easy Ways to Fail

25. October, 2007

It’s been said over and over again and now, once more: Five Easy Ways to Fail lists five simple ways to make sure a project will fail:

  • Hire without brains
  • Sloppy schedules
  • Demand reality matches your deadlines
  • Spread tasks evenly
  • Work as many hours as you can

Another insight by Joel Spolsky


Resizing a 3ware RAID-5 Array With Linux

25. October, 2007

Ever wanted to extend the available space in your RAID 5 array? Whenever I do, I’m missing a consistent recipe how to do it. The following applies to OpenSuSE 10.2 and a 3ware 9550SX controller with 8 lanes. If your setup is different, adjust as necessary. Here are the steps:

  1. Add the drive in a free slot.
  2. If it doesn’t show up in the web gui (Management -> Maintenance under “Available Drives”), click “Rescan Controller”
  3. Select the RAID-5 array you want to expand (not the free disk!)
  4. Click on “Migrate Unit”. The web gui should offer you a list of drives to add and a few other settings you can change in the process.
  5. Click OK to start the migration. If your array is large, this can take a long time. I migrated from 1.3TB to 1.6TB. This took 24h.
  6. After the migration has completed, you’ll have to reboot. Linux will see the new bytes only after the reboot but there is no danger in using the drive in this strange state for as long as you like. You just can’t claim the new space but you can’t loose any data, either.
  7. After reboot, make sure that no filesystems on the expanded RAID array are mounted. If they are, unmount them.
  8. If you run “vgdisplay” as root, it should show you the old size.
  9. Run “pvresize /dev/sdb” as root (replace the device name with yours). This will make Linux notice the new size. Note that it is safe to run this command without a reboot. It just won’t do anything in this case. It will only print “1 physical volume resized” but when you run “vgdisplay”, the size won’t have changed.
  10. Run “vgdisplay” again to make sure the new size is correct.
  11. Run “yast2 lvm_config” to add the free space to any existing file systems or to create new ones.

That’s all, folks.


Heroes (Storytelling)

25. October, 2007

As an author, you need to love your characters. You need to love them so much that you can make their lives really miserable. That doesn’t mean slaughtering their families. Killing is easy. Giving them depth is hard.

Characters must have reasons for what they do. Take the doctor in “Alien”. In the beginning of the movie, he opens the airlock blocked by Ripley and lets the contaminated crew members in. At that point, we think he’s doing this because he’s a doctor and he wants to help. Later, it turns out he is an android specifically programmed to gather alien lifeforms, ever at the expense of the crew. This gives the character depth that he doesn’t have when you just make him do things to move the story on.

It’s not necessary to explain everything to the reader; but every action should have a reason and at least you as the author should know that reason. Otherwise, the actions will soon start to become erratic and random. The readers will notice a pattern: There doesn’t seem to be a reason why someone does something except to drive the story on. If you want to check your story against this, ask yourself: Does the character at this point in the story even know why he should do this? Or is he just making life easier for me?

Rambo is another good example for this. It also demonstrates my main point: You must make life as hard as possible for your character. When Rambo decides to stand up against the sheriff, that is the hard decision (just shrugging and walking away would have been much more easy). After that event, things get out of control. The deputies handle Rambo like any other petty criminal, only Rambo is not your standard drunk picked from a gutter. Their abuse triggers Rambo’s instincts that kept him alive in the jungle. Blood is spilled.

Again, all characters could make the decision to step back, calm down, think. Instead, everyone tries to corner Rambo. They are driving him. Rambo escapes them as good as he can and only shoots down the helicopter when his own life is in danger. Again the pattern: Take the hard way.

The “Die Hard” movies work along the same lines. John McLane has a lot of chances just to hide in a corner and wait until everything is over but he never does. He always struggles to get the upper hand. That is what makes a character into a hero.

Many authors don’t get this (at least, it doesn’t make it to the screen). They put big and bigger guns into the hands of their “heroes” (“Eraser”, anyone?) They add bigger explosions or make the evil guys commit worse atrocities. Cameras zoom in deeper and longer when blood is spilled. Guts fly around. Special effects take over. When Norman Bates killed the woman under the shower in “Psycho”, Hitchcock keeps the camera on the drain. We don’t even see the act itself but the scene is more intense than anything I’ve seen in the last twenty years.

If you as an author take the easy way out, so will your character. If you put a lot of effort into making life miserable for your hero (little or no ammo, no shoes, no food, no shelter, no help, no way out) and you can still come up with believable reasons why your hero can survive against all these odds, then your hero will be great.

Or to put it another way: How could your hero be better than your effort writing about him?


Telepods of Doom 2

19. October, 2007

On Telepods of Doom, Mike P. argues:

We can only assume that a machine can reconstruct experience, consciousness and the human soul.

First of all, the machine maybe doesn’t have to reconstruct the soul of the being transported. Our everyday experience shows that the soul moves along with the body. There doesn’t seem to be a limitation on how fast the body can move (at least not up to the speed we can achieve) without losing contact to its soul. In fact, looking at the problem from a quantum physics view, there is no reason to believe that the soul has to care about the actual location of the body. This means that if the wave form which represents our body is teleported across the universe, the soul might just stick to it.

Of course, I might be wrong and the soul might loose contact the moment the body is teleported. On the positive side, this would be a final proof that a soul exists (or at least something beyond the sub-atomic level). On the negative side, this would open a whole new world of tools to people who are not prepared for such power.

When someone manages to prove the existence of the soul, people will start to work on way to measure it. To access it. To modify it. Area Denial Systems already offer convenient new ways of torturing anyone you happen to dislike without leaving traces. For the victims, this makes it impossible to prove the act in court, making their situation twice as bad.

Imagine machines which can access the soul.

Luckily, nature has laws which will make sure we become extinct unless we are able to handle the powers which we seize.


Telepods of Doom

26. September, 2007

On BeContrary is a discussion about Telepods of Doom. The question goes like this:

It is the year 2112. Telepods have been in use for a decade to instantly transport matter from one part of the universe to another. You are waiting in line with your family at a telepod station to go to Tau Ceti. In front of you in the queue you meet the inventor of the telepods. He tells you that the telepods only appear to move matter, what they actualy do is create an exact duplicate at the destination and destroy the original in the process.

Do you get in the telepod?

As my math teacher would say: You’re mixing up two frames of reference. In quantum physics, objects exist only once. There can be similar objects but these can never be exactly the same (they must differ in at least one attribute, for example in spin). Don’t use that argument when the MPAA comes after you. (“That music isn’t what was on CD! It must be different! Quantum theory says so!”)

One way to make exact copies is to destroy the original and transfer all attributes onto another object (thus destroying the other object and creating a new “original”). In the real (macro) world, this can lead to all kinds of problems: If the destroy happens before the “apply attributes”, you lose the object. If the destroy doesn’t happen at all, you suddenly have two copies. If only a part of the attributes are copied, you have an imperfect copy.

In the quantum world, none of these effects can happen. It’s either all or nothing because there is no state in between. Quantum particles can move through “solid” walls because they never spend any time inside the wall. In one moment, they are on one side, the next, they are on the other. The theory doesn’t ask for continuous movement. It just says “when you look several times, there is a certain chance that you’ll see the particle.” There is no explanation how it gets from one place to the other and how it spends the time when you don’t see it.

Since no one has found a flaw in the theory so far, it seems to be an accurate description of reality. That it contradicts our view of reality means that our view of reality is imperfect, not that quantum theory is wrong. Or as Douglas Adams put it:

“There is a theory which states that if ever anybody discovers exactly what the Universe is for and why it is here, it will instantly disappear and be replaced by something even more bizarre and inexplicable. There is another theory which states that this has already happened.”
— Douglas Adams


Undo for Webapps

25. September, 2007

While AJAX webapps grow more and more functionality, a very important one was missing so far: Undo. Imagine Word without Undo. ‘Nuff said.

Aza Raskin has a solution. Well done!