Exemple #1
0
    def addClippedPoly(self, polygon, **attr):
        """fe.addClippedPoly(polygon, ...)

        Adds and returns exactly fig.Polygon objects for each part of
        the given polygon which is in the clipping range.  Again, note
        the possibility of setting properties (depth, penColor,
        lineStyle, lineWidth, ...) on all resulting objects via
        keyword arguments (cf. documentation of the FigExporter
        class).

        If simplifyEpsilon (default: 0.5) is not None, simplifyPolygon
        is called on the *scaled* polygon (i.e. the default is to
        simplify the polygon to 0.5 fig units, which are integer
        anyways)."""

        if "fillColor" in attr and not "fillStyle" in attr:
            attr["fillStyle"] = fig.FillStyle.Solid

        # no ROI to clip to?
        if not self.roi:
            return [self.addEdge(polygon, **attr)]

        if type(polygon) != Polygon:
            if not isinstance(polygon, list):
                polygon = Polygon(list(polygon))
            else:
                polygon = Polygon(polygon)

        clipRect = BoundingBox(self.roi)
        o = self.offset + attr.get('offset', (0, 0))
        clipRect.moveBy(-o)

        # handle all-or-none cases:
        if not clipRect.intersects(polygon.boundingBox()):
            return []
        if clipRect.contains(polygon.boundingBox()):
            return [self.addEdge(polygon, **attr)]

        # general case: perform clipping, add parts one-by-one:
        result = []  # fig.Compound(container) - I dont't dare grouping here..
        closeAtBorder = (attr.get("fillStyle", fig.FillStyle.None) !=
                         fig.FillStyle.None)
        for part in clipPoly(polygon, clipRect, closeAtBorder):
            if part.length():  # don't add zero-length polygons
                result.append(self.addEdge(part, **attr))
        return result
Exemple #2
0
    def addClippedPoly(self, polygon, **attr):
        """fe.addClippedPoly(polygon, ...)

        Adds and returns exactly fig.Polygon objects for each part of
        the given polygon which is in the clipping range.  Again, note
        the possibility of setting properties (depth, penColor,
        lineStyle, lineWidth, ...) on all resulting objects via
        keyword arguments (cf. documentation of the FigExporter
        class).

        If simplifyEpsilon (default: 0.5) is not None, simplifyPolygon
        is called on the *scaled* polygon (i.e. the default is to
        simplify the polygon to 0.5 fig units, which are integer
        anyways)."""

        if "fillColor" in attr and not "fillStyle" in attr:
            attr["fillStyle"] = fig.FillStyle.Solid

        # no ROI to clip to?
        if not self.roi:
            return [self.addEdge(polygon, **attr)]

        if type(polygon) != Polygon:
            if not isinstance(polygon, list):
                polygon = Polygon(list(polygon))
            else:
                polygon = Polygon(polygon)

        clipRect = BoundingBox(self.roi)
        o = self.offset + attr.get('offset', (0,0))
        clipRect.moveBy(-o)

        # handle all-or-none cases:
        if not clipRect.intersects(polygon.boundingBox()):
            return []
        if clipRect.contains(polygon.boundingBox()):
            return [self.addEdge(polygon, **attr)]

        # general case: perform clipping, add parts one-by-one:
        result = [] # fig.Compound(container) - I dont't dare grouping here..
        closeAtBorder = (
            attr.get("fillStyle", fig.FillStyle.None) != fig.FillStyle.None)
        for part in clipPoly(polygon, clipRect, closeAtBorder):
            if part.length(): # don't add zero-length polygons
                result.append(self.addEdge(part, **attr))
        return result
Exemple #3
0
    def addROIRect(self, roi=None, container=True, **attr):
        """fe.addROIRect(roi, depth = 85, ...)

        Adds a rectangle around the given roi (ignoring fe.offset).
        If roi == None (default), the roi of the FigExporter itself is used.
        The fig.PolyBox object is returned."""

        assert roi or self.roi, "addROIRect(): no ROI given!?"

        if container == True:
            container = self.f

        if isinstance(roi, Rect2D):
            roi = BoundingBox(roi)
            roi.moveBy((-0.5, -0.5))

        if roi is None:
            roi = self.roi
            assert not isinstance(roi, Rect2D)
            roi = BoundingBox(roi)
            roi.moveBy(-self.offset)  # BAAH!  What a bad design (self.roi)
        elif self.roi and not self.roi.contains(roi):
            sys.stderr.write("WARNING: addROIRect: ROI out of bounds!\n")
            poly = Polygon([
                roi.begin(),
                roi.begin() + (0, roi.size()[1]),
                roi.end(),
                roi.begin() + (roi.size()[0], 0),
                roi.begin()
            ])
            result = self.addClippedPoly(poly, container=container, **attr)
            if result:
                assert len(result) == 1
                return result[0]
            return

        roi = BoundingBox(roi)
        roi.moveBy(self.offset)
        if self.roi:
            roi.moveBy(-self.roi.begin())

        if "fillColor" in attr and not "fillStyle" in attr:
            attr["fillStyle"] = fig.FillStyle.Solid

        result = fig.PolyBox(roi.begin()[0] * self.scale,
                             roi.begin()[1] * self.scale,
                             roi.end()[0] * self.scale,
                             roi.end()[1] * self.scale)
        container.append(result)
        for a in attr:
            setattr(result, a, attr[a])
        return result
Exemple #4
0
    def addROIRect(self, roi = None, container = True, **attr):
        """fe.addROIRect(roi, depth = 85, ...)

        Adds a rectangle around the given roi (ignoring fe.offset).
        If roi == None (default), the roi of the FigExporter itself is used.
        The fig.PolyBox object is returned."""

        assert roi or self.roi, "addROIRect(): no ROI given!?"

        if container == True:
            container = self.f

        if isinstance(roi, Rect2D):
            roi = BoundingBox(roi)
            roi.moveBy((-0.5, -0.5))

        if roi is None:
            roi = self.roi
            assert not isinstance(roi, Rect2D)
            roi = BoundingBox(roi)
            roi.moveBy(-self.offset) # BAAH!  What a bad design (self.roi)
        elif self.roi and not self.roi.contains(roi):
            sys.stderr.write("WARNING: addROIRect: ROI out of bounds!\n")
            poly = Polygon([roi.begin(), roi.begin() + (0, roi.size()[1]),
                            roi.end(), roi.begin() + (roi.size()[0], 0),
                            roi.begin()])
            result = self.addClippedPoly(poly,
                                         container = container,
                                         **attr)
            if result:
                assert len(result) == 1
                return result[0]
            return

        roi = BoundingBox(roi)
        roi.moveBy(self.offset)
        if self.roi:
            roi.moveBy(-self.roi.begin())

        if "fillColor" in attr and not "fillStyle" in attr:
            attr["fillStyle"] = fig.FillStyle.Solid

        result = fig.PolyBox(roi.begin()[0] * self.scale,
                             roi.begin()[1] * self.scale,
                             roi.end()[0] * self.scale,
                             roi.end()[1] * self.scale)
        container.append(result)
        for a in attr:
            setattr(result, a, attr[a])
        return result