[vtkusers] ​Re: hemispherical cap with hole at the bottom (David Gobbi)

Andrew Maclean andrew.amaclean at gmail.com
Tue Apr 16 01:38:55 EDT 2019


Here is a quick example to illustrate what David said, I'll add this and a
C++ version to VTKExamples.
Petr, you should consider using [VTK Discourse](https://discourse.vtk.org/)
instead of this site.

##############################################

#!/usr/bin/env python

import math

import vtk


def get_program_parameters():
    import argparse
    description = ''
    epilogue = '''

   '''
    parser = argparse.ArgumentParser(description=description, epilog=epilogue,

formatter_class=argparse.RawDescriptionHelpFormatter)
    parser.add_argument('angle', default=90, type=float, nargs='?',
                        help='The arc of the sphere, an angle in
degrees from +y in the +x direction.')
    parser.add_argument('step', default=1, type=float, nargs='?',
help='The step angle in degrees.')
    parser.add_argument('radius', default=1, type=float, nargs='?',
help='The radius of the sphere.')
    parser.add_argument('-c', '--closed', action='store_true',
help='Add a closed cap.')
    args = parser.parse_args()
    return args.angle, args.step, args.radius, args.closed


def main():
    angle, step, radius, closed = get_program_parameters()
    angle = math.radians(angle)
    step = math.radians(step)
    precision = 1.0e-6
    start = math.radians(90)
    pts = list()
    # Do the curved line
    theta = 0.0
    while theta <= angle:
        x = radius * math.cos(start - theta)
        z = radius * math.sin(-start + theta)
        if abs(x) < precision:
            x = 0
        if abs(z) < precision:
            z = 0
        pts.append((x, 0, z))
        theta += step

    if closed:
        # Drop a perpendicular from the last point to the x-axis
        last_point = pts[-1]
        num_pts = 10
        interval = float(num_pts) / radius
        if last_point[0] > precision:
            for i in range(1, num_pts):
                x = last_point[0] - i / interval
                z = last_point[2]
                if abs(x) < precision:
                    x = 0
                if abs(z) < precision:
                    z = 0
                pts.append((x, 0, z))
        if pts[-1][0] > precision:
            pts.append((0, 0, z))

    # Setup points and lines
    points = vtk.vtkPoints()
    lines = vtk.vtkCellArray()
    for pt in pts:
        pt_id = points.InsertNextPoint(pt)
    for i in range(0, len(pts) - 1):
        line = vtk.vtkLine()
        line.GetPointIds().SetId(0, i)
        line.GetPointIds().SetId(1, i + 1)
        lines.InsertNextCell(line)

    polydata = vtk.vtkPolyData()
    polydata.SetPoints(points)
    polydata.SetLines(lines)

    # Extrude the profile to make the capped sphere.
    #
    extrude = vtk.vtkRotationalExtrusionFilter()
    extrude.SetInputData(polydata)
    extrude.SetResolution(60)

    #  Visualize
    colors = vtk.vtkNamedColors()

    mapper = vtk.vtkPolyDataMapper()
    # To see the line
    # mapper.SetInputData(polydata)
    # To see the surface
    mapper.SetInputConnection(extrude.GetOutputPort())

    actor = vtk.vtkActor()
    actor.SetMapper(mapper)
    actor.GetProperty().SetLineWidth(4)
    actor.GetProperty().SetColor(colors.GetColor3d('Cornsilk'))

    ren = vtk.vtkRenderer()
    renWin = vtk.vtkRenderWindow()
    renWin.AddRenderer(ren)
    iren = vtk.vtkRenderWindowInteractor()
    iren.SetRenderWindow(renWin)

    ren.AddActor(actor)
    ren.SetBackground(colors.GetColor3d('DarkGreen'))

    ren.ResetCamera()
    ren.GetActiveCamera().Azimuth(0)
    ren.GetActiveCamera().Elevation(60)
    ren.ResetCameraClippingRange()

    renWin.SetSize(600, 600)
    renWin.Render()
    renWin.SetWindowName('CappedSphere')
    iren.Start()


if __name__ == '__main__':
    main()

##############################################




>
> ---------- Forwarded message ----------
> From: David Gobbi <david.gobbi at gmail.com>
> To: VTK Users <vtkusers at vtk.org>
> Cc:
> Bcc:
> Date: Sun, 14 Apr 2019 15:06:17 -0600
> Subject: Re: [vtkusers] hemispherical cap with hole at the bottom
> Hi Petr,
>
> Unfortunately, vtkClipPolyData doesn't close off the surface after
> clipping.  It wasn't designed to do that.  In fact, closing things off
> after clipping is a rather difficult problem.
>
> I recommend that you take another approach.  Your cap is rotationally
> symmetric, right?  Try the rotational extrusion filter:
> https://vtk.org/doc/nightly/html/classvtkRotationalExtrusionFilter.html
>
> All you need to do is generate a cross-section of your cap, i.e. an arc.
> Just do this by writing a couple "for" loops in python to build the arc
> using sin() and cos(), make a polyline and stuff it in a vtkPolyData.  For
> example,
> https://lorensen.github.io/VTKExamples/site/Python/GeometricObjects/PolyLine1/
>
> Once you have your cross-section, pass it through
> vtkRotationalExtrusionFilter to generate the shape.
>
>    David
>
> On Sun, Apr 14, 2019 at 9:30 AM Petr Vokac <petr.2006 at centrum.cz> wrote:
>
>> I am trying to create hemispherical cap using two spheres, clipping them
>> by
>> plane - it works, and clipping them by cylinder - it does not work as it
>> leaves two surfaces unconnected, why?
>>
>> python script:
>>
>> #!/usr/bin/env python
>>
>> import vtk
>> sphere = vtk.vtkSphereSource()
>> sphere.SetRadius(10.0)
>>
>> spherei = vtk.vtkSphereSource()
>> spherei.SetRadius(8.0)
>>
>> sphereir=vtk.vtkReverseSense()
>> sphereir.SetInputConnection(spherei.GetOutputPort())
>>
>> ext=vtk.vtkAppendPolyData()
>> ext.AddInputConnection(sphere.GetOutputPort())
>> ext.AddInputConnection(sphereir.GetOutputPort())
>>
>> plane = vtk.vtkPlane()
>> plane.SetOrigin([0.0,0.0,0.0])
>> plane.SetNormal([0.0,-1.0,0.0])
>>
>> clipclose=vtk.vtkClipClosedSurface()
>> pc = vtk.vtkPlaneCollection()
>> pc.AddItem(plane)
>> clipclose.SetClippingPlanes(pc)
>> clipclose.SetInputConnection(ext.GetOutputPort())
>>
>> cyl = vtk.vtkCylinder()
>> cyl.SetCenter(0,0,0)
>> cyl.SetRadius(5.0)
>> ib = vtk.vtkImplicitBoolean()
>> ib.AddFunction(cyl)
>>
>> clipper = vtk.vtkClipPolyData()
>> clipper.SetInputConnection(clipclose.GetOutputPort())
>> clipper.SetClipFunction(ib)
>> clipper.GenerateClippedOutputOn()
>>
>> mapper = vtk.vtkPolyDataMapper()
>> mapper.SetInputConnection(clipper.GetOutputPort())
>>
>> actor = vtk.vtkActor()
>> actor.SetMapper(mapper)
>>
>> ren = vtk.vtkRenderer()
>> ren.AddActor(actor)
>> ren.ResetCamera()
>>
>> renWin = vtk.vtkRenderWindow()
>> renWin.AddRenderer(ren)
>> iren = vtk.vtkRenderWindowInteractor()
>> iren.SetRenderWindow(renWin)
>>
>> iren.Initialize()
>> renWin.Render()
>> iren.Start()
>>
>>
>>
>>
>>
>>
>> --
>> Sent from: http://vtk.1045678.n5.nabble.com/VTK-Users-f1224199.html
>> _______________________________________________
>> Powered by www.kitware.com
>>
>> Visit other Kitware open-source projects at
>> http://www.kitware.com/opensource/opensource.html
>>
>> Please keep messages on-topic and check the VTK FAQ at:
>> http://www.vtk.org/Wiki/VTK_FAQ
>>
>> Search the list archives at: http://markmail.org/search/?q=vtkusers
>>
>> Follow this link to subscribe/unsubscribe:
>> https://vtk.org/mailman/listinfo/vtkusers
>>
>
>
>
> --
___________________________________________
Andrew J. P. Maclean

___________________________________________
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://vtk.org/pipermail/vtkusers/attachments/20190416/bcabb965/attachment.html>


More information about the vtkusers mailing list