Showing posts with label convex hull. Show all posts
Showing posts with label convex hull. Show all posts

3D Convex hull in Python, a Blender implementation.

A slight adaption of the code in my previous post to make it directly usable as a add mesh extension in Blender.

A Blender add mesh extension

As of Blender 2.64 there is a native Convex Hull operator availablein Blender.

I won't elaborate too much on my adaption as it has nothing to do with web development. The script can be downloaded from my site and should be installed in Blenders addons directory and enabled in the user preferences. It is tested with Blender 2.61.

I still think it is a nice implementation and although it is pure Python, it performs quite well. Of course there is a native convex hull implementation in Blender game engine but that one isn't accessible from Python. Previous implemenations relied on external programs like qhull, which might be faster for large point sets but having a pure Python implementation that is tightly integrated with Blender feels cleaner. It certainly can handle points sets of up to a couple of thousand points.

I have adapted it to randomize the order of the vertices if the hull algorithm bombs. This will happen if adding a point involves calculating the volume of many degenerate tetrahedrons which is the case when trying to compute the hull of Blenders default uv-sphere. I tried to use Python's decimal module but the results did not improve so now if an error is encountered the vertices are reordered and a second attempt is made. Not clean, but simple.

3D Convex hull in Python

In this article I present a present a reimplementation in pure Python of Joseph O'Rourke's incremental 3D convex hull algorithm from his book Computational Geometry in C.

A convex hull in pure Python

This is the second, rather off topic, article on computational geometry in this blog. The previous article presented an implementation of the marching tetrahedrons algorithm.
The goal of this article is to provide object oriented, pythonic code to compute the convex hull of a collection of 3D points. The code is contained in a single Python module that may be downloaded from GitHub.
A sample of how to use this module is shown below, where we create a a roughly spherical cloud of points, calculate its convex hull and print this hull in STL format to stdout. The resulting object is shown in the image (as seen in Blender).
from random import random
from chull import Vector,Hull

sphere=[]
for i in range(2000):
 x,y,z = 2*random()-1,2*random()-1,2*random()-1
 if x*x+y*y+z*z < 1.0:
  sphere.append(Vector(x,y,z))
h=Hull(sphere)
h.Print()

Difference from the original implementation

The original code restricted the coordinates of points to integers, here there is no such restriction. This might result in errors if the coordinates are large.