bool SP_Node_CH::ACKHandler(SP_Msg& inMsg)
{
	if(!(inMsg.type == SP_MsgType::ACK))
		return false;
	
	//Always deque the current outMessage (the one we were waiting on)
	SP_Msg* existingMsg = GetOutMsg(0);
	if(existingMsg != 0 && existingMsg->type == SP_MsgType::ADD && this->state == SP_CommState::CONNECTED)
	{
		RemoveFirstOutMsg();
		
		//Adds slave neighbor
		SP_Node::SP_Node_Info* newNeighbor = new SP_Node::SP_Node_Info();
		newNeighbor->addr = inMsg.from;
		newNeighbor->type = inMsg.fromNodeType;
		AddNeighbor(newNeighbor);
		
		return true;
	}
	
	//Or if we sent data to someone
	if(existingMsg != 0 && existingMsg->type == SP_MsgType::DATA && this->state == SP_CommState::CONNECTED)
	{
		RemoveFirstOutMsg();
		return true;
	}
	
	return false;
}
示例#2
0
void Node::AddMutualNeighborIfInRange(Node &n) {
	double dist = DistanceToNode(n);
	if(dist <= SquaredRange)
		AddNeighbor(n.Name, dist);
	if(dist <= n.SquaredRange)
		n.AddNeighbor(Name, dist);
}
示例#3
0
void SpatialGraph::ComputeNeighbors( SpatialGraphKDNode* node )
{
	if( node->HasChildren() )
	{
		ComputeNeighbors(node->LHC);
		ComputeNeighbors(node->RHC);
		return;
	}

	Vector2 checkN = Vector2::UnitY * _smallestDimensions.Y;
	Vector2 checkS = Vector2::UnitY * -_smallestDimensions.Y;
	Vector2 checkE = Vector2::UnitX * _smallestDimensions.X;
	Vector2 checkW = Vector2::UnitX * -_smallestDimensions.X;

	/* Vector2 centroid = */ node->BBox.Centroid();

	Vector2List gridPoints;
	int xPoints, yPoints;
	node->GetGridPoints(gridPoints, xPoints, yPoints );

	//Check north neighbors
	for( int i = 0; i < xPoints; i++ )
	{
		AddNeighbor( node, gridPoints[GetColumnMajorIndex(i,0,xPoints)] + checkN );
	}

	//Check south neighbors
	for( int i = 0; i < xPoints; i++ )
	{
		AddNeighbor( node, gridPoints[GetColumnMajorIndex(i,yPoints-1,xPoints)] + checkS );
	}

	//Check east neighbors
	for( int i = 0; i < yPoints; i++ )
	{
		AddNeighbor( node, gridPoints[GetColumnMajorIndex(xPoints-1,i,xPoints)] + checkE );
	}

	//Check west neighbors
	for( int i = 0; i < yPoints; i++ )
	{
		AddNeighbor( node, gridPoints[GetColumnMajorIndex(0,i,xPoints)] + checkW );
	}

}
bool SP_Node_CH::ConnectedHandler(SP_Msg& inMsg)
{
	//Only respond to ADD messages directly to us
	if(!(
	inMsg.type == SP_MsgType::ADD))
		return false;
	
	bool shouldRespond = false;
	//Respond if we have an existing NEW out message and no neighbors
	SP_Msg* existingMsg = GetOutMsg(0);
	if(existingMsg != 0 && existingMsg->type == SP_MsgType::NEW && neighborCount == 0)
	{
		//Remove message
		RemoveFirstOutMsg();
		shouldRespond = true;
		
		//Add neighbor
		SP_Node::SP_Node_Info* newNeighbor = new SP_Node::SP_Node_Info();
		newNeighbor->addr = inMsg.from;
		newNeighbor->type = inMsg.fromNodeType;
		AddNeighbor(newNeighbor);
		
		state = SP_CommState::CONNECTED;
	}
	//Or if responding to node that's our upstream neighbor
	else if(neighbors[0] != 0 && neighbors[0]->addr == inMsg.from)
	{
		shouldRespond = true;
	}
	
	if(!shouldRespond) //Don't continue if no response
		return false;
		
	//Respond with ACK
	existingMsg = GetFirstOutMsgOfType(SP_MsgType::ACK);
	if(existingMsg == 0)
	{
		//Add new ACK if none exists
		SP_Msg* outMsg = new SP_Msg();
		outMsg->reliable = false;
		outMsg->to = inMsg.from;
		outMsg->from = this->addr;
		outMsg->fromNodeType = GetNodeType();
		outMsg->type = SP_MsgType::ACK;
		outMsg->dataLen = 0;
		outMsg->data = 0;
		//Set this message as pending
		this->PendMsg(outMsg);
	}
	return true;
}
示例#5
0
void GmGridMapView::GetNeighbors(int node, FastVector<AStar::EdgeInfo<int,float>>& neighbors)
{
    neighbors.clear();
    neighbors.resize(8);
    float cost = GetCellCost(node);
    float step1 = 1 * cost;
    float stepD = SQRT_2 * cost;
    AddNeighbor( GetNodeDxDy(node, 1, 0) , step1, neighbors);
    AddNeighbor( GetNodeDxDy(node, - 1, 0) , step1, neighbors);
    AddNeighbor( GetNodeDxDy(node, 0, 1) , step1, neighbors);
    AddNeighbor( GetNodeDxDy(node, 0, -1) , step1, neighbors);

    AddNeighbor( GetNodeDxDy(node, 1, 1), stepD, neighbors);
    AddNeighbor( GetNodeDxDy(node, 1, -1), stepD, neighbors);
    AddNeighbor( GetNodeDxDy(node, -1, 1), stepD, neighbors);
    AddNeighbor( GetNodeDxDy(node, -1, -1), stepD, neighbors);
	
}
// This function is symmetric wrt pMain and pOther. It sets up valid neighboring data for
// the relationship between both of them.
static void SetupEdgeNeighbors( 
	const CCoreDispInfo *pListBase,
	CCoreDispInfo *pMain, 
	CCoreDispInfo *pOther )
{
	// Initialize..
	for( int iEdge=0; iEdge < 4; iEdge++ )
	{
		// Setup the edge points and the midpoint.
		Vector pt[2], mid;
		pMain->GetSurface()->GetPoint( iEdge, pt[0] );
		pMain->GetSurface()->GetPoint( (iEdge + 1) & 3, pt[1] );
		mid = (pt[0] + pt[1]) * 0.5f;

		// Find neighbors.
		int iNBEdge;
		if( FindEdge( pOther, pt[1], pt[0], iNBEdge ) )
		{
			AddNeighbor( 
				pListBase, 
				pMain, iEdge, 0, CORNER_TO_CORNER, 
				pOther, iNBEdge, CORNER_TO_CORNER );
		}
		else
		{
			// Look for one that takes up our whole side.
			if( FindEdge( pOther, pt[1], pt[0]*2 - pt[1], iNBEdge ) )
			{
				AddNeighbor( 
					pListBase,
					pMain, iEdge, 0, CORNER_TO_CORNER, 
					pOther, iNBEdge, CORNER_TO_MIDPOINT );
			}
			else if( FindEdge( pOther, pt[1]*2 - pt[0], pt[0], iNBEdge ) )
			{
				AddNeighbor( 
					pListBase,
					pMain, iEdge, 0, CORNER_TO_CORNER, 
					pOther, iNBEdge, MIDPOINT_TO_CORNER );
			}
			else
			{			
				// Ok, look for 1 or two that abut this side.
				if( FindEdge( pOther, mid, pt[0], iNBEdge ) )
				{
					AddNeighbor( 
						pListBase,
						pMain, iEdge, g_bEdgeNeighborFlip[iEdge], CORNER_TO_MIDPOINT, 
						pOther, iNBEdge, CORNER_TO_CORNER );
				}
				
				if( FindEdge( pOther, pt[1], mid, iNBEdge ) )
				{
					AddNeighbor( 
						pListBase,
						pMain, iEdge, !g_bEdgeNeighborFlip[iEdge], MIDPOINT_TO_CORNER, 
						pOther, iNBEdge, CORNER_TO_CORNER );
				}
			}
		}
	}
}