void ClusterHierarchyBuilder::InheritEdges(Node *node)
{
	node->mEdgeLLHead = 0;
	
	for(int i = 0;i < node->mNumChildren;i++)
	{
		Node *child = node->mChildren[i];
		
		Edge *filtered = FilterSinglyLinkedList(child->mEdgeLLHead, StripLoopsTraits(child,node));
		child->mEdgeLLHead = 0;
		
		Edge *cur = filtered;
		while(cur)
		{
			Edge *next = cur->LLGetNext(child);
			if(cur->mFrom == child)
				cur->mFrom = node;
			else
			{
				assert(cur->mTo == child);
				cur->mTo = node;
			}
			
			cur->mSqrLen = (cur->mFrom->mCentroid - cur->mTo->mCentroid).GetSqrLen();
			mEdgesHeap.Remove(cur);
			
			cur = next;
		}
		
		if(node->mEdgeLLHead)
		{
			node->mEdgeLLHead = filtered;
		}
		else
		{
			node->mEdgeLLHead = MergeSinglyLinkedList(node->mEdgeLLHead, filtered, SortEdgesTraits(node));
		}
	}
	
	
	node->mEdgeLLHead = RemoveDupsSinglyLinkedList(node->mEdgeLLHead,RemoveDupEdgesTraits(node));
	
	for(Edge *cur = node->mEdgeLLHead;cur;cur = cur->LLGetNext(node))
	{
		mEdgesHeap.Push(cur);
	}
}
void ClusterHierarchyBuilder::CreateInitialEdges(const Vec2 *pts,int numPts)
{
	FortunesAlgorithm delaunay(pts,numPts);
	
	mEdges = new Edge[delaunay.mTriangles.size() * 3];
	Edge *freeEdge = mEdges;
	for(const FortunesAlgorithm::Triangle &tri : delaunay.mTriangles)
	{
		int prev = tri.mIndices[2];
		for(int j = 0;j < 3;j++)
		{
			int cur = tri.mIndices[j];
			
			int from = prev;
			int to = cur;
			if(from > to)
				std::swap(to,from);
			
			Edge *edge = freeEdge++;
			
			edge->mFrom = mNodes[from];
			edge->mFromLLNext = mNodes[from]->mEdgeLLHead;
			mNodes[from]->mEdgeLLHead = edge;
			
			edge->mTo = mNodes[to];
			edge->mToLLNext = mNodes[to]->mEdgeLLHead;
			mNodes[to]->mEdgeLLHead = edge;
			
			edge->mSqrLen = (mNodes[from]->mCentroid - mNodes[to]->mCentroid).GetSqrLen();
			edge->mInHeap = false;
			
			prev = cur;
		}
	}
	
	for(Node *node : mNodes)
	{
		node->mEdgeLLHead = SortSinglyLinkedList(node->mEdgeLLHead,SortEdgesTraits(node));
		node->mEdgeLLHead = RemoveDupsSinglyLinkedList(node->mEdgeLLHead, RemoveDupEdgesTraits(node));
		
		for(Edge *edge = node->mEdgeLLHead;edge;edge = edge->LLGetNext(node))
		{
			if(!edge->mInHeap)
			{
				edge->mInHeap = true;
				mEdgesHeap.Push(edge);
			}
		}
	}
}