U
    W`}f                     @   s0   d dl Zd dlmZ dd Zdd Zdd ZdS )	    N)normc                 C   s,  |j \}}tjd|ftd}|dkr4d\}}	}
}}n|\}}	}
}}| d d }| d d }| d d }| d d }|dddf }|dddf }|dddf }t|ddddf dd}t||}t||}|d |d  }|d||  |	|d   ||d    d|
 | |  ||d|d     }|d||  |	|d   ||d    |
|d|d     d| | |  }|| | |dddf< || | |dddf< |dk}t|dddf dk|dddf |k }t|dddf dk|dddf |k }t|t||}|||fS )	  
    Projects a list of points to the camera defined transform, intrinsics and distortion
    :param intrinsic_matrix: 3x3 intrinsic camera matrix
    :param distortion: should be as follows: (k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6]])
    :param width: the image width
    :param height: the image height
    :param pts: a list of point coordinates (in the camera frame) with the following format: np array 4xn or 3xn
    :return: a list of pixel coordinates with the same length as pts
       )dtypeNr   r   r   r   r   r         )axis)shapenpzerosfloatr   dividelogical_and)intrinsic_matrix
distortionwidthheightpts_n_ptspixsk1k2p1p2k3fxfycxcyxyzdistsxlylr2xllyllvalid_z
valid_xpix
valid_ypix
valid_pixs r.   ;/home/daniela/catkin_ws/src/hpe/scripts/utils/projection.pyprojectToCamera   s2    
	LL,,r0   c                 C   s  |j \}}| d }| d }| d }	| d }
|dk	r>|dd nd\}}}}}|d |d	 |d
   }}}|| }|| }|d
 |d
  }|d	||  ||d
   ||d    d
| | |  ||d
|d
     }|d	||  ||d
   ||d    ||d
|d
     d
| | |  }t|| |	 g|| |
 gg}|dk}|dddf dk|dddf |k @ }|d	ddf dk|d	ddf |k @ }||@ |@ }||fS )r   r   r   r   r   r   r   r   r   N   r   r   r   r   r   r
   r   array)r   r   r   r   r   r   r   r   r   r   r    r   r   r   r   r   r!   r"   r#   r%   r&   r'   r(   r)   r   r*   r+   r,   r-   r.   r.   r/   projectToCamera_fasterA   s$    
"LL"((r8   c                 C   sF  |j d }| d }| d }| d }| d }	|dk	r>|dd nd\}
}}}}|d	 |d |d
   }}}|| }|| }|d
 |d
  }|d|
|  ||d
   ||d    d
| | |  ||d
|d
     }|d|
|  ||d
   ||d    ||d
|d
     d
| | |  }t|| | g|| |	 gg}|S )a  
    Projects a batch of points to the camera defined transform, intrinsics, and distortion
    :param intrinsic_matrix: 3x3 intrinsic camera matrix
    :param distortion: should be as follows: (k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6]])
    :param width: the image width
    :param height: the image height
    :param pts: a list of point coordinates (in the camera frame) with the following format: np array 4xn or 3xn
    :return: a list of pixel coordinates with the same length as pts
    r   r1   r2   r3   r4   Nr5   r   r   r   r   r6   )r   r   r   r   r   r   r   r   r   r    r   r   r   r   r   r!   r"   r#   r%   r&   r'   r(   r)   r   r.   r.   r/   batch_projectToCamera   s    
"LL"r9   )numpyr   numpy.linalgr   r0   r8   r9   r.   r.   r.   r/   <module>   s   <V