コード例 #1
0
    def process_cubic(self, offset_path, blade_path, params, quality):
        """ Add offset correction to a cubic bezier.
        """
        r = self.config.offset
        p0 = blade_path.currentPosition()
        p1, p2, p3 = params
        self.add_continuity_correction(offset_path, blade_path, p1)

        curve = QPainterPath()
        curve.moveTo(p0)
        curve.cubicTo(*params)
        p = QPainterPath()
        p.moveTo(p0)

        if quality == 1:
            polygon = curve.toSubpathPolygons(IDENITY_MATRIX)[0]
        else:
            m = QTransform.fromScale(quality, quality)
            m_inv = QTransform.fromScale(1/quality, 1/quality)
            polygon = m_inv.map(curve.toSubpathPolygons(m)[0])

        for point in polygon:
            p.lineTo(point)
            t = curve.percentAtLength(p.length())
            angle = curve.angleAtPercent(t)
            a = radians(angle)
            dx, dy = r*cos(a), -r*sin(a)
            offset_path.lineTo(point.x()+dx, point.y()+dy)

        blade_path.cubicTo(*params)
コード例 #2
0
    def add_continuity_correction(self, offset_path, blade_path, point):
        """ Adds if the upcoming angle and previous angle are not the same
        we need to correct for that difference by "arcing back" about the
        current blade point with a radius equal to the offset.

        """
        # Current blade position
        cur = blade_path.currentPosition()

        # Determine direction of next move
        sp = QPainterPath()
        sp.moveTo(cur)
        sp.lineTo(point)
        next_angle = sp.angleAtPercent(1)

        # Direction of last move
        angle = blade_path.angleAtPercent(1)

        # If not continuous it needs corrected with an arc
        if isnan(angle) or isnan(next_angle):
            return
        if abs(angle - next_angle) > self.config.cutoff:
            r = self.config.offset
            a = radians(next_angle)
            dx, dy = r*cos(a), -r*sin(a)
            po = QPointF(cur.x()+dx, cur.y()+dy)

            c = offset_path.currentPosition()
            dx, dy = po.x()-cur.x()+c.x()-cur.x(), po.y()-cur.y()+c.y()-cur.y()
            c1 = QPointF(cur.x()+dx, cur.y()+dy)
            offset_path.quadTo(c1, po)
コード例 #3
0
ファイル: models.py プロジェクト: yangbiaocn/inkcut
    def move_path(self):
        """ Returns the path the head moves when not cutting

        """
        # Compute the negative
        path = QPainterPath()
        for i in range(self.model.elementCount()):
            e = self.model.elementAt(i)
            if e.isMoveTo():
                path.lineTo(e.x, e.y)
            else:
                path.moveTo(e.x, e.y)
        return path
コード例 #4
0
 def apply_blade_offset(self, poly, offset):
     """ Apply blade offset to the given polygon by appending a quadratic
     bezier to each point .
     
     """
     # Use a QPainterPath to track the distance in c++
     path = QPainterPath()
     cutoff = cos(radians(self.config.cutoff)) # Forget 
     last = None
     n = len(poly)
     for i, p in enumerate(poly):
         if i == 0:
             path.moveTo(p)
             last_path = QPainterPath()
             last_path.moveTo(p)
             last = p
             continue
         
         # Move to the point
         path.lineTo(p)
         
         if i+1 == n:
             # Done
             break
         
         # Get next point
         next = poly.at(i+1)
         
         # Make our paths
         last_path.lineTo(p)
         next_path = QPainterPath()
         next_path.moveTo(p)
         next_path.lineTo(next)
         
         # Get angle between the two components
         u, v = QVector2D(last-p), QVector2D(next-p)
         cos_theta = QVector2D.dotProduct(u.normalized(), v.normalized())
         
         # If the angle is large enough to need compensation
         if (cos_theta < cutoff and
                 last_path.length() > offset and
                 next_path.length() > offset):
             # Calculate the extended point
             t = last_path.percentAtLength(offset)
             c1 = p+(last_path.pointAtPercent(t)-last)
             c2 = p
             t = next_path.percentAtLength(offset)
             ep = next_path.pointAtPercent(t)
             if offset > 2:
                 # Can smooth it for larger offsets
                 path.cubicTo(c1, c2, ep)
             else:
                 # This works for small offsets < 0.5 mm 
                 path.lineTo(c1)
                 path.lineTo(ep)
         
         # Update last
         last_path = next_path
         last = p
     return path.toSubpathPolygons(IDENITY_MATRIX)
コード例 #5
0
def split_painter_path(path):
    """ Split a QPainterPath into subpaths. """
    if not isinstance(path, QPainterPath):
        raise TypeError("path must be a QPainterPath, got: {}".format(path))

    # Element types
    MoveToElement = QPainterPath.MoveToElement
    LineToElement = QPainterPath.LineToElement
    CurveToElement = QPainterPath.CurveToElement
    CurveToDataElement = QPainterPath.CurveToDataElement

    subpaths = []
    params = []
    e = None

    def finish_curve(p, params):
        if len(params) == 2:
            p.quadTo(*params)
        elif len(params) == 3:
            p.cubicTo(*params)
        else:
            raise ValueError("Invalid curve parameters: {}".format(params))

    for i in range(path.elementCount()):
        e = path.elementAt(i)

        # Finish the previous curve (if there was one)
        if params and e.type != CurveToDataElement:
            finish_curve(p, params)
            params = []

        # Reconstruct the path
        if e.type == MoveToElement:
            p = QPainterPath()
            p.moveTo(e.x, e.y)
            subpaths.append(p)
        elif e.type == LineToElement:
            p.lineTo(e.x, e.y)
        elif e.type == CurveToElement:
            params = [QPointF(e.x, e.y)]
        elif e.type == CurveToDataElement:
            params.append(QPointF(e.x, e.y))

    # Finish the previous curve (if there was one)
    if params and e and e.type != CurveToDataElement:
        finish_curve(p, params)
    return subpaths
コード例 #6
0
ファイル: utils.py プロジェクト: frmdstryr/Inkcut
def split_painter_path(path):
    """ Split a QPainterPath into subpaths. """
    if not isinstance(path, QPainterPath):
        raise TypeError("path must be a QPainterPath, got: {}".format(path))

    # Element types
    MoveToElement = QPainterPath.MoveToElement
    LineToElement = QPainterPath.LineToElement
    CurveToElement = QPainterPath.CurveToElement
    CurveToDataElement = QPainterPath.CurveToDataElement

    subpaths = []
    params = []
    e = None

    def finish_curve(p, params):
        if len(params) == 2:
            p.quadTo(*params)
        elif len(params) == 3:
            p.cubicTo(*params)
        else:
            raise ValueError("Invalid curve parameters: {}".format(params))

    for i in range(path.elementCount()):
        e = path.elementAt(i)

        # Finish the previous curve (if there was one)
        if params and e.type != CurveToDataElement:
            finish_curve(p, params)
            params = []

        # Reconstruct the path 
        if e.type == MoveToElement:
            p = QPainterPath()
            p.moveTo(e.x, e.y)
            subpaths.append(p)
        elif e.type == LineToElement:
            p.lineTo(e.x, e.y)
        elif e.type == CurveToElement:
            params = [QPointF(e.x, e.y)]
        elif e.type == CurveToDataElement:
            params.append(QPointF(e.x, e.y))

    # Finish the previous curve (if there was one)
    if params and e and e.type != CurveToDataElement:
        finish_curve(p, params)
    return subpaths
コード例 #7
0
 def splitAtPercent(self, t):
     paths = []
     path = QPainterPath()
     i = 0
     while i < self.elementCount():
         e = self.elementAt(i)
         if e.type == ElementType.MoveToElement:
             if not path.isEmpty():
                 paths.append(path)
             path = QPainterPath(QPointF(e.x, e.y))
         elif e.type == ElementType.LineToElement:
             path.lineTo(QPointF(e.x, e.y))
         elif e.type == ElementType.CurveToElement:
             e1, e2 = self.elementAt(i + 1), self.elementAt(i + 2)
             path.cubicTo(QPointF(e.x, e.y), QPointF(e1.x, e1.y),
                          QPointF(e2.x, e2.y))
             i += 2
         else:
             raise ValueError("Invalid element type %s" % (e.type, ))
         i += 1
     if not path.isEmpty():
         paths.append(path)
     return paths
コード例 #8
0
    def request_relayout(self):
        # y = 0.0

        # for child in self.children():
        #     if not isinstance(child, QtContainer):
        #         continue
        #     scene_proxy = self._proxies[child]
        #     width, height = child._layout_manager.best_size()
        #     scene_proxy.setPos(0.0, y)
        #     y += height + 25.0

        # Remove all paths
        for p in self._edge_paths:
            self.scene.removeItem(p)
        self._edge_paths = []

        children_names = {child.declaration.name for child in self.children() if isinstance(child, QtContainer)}

        if self.declaration.edges and \
                any(from_ not in children_names or to not in children_names for (from_, to) in self.declaration.edges):
            # hasn't finished being set up yet
            return

        for child in self.children():
            if not isinstance(child, QtContainer):
                continue
            scene_proxy = self._proxy(child)
            width, height = child._layout_manager.best_size()
            scene_proxy.setGeometry(QRectF(0.0, 0.0, width, height))

        node_coords, edge_coords = self._layout_nodes_and_edges(self.declaration.func_addr)

        if not node_coords:
            return

        for child in self.children():
            if not isinstance(child, QtContainer):
                continue
            scene_proxy = self._proxies[child]
            # width, height = child._layout_manager.best_size()
            x, y = node_coords[child.declaration.addr]
            scene_proxy.setPos(x, y)

        for edges in edge_coords:
            for from_, to_ in zip(edges, edges[1:]):
                painter = QPainterPath(QPointF(*from_))
                painter.lineTo(QPointF(*to_))
                p = self.scene.addPath(painter)
                self._edge_paths.append(p)

        rect = self.scene.itemsBoundingRect()
        # Enlarge the rect so there is enough room at right and bottom
        rect.setX(rect.x() - self.LEFT_PADDING)
        rect.setY(rect.y() - self.TOP_PADDING)
        rect.setWidth(rect.width() + 2 * self.LEFT_PADDING)
        rect.setHeight(rect.height() + 2 * self.TOP_PADDING)

        self.scene.setSceneRect(rect)
        self.widget.viewport().update()

        self.show_selected()