Git Product home page Git Product logo

Comments (5)

sebacastroh avatar sebacastroh commented on July 17, 2024

I had the same problem, so I translated the code in the previous link to Python. You are free to use it.

from math import sqrt
import numpy as np

def _DouglasPeucker(points, startIndex, lastIndex, epsilon):
    stk = []
    stk.append([startIndex, lastIndex])
    globalStartIndex = startIndex
    bitArray = np.ones(lastIndex-startIndex+1, dtype=bool)

    while len(stk) > 0:
        startIndex = stk[-1][0]
        lastIndex = stk[-1][1]
        stk.pop()

        dmax = 0.
        index = startIndex

        for i in xrange(index+1, lastIndex):
            if bitArray[i - globalStartIndex]:
                d = PointLineDistance(points[i], points[startIndex], points[lastIndex])
                if d > dmax:
                    index = i
                    dmax = d
        if dmax > epsilon:
            stk.append([startIndex, index])
            stk.append([index, lastIndex])
        else:
            for i in xrange(startIndex + 1, lastIndex):
                bitArray[i - globalStartIndex] = False
    return bitArray

def DouglasPeucker(points, epsilon):
    bitArray = _DouglasPeucker(points, 0, len(points)-1, epsilon)
    resList = []
    for i in xrange(len(points)):
        if bitArray[i]:
            resList.append(points[i])
    return np.array(resList)

def PointLineDistance(point, start, end):
    if np.all(np.equal(start, end)) :
        return np.linalg.norm(point, start)
    n = abs((end[0] - start[0]) * (start[1] - point[1]) - (start[0] - point[0]) * (end[1] - start[1]))
    d = sqrt((end[0] - start[0]) * (end[0] - start[0]) + (end[1] - start[1]) * (end[1] - start[1]))
    return n/d

from rdp.

FlorianWilhelm avatar FlorianWilhelm commented on July 17, 2024

@sebacastroh Wow, thanks, I just wanted to start with it but it's definitely better this way 👍
@fhirschmann Do you need help with integrating that code? Since your package seems to be the most popular Python package for RDP I guess it's good to include it. Maybe even someone wants to add a Cython version based on that code later on.

from rdp.

fhirschmann avatar fhirschmann commented on July 17, 2024

Sorry for the long delay!

I agree that without tail call optimization in Python, an iterative version is better suited.

I will start integrating the code of @sebacastroh and also quickcheck it against the recursive version.

@Namek, are you fine with integrating the above code which is based on your code to the Python rdp library?

from rdp.

Namek avatar Namek commented on July 17, 2024

@Namek, are you fine with integrating the above code which is based on your code to the Python rdp library?

@fhirschmann Sure! It's open source and has no specific license. If it's properly done and works for you then just take it.

from rdp.

fhirschmann avatar fhirschmann commented on July 17, 2024

Iterative version added in v0.7. Thanks!

from rdp.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.