detectItems.py 6.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
"""
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
Josef Brandt's avatar
Josef Brandt committed
22
from typing import TYPE_CHECKING
23 24
from ...analysis.particleEditor import ParticleContextMenu
from ...analysis.particleCharacterization import FTIRAperture
Josef Brandt's avatar
Josef Brandt committed
25 26
if TYPE_CHECKING:
    from ...sampleview import SampleView
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


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):
Josef Brandt's avatar
Josef Brandt committed
54
    def __init__(self, viewparent: 'SampleView', contourData, pos: tuple =(0, 0), particle_uid: int = 0):
55
        super().__init__()
Josef Brandt's avatar
Josef Brandt committed
56 57
        self.viewparent: 'SampleView' = viewparent
        self.particleUID: int = particle_uid
58 59 60
        self.setZValue(1)
        self.setPos(pos[0], pos[1])
        self.setFlag(QtWidgets.QGraphicsItem.ItemIsSelectable)
Josef Brandt's avatar
Josef Brandt committed
61
        self.contextMenu = None
62 63 64 65 66
        self.brect = QtCore.QRectF(0, 0, 1, 1)

        self.contourData = contourData
        self.polygon = None

67 68
        self.hidden: bool = False
        self.transparent: bool = False
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
        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

Josef Brandt's avatar
Josef Brandt committed
96 97 98 99 100 101 102 103 104
    def setIndex(self, index):
        self.particleIndex = index

    def setColor(self, color: QtGui.QColor):
        """
        sets color of contour
        :param QtGui.QColor color:
        :return:
        """
105 106
        self.color = color

Josef Brandt's avatar
Josef Brandt committed
107 108 109 110 111 112 113
    def getColor(self):
        """
        returns color of contour
        :return QtGui.QColor:
        """
        return self.color

114
    def setHidden(self, hidden):
Josef Brandt's avatar
Josef Brandt committed
115 116 117
        """
        sets hidden state of contour
        """
118 119
        self.hidden = hidden

Josef Brandt's avatar
Josef Brandt committed
120 121 122 123 124 125 126
    def getHidden(self):
        """
        returns hidden state of contour
        :return bool:
        """
        return self.hidden

127 128 129 130 131 132 133 134 135 136
    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),
137
                                            int(self.color.blue() * 0.7)))
138
                if not self.hidden:
139
                    alpha = 128 if self.transparent else 255
140
                    painter.setBrush(QtGui.QColor(self.color.red(), self.color.green(), self.color.blue(), alpha))
141 142 143 144 145

            painter.drawPolygon(self.polygon)

    def contextMenuEvent(self, event):
        if self.isSelected:
Josef Brandt's avatar
Josef Brandt committed
146
            self.contextMenu = ParticleContextMenu(self.viewparent)
147 148 149 150 151 152 153 154
            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
155 156
        self.hidden: bool = False
        self.transparent: bool = False
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
        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):
186 187 188 189 190 191 192 193
        if not self.hidden:
            color: QtGui.QColor = QtCore.Qt.green
            if self.transparent:
                color.setAlpha(128)
            painter.setPen(color)
            painter.setBrush(color)
            painter.setOpacity(0.8)
            painter.drawPolygon(self.polygon)
194 195 196