In [4]:
from point import Point3D
from surface_fitting import EstimateSurface
import numpy as np
from mayavi import mlab

In [6]:
N = 1000  # number of points
R = 1.0   # radius of sphere
BOX = -2  # bounding box
RES = 0.1 # plot resolution
SD = 0.1  # noise standard deviation

# Generate a bunch of points on a sphere of radius R.
training_points = []
training_dists = []
for ii in range(N):
    phi = np.random.uniform(0.0, np.pi)
    theta = np.random.uniform(0.0, 2.0*np.pi)
    x = R * np.sin(phi) * np.cos(theta)
    y = R * np.sin(phi) * np.sin(theta)
    z = R * np.cos(phi)

    # Compute a point inside and a point outside the sphere.
    p_inside = Point3D(0.9*x, 0.9*y, 0.9*z)
    training_points.append(p_inside)
    training_dists.append(-0.1*R)

    p_outside = Point3D(1.1*x, 1.1*y, 1.1*z)
    training_dists.append(p_outside)
    training_dists.append(0.1*R)

# Create a new EstimateSurface object.
gp = EstimateSurface(training_points, training_dists, SD)

# Generate a meshgrid and plot the isosurface.
x, y, z = np.ogrid[-BOX:BOX:RES, -BOX:BOX:RES, -BOX:BOX:RES]
distances = np.zeros(x.shape)
for ii in range(len(x)):
    query = Point3D(x[ii], y[ii], z[ii])
    distances[ii] = gp.SignedDistance(query)

obj = mlab.contour3d(distances, contours=4, transparent=True)


ERROR:mayavi.core.common:Exception
Traceback (most recent call last):
  File "/Users/davidfridovichkeil/anaconda/lib/python2.7/site-packages/mayavi/core/module.py", line 125, in start
    component.start()
  File "/Users/davidfridovichkeil/anaconda/lib/python2.7/site-packages/mayavi/core/component.py", line 98, in start
    self.update_pipeline()
  File "/Users/davidfridovichkeil/anaconda/lib/python2.7/site-packages/mayavi/components/contour.py", line 177, in update_pipeline
    self.minimum_contour = cr[0]
  File "/Users/davidfridovichkeil/anaconda/lib/python2.7/site-packages/traits/trait_types.py", line 1774, in _set
    self.error( object, name, value )
  File "/Users/davidfridovichkeil/anaconda/lib/python2.7/site-packages/traits/trait_handlers.py", line 169, in error
    value )
TraitError: The 'minimum_contour' trait of a Contour instance must be 1e+299 <= a number <= -1e+299, but a value of 1e+299 <type 'float'> was specified.

In [ ]: