[vtkusers] How to “soften” the edges of a polyline?

Dov Grobgeld dov.grobgeld at gmail.com
Sun May 11 15:15:13 EDT 2014


The following is code that I wrote some years ago that takes a 3D path and
smoothes the corners by a requested smoothing radius. It may be used as a
preprocessor before calling a tube filter. It is in python and uses the
euclid package, but it should be easy to translate to whatever language and
system you need:

def smooth_corner_points(points,
                         smooth_dist = 2,
                         num_corner_smooth_points = 5):
    """Smooth corners of the input path in points by adding additional
   points corresponding to connecting the pair of points at a
respective    distance of smooth_dist from a corner by a circular arc
with    num_corner_smooth_points extra points.    """
    if num_corner_smooth_points < 2:
        return points

    # This section takes a list of points and calculates a new list
    # of points that have new nodes added to make corners "smoother".
    points_smooth = []
    prev_vdir = None
    for i in range(len(points)):
        p = points[i]

        if i < len(points)-1:
            vdir = points[i+1]-points[i]

        if not prev_vdir is None:
            circle_vdir =
(0.5*(prev_vdir.normalized()+vdir.normalized())).normalized()
        else:
            circle_vdir = vdir.normalized()

        # Calculate smoothing for interior points
        if i>0 and i < len(points)-1:
            sd = smooth_dist
            if sd > prev_vdir.magnitude()/3:
                sd = prev_vdir.magnitude()/3
            if sd > vdir.magnitude()/3:
                sd = vdir.magnitude()/3#            print "sd = ", sd
            s1 = p - sd*prev_vdir.normalized()
            s2 = p + sd*vdir.normalized()

            # Calc the smooth point.
            theta = math.pi - math.acos(prev_vdir.normalized()
                                        .dot(vdir.normalized()))#
      print "theta = ", theta*rad2deg
            smooth_radius = sd * math.tan(theta/2)
            vperp = prev_vdir.cross(vdir).normalized()
            vplane = vperp.cross(prev_vdir).normalized()
            sr = s1 + vplane * smooth_radius

            # Replace p with several smooth points
            beta = 2.0 * (math.pi/2-theta/2)
            for j in range(num_corner_smooth_points):
                # Angle to rotate around
                b = beta/(num_corner_smooth_points-1)*j
                pp = sr + smooth_radius * (-math.cos(b) * vplane
                                           + math.sin(b) *
prev_vdir.normalized())
                points_smooth += [pp]
        else:
            points_smooth += [p]
        prev_vdir = vdir

    return points_smooth




On Sun, May 11, 2014 at 5:35 PM, Bill Lorensen <bill.lorensen at gmail.com>wrote:

> You could try the vtkImplicitModeller. The resulting line resolution
> will depend on the sample size. 'll see if I can put together an
> example in the next day or two…
>
>
> On Sat, May 10, 2014 at 7:39 PM, Ahmet Doğan <isimtic at gmail.com> wrote:
> > That way, I need to write a filter which can cut the edge and put there a
> > arcs correlated with angle.
> >
> > On 10.05.2014 23:16, Bill Lorensen wrote:
> >>
> >> That is the best we can do...
> >>
> >>
> >> On Sat, May 10, 2014 at 4:01 PM, Ahmet Doğan <isimtic at gmail.com> wrote:
> >>>
> >>> Hi Bill,
> >>>
> >>> http://i61.tinypic.com/2ps15dd.jpg
> >>>
> >>> Thank you for answer vtkStripper has closed gap on edge but still
> sharp I
> >>> want to have soft edge like in image.
> >>> By the way after I applied the vtkStripper one has been thicker than
> two
> >>> why?
> >>>
> >>> Kind Regards.
> >>>
> >>>
> >>> On 10.05.2014 22:41, Bill Lorensen wrote:
> >>>>
> >>>> Pass your polydata through vtkStripper before you run the TibeFilter.
> >>>>
> >>>>
> >>>> On Sat, May 10, 2014 at 3:15 PM, Ahmet Doğan <isimtic at gmail.com>
> wrote:
> >>>>>
> >>>>> Hi Dženan,
> >>>>> spline isn't exactly I need. I want to bend polyline edges like in
> >>>>> picture.
> >>>>> Is there any way to do it in vtk.
> >>>>>
> >>>>> http://i57.tinypic.com/t658as.jpg
> >>>>>
> >>>>> Kind Regards.
> >>>>>
> >>>>> On 10.05.2014 00:38, Dženan Zukić wrote:
> >>>>>
> >>>>> I doubt something like that already exists. However you should search
> >>>>> through the hierarchy, with this as the starting point:
> >>>>> http://www.vtk.org/doc/nightly/html/classvtkSplineFilter.html
> >>>>>
> >>>>>
> >>>>> On Fri, May 9, 2014 at 11:08 PM, Ahmet Doğan <isimtic at gmail.com>
> wrote:
> >>>>>>
> >>>>>> Hi Dženan,
> >>>>>> Thank you for answer, i mean exactly smoothing polyline edges.
> >>>>>> For example like here:
> >>>>>>
> >>>>>>
> >>>>>>
> http://stackoverflow.com/questions/10162864/how-to-soften-the-edges-of-a-polyline
> >>>>>> and I dont want to interpolate all lines just edges.
> >>>>>>
> >>>>>>
> >>>>>>
> >>>>>>
> >>>>>> On 09.05.2014 17:12, Dženan Zukić wrote:
> >>>>>>
> >>>>>> Does tubeFilter->CappingOn() do what you want? Otherwise you should
> >>>>>> explain in more detail what you want, because it is not clear to me.
> >>>>>>
> >>>>>>
> >>>>>> On Fri, May 9, 2014 at 11:16 AM, isimtic <isimtic at gmail.com> wrote:
> >>>>>>>
> >>>>>>> Hi everyone,
> >>>>>>>
> >>>>>>> I just want to smooth or interpolate edges not all how can I do
> that
> >>>>>>> in
> >>>>>>> vtk
> >>>>>>> when I work with polyline in polydata. I am gonna use it with tube
> >>>>>>> filter
> >>>>>>> and I dont want edges to open
> >>>>>>>
> >>>>>>> Kind Regards.
> >>>>>>>
> >>>>>>>
> >>>>>>>
> >>>>>>> --
> >>>>>>> View this message in context:
> >>>>>>>
> >>>>>>>
> >>>>>>>
> http://vtk.1045678.n5.nabble.com/How-to-soften-the-edges-of-a-polyline-tp5727045.html
> >>>>>>> Sent from the VTK - Users mailing list archive at Nabble.com.
> >>>>>>> _______________________________________________
> >>>>>>> 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
> >>>>>>>
> >>>>>>> Follow this link to subscribe/unsubscribe:
> >>>>>>> http://www.vtk.org/mailman/listinfo/vtkusers
> >>>>>>
> >>>>>>
> >>>>>>
> >>>>>
> >>>>> _______________________________________________
> >>>>> 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
> >>>>>
> >>>>> Follow this link to subscribe/unsubscribe:
> >>>>> http://www.vtk.org/mailman/listinfo/vtkusers
> >>>>>
> >>>>
> >>
> >>
> >
>
>
>
> --
> Unpaid intern in BillsBasement at noware dot com
> _______________________________________________
> 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
>
> Follow this link to subscribe/unsubscribe:
> http://www.vtk.org/mailman/listinfo/vtkusers
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20140511/35b0a1a7/attachment.html>


More information about the vtkusers mailing list