using System; using System.Linq; using System.Collections.Generic; namespace UnityEngine.ProBuilder { /// /// Represents an edge composed of both the local index and the common index. /// /// Use this to compare vertex indices that are coincident. /// [Coincident](../manual/gloss.html#coincident) vertices share the same coordinate space, but are separate values /// in the vertex array. ProBuilder tracks these coincident values in the /// array. A "common" (also called "shared") index is the index of a vertex in the sharedVertices array. /// /// UnityEngine.ProBuilder.ProBuilderMesh.sharedVertices /// UnityEngine.ProBuilder.Edge public struct EdgeLookup : IEquatable { Edge m_Local; Edge m_Common; /// /// Gets or sets the local edges. /// /// Local edges point to an index in the vertices array. /// public Edge local { get { return m_Local; } set { m_Local = value; } } /// /// Gets or sets the common edges. /// /// Commmon edges point to the vertex index in the sharedVertices array. /// public Edge common { get { return m_Common; } set { m_Common = value; } } /// /// Creates an edge lookup from common and local Edge instances. /// /// An edge composed of common indexes (corresponds to ). /// An edge composed of vertex indexes (corresponds to mesh vertex arrays). public EdgeLookup(Edge common, Edge local) { m_Common = common; m_Local = local; } /// /// Creates an edge lookup from two set of vertices that represent the common and local edges. /// /// Common edge x. /// Common edge y. /// Local edge x. /// Local edge y. public EdgeLookup(int cx, int cy, int x, int y) { m_Common = new Edge(cx, cy); m_Local = new Edge(x, y); } /// /// Compares each EdgeLookup common edge (does not take into account local edge differences). /// /// The EdgeLookup to compare against. /// True if the common edges are equal, false if not. public bool Equals(EdgeLookup other) { return other.common.Equals(common); } /// /// Compares each EdgeLookup common edge (does not take into account local edge differences). /// /// The EdgeLookup to compare against. False if obj is not an EdgeLookup type. /// True if the common edges are equal; false if not. public override bool Equals(object obj) { return !ReferenceEquals(obj, null) && Equals((EdgeLookup)obj); } /// /// Returns the hash code for this instance. /// /// An integer that is the hash code for this instance. public override int GetHashCode() { return common.GetHashCode(); } /// /// Compares two objects for equality. /// /// The first EdgeLookup instance. /// The second EdgeLookup instance. /// True if the objects are equal; false if not. public static bool operator==(EdgeLookup a, EdgeLookup b) { return Equals(a, b); } /// /// Returns true if the two objects are not equal. /// /// The first EdgeLookup instance. /// The second EdgeLookup instance. /// True if the objects are not equal; false if they are equal. public static bool operator!=(EdgeLookup a, EdgeLookup b) { return !Equals(a, b); } /// /// Returns a string representation of the common edge property. /// /// String formatted as: "Common: (`common.a`, `common.b`), local: (`local.a`, `local.b`)" public override string ToString() { return string.Format("Common: ({0}, {1}), local: ({2}, {3})", common.a, common.b, local.a, local.b); } /// /// Creates a list of EdgeLookup edges from a set of local edges and a sharedVertices dictionary. /// /// A collection of local edges. /// A shared index lookup dictionary (see ). /// A set of EdgeLookup edges that you can iterate over. public static IEnumerable GetEdgeLookup(IEnumerable edges, Dictionary lookup) { return edges.Select(x => new EdgeLookup(new Edge(lookup[x.a], lookup[x.b]), x)); } /// /// Creates a hashset of edge lookup values from a collection of local edges and a shared indexes lookup. /// /// A collection of local edges. /// A shared index lookup dictionary (see ). /// A HashSet of EdgeLookup edges. EdgeLookup values are compared by their common property only: local edges are not compared. public static HashSet GetEdgeLookupHashSet(IEnumerable edges, Dictionary lookup) { if (lookup == null || edges == null) return null; var hash = new HashSet(); foreach (var local in edges) hash.Add(new EdgeLookup(new Edge(lookup[local.a], lookup[local.b]), local)); return hash; } } }