def pointDensity(mesh, dims=(40, 40, 40), bounds=None, radius=None, computeGradient=False, locator=None): """ Generate a density field from a point cloud. Input can also be a set of 3D coordinates. Output is a ``Volume``. The local neighborhood is specified as the `radius` around each sample position (each voxel). The density is expressed as the number of counts in the radius search. :param int,list dims: numer of voxels in x, y and z of the output Volume. :param bool computeGradient: Turn on/off the generation of the gradient vector, gradient magnitude scalar, and function classification scalar. By default this is off. Note that this will increase execution time and the size of the output. (The names of these point data arrays are: "Gradient", "Gradient Magnitude", and "Classification".) :param vtkStaticPointLocator locator: can be assigned from a previous call for speed. See example script: |pointDensity.py|_ """ if not utils.isSequence(dims): dims = (dims, dims, dims) pdf = vtk.vtkPointDensityFilter() if utils.isSequence(mesh): # user passing coords if len(mesh) == 3: mesh = np.c_[mesh[0], mesh[1], mesh[2]] poly = utils.buildPolyData(mesh) b = poly.GetBounds() diag = np.sqrt((b[1] - b[0])**2 + (b[3] - b[2])**2 + (b[5] - b[4])**2) else: poly = mesh.polydata() b = poly.GetBounds() diag = mesh.diagonalSize() pdf.SetInputData(poly) pdf.SetSampleDimensions(dims) pdf.SetDensityEstimateToFixedRadius() pdf.SetDensityFormToNumberOfPoints() if locator: pdf.SetLocator(locator) if radius is None: radius = diag / 15 pdf.SetRadius(radius) if bounds is None: bounds = b pdf.SetModelBounds(bounds) pdf.SetComputeGradient(computeGradient) pdf.Update() img = pdf.GetOutput() vol = Volume(img) vol.name = "PointDensity" vol.info['radius'] = radius vol.locator = pdf.GetLocator() return vol
def __init__(self, *inputobj, **options): c = options.pop("c", None) alpha = options.pop("alpha", 1) exterior = options.pop("exterior", False) fast = options.pop("fast", False) computeNormals = options.pop("computeNormals", False) mesh, u = _inputsort(inputobj) if not mesh: return if exterior: import dolfin meshc = dolfin.BoundaryMesh(mesh, 'exterior') else: meshc = mesh if hasattr(mesh, "coordinates"): coords = mesh.coordinates() else: coords = mesh.geometry.points poly = utils.buildPolyData(coords, meshc.cells(), fast=fast, tetras=True) Mesh.__init__( self, poly, c=c, alpha=alpha, computeNormals=computeNormals, ) self.mesh = mesh # holds a dolfin Mesh obj self.u = u # holds a dolfin function_data # holds the actual values of u on the mesh self.u_values = _compute_uvalues(u, mesh)