判断一个点是否在三角形内部

2020-12-05  本文已影响0人  太刀

如何判定一个点是否在三角形内部

光栅化的关键步骤是计算某个三角形覆盖到了哪些像素,需要依次判断每个像素的中心是否在三角形内部,那么,给定一个点和一个三角形(由三个点定义),如何判定该点是否在三角形中呢?

using UnityEngine;
using System.Collections;

public class Utils
{
    public static bool isPointInPlane(Vector3 point, Vector3 a, Vector3 b, Vector3 c)
    {
        Vector3 ab = Vector3.Normalize(b - a);
        Vector3 bc = Vector3.Normalize(c - b);
        Vector3 ca = Vector3.Normalize(a - c);
        Vector3 ap = Vector3.Normalize(point - a);
        Vector3 bp = Vector3.Normalize(point - b);
        Vector3 cp = Vector3.Normalize(point - c);

        float dotValue1 = Mathf.Abs(Vector3.Dot(ab, ap));
        float dotValue2 = Mathf.Abs(Vector3.Dot(bc, bp));
        float dotValue3 = Mathf.Abs(Vector3.Dot(ca, cp));

        // 是否在三角形边的延长线上
        if (dotValue1 == 1 || dotValue2 == 1 || dotValue3 == 1)
        {
            return true;
        }
        else
        {
            // 与任意两条边叉积得到的法线是否同向或反向
            Vector3 n1 = Vector3.Normalize(Vector3.Cross(ab, ap));
            Vector3 n2 = Vector3.Normalize(Vector3.Cross(bc, ap));

            float cos = Mathf.Abs(Vector3.Dot(n1, n2));

            return cos == 1;
        }
        
    }

    public static bool isPointInTrangle(Vector3 point, Vector3 a, Vector3 b, Vector3 c)
    {
        if (!isPointInPlane(point, a, b, c))
        {
            return false;
        }

        Vector3 ab = b - a;
        Vector3 bc = c - b;
        Vector3 ca = a - c;
        Vector3 ap = point - a;
        Vector3 bp = point - b;
        Vector3 cp = point - c;

        Vector3 n1 = Vector3.Normalize(Vector3.Cross(ab, ap));
        Vector3 n2 = Vector3.Normalize(Vector3.Cross(bc, bp));
        Vector3 n3 = Vector3.Normalize(Vector3.Cross(ca, cp));

        float dotValue1 = Vector3.Dot(n1, n2);
        float dotValue2 = Vector3.Dot(n2, n3);
        float dotValue3 = Vector3.Dot(n3, n1);

        return (dotValue1 == dotValue2) && (dotValue2 == dotValue3) && (dotValue3 == 1);
    }
}

添加如下的测试代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HelloUnity : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Application.targetFrameRate = 60;

        Vector3 a = Vector3.zero;
        Vector3 b = Vector3.right;
        Vector3 c = Vector3.up;

        // Vector3 p = Vector3.forward; // false, false
        // Vector3 p = Vector3.left;    // true, false
        // Vector3 p = new Vector3(0.1f, 0.1f, 0);  // true, true
        // Vector3 p = new Vector3(1f, 1f, 0); // true, false
        Vector3 p = new Vector3(0.2f, 0.2f, 0.2f);  // false, false

        bool inPlane = Utils.isPointInPlane(p, a, b, c);
        bool inTrangle = Utils.isPointInTrangle(p, a, b, c);

        Debug.Log("inPlane:" + inPlane + ", inTrangle:" + inTrangle);
    }
}

上一篇 下一篇

猜你喜欢

热点阅读