def setSegmentStartTangent(segment, tangent): if len(segment.points) == 2: ''' Convert straight segment to 4-point cubic bezier. ''' p0, p3 = segment.points p2 = p0.midpoint(p3) p1 = p0.plus(tangent.scale(p0.distanceTo(p3) * 0.5)) result = TFSSegment(p0, p1, p2, p3) elif len(segment.points) == 3: ''' Update 3-point cubic bezier. ''' p0, p1, p2 = segment.points p1 = TFSIntersection.getIntersectPoint(p0, p0.plus(tangent), p1, p2) result = TFSSegment(p0, p1, p2) elif len(segment.points) == 4: ''' Update 4-point cubic bezier. ''' p0, p1, p2, p3 = segment.points p1 = p0.plus(tangent.scale(p0.distanceTo(p1))) result = TFSSegment(p0, p1, p2, p3) else: raise Exception('Invalid segment: ' + segment.description()) # print 'updated segment:', segment.description(), 'to:', result.description() return result
def updateSegment(segment): startPoint = segment.startPoint() # startOffset = TFSPoint0() if segment.startPoint() in selectedPoints: # startOffset = pointAverage.minus(startPoint) startPoint = pointAverage endPoint = segment.endPoint() # endOffset = TFSPoint0() if segment.endPoint() in selectedPoints: # endOffset = pointAverage.minus(endPoint) endPoint = pointAverage if startPoint == endPoint: return None newPoints = [startPoint,] if len(segment.points) == 2: pass elif len(segment.points) == 3: if updateControlPoints: # Preserve the control angles. controlPoint0 = TFSIntersection.getIntersectPoint(startPoint, startPoint.plus(segment.startTangent()), endPoint, endPoint.plus(segment.endTangent())) else: controlPoint0 = segment.points[1] newPoints += [controlPoint0,] # offset = startOffset.blend(endOffset) # controlPoint0 = segment.points[1].plus(offset) # newPoints += [controlPoint0,] elif len(segment.points) == 4: if updateControlPoints: factor = startPoint.distanceTo(endPoint) / segment.endPointDistance() controlPoint0 = startPoint.plus(segment.startTangent().scale(segment.startVector().length() * factor)) controlPoint1 = endPoint.plus(segment.endTangent().scale(segment.endVector().length() * -factor)) else: controlPoint0 = segment.points[1] controlPoint1 = segment.points[2] newPoints += [controlPoint0, controlPoint1,] # controlPoint0 = segment.points[1].plus(startOffset) # controlPoint1 = segment.points[2].plus(endOffset) # newPoints += [controlPoint0, controlPoint1,] else: raise Exception('Invalid segment: ' + segment.description()) newPoints += [endPoint,] return TFSSegment(*newPoints)