def marching_cubes(volume, smooth=True, decimate=True, **kwargs): from tvtk.api import tvtk from tvtk.common import configure_input imgdata = tvtk.ImageData(dimensions=volume.shape) imgdata.point_data.scalars = volume.flatten('F') contours = tvtk.ContourFilter(number_of_contours=1) contours.set_value(0, 1) configure_input(contours, imgdata) if smooth: smoothargs = dict(number_of_iterations=40, feature_angle=90, pass_band=.05) smoothargs.update(kwargs) contours = tvtk.WindowedSincPolyDataFilter(input=contours.output, **smoothargs) if decimate: contours = tvtk.QuadricDecimation(input=contours.output, target_reduction=.75) contours.update() pts = contours.output.points.to_array() polys = contours.output.polys.to_array().reshape(-1, 4)[:, 1:] return pts, polys
def _decimate_surface(points, triangles, reduction): """Aux function""" if 'DISPLAY' not in os.environ and sys.platform != 'win32': os.environ['ETS_TOOLKIT'] = 'null' try: from tvtk.api import tvtk except ImportError: raise ValueError('This function requires the TVTK package to be ' 'installed') if triangles.max() > len(points) - 1: raise ValueError('The triangles refer to undefined points. ' 'Please check your mesh.') src = tvtk.PolyData(points=points, polys=triangles) decimate = tvtk.QuadricDecimation(input=src, target_reduction=reduction) decimate.update() out = decimate.output tris = out.polys.to_array() # n-tuples + interleaved n-next -- reshape trick return out.points.to_array(), tris.reshape(tris.size / 4, 4)[:, 1:]