# -*- coding: utf-8 -*-
"""
Created on Mon Jul 29 10:28:21 2024
    未来需将这一部分和轨迹分析代码集成

@author: ym
"""
import numpy as np
import cv2
from scipy.spatial.distance import cdist

class TProp:
    def __init__(self, boxes):
        
        self.boxes = boxes
        
    
    


class TProp:
    '''抽象基类，不能实例化对象'''
    def __init__(self, boxes):
        '''
        boxes: [x1, y1, x2, y2, track_id, score, cls, frame_index, box_index] 
                0   1   2   3      4        5     6       7           8     
        '''
        # assert len(set(boxes[:, 4].astype(int))) == 1, "For a Track, track_id more than 1"
        # assert len(set(boxes[:, 6].astype(int))) == 1, "For a Track, class number more than 1"
        
        self.boxes = boxes
        
        '''5个关键点（中心点、左上点、右上点、左下点、右下点 ）坐标'''
        self.compute_cornpoints()
        
        '''5个关键点轨迹特征，可以在子类中实现，降低顺序处理时的计算量
          （中心点、左上点、右上点、左下点、右下点 ）轨迹特征'''
        self.compute_cornpts_feats()
        
        self.distmax = max(self.trajdist)
        
        
    def compute_cornpoints(self):
        '''
        cornpoints 共10项，分别是个点的坐标值（x, y）
            (center, top_left, top_right, bottom_left, bottom_right)
        '''
        boxes = self.boxes
        cornpoints = np.zeros((self.frnum, 10))
        cornpoints[:,0] = (boxes[:, 0] + boxes[:, 2]) / 2
        cornpoints[:,1] = (boxes[:, 1] + boxes[:, 3]) / 2
        cornpoints[:,2], cornpoints[:,3] = boxes[:, 0], boxes[:, 1]
        cornpoints[:,4], cornpoints[:,5] = boxes[:, 2], boxes[:, 1]
        cornpoints[:,6], cornpoints[:,7] = boxes[:, 0], boxes[:, 3]
        cornpoints[:,8], cornpoints[:,9] = boxes[:, 2], boxes[:, 3]

        self.cornpoints = cornpoints
    def compute_cornpts_feats(self):
        '''
        '''
        trajectory = []
        trajlens = []
        trajdist = []
        trajrects = []
        for k in range(5):
            # diff_xy2 = np.power(np.diff(self.cornpoints[:, 2*k:2*(k+1)], axis = 0), 2)
            # trajlen = np.sum(np.sqrt(np.sum(diff_xy2, axis = 1)))
            
            X = self.cornpoints[:, 2*k:2*(k+1)]
            
            traj = np.linalg.norm(np.diff(X, axis=0), axis=1)
            trajectory.append(traj)
            
            trajlen = np.sum(traj)
            trajlens.append(trajlen)
            
            ptdist = np.max(cdist(X, X))
            trajdist.append(ptdist)
            
            '''最小外接矩形：
                   rect[0]: 中心(x, y)
                   rect[1]: (w, h)
                   rect[0]: 旋转角度 (-90°, 0]  
            '''  
            rect = cv2.minAreaRect(X.astype(np.int64))
            trajrects.append(rect)
        
        self.trajectory = trajectory
        self.trajlens = trajlens
        self.trajdist = trajdist
        self.trajrects = trajrects