def inflateSegmentLeft(segment, hDistance, vDistance): p0 = segment.startPoint() p1 = segment.endPoint() startTangent = segment.startTangent() if len(segment) == 2: offset = scaleVectorHV(startTangent.rotate(math.pi * 0.5), hDistance, vDistance) p0 = p0.plus(offset) p1 = p1.plus(offset) newPoints = (p0, p1) elif len(segment) == 3: endTangent = segment.endTangent() startOffset = scaleVectorHV(startTangent.rotate(math.pi * 0.5), hDistance, vDistance) endOffset = scaleVectorHV(endTangent.rotate(math.pi * 0.5), hDistance, vDistance) p0 = p0.plus(startOffset) p1 = p1.plus(endOffset) cp0 = TFSntersection.intersectionWithTangents(p0, startTangent, p1, endTangent.invert()) newPoints = (p0, cp0, p1) elif len(segment) == 4: endTangent = segment.endTangent() startOffset = scaleVectorHV(startTangent.rotate(math.pi * 0.5), hDistance, vDistance) endOffset = scaleVectorHV(endTangent.rotate(math.pi * 0.5), hDistance, vDistance) oldScale = p0.distanceTo(p1) p0 = p0.plus(startOffset) p1 = p1.plus(endOffset) newScale = p0.distanceTo(p1) cp0 = p0.plus(segment.startVector().scale(newScale / oldScale)) cp1 = p1.minus(segment.endVector().scale(newScale / oldScale)) newPoints = (p0, cp0, cp1, p1) else: raise Exception('Invalid segment') try: return TFSSegment(*newPoints).roundWithDefaultPrecision() except TFSValidationException, e: ''' Inflating a segment can result in an empty or otherwise invalid segment. In fact, this will happen often since we'll be deflating previously inflated rounding curves. That's fine; ignore them. ''' return None
def _flateSegmentLeft(segment, hDistance, vDistance=None): """ """ if vDistance is None: vDistance = hDistance if segment.startVector().length() == 0 or segment.endVector().length() == 0: raise Exception("Cannot flate a segment without valid tangents: " + segment.description()) p0 = segment.startPoint() p1 = segment.endPoint() startTangent = segment.startTangent() if len(segment) == 2: offset = scaleVectorHV(startTangent.rotate(math.pi * 0.5), hDistance, vDistance) p0 = p0.plus(offset) p1 = p1.plus(offset) newPoints = (p0, p1) elif len(segment) == 3: endTangent = segment.endTangent() startOffset = scaleVectorHV(startTangent.rotate(math.pi * 0.5), hDistance, vDistance) endOffset = scaleVectorHV(endTangent.rotate(math.pi * 0.5), hDistance, vDistance) p0 = p0.plus(startOffset) p1 = p1.plus(endOffset) cp0 = TFSntersection.intersectionWithTangents(p0, startTangent, p1, endTangent.invert()) newPoints = (p0, cp0, p1) elif len(segment) == 4: endTangent = segment.endTangent() startOffset = scaleVectorHV(startTangent.rotate(math.pi * 0.5), hDistance, vDistance) endOffset = scaleVectorHV(endTangent.rotate(math.pi * 0.5), hDistance, vDistance) oldScale = p0.distanceTo(p1) p0 = p0.plus(startOffset) p1 = p1.plus(endOffset) newScale = p0.distanceTo(p1) cp0 = p0.plus(segment.startVector().scale(newScale / oldScale)) cp1 = p1.minus(segment.endVector().scale(newScale / oldScale)) newPoints = (p0, cp0, cp1, p1) else: raise Exception("Invalid segment") try: result = TFSSegment(*newPoints).roundWithDefaultPrecision() """ Segments can be turned "inside out" when deflating. For example, deflating an arc by more than its "radius". We want to discard these segments. We can detect them by checking whether the naive endpoint tangent has reversed. """ affinity = result.naiveEndpointTangent().dotProduct(segment.naiveEndpointTangent()) if affinity < 0: return None return result except TFSValidationException, e: """ flating a segment can result in an empty or otherwise invalid segment. In fact, this will happen often since we'll be deflating previously inflated rounding curves. That's fine; ignore them. """ return None
def _flateSegmentLeft(segment, hDistance, vDistance=None): ''' ''' if vDistance is None: vDistance = hDistance if (segment.startVector().length() == 0 or segment.endVector().length() == 0): raise Exception('Cannot flate a segment without valid tangents: ' + segment.description()) p0 = segment.startPoint() p1 = segment.endPoint() startTangent = segment.startTangent() if len(segment) == 2: offset = scaleVectorHV(startTangent.rotate(math.pi * 0.5), hDistance, vDistance) p0 = p0.plus(offset) p1 = p1.plus(offset) newPoints = (p0, p1) elif len(segment) == 3: endTangent = segment.endTangent() startOffset = scaleVectorHV(startTangent.rotate(math.pi * 0.5), hDistance, vDistance) endOffset = scaleVectorHV(endTangent.rotate(math.pi * 0.5), hDistance, vDistance) p0 = p0.plus(startOffset) p1 = p1.plus(endOffset) cp0 = TFSntersection.intersectionWithTangents(p0, startTangent, p1, endTangent.invert()) newPoints = (p0, cp0, p1) elif len(segment) == 4: endTangent = segment.endTangent() startOffset = scaleVectorHV(startTangent.rotate(math.pi * 0.5), hDistance, vDistance) endOffset = scaleVectorHV(endTangent.rotate(math.pi * 0.5), hDistance, vDistance) oldScale = p0.distanceTo(p1) p0 = p0.plus(startOffset) p1 = p1.plus(endOffset) newScale = p0.distanceTo(p1) cp0 = p0.plus(segment.startVector().scale(newScale / oldScale)) cp1 = p1.minus(segment.endVector().scale(newScale / oldScale)) newPoints = (p0, cp0, cp1, p1) else: raise Exception('Invalid segment') try: result = TFSSegment(*newPoints).roundWithDefaultPrecision() ''' Segments can be turned "inside out" when deflating. For example, deflating an arc by more than its "radius". We want to discard these segments. We can detect them by checking whether the naive endpoint tangent has reversed. ''' affinity = result.naiveEndpointTangent().dotProduct( segment.naiveEndpointTangent()) if affinity < 0: return None return result except TFSValidationException as e: ''' flating a segment can result in an empty or otherwise invalid segment. In fact, this will happen often since we'll be deflating previously inflated rounding curves. That's fine; ignore them. ''' return None