John Ratcliff's Code Suppository

A place where I insert my code into the anus of the Internet.

 

Thursday, August 18, 2011

Raycasting using AABB (Axis Aligned Bounding Volume) trees

Note: This photograph has nothing to do with this post; I just love showing off my new puppy Winston!

Today I am releasing an arguably somewhat useful code snippet which can preform high-speed raycasting against an arbitrary triangle mesh.  Now, this isn't particularly unique.  There are many libraries on the internet for doing this; however this may be the only one presented as an actual 'code snippet'; meaning just a single header file and a single relatively modestly sized CPP implementation.

If you need a robust general purpose raycasting and collision system I strongly recommend you use OPCODE written by my friend Pierre Terdiman.  OpCode is a phenomenal library that is extremely high performance.  You can also use the PhysX SDK from NVIDIA; which has a version of OPCODE internally which is also very high performance as well.

However, OPCODE does a whole lot more than just simple raycasting against a triangle mesh; it has a lot of other features and is distributed as about 100 source files comprising tens of thousands of lines of code.  Again, this isn't an issue if you are working on a large project and want to add the library in.

All that said, sometimes you are working on a small utility program or tool and you just need a quick and dirty way to raycast against a triangle mesh to speed up an algorithm.  In this case, you might find my implementation useful since you only need to add two small source files to your project.

This code snippet might also serve as a nice educational tool for students trying to understand how to build and use an axis-aligned-bounding-volume AABB tree.

You can download the source code from this Google Code project page.


RaycastMesh

The project includes a sample application which will load a 3d mesh, perform a million raycast operations, and save out a PNG image file showing the results.

However, to include it into your own project you need only the file 'RaycastMesh.h' and 'RaycastMesh.cpp'




3 Comments:

  • At 4:04 PM, Anonymous Anonymous said…

    John-

    I think this is exactly what I need for a project I've been putting off for a while.

    Your site is by far my favorite place to go for code that doesn't burden me with hours of integration trouble when I want to use it.

    Thanks for sharing,
    Robert

     
  • At 7:25 PM, Anonymous oregon security systems said…

    Good ideas! I like the way you express your idea and the topic you choose. KEep on your sharing! I appreciate it.

     
  • At 10:13 AM, Anonymous Anonymous said…

    Nice code. How about a swept sphere collision test, i.e. a raycast with thickness? I tried to implement that and failed.

     

Post a Comment

<< Home