Python vtk.vtkSphereSource() Examples

The following are 13 code examples of vtk.vtkSphereSource(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module vtk , or try the search function .
Example #1
Source File: Visualization.py    From PyNite with MIT License 6 votes vote down vote up
def __init__(self, node, scale_factor, text_height=5, combo_name='Combo 1'):
  
    # Calculate the node's deformed position
    newX = node.X + scale_factor*(node.DX[combo_name])
    newY = node.Y + scale_factor*(node.DY[combo_name])
    newZ = node.Z + scale_factor*(node.DZ[combo_name])

    # Generate a sphere for the node
    sphere = vtk.vtkSphereSource()
    sphere.SetCenter(newX, newY, newZ)
    sphere.SetRadius(0.6*text_height)

    # Set up a mapper for the node
    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputConnection(sphere.GetOutputPort())

    # Set up an actor for the node
    self.actor = vtk.vtkActor()
    self.actor.GetProperty().SetColor(255, 255, 0) # Yellow
    self.actor.SetMapper(mapper)
        
    # Create the text for the node label
    label = vtk.vtkVectorText()
    label.SetText(node.Name)

    # Set up a mapper for the node label
    lblMapper = vtk.vtkPolyDataMapper()
    lblMapper.SetInputConnection(label.GetOutputPort())

    # Set up an actor for the node label
    self.lblActor = vtk.vtkFollower()
    self.lblActor.SetMapper(lblMapper)
    self.lblActor.SetScale(text_height, text_height, text_height)
    self.lblActor.SetPosition(newX + 0.6*text_height, newY + 0.6*text_height, newZ)
    self.lblActor.GetProperty().SetColor(255, 255, 0) # Yellow

#%%
# Converts a member object into a member in its deformed position for the viewer 
Example #2
Source File: test_wrapping.py    From BrainSpace with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_pipeline():
    # check defaults
    s = vtk.vtkSphereSource()
    f = vtk.vtkSmoothPolyDataFilter()
    out = serial_connect(s, f)
    assert isinstance(out, BSPolyData)
    assert out.n_points > 0

    # check update filter
    s = vtk.vtkSphereSource()
    f = vtk.vtkSmoothPolyDataFilter()
    out = serial_connect(s, f, as_data=False)
    assert isinstance(out, BSAlgorithm)
    assert out.GetOutput().GetNumberOfPoints() > 0

    # check filter no update
    s = vtk.vtkSphereSource()
    f = vtk.vtkSmoothPolyDataFilter()
    out = serial_connect(s, f, as_data=False, update=False)
    assert isinstance(out, BSAlgorithm)
    assert out.GetOutput().GetNumberOfPoints() == 0

    # check non-existing port
    s = vtk.vtkSphereSource()
    f = vtk.vtkSmoothPolyDataFilter()
    out = serial_connect(s, f, port=1)
    assert out is None

    # check get all possible ports
    s = vtk.vtkSphereSource()
    f = vtk.vtkSmoothPolyDataFilter()
    out = serial_connect(s, f, port=-1)
    assert isinstance(out, list)
    assert len(out) == f.GetNumberOfOutputPorts()
    assert isinstance(out[0], BSPolyData)
    assert out[0].n_points > 0

    # check accept wrappers
    s = wrap_vtk(vtk.vtkSphereSource)
    f = wrap_vtk(vtk.vtkSmoothPolyDataFilter)
    assert isinstance(serial_connect(s, f), BSPolyData) 
Example #3
Source File: test_mesh.py    From BrainSpace with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _generate_sphere():
    s = vtk.vtkSphereSource()
    s.Update()
    return wrap_vtk(s.GetOutput()) 
Example #4
Source File: test_plotting.py    From BrainSpace with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def plotter_single_renderer():
    s = to_data(vtk.vtkSphereSource())

    p = Plotter(offscreen=True)
    ren0 = p.AddRenderer(row=0, col=0)
    ac0 = ren0.AddActor()
    ac0.SetMapper(inputdata=s)
    return p 
Example #5
Source File: test_plotting.py    From BrainSpace with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_build_plotter():
    s1 = to_data(vtk.vtkSphereSource())
    s2 = to_data(vtk.vtkSphereSource())

    surfs = {'s1': s1, 's2': s2}
    layout = np.array([['s1', 's2'], ['s2', 's2']])
    p = build_plotter(surfs, layout, offscreen=True)
    assert isinstance(p, Plotter) 
Example #6
Source File: test_plotting.py    From BrainSpace with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_plot_surf():
    s1 = to_data(vtk.vtkSphereSource())
    s2 = to_data(vtk.vtkSphereSource())

    surfs = {'s1': s1, 's2': s2}
    layout = np.array([['s1', 's2'], ['s2', 's2']])
    plot_surf(surfs, layout, offscreen=True) 
Example #7
Source File: test_plotting.py    From BrainSpace with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_plot_hemispheres():
    s1 = to_data(vtk.vtkSphereSource())
    s2 = to_data(vtk.vtkSphereSource())

    plot_hemispheres(s1, s2, offscreen=True) 
Example #8
Source File: helpers.py    From pyvista with MIT License 5 votes vote down vote up
def check_depth_peeling(number_of_peels=100, occlusion_ratio=0.0):
    """Check if depth peeling is available.

    Attempts to use depth peeling to see if it is available for the current
    environment. Returns ``True`` if depth peeling is available and has been
    successfully leveraged, otherwise ``False``.

    """
    # Try Depth Peeling with a basic scene
    source = vtk.vtkSphereSource()
    mapper = vtk.vtkPolyDataMapper()
    mapper.SetInputConnection(source.GetOutputPort())
    actor = vtk.vtkActor()
    actor.SetMapper(mapper)
    # requires opacity < 1
    actor.GetProperty().SetOpacity(0.5)
    renderer = vtk.vtkRenderer()
    renderWindow = vtk.vtkRenderWindow()
    renderWindow.AddRenderer(renderer)
    renderWindow.SetOffScreenRendering(True)
    renderWindow.SetAlphaBitPlanes(True)
    renderWindow.SetMultiSamples(0)
    renderer.AddActor(actor)
    renderer.SetUseDepthPeeling(True)
    renderer.SetMaximumNumberOfPeels(number_of_peels)
    renderer.SetOcclusionRatio(occlusion_ratio)
    renderWindow.Render()
    return renderer.GetLastRenderingUsedDepthPeeling() == 1 
Example #9
Source File: test_code_vasp_01.py    From PyChemia with MIT License 5 votes vote down vote up
def MakeSphere():
    """
    Make a sphere as the source.
    :return: vtkPolyData with normal and scalar data.
    """
    source = vtk.vtkSphereSource()
    source.SetCenter(0.0, 0.0, 0.0)
    source.SetRadius(10.0)
    source.SetThetaResolution(32)
    source.SetPhiResolution(32)
    source.Update()
    return MakeElevations(source.GetOutput()) 
Example #10
Source File: pointobject.py    From Det3D with Apache License 2.0 5 votes vote down vote up
def CreateSphere(self, origin, r):
        "Create a sphere with given origin (x,y,z) and radius r"
        sphere = vtk.vtkSphereSource()
        sphere.SetCenter(origin)
        sphere.SetRadius(r)
        sphere.SetPhiResolution(25)
        sphere.SetThetaResolution(25)
        sphere.Update()

        self.pd = vtk.vtkPolyData()
        self.pd.DeepCopy(sphere.GetOutput())
        self.scalars = None
        self.SetupPipelineMesh() 
Example #11
Source File: test_wrapping.py    From BrainSpace with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def test_cell_types():
    ss = vtk.vtkSphereSource()
    ss.Update()
    st = wrap_vtk(ss.GetOutput())
    sl = mc.to_lines(st)
    sv = mc.to_vertex(st)

    assert checks.get_cell_types(st) == np.array([VTK_TRIANGLE])
    assert checks.get_cell_types(st.VTKObject) == np.array([VTK_TRIANGLE])
    assert checks.get_cell_types(sl) == np.array([VTK_LINE])
    assert checks.get_cell_types(sv) == np.array([VTK_VERTEX])

    assert checks.get_number_of_cell_types(st) == 1
    assert checks.get_number_of_cell_types(st.VTKObject) == 1
    assert checks.get_number_of_cell_types(sl) == 1
    assert checks.get_number_of_cell_types(sv) == 1

    assert checks.has_unique_cell_type(st)
    assert checks.has_unique_cell_type(st.VTKObject)
    assert checks.has_unique_cell_type(sl)
    assert checks.has_unique_cell_type(sv)

    assert checks.has_only_triangle(st)
    assert checks.has_only_triangle(st.VTKObject)
    assert checks.has_only_line(sl)
    assert checks.has_only_vertex(sv)

    ss2 = vtk.vtkSphereSource()
    ss2.SetRadius(3)
    ss2.Update()
    s2 = ss2.GetOutput()

    app = vtk.vtkAppendPolyData()
    app.AddInputData(sl.VTKObject)
    app.AddInputData(s2)
    app.Update()
    spl = wrap_vtk(app.GetOutput())

    cell_types = np.sort([VTK_TRIANGLE, VTK_LINE])
    assert np.all(checks.get_cell_types(spl) == cell_types)
    assert checks.get_number_of_cell_types(spl) == cell_types.size
    assert checks.has_unique_cell_type(spl) is False
    assert checks.has_only_triangle(spl) is False
    assert checks.has_only_line(spl) is False
    assert checks.has_only_vertex(spl) is False 
Example #12
Source File: test_null_models.py    From BrainSpace with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def test_moran():
    # Sphere with points as locations to build spatial matrix
    sphere = wrap_vtk(vtk.vtkSphereSource, radius=20, thetaResolution=10,
                      phiResolution=5)
    sphere = to_data(sphere)
    n_pts = sphere.n_points

    # Features to randomize
    rs = np.random.RandomState(0)
    feats = rs.randn(n_pts, 2)

    # build spatial weight matrix
    a = me.get_immediate_distance(sphere)
    a.data **= -1

    # test default
    v, w = compute_mem(a, tol=1e-7)
    assert w.shape[0] <= (n_pts - 1)
    assert v.shape == (n_pts, w.shape[0])

    r1 = moran_randomization(feats[:, 0], v, n_rep=10, random_state=0)
    assert r1.shape == (10, n_pts)

    r2 = moran_randomization(feats, v, n_rep=10, random_state=0)
    assert r2.shape == (10, n_pts, 2)

    # test default dense
    mem, ev = compute_mem(a.toarray(), tol=1e-7)
    assert np.allclose(w, ev)
    assert np.allclose(v, mem)

    r1 = moran_randomization(feats[:, 0], mem, n_rep=10, random_state=0)
    assert r1.shape == (10, n_pts)

    r2 = moran_randomization(feats, mem, n_rep=10, random_state=0)
    assert r2.shape == (10, n_pts, 2)

    # test object api
    msr = MoranRandomization(n_rep=10, random_state=0, tol=1e-7)
    msr.fit(a)
    assert np.allclose(msr.mev_, ev)
    assert np.allclose(msr.mem_, mem)
    assert np.allclose(r1, msr.randomize(feats[:, 0]))
    assert np.allclose(r2, msr.randomize(feats))

    # test object api with PolyData
    msr = MoranRandomization(n_rep=10, random_state=0, tol=1e-7)
    msr.fit(sphere)
    assert np.allclose(msr.mev_, ev)
    assert np.allclose(msr.mem_, mem)
    assert np.allclose(r1, msr.randomize(feats[:, 0]))
    assert np.allclose(r2, msr.randomize(feats)) 
Example #13
Source File: geometric_objects.py    From pyvista with MIT License 4 votes vote down vote up
def Sphere(radius=0.5, center=(0, 0, 0), direction=(0, 0, 1), theta_resolution=30,
           phi_resolution=30, start_theta=0, end_theta=360, start_phi=0, end_phi=180):
    """Create a vtk Sphere.

    Parameters
    ----------
    radius : float, optional
        Sphere radius

    center : np.ndarray or list, optional
        Center in [x, y, z]

    direction : list or np.ndarray
        Direction the top of the sphere points to in [x, y, z]

    theta_resolution: int , optional
        Set the number of points in the longitude direction (ranging from
        start_theta to end theta).

    phi_resolution : int, optional
        Set the number of points in the latitude direction (ranging from
        start_phi to end_phi).

    start_theta : float, optional
        Starting longitude angle.

    end_theta : float, optional
        Ending longitude angle.

    start_phi : float, optional
        Starting latitude angle.

    end_phi : float, optional
        Ending latitude angle.

    Return
    ------
    sphere : pyvista.PolyData
        Sphere mesh.

    """
    sphere = vtk.vtkSphereSource()
    sphere.SetRadius(radius)
    sphere.SetThetaResolution(theta_resolution)
    sphere.SetPhiResolution(phi_resolution)
    sphere.SetStartTheta(start_theta)
    sphere.SetEndTheta(end_theta)
    sphere.SetStartPhi(start_phi)
    sphere.SetEndPhi(end_phi)
    sphere.Update()
    surf = pyvista.PolyData(sphere.GetOutput())
    surf.rotate_y(-90)
    translate(surf, center, direction)
    return surf