Short answer
Yes, it is possible to speed up the Delaunay-triangulation and make it as fast as it is in Matlab.
If you are not afraid of some setup-work, then one possibility is to use a package which calls a c-implementation of the Delaunay-triangulation. One package I know is qh-math which is available in the Wolfram-library:
This package includes source code and support files needed to create a MathLink-based interface to the Qhull library (http://www.qhull.org) algorithm for Delaunay Triangulation. The sources are based on work done originally by Alban Tsui at the Imperial College of Science, Technology and Medicine.
And btw, this is exactly what Matlab is using: http://www.qhull.org/html/qh-faq.htm#math
Usage
I assume the program qh-math.exe
is located in my download-folder. For your system you have to change this in the Install
call. The usage is pretty easy. First you Install
the MathLink program and after this you can call qDelaunayTriangulation[..]
like a normal Mathemtatica function:
lnk = Install["/home/patrick/Downloads/qh-math/qh-math.exe"];
And then you can triangulate your points
meshGrid[{x1_, x2_, y1_, y2_}, h0_] :=
With[{yh0 = h0*Sqrt[3.]/2},
Array[{(#1 - 1)*h0 + x1 + (1 + (-1)^#2) h0/4, (#2 - 1)*yh0 +
y1} &, Ceiling@{(x2 - x1)/h0, (y2 - y1)/yh0}]]~Flatten~1;
p = meshGrid[{-1, 1, -1, 1}, 0.05];
{t, del} = AbsoluteTiming[qDelaunayTriangulation[p]];
On my machine this took only t=0.032471
seconds. The the result looks nice
Graphics[MapIndexed[{ColorData[29, First[#2]],
Polygon[#1]} &, (Part[p, #] & /@ del)]]
Please note that the output is different from DelaunayTriangulation
. This version really gives a triangle index list like {{5, 6, 2}, {10, 7, 4}, {1, 5, 6},...
.
Freshly compiled qh-math.exe for Windows
Due to the great efforts of @Oleksandr R. we have now compiled versions of qh-math.exe
and all the commandline tools from qhull. Please download a zip with all files for your system:
Compiling your own qh-math
I'm on Linux here and since there is no executable program included I had to compile it by myself. Since it can happen, that your program does not work (it's kind of old) you may have to compile it for your machine too. Therefore, I explain it step by step
Compiling: First you download the archive with the sources and unpack it. The following steps all takes place in the terminal. On Windoze you may want to do this in Visual Studio or with Cygwin.
First I store the path-name to my dev-directory of MathLink in a variable
MROOT="/usr/local/Wolfram/Mathematica/8.0/SystemFiles/Links/MathLink/\
DeveloperKit/Linux-x86-64/CompilerAdditions/"
Then I had to install the qhull
development files. Here, I could use my package manager, while on other systems you may need to download and install it from the home page of qhull.
sudo apt-get install libqhull-dev
Then you go into the unpacked folder of qh-math and use mprep
of Mathematica to process the template file
$MROOT/mprep -o qh-math.tm.c qh-math.tm
Now you can compile the sources into a MathLink program
gcc -I${MROOT} -L${MROOT} -I/usr/include/qhull -lqhull -lML64i3 -lm \
-lpthread -lrt -lstdc++ qh-math.c qh-math.tm.c
If you use a recent version of qhull you have to rename the variable in qh-math.c
char qh_version[] = "qh-math.c 2000/7/6";
into maybe qh_version_blub
. Otherwise it clashes with a definition in the qhull lib.
The final MathLink program qh-math.exe
is now ready to use in this directory.
Graphics`Mesh`Delaunay[]
... $\endgroup$