package com.sensetime.demo.util;

import android.graphics.PointF;
import android.graphics.Rect;

public class FaceRotationUtil {

    /**
     * rotate face rect
     *
     * @param rect used to rotate
     * @param width The width of image
     * @param height The height of image
     * @param isFlip if isFlip faceInfo
     * @param degrees The degrees of camera display
     */
    public static Rect rotateFaceRect(Rect rect, int width, int height, boolean isFlip,
                                       int degrees) {
        int tmp;
        switch (degrees) {
            // landscape
            case 0:
                // 前置摄像头时左右翻转
                if (isFlip) {
                    tmp = rect.left;
                    rect.left = width - rect.right;
                    rect.right = width - tmp;
                }
                break;
            // portrait
            case 90:
                // 顺时针旋转90°
                tmp = rect.left;
                rect.left = height - rect.bottom;
                rect.bottom = rect.right;
                rect.right = height - rect.top;
                rect.top = tmp;
                // 前置摄像头时左右翻转
                if (isFlip) {
                    tmp = rect.left;
                    // 由于旋转了90度，所以现在的height相当于旋转后的width
                    rect.left = height - rect.right;
                    rect.right = height - tmp;
                }
                break;
            case 180:
                // 上下翻转
                tmp = rect.top;
                rect.top = height - rect.bottom;
                rect.bottom = height - tmp;
                tmp = rect.left;
                rect.left = width - rect.right;
                rect.right = width - tmp;
                // 前置摄像头时再左右翻转
                if (isFlip) {
                    tmp = rect.left;
                    rect.left = width - rect.right;
                    rect.right = width - tmp;
                }
                break;
            case 270:
                // 顺时针旋转90°
                tmp = rect.left;
                rect.left = height - rect.bottom;
                rect.bottom = rect.right;
                rect.right = height - rect.top;
                rect.top = tmp;
                // 左右翻转
                tmp = rect.left;
                rect.left = height - rect.right;
                rect.right = height - tmp;
                // 上下翻转
                tmp = rect.top;
                rect.top = width - rect.bottom;
                rect.bottom = width - tmp;
                // 前置摄像头时再左右翻转
                if (isFlip) {
                    tmp = rect.left;
                    // 由于旋转了90度，所以现在的height相当于旋转后的width
                    rect.left = height - rect.right;
                    rect.right = height - tmp;
                }
                break;
            default:
                break;
        }
        return rect;
    }

    /**
     * rotate face key points
     *
     * @param point used to rotate
     * @param width The width of image
     * @param height The height of image
     * @param isFlip if flip faceInfo
     * @param degrees The degrees of camera display
     */
    public static PointF rotatePoints(PointF point, int width, int height, boolean isFlip,
                                       int degrees) {
        float tmp;
        switch (degrees) {
            // landscape
            case 0:
                if (isFlip) {
                    point.x = width - point.x;
                }
                break;
            // portrait
            case 90:
                tmp = point.x;
                point.x = height - point.y;
                point.y = tmp;
                if (isFlip) {
                    point.x = height - point.x;
                }
                break;
            case 180:
                point.y = height - point.y;
                point.x = width - point.x;
                if (isFlip) {
                    point.x = width - point.x;
                }
                break;
            case 270:
                tmp = point.y;
                point.y = width - point.x;
                point.x = tmp;
                if (isFlip) {
                    point.x = height - point.x;
                }
                break;
            default:
                break;
        }
        return point;
    }


}
