public static Vector3 RandomTrianglePoint(this ThreeVector3 triangle) { float r1 = Mathf.Sqrt(Random.Range(0.0f, 1.0f)); float r2 = Random.Range(0.0f, 1.0f); return((1 - r1) * triangle.x + r1 * (1 - r2) * triangle.y + r1 * r2 * triangle.z); }
public static float CalcTriangleArea(this ThreeVector3 triangle) { float a = Vector3.Distance(triangle.x, triangle.y); float b = Vector3.Distance(triangle.y, triangle.z); float c = Vector3.Distance(triangle.z, triangle.x); float s = (a + b + c) / 2.0f; return(Mathf.Sqrt(s * (s - a) * (s - b) * (s - c))); }
public static void Triangulate(Vector3[] polygon, Vector3 normal, out List <ThreeVector3> triangles) { if (polygon.Length < 3) { triangles = new List <ThreeVector3>(); return; } //var planeNormal = Vector3.Cross(polygon[1] - polygon[0], polygon[2] - polygon[0]); //planeNormal.Normalize(); //if (Vector3.Angle(planeNormal, normal) > Vector3.Angle(-planeNormal, normal)) //{ // planeNormal = -planeNormal; //} //Quaternion rotation = Quaternion.identity; //if (planeNormal != Vector3.forward) //{ // rotation = Quaternion.FromToRotation(planeNormal, Vector3.forward); //} Quaternion rotation = Quaternion.FromToRotation(normal, Vector3.forward); // Rotate 1 point and note where it ends up in Z float z = (rotation * polygon[0]).z; var poly = new Polygon(ConvertPoints(polygon, rotation)); DTSweepContext tcx = new DTSweepContext(); tcx.PrepareTriangulation(poly); DTSweep.Triangulate(tcx); tcx = null; Quaternion invRot = Quaternion.Inverse(rotation); triangles = new List <ThreeVector3>(); foreach (DelaunayTriangle t in poly.Triangles) { ThreeVector3 tri = new ThreeVector3(); for (int i = 0; i < 3; ++i) { var p = t.Points[i]; Vector3 pos = new Vector3(p.Xf, p.Yf, z); tri[i] = invRot * pos; } triangles.Add(tri); } }
public static bool IsContainsPoint(this ThreeVector3 triangle, Vector3 point) { return(PointsOnSameSideLine(point, triangle.x, triangle.y, triangle.z) && PointsOnSameSideLine(point, triangle.y, triangle.x, triangle.z) && PointsOnSameSideLine(point, triangle.z, triangle.x, triangle.y)); }