detectItems.py 5.74 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
"""
GEPARD - Gepard-Enabled PARticle Detection
Copyright (C) 2018  Lars Bittrich and Josef Brandt, Leibniz-Institut für
Polymerforschung Dresden e. V. <bittrich-lars@ipfdd.de>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program, see COPYING.
If not, see <https://www.gnu.org/licenses/>.
"""
from PyQt5 import QtWidgets, QtCore, QtGui
from copy import deepcopy
from ...analysis.particleEditor import ParticleContextMenu
from ...analysis.particleCharacterization import FTIRAperture


class SeedPoint(QtWidgets.QGraphicsItem):
    def __init__(self, radius, seedtype, pos=(0, 0)):
        super().__init__()
        self.setPos(pos[0], pos[1])
        self.seedtype = seedtype
        self.radius = radius

    def boundingRect(self):
        return QtCore.QRectF(-self.radius - 1, -self.radius - 1,
                             2*self.radius + 2, 2*self.radius + 2)

    def paint(self, painter, option, widget):
        if self.seedtype == 'add':
            painter.setPen(QtCore.Qt.white)
            painter.setBrush(QtCore.Qt.white)
        elif self.seedtype == 'remove':
            painter.setPen(QtCore.Qt.magenta)
            painter.setBrush(QtCore.Qt.magenta)

        rect = QtCore.QRectF(-self.radius, -self.radius,
                             2*self.radius, 2*self.radius)
        painter.drawEllipse(rect)


class SegmentationContour(QtWidgets.QGraphicsItem):
    def __init__(self, viewparent, contourData, pos=(0, 0)):
        super().__init__()
        self.viewparent = viewparent
        self.setZValue(1)
        self.setPos(pos[0], pos[1])
        self.setFlag(QtWidgets.QGraphicsItem.ItemIsSelectable)
        self.brect = QtCore.QRectF(0, 0, 1, 1)

        self.contourData = contourData
        self.polygon = None

        self.hidden = False
        self.color = QtGui.QColor(50, 255, 50, 200)
        self.particleIndex = None
        self.isSelected = False
        self.getBrectAndPolygon()

    def getBrectAndPolygon(self):
        """
        Calculates the bounding rect (needed for drawing the QGraphicsView) and converts the contourdata to a polygon.
        :return:
        """
        polygon = QtGui.QPolygonF()
        x0 = self.contourData[:, 0, 0].min()
        x1 = self.contourData[:, 0, 0].max()
        y0 = self.contourData[:, 0, 1].min()
        y1 = self.contourData[:, 0, 1].max()
        for point in self.contourData:
            polygon.append(QtCore.QPointF(point[0, 0], point[0, 1]))

        self.brect.setCoords(x0, y0, x1, y1)
        self.polygon = polygon

    def boundingRect(self):
        return self.brect

    def setIndex(self, index):
        self.particleIndex = index

    def setColor(self, color):
        self.color = color

    def setHidden(self, hidden):
        self.hidden = hidden

    def paint(self, painter, option, widget):
        if self.polygon is not None:
            painter.setPen(QtCore.Qt.green)
            if self.isSelected:
                alpha = 200
                if not self.hidden:
                    painter.setBrush(QtGui.QColor(200, 200, 200, alpha))
                painter.setPen(QtCore.Qt.white)
            else:
                painter.setPen(QtGui.QColor(int(self.color.red() * 0.7), int(self.color.green() * 0.7),
                                            int(self.color.blue() * 0.7), self.color.alpha()))
                if not self.hidden:
                    painter.setBrush(self.color)

            painter.drawPolygon(self.polygon)
            self.viewparent.update()

    def contextMenuEvent(self, event):
        if self.isSelected:
            self.contextMenu = ParticleContextMenu(self.viewparent)
            self.viewparent.particleEditor.connectToSignals(self.contextMenu)
            self.contextMenu.executeAtScreenPos(event.screenPos())


class FTIRApertureIndicator(QtWidgets.QGraphicsItem):
    def __init__(self, aperture: FTIRAperture):
        super(FTIRApertureIndicator, self).__init__()
        self.aperture = aperture
        self.setZValue(2)
        self.setPos(aperture.centerX, aperture.centerY)
        self.polygon: QtGui.QPolygonF = QtGui.QPolygonF()
        self.getBrectAndPolygon()

    def getBrectAndPolygon(self) -> None:
        """
        Calculates the bounding rect (needed for drawing the QGraphicsView) and converts the aperture to a polygon.
        :return:
        """
        meanX: float = self.aperture.width / 2
        meanY: float = self.aperture.height / 2
        points: np.ndarray = np.array([[-meanX, meanY],
                                       [meanX, meanY],
                                       [meanX, -meanY],
                                       [-meanX, -meanY]])

        angleRad: np.ndarray = np.deg2rad(self.aperture.angle)
        sin, cos = np.sin(angleRad), np.cos(angleRad)
        for i in range(points.shape[0]):
            point: np.ndarray = deepcopy(points[i, :])
            points[i, 0] = point[0] * cos - point[1] * sin
            points[i, 1] = point[0] * sin + point[1] * cos
            self.polygon.append(QtCore.QPointF(points[i, 0], points[i, 1]))

    def boundingRect(self) -> QtCore.QRectF:
        return self.polygon.boundingRect()

    def paint(self, painter, option, widget):
        painter.setPen(QtCore.Qt.green)
        painter.setBrush(QtCore.Qt.green)
        painter.setOpacity(0.6)
        painter.drawPolygon(self.polygon)