def get_extreme_points(self, index):
        centroid = self.get_position(index)
        contour = self.get_contour(index)

        if centroid is not None and contour is not None:
            dists = list(
                map(lambda p: self.__lin_dist(p[0], centroid), contour))
            ndx = dists.index(max(dists))
            head = tuple(contour[ndx][0])

            dists = list(
                map(lambda p: self.__lin_dist(p[0], contour[ndx][0]), contour))
            ndx = dists.index(max(dists))
            tail = tuple(contour[ndx][0])

            angle = self.get_angle(index)
            if angle is not None:
                angle1 = points_angle(centroid, head)
                angle2 = points_angle(centroid, tail)

                diff1 = min_dist_angles(angle, angle1)
                diff2 = min_dist_angles(angle, angle2)
                if diff1 > diff2:
                    return tail, head

            return head, tail
        else:
            return None, None
Beispiel #2
0
    def __switchangle_btn_event(self):
        if len(self._sel_pts) == 2:
            for i in range(self._sel_pts[0], self._sel_pts[1] + 1):
                head, tail = self.get_extreme_points(i)
                centroid = self.get_position(i)
                self._angles[i] = points_angle(centroid, tail)

            self.mainwindow._player.refresh()
 def get_velocity_angle(self, index):
     vel = self.get_velocity(index)
     p0 = self.get_position(index)
     if vel is not None and p0 is not None:
         p1 = p0[0] - vel[0], p0[1] - vel[1]
         return points_angle(p0, p1)
     else:
         return None
    def flip(self, index):
        """
        flip the orientation of the contour
        """
        head, tail = self.get_extreme_points(index)
        centroid = self.get_position(index)

        if tail is not None and centroid is not None:
            self.set_angle(index, points_angle(centroid, tail))
    def set_contour(self, index, contour, angle=None):

        # add contours in case they do not exists
        if index >= len(self._contours):
            for i in range(len(self._contours), index + 1):
                self._contours.append(None)

        if index >= len(self._angles):
            for i in range(len(self._angles), index + 1):
                self._angles.append(None)

        self._contours[index] = contour

        # No angle was provided to save, we will try to calculate one
        if angle is None:
            p1, p2 = self.get_extreme_points(index)
            centroid = self.get_position(index)
            if centroid is not None:
                angle1 = points_angle(centroid, p1)
                angle2 = points_angle(centroid, p2)

                # Search for the last angle
                last_angle = None
                for i in range(index - 1, -1, -1):
                    tmp_angle = self._angles[i]
                    if tmp_angle is not None:
                        last_angle = tmp_angle
                        break

                # try to match the current angle with the last angle.
                # angles cannot switch values drastically
                if last_angle is not None:
                    diff1 = min_dist_angles(last_angle, angle1)
                    diff2 = min_dist_angles(last_angle, angle2)

                    self._angles[index] = angle2 if diff1 > diff2 else angle1
                else:
                    #print math.degrees(angle)
                    self._angles[index] = angle1
            else:
                self._angles[index] = angle
        else:
            self._angles[index] = angle
    def get_minimumenclosingtriangle_angle(self, index):
        triangle = self.get_minimumenclosingtriangle(index)
        if triangle is None: return None

        p1, p2, p3 = triangle
        p1, p2, p3 = tuple(p1.flatten()), tuple(p2.flatten()), tuple(
            p3.flatten())

        center = (p1[0] + p2[0] + p3[0]) / 3, (p1[1] + p2[1] + p3[1]) / 3
        points = sorted([
            (lin_dist(p1, p2) + lin_dist(p1, p3), p1),
            (lin_dist(p1, p2) + lin_dist(p2, p3), p2),
            (lin_dist(p3, p2) + lin_dist(p1, p3), p3),
        ],
                        reverse=True)
        farthest_pt = points[0][1]

        return points_angle(center, farthest_pt)