From a505cf200fca2c3e55b751705a9b2bc60199294e Mon Sep 17 00:00:00 2001 From: Sebastian Kremiec Date: Fri, 30 Oct 2015 10:54:24 +0100 Subject: [PATCH] Marching cubes --- examples/marching_cubes.ipynb | 52 +++++++++++++++++++++++++++++++++++ k3d/factory.py | 11 ++++++++ 2 files changed, 63 insertions(+) create mode 100644 examples/marching_cubes.ipynb diff --git a/examples/marching_cubes.ipynb b/examples/marching_cubes.ipynb new file mode 100644 index 00000000..4d27c32a --- /dev/null +++ b/examples/marching_cubes.ipynb @@ -0,0 +1,52 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "from k3d import K3D\n", + "from numpy import mgrid\n", + "\n", + "resolution = 30\n", + "X, Y, Z = mgrid[:resolution, :resolution, :resolution]\n", + "scalars_field = ((X-15.0)/15.0)**2 + ((Y-15.0)/15.0)**2 + ((Z-15.0)/15.0)**2\n", + "\n", + "view_matrix = (\n", + " 1.0, 0.0, 0.0, 0.2,\n", + " 0.0, 1.0, 0.0, 0.0,\n", + " 0.0, 0.0, 1.0, 27.5,\n", + " 0.0, 0.0, 0.0, 1.0\n", + ")\n", + "\n", + "plot = K3D()\n", + "plot += K3D.marching_cubes(view_matrix, scalars_field, resolution, isolation=0.8)\n", + "plot.display()" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 2", + "language": "python", + "name": "python2" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +} diff --git a/k3d/factory.py b/k3d/factory.py index 4721aefc..d94de1fe 100644 --- a/k3d/factory.py +++ b/k3d/factory.py @@ -54,6 +54,17 @@ def surface(cls, view_matrix, heights, resolution): 'resolution': resolution, } + @classmethod + def marching_cubes(cls, view_matrix, scalars_field, resolution, isolation, color=0xFFFFFF): + return { + 'type': 'MarchingCubes', + 'modelViewMatrix': cls.__matrix_to_list(view_matrix), + 'resolution': resolution, + 'color': color, + 'isolation': isolation, + 'scalarsField': cls.__matrix_to_list(scalars_field), + } + @classmethod def __to_list(cls, arg): if not hasattr(arg, '__iter__'):