using System; using System.Linq; using System.Collections.Generic; namespace UnityEngine.ProBuilder { /// /// An edge composed of both the local index and common index. ///
///
/// This is useful when comparing vertex indexes that are coincident. Coincident vertices are defined as vertices that are share the same coordinate space, but are separate values in the vertex array. ProBuilder tracks these coincident values in the @"UnityEngine.ProBuilder.ProBuilderMesh.sharedIndexes" array. A "common" (also called "shared") index is the index of a vertex in the sharedIndexes array. ///
/// /// public struct EdgeLookup : IEquatable { Edge m_Local; Edge m_Common; /// /// Local edges point to an index in the vertices array. /// public Edge local { get { return m_Local; } set { m_Local = value; } } /// /// Commmon edges point to the vertex index in the sharedIndexes array. /// public Edge common { get { return m_Common; } set { m_Common = value; } } /// /// Create an edge lookup from a common and local edge. /// /// An edge composed of common indexes (corresponds to @"UnityEngine.ProBuilder.ProBuilderMesh.sharedIndexes"). /// An edge composed of vertex indexes (corresponds to mesh vertex arrays). public EdgeLookup(Edge common, Edge local) { m_Common = common; m_Local = local; } /// /// Create an edge lookup from 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); } public override int GetHashCode() { return common.GetHashCode(); } public static bool operator==(EdgeLookup a, EdgeLookup b) { return Equals(a, b); } public static bool operator!=(EdgeLookup a, EdgeLookup b) { return !Equals(a, b); } /// /// Returns a string representation of the common edge property. /// /// public override string ToString() { return string.Format("Common: ({0}, {1}), local: ({2}, {3})", common.a, common.b, local.a, local.b); } /// /// Create a list of EdgeLookup edges from a set of local edges and a sharedIndexes dictionary. /// /// A collection of local edges. /// A shared index lookup dictionary (see ProBuilderMesh.sharedIndexes). /// A set of EdgeLookup edges. public static IEnumerable GetEdgeLookup(IEnumerable edges, Dictionary lookup) { return edges.Select(x => new EdgeLookup(new Edge(lookup[x.a], lookup[x.b]), x)); } /// /// Create 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 ProBuilderMesh.sharedIndexes). /// 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; } } }