/**
	 *	Associates indices with a single shared index.  Does not perfrom any additional operations 
	 *	to repair triangle structure or vertex placement.
	 */
	public static void MergeSharedIndices(ref pb_IntArray[] sharedIndices, int a, int b)
	{
		int aIndex = sharedIndices.IndexOf(a);
		int oldBIndex = sharedIndices.IndexOf(b);
	
		pb_IntArrayUtility.AddValueAtIndex(ref sharedIndices, aIndex, b);

		int[] arr = sharedIndices[oldBIndex].array;
		sharedIndices[oldBIndex].array = arr.RemoveAt(System.Array.IndexOf(arr, b));
		pb_IntArray.RemoveEmptyOrNull(ref sharedIndices);
	}	
	/**
	 *	\brief Averages shared normals with the mask of 'all' (indices contained in perimeter edge)
	 */
	private static Vector3 Norm( int tri, pb_IntArray[] shared, int[] all, Vector3[] norm )
	{
		int sind = shared.IndexOf(tri);
		
		if(sind < 0)
			return Vector3.zero;

		int[] triGroup = shared[sind];

		Vector3 n = Vector3.zero;
		int count = 0;
		for(int i = 0; i < all.Length; i++)
		{
			// this is a point in the perimeter, add it to the average
			if( System.Array.IndexOf(triGroup, all[i]) > -1 )
			{
				n += norm[all[i]];
				count++;
			}
		}
		return n / (float)count;
	}
	/**
	 * Checks if an array contains a value and also compares shared indices using sharedIndices.
	 */
	public static int IndexOf(this int[] array, int val, pb_IntArray[] sharedIndices)
	{
		int indInShared = sharedIndices.IndexOf(val);
		if(indInShared < 0) return -1;

		int[] allValues = sharedIndices[indInShared];

		for(int i = 0; i < array.Length; i++)
			if(System.Array.IndexOf(allValues, array[i]) > -1)
				return i;

		return -1;
	}