//=============================================================================
// Here we search both the process pending edge list and the processed edges list.
SurfaceMesh::Edge* SurfaceMesh::PathConnectedComponent::FindEdge( Vertex* vertex0, Vertex* vertex1 )
{
	Edge* foundEdge = 0;
	for( Edge* edge = ( Edge* )edgeQueue.LeftMost(); edge && !foundEdge; edge = ( Edge* )edge->Right() )
		if( ( edge->vertex[0] == vertex0 && edge->vertex[1] == vertex1 ) || ( edge->vertex[0] == vertex1 && edge->vertex[1] == vertex0 ) )
			foundEdge = edge;
	for( Edge* edge = ( Edge* )processedEdges.LeftMost(); edge && !foundEdge; edge = ( Edge* )edge->Right() )
		if( ( edge->vertex[0] == vertex0 && edge->vertex[1] == vertex1 ) || ( edge->vertex[0] == vertex1 && edge->vertex[1] == vertex0 ) )
			foundEdge = edge;
	return foundEdge;
}
//=============================================================================
SurfaceMesh::Vertex* SurfaceMesh::PathConnectedComponent::FindVertexForEdge( Edge* processEdge, Vertex*& ccwVertex, Vertex*& cwVertex, Plane& edgePlane, const GenerationParameters& genParms )
{
	// We first try to find an existing vertex as the CW or CCW vertex WRT this edge.
	Vertex* foundVertex = 0;
	ccwVertex = processEdge->FindAdjacentVertex( Edge::CCW_VERTEX, ++visitationKey );
	cwVertex = processEdge->FindAdjacentVertex( Edge::CW_VERTEX, ++visitationKey );
	if( !( ccwVertex && cwVertex ) )
		return 0;
	processEdge->MakeEdgePlane( edgePlane );
	if( Plane::SIDE_FRONT == PlaneSide( edgePlane, ccwVertex->point, genParms.epsilon ) &&
		CalculateInteriorAngle( processEdge->vertex[0], processEdge->vertex[1], ccwVertex ) < genParms.frontierAngle )
	{
		foundVertex = ccwVertex;
	}
	else if( Plane::SIDE_FRONT == PlaneSide( edgePlane, cwVertex->point, genParms.epsilon ) &&
		CalculateInteriorAngle( processEdge->vertex[1], processEdge->vertex[0], cwVertex ) < genParms.frontierAngle )
	{
		foundVertex = cwVertex;
	}
	else
	{
		// Don't visit the vertices of the given edge.
		visitationKey++;
		processEdge->vertex[0]->visitationKey = visitationKey;
		processEdge->vertex[1]->visitationKey = visitationKey;

		// Go visit all vertices on the periphery of the partially generated mesh.
		// Look for a vertex that we need to connect with.
		double minimumDistance = 0.0;
		Vertex* likelyVertex = 0;
		for( Edge* edge = ( Edge* )edgeQueue.LeftMost(); edge; edge = ( Edge* )edge->Right() )
		{
			for( int index = 0; index < 2; index++ )
			{
				Vertex* vertex = edge->vertex[ index ];
				if( vertex->visitationKey != visitationKey )
				{
					vertex->visitationKey = visitationKey;

					if( Plane::SIDE_FRONT == PlaneSide( edgePlane, vertex->point, genParms.epsilon ) &&
						CalculateInteriorAngle( processEdge->vertex[0], processEdge->vertex[1], vertex ) < genParms.frontierAngle &&
						CalculateInteriorAngle( processEdge->vertex[1], processEdge->vertex[0], vertex ) < genParms.frontierAngle )
					{
						Vector edgeVec;
						Sub( edgeVec, processEdge->vertex[1]->point, processEdge->vertex[0]->point );
						Normalize( edgeVec, edgeVec );
						Vector vec;
						Sub( vec, vertex->point, processEdge->vertex[0]->point );
						Reject( vec, vec, edgeVec );
						double distance = Length( vec );
						if( minimumDistance == 0.0 || distance < minimumDistance )
						{
							minimumDistance = distance;
							likelyVertex = vertex;
						}
					}
				}
			}
		}

		if( likelyVertex && minimumDistance <= genParms.walkDistance * 1.1 )
			foundVertex = likelyVertex;
	}

	return foundVertex;
}