A raytracer in python – part 1: basic functionality

Some time ago I visited Sydney, and I made a tragic mistake: I entered the University bookshop. Why a mistake, you say? I am book maniac. As soon as I enter a book shop (live or on web) I end up spending up to a thousands euro every time. This time, it was not the credit card, but the limit of 20 kg on my luggage to put a limit on what I could buy. Needless to say I got really good stuff, in particular this 761 pages of pure awesomeness: “Ray tracing from the ground up”. This book teaches you how to write a raytracer, step by step from a first basic skeleton to incredibly complex optic effects. It also provides code, available for download under GPL. I haven’t downloaded it, but according to the book it’s in C++. Since I want to keep myself fresh with python, I will write my own version in this language.

The first objection I may hear is performance. Native python is much slower than C++. I agree, and I do it on purpose. My objective is in fact not only to write a raytracer for fun (and the world is full of already awesome raytracers), but also to perform some infrequent python exercises: interfacing with C, parallelization and, hopefully, some OpenCL programming. This is my hope, at least. I will try to spend some time on it and see how far I can get.

What is raytracing and how does it work ?

Raytracing is a technique to produce a photorealistic image. It works by projecting rays from the observer to the scene, and coloring pixels on a viewplane for every ray that intersects an object.

This mechanism resembles how vision works, although in the opposite direction. Light rays from a lamp hit objects and their reflection happens to scatter around. Some of these reflections will enter our eyes and allow us to see the world around us. Doing the opposite, tracing from the observer, is clearly more efficient as we don’t care about the rays not hitting the observer (at least in its basic implementation), only those who do.

Performing raytracing (or to be more accurate, for now just its basic form raycasting)  involves the following steps:

  1. define a geometric object in space, like for example a sphere
  2. define a view panel made of pixels
  3. shoot one straight line (ray) from the center of each pixel
  4. if the ray intersects the object, mark the pixel colored, otherwise mark it with the background color

That’s it. Basically, it’s an exercise in geometry: finding intersections between lines and 3D objects in 3D space. This first program, ray.py, does exactly that. You will need to install the Python Imaging Library, pygame and numpy. The result is intriguing:

A rendered sphere

Ok, I have a very loose definition of “intriguing”, but it’s a start.

Posted in Books, Python, Raytracing. Comments Off »

Pirates Pirates!

So I discovered that I apparently entered the sacred shrine of those people whose work is available in the warez underground.

I don’t think I am allowed to state my personal opinion on this regard. Suffice to say I think this means for sure that I reached a fair level of achievement.

Quick, someone get some ninjas, so we can deliver pizzas!

Posted in Books, Personal. Comments Off »

Computing for Comparative Microbial Genomics

I am proud to announce that Springer has finally released on the web (and Amazon as well) the descriptive information of the textbook I took part on: Computing for Comparative Microbial Genomics. It was a fantastic and incredible experience, for which I will always be grateful to Dave Ussery, my supervisor at Technical University of Denmark, and to Trudy Wassenaar, an independent professional and Associate Professor at the same institute.

Together with the book, I deployed a supplemental information website, comparativemicrobial.com. At the moment, it does not contain much information, apart of some biographies and a couple of links. We expect to enrich it with simple tutorial code and up-to-date news as time passes, following the feedback we obtain from the readers.

EDIT: specified that only the description is available on the web, not the textbook itself ;)

Posted in Authoring, Bioinformatics, Books, Websites. Comments Off »

New books in my bookshelf

I just added two new books in my bookshelf: Refactoring Databases: Evolutionary Database Design and The Mythical Man Month and Other Essays on Software Engineering.

I am currently reading the latter. There are very valid reasons for this book to be a classic. It provides a great description on how real-world project management works, and which are the important steps and strategies to cope with well known mistakes and issues during development of large systems. It is very broad, never too specific, but it is full of insights. A mind-opening book I strongly suggest.

Posted in Books, Project Management, Refactoring. Comments Off »