helpers.py 1.59 KB
Newer Older
1 2 3 4 5 6 7
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jan 28 19:32:50 2020

@author: luna
"""
Josef Brandt's avatar
Josef Brandt committed
8 9 10
from PyQt5 import QtCore, QtGui
import numpy as np

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

class ParticleBinSorter(object):
    def __init__(self):
        super(ParticleBinSorter, self).__init__()
        self.bins = [5, 10, 20, 50, 100, 200, 500]
        
    def sort_particles_into_bins(self, particleList):
        particlesInBins = self._get_empty_bins()
        
        for particle in particleList:
            binIndex = self._get_binIndex_of_particle(particle)
            particlesInBins[binIndex].append(particle)
        return particlesInBins
    
    def _get_empty_bins(self):
Josef Brandt's avatar
Josef Brandt committed
26
        return [[] for _ in range(len(self.bins)+1)]
27 28 29 30 31 32 33 34 35
    
    def _get_binIndex_of_particle(self, particle):
        size = particle.getParticleSize()
        binIndex = 0
        for upperLimit in self.bins:
            if size <= upperLimit:
                break
            else:
                binIndex += 1
Josef Brandt's avatar
Josef Brandt committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
        return binIndex


def box_contains_contour(boxXY: tuple, boxWidthHeight: tuple, contour: np.array) -> bool:
    contourPolygon = QtGui.QPolygonF()
    for point in contour:
        contourPolygon.append(QtCore.QPointF(point[0, 0], point[0, 1]))

    boxPolygon = QtGui.QPolygonF()
    boxPolygon.append(QtCore.QPointF(boxXY[0], boxXY[1]))
    boxPolygon.append(QtCore.QPointF(boxXY[0], boxXY[1] + boxWidthHeight[1]))
    boxPolygon.append(QtCore.QPointF(boxXY[0] + boxWidthHeight[0], boxXY[1]))
    boxPolygon.append(QtCore.QPointF(boxXY[0] + boxWidthHeight[0], boxXY[1] + boxWidthHeight[1]))

    return contourPolygon.intersects(boxPolygon)