using UnityEngine; namespace BrainFailProductions.PolyFew.AsImpL.MathUtil { /// /// Vertex structure used for triangulation. /// /// public class Vertex { private Vertex prevVertex; private Vertex nextVertex; private float triangleArea; private bool triangleHasChanged; /// /// Coordinates in 3D space. /// public Vector3 Position { get; private set; } /// /// Saved index in the original list. /// public int OriginalIndex { get; private set; } /// /// Reference to the previous vertex this vertex is attached to. /// public Vertex PreviousVertex { get { return prevVertex; } set { triangleHasChanged = prevVertex != value; prevVertex = value; } } /// /// Reference to the next vertex this vertex is attached to. /// public Vertex NextVertex { get { return nextVertex; } set { triangleHasChanged = nextVertex != value; nextVertex = value; } } /// /// Area of the triangle this vertex belogs to, /// automatically computed each time the connected vertices change. /// public float TriangleArea { get { if (triangleHasChanged) { ComputeTriangleArea(); } return triangleArea; } } /// /// Construct a Vertex by defining its index in the original list and its position in 3D space. /// /// Index in the original list. /// Position in 3D space. public Vertex(int originalIndex, Vector3 position) { OriginalIndex = originalIndex; Position = position; } /// /// Get 2D position of this vertex on the plane defined by the given normal. /// /// Normal of the plane used to project 3D vertices in 2D. /// public Vector2 GetPosOnPlane(Vector3 planeNormal) { Quaternion planeRotation = new Quaternion(); planeRotation.SetFromToRotation(planeNormal, Vector3.back); Vector3 projPos = planeRotation * Position; Vector2 pos_2d_xy = new Vector2(projPos.x, projPos.y); return pos_2d_xy; } private void ComputeTriangleArea() { Vector3 side1 = PreviousVertex.Position - Position; Vector3 side2 = NextVertex.Position - Position; Vector3 crossProd = Vector3.Cross(side1, side2); triangleArea = crossProd.magnitude / 2f; } } }