Example #1
0
def get_vtk_cell_locator(surface):
    """Wrapper for vtkCellLocator

    Args:
        surface (vtkPolyData): input surface

    Returns:
        return (vtkCellLocator): Cell locator of the input surface.
    """
    locator = vtk.vtkStaticCellLocator()
    locator.SetDataSet(surface)
    locator.BuildLocator()

    return locator
Example #2
0
streamMapper = vtk.vtkPolyDataMapper()
streamMapper.SetInputConnection(rf.GetOutputPort())
streamMapper.SetScalarRange(output.GetScalarRange())
streamline = vtk.vtkActor()
streamline.SetMapper(streamMapper)

outline = vtk.vtkStructuredGridOutlineFilter()
outline.SetInputData(output)
outlineMapper = vtk.vtkPolyDataMapper()
outlineMapper.SetInputConnection(outline.GetOutputPort())
outlineActor = vtk.vtkActor()
outlineActor.SetMapper(outlineMapper)

# Use a vtkStaticCellLocator
staticLoc = vtk.vtkStaticCellLocator()
ivp2 = vtk.vtkCellLocatorInterpolatedVelocityField()
ivp2.SetCellLocatorPrototype(staticLoc)
streamer2 = vtk.vtkStreamTracer()
streamer2.SetInputData(output)
streamer2.SetSourceData(ps.GetOutput())
streamer2.SetMaximumPropagation(100)
streamer2.SetInitialIntegrationStep(.2)
streamer2.SetIntegrationDirectionToForward()
streamer2.SetComputeVorticity(1)
streamer2.SetIntegrator(rk4)
streamer2.SetInterpolatorPrototype(ivp2)
streamer2.Update()

rf2 = vtk.vtkRibbonFilter()
rf2.SetInputConnection(streamer2.GetOutputPort())
Example #3
0
    def ray_cast_color_thickness(poly_data,
                                 hit_point_list,
                                 cast_axis,
                                 dimensions,
                                 mm_of_air_past_bone,
                                 update_status,
                                 gradient_scale_factor=10.0):
        # ray direction cast axis index
        castIndex = BoneThicknessMappingLogic.determine_cast_axis_index(
            cast_axis)

        update_status(text="Building static cell locator...", progress=81)
        cellLocator = vtk.vtkStaticCellLocator()
        cellLocator.SetDataSet(poly_data)
        cellLocator.BuildLocator()

        total = len(hit_point_list)
        update_status(text="Calculating thickness (may take long, " +
                      str(total) + " rays)...",
                      progress=82)
        startTime = time.time()

        def init_array(name):
            a = vtk.vtkFloatArray()
            a.SetName(name)
            return a

        def interpret_distance(points):
            firstIn, lastOut = points[0], points[1]
            if len(points) > 2:
                for inOutPair in zip(*[iter(points[2:])] * 2):
                    # TODO incorporate cast bounds
                    # if newIn[1][cast_axis] < minBound and newOut[1][cast_axis] < minBound
                    if calculate_distance(
                            lastOut[1], inOutPair[0]
                        [1]) < mm_of_air_past_bone * gradient_scale_factor:
                        lastOut = inOutPair[1]
                return calculate_distance(firstIn[1], lastOut[1])
            else:
                return calculate_distance(firstIn[1], points[-1][1])

        def calculate_distance(point1, point2):
            d = numpy.linalg.norm(
                numpy.array((point1[0], point1[1], point1[2])) -
                numpy.array((point2[0], point2[1], point2[2])))
            d = d * gradient_scale_factor
            return d

        skullThicknessArray, airCellDistanceArray = init_array(
            BoneThicknessMappingType.THICKNESS), init_array(
                BoneThicknessMappingType.AIR_CELL)
        tol, pCoords, subId = 0.000, [0, 0, 0], vtk.reference(0)
        pointsOfIntersection, cellsOfIntersection = vtk.vtkPoints(
        ), vtk.vtkIdList()
        for i, hitPoint in enumerate(hit_point_list):
            stretchFactor = dimensions[castIndex]
            start = [
                hitPoint.point[0] + hitPoint.normal[0] * stretchFactor,
                hitPoint.point[1] + hitPoint.normal[1] * stretchFactor,
                hitPoint.point[2] + hitPoint.normal[2] * stretchFactor
            ]
            end = [
                hitPoint.point[0] - hitPoint.normal[0] * stretchFactor,
                hitPoint.point[1] - hitPoint.normal[1] * stretchFactor,
                hitPoint.point[2] - hitPoint.normal[2] * stretchFactor
            ]
            cellLocator.FindCellsAlongLine(start, end, tol,
                                           cellsOfIntersection)
            distances = []
            for cellIndex in range(cellsOfIntersection.GetNumberOfIds()):
                t = vtk.reference(0.0)
                p = [0.0, 0.0, 0.0]
                if poly_data.GetCell(cellsOfIntersection.GetId(
                        cellIndex)).IntersectWithLine(
                            start, end, tol, t, p, pCoords,
                            subId) and 0.0 <= t <= 1.0:
                    distances.append([t, p])
            if len(distances) >= 2:
                distances = sorted(distances, key=lambda kv: kv[0])
                skullThicknessArray.InsertTuple1(hitPoint.pid,
                                                 interpret_distance(distances))
                airCellDistanceArray.InsertTuple1(
                    hitPoint.pid,
                    calculate_distance(distances[0][1], distances[1][1]))
            else:
                skullThicknessArray.InsertTuple1(hitPoint.pid, 0)
                airCellDistanceArray.InsertTuple1(hitPoint.pid, 0)
            # update rays casted status
            if i % 200 == 0:
                update_status(
                    text="Calculating thickness (~{0} of {1} rays)".format(
                        i, total),
                    progress=82 + int(round((i * 1.0 / total * 1.0) * 18.0)))
        update_status(text="Finished thickness calculation in " +
                      str("%.1f" % (time.time() - startTime)) + "s...",
                      progress=100)
        return skullThicknessArray, airCellDistanceArray
# serve as starting points that shoot rays towards the
# center of the fist sphere.
#
sphere = vtk.vtkSphereSource()
sphere.SetThetaResolution(2*res)
sphere.SetPhiResolution(res)
sphere.Update()

mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(sphere.GetOutputPort())

actor = vtk.vtkActor()
actor.SetMapper(mapper)

# Now the locator
loc = vtk.vtkStaticCellLocator()
loc.SetDataSet(sphere.GetOutput())
loc.SetNumberOfCellsPerNode(5)
loc.BuildLocator()

locPD = vtk.vtkPolyData()
loc.GenerateRepresentation(4,locPD)
locMapper = vtk.vtkPolyDataMapper()
locMapper.SetInputData(locPD)
locActor = vtk.vtkActor()
locActor.SetMapper(locMapper)
locActor.GetProperty().SetRepresentationToWireframe()

# Now the outer sphere
sphere2 = vtk.vtkSphereSource()
sphere2.SetThetaResolution(res)
Example #5
0
output = clipper.GetOutput()
numCells = output.GetNumberOfCells()
bounds = output.GetBounds()
#print bounds

# Support subsequent method calls
genCell = vtk.vtkGenericCell()
t = vtk.reference(0.0)
x = [0,0,0]
pc = [0,0,0]
subId = vtk.reference(0)
cellId = vtk.reference(0)

# Build the locator
locator = vtk.vtkStaticCellLocator()
#locator = vtk.vtkCellLocator()
locator.SetDataSet(output)
locator.AutomaticOn()
locator.SetNumberOfCellsPerNode(20)
locator.CacheCellBoundsOn()
locator.BuildLocator()

# Now visualize the locator
locatorPD = vtk.vtkPolyData()
locator.GenerateRepresentation(0,locatorPD)

locatorMapper = vtk.vtkPolyDataMapper()
locatorMapper.SetInputData(locatorPD)

locatorActor = vtk.vtkActor()