The Tidy Up Paths function is useful for removing extra points created by auto tracing. But it also converts points to lots of little curves, which are hard to work with and cause slow performance. It would be nice if there was a Reduce Points command that displayed the point count and had a slider to control a threshold value that determines how many points get removed.
This sounds great, but how do you determine which points to remove and which to keep?
I have built a function that tries to get rid of points close to each other, and it works OK for a very specific purpose, but not necessarily for what someone else has in mind.
def cleanUpPath( self, thisPath, threshold=12.0 ): try: pathlength = len(thisPath) for i in range(pathlength)[::-1]: n = thisPath.nodes[i] previousNode = None if i > 0: previousNode = thisPath.nodes[i-1] if previousNode.type == 65 and i > 2: previousNode = thisPath.nodes[i-3] nextNode = None if i < pathlength: nextNode = thisPath.nodes[i+1] if nextNode.type == 65 and i < pathlength-2: nextNode = thisPath.nodes[(i+3)%pathlength] if nextNode and previousNode: nextDistance = self.distance(nextNode.position, n.position) previousDistance = self.distance(previousNode.position, n.position) nextDistanceIsSmall = previousDistance < threshold previousDistanceIsSmall = nextDistance < threshold previousAngle = self.angle(n.position, previousNode.position) nextAngle = self.angle(nextNode.position, n.position) angleDifferenceIsSmall = previousAngle - nextAngle < 6.0 if (nextDistanceIsSmall and previousDistanceIsSmall) or ((nextDistanceIsSmall or previousDistanceIsSmall) and angleDifferenceIsSmall): thisPath.removeNodeCheckKeepShape_( n ) except Exception as e: self.logToConsole( "cleanUpPath: %s" % str(e) )
I’m working on this. There are algorithms for simplifying paths, by taking a sample of the curve at regular points and then interpolating the curve through the sample. See http://paperjs.org/examples/path-simplification/
I played around with this kind of algorithm (for a different thing). We need a slightly different behavior as we need to favor extreme points. And it should keep segments that can’t be simplified exactly the same.