Esempio n. 1
0
int PathGraph::findNearestNode ( PathNodeType searchType, Vector const & position_p ) const
{
	int minNode = -1;
	float minDist2 = REAL_MAX;

	int nodeCount = getNodeCount();

	for(int i = 0; i < nodeCount; i++)
	{
		PathNode const * node = getNode(i);

		if(node == NULL) continue;
		
		if(getEdgeCount(i) == 0) continue;
		
		PathNodeType type = node->getType();

		if(type != searchType) continue;

		float dist2 = position_p.magnitudeBetweenSquared(node->getPosition_p());

		if(dist2 < minDist2)
		{
			minNode = i;
			minDist2 = dist2;
		}
	}

	return minNode;
}
Esempio n. 2
0
int PathGraph::findEntrance ( int key ) const
{
	int nodeCount = getNodeCount();

	for(int i = 0; i < nodeCount; i++)
	{
		PathNode const * node = getNode(i);

		if(node == NULL) continue;
		
		if(getEdgeCount(i) == 0) continue;
		
		PathNodeType type = node->getType();

		if((type == PNT_CellPortal) || (type == PNT_BuildingEntrance) || (type == PNT_CityEntrance))
		{
			if(node->getKey() == key)
			{
				return i;
			}
		}
	}

	return -1;
}
Esempio n. 3
0
int writeMatrixToFile(char * oldname, int **M, int N) {
  int i,j;
  FILE * fout= NULL;
  i = strlen(oldname);
  oldname[i-3]='r';
  oldname[i-2]='a';
  oldname[i-1]='m';
  fout = fopen(oldname, "w");
  if (fout == NULL) {
    printf("Não foi possível abrir o ficheiro '%s'!", oldname);
    return 3;
  }
  fprintf(fout, "%d\n", N);
  fprintf(fout, "%d\n", getEdgeCount(M,N));
  for (i=0; i<N; i++) {
    for (j=i+1; j<N; j++) {
      if (i!=j && M[i][j] != 0)
        fprintf(fout, "%d %d %d\n", i, j, M[i][j]);
    }
  }

  fclose(fout);

  i = strlen(oldname);
  oldname[i-3]='a';
  oldname[i-2]='d';
  oldname[i-1]='j';
  return 1;
}
Esempio n. 4
0
void TRegion::print()
{
	std::cout << "\nNum edges: " << getEdgeCount() << std::endl;
	for (UINT i = 0; i < getEdgeCount(); i++) {
		std::cout << "\nEdge #" << i;
		std::cout << ":P0(" << getEdge(i)->m_s->getChunk(0)->getP0().x << "," << getEdge(i)->m_s->getChunk(0)->getP0().y << ")";
		std::cout << ":P2(" << getEdge(i)->m_s->getChunk(getEdge(i)->m_s->getChunkCount() - 1)->getP2().x << "," << getEdge(i)->m_s->getChunk(getEdge(i)->m_s->getChunkCount() - 1)->getP2().y << ")";
		std::cout << std::endl;
	}
	if (m_imp->m_includedRegionArray.size()) {
		std::cout << "***** questa regione contiene:" << std::endl;
		for (UINT i = 0; i < m_imp->m_includedRegionArray.size(); i++)
			m_imp->m_includedRegionArray[i]->print();
		std::cout << "***** fine" << std::endl;
	}
}
Esempio n. 5
0
void    GenGraph< Config >::removeAllEdges( WeakNode source, WeakNode destination )
{
    if ( source.expired() ||
         destination.expired() )
        throw gtpo::bad_topology_error( "gtpo::GenGraph<>::removeEdge(): Topology error." );

    while ( getEdgeCount( source, destination ) > 0 )
        removeEdge( source, destination );
}
Esempio n. 6
0
void Graph::printGraph()
{
   unsigned int retval = -1;
   

   for(std::map<int, graphPoint* >::iterator it = graphNodes.begin(); it != graphNodes.end(); ++it)
   {
      it->second->printGraphPoint();
   }

   std::cout << "TOTAL NODES: " << getNodeCount() << "\tTOTAL EDGES: " << getEdgeCount() << "\n" << std::endl;
 

}
Esempio n. 7
0
double temporalFunc(ASPath path, TraceNode *p1, TraceNode *p2) {
	Edge **elist;
	int ecount;
	double *vlist;
	
	int i;
	double above = 0;
	double below = 0;
	double avgv;

	ecount = getEdgeCount(path);
	elist = getEdges(path);
	vlist = (double*)malloc(sizeof(double) * ecount);
	avgv = averageV(path, p1, p2);

	for (i = 0; i < ecount; i++) {
		if(elist[i]->cost == 0) { return 0; }
		vlist[i] = elist[i]->length / elist[i]->cost;
	}

	for (i = 0; i < ecount; i++) {
		above += vlist[i];
	}
	above *= avgv;

	for (i = 0; i < getEdgeCount(path); i++) {
		below += vlist[i] * vlist[i];
	}
	below *= ecount * avgv * avgv;
	below = sqrt(below);

	free(vlist);
	free(elist);
	if(below == 0) { return 0; }
	else { return above / below; }
}
Esempio n. 8
0
TRegionId TRegion::getId()
{
	assert(getEdgeCount() > 0);
	TEdge *edge;

	for (UINT i = 0; i < m_imp->m_edge.size(); i++)
		if (m_imp->m_edge[i]->m_index >= 0) {
			edge = m_imp->m_edge[i];
			return TRegionId(edge->m_s->getId(),
							 (float)((edge->m_w0 + edge->m_w1) * 0.5), edge->m_w0 < edge->m_w1);
		}

	edge = m_imp->m_edge[0];
	return TRegionId(edge->m_s->getId(),
					 (float)((edge->m_w0 + edge->m_w1) * 0.5), edge->m_w0 < edge->m_w1);
}
Esempio n. 9
0
int TRegion::getStyle() const
{
	int ret = 0;
	UINT i = 0, j, n = getEdgeCount();
	for (; i < n; i++) {
		int styleId = getEdge(i)->getStyle();
		if (styleId != 0 && ret == 0) {
			//assert(styleId<100);
			ret = styleId;
			if (i > 0)
				for (j = 0; j < i; j++)
					getEdge(i)->setStyle(ret);
		} else if (styleId != ret)
			getEdge(i)->setStyle(ret);
	}
	return ret;
}
Esempio n. 10
0
void TRegion::setStyle(int colorStyle)
{

	for (UINT i = 0; i < getEdgeCount(); i++)
		getEdge(i)->setStyle(colorStyle);

	/*
  if (!colorStyle || (colorStyle && colorStyle->isFillStyle())  )
  {
    for (UINT i=0; i<getEdgeCount(); i++)
      getEdge(i)->setColorStyle(colorStyle);

    delete m_imp->m_prop;
    m_imp->m_prop = 0;
  }
  */
}
Esempio n. 11
0
int PathGraph::findNode ( PathNodeType type, int key ) const
{
	int nodeCount = getNodeCount();

	for(int i = 0; i < nodeCount; i++)
	{
		PathNode const * node = getNode(i);

		if(getEdgeCount(i) == 0) continue;
		
		if((node != NULL) && (node->getType() == type) && (node->getKey() == key))
		{
			return i;
		}
	}

	return -1;
}
Esempio n. 12
0
/**

  get edge

  @param[in] idx Index of edge in graph
  @param[out] a index of 1st vertex
  @param[out] b index of second edge

  @return true if edge found

*/
bool cGraph::getEdge( int& iva, int& ivb, int idx )
{
	if( 0 > idx || idx >= getEdgeCount() )
		return false;

	edge_iter_t ei = edges(myGraph).first;

	/* For some reason this does not work! */
	//ei += idx;

	// We have to do this instead  ( http://stackoverflow.com/q/17001541/16582 )
	for( int k = 0; k < idx; k++ ) {
		ei++;
	}

	iva = source( *ei, myGraph );
	ivb = target( *ei, myGraph );

	return true;


}
Esempio n. 13
0
void PathGraph::findNodesInRange ( Vector const & position_p, float range, PathNodeList & results ) const
{
	float range2 = range * range;

	int nodeCount = getNodeCount();

	for(int i = 0; i < nodeCount; i++)
	{
		PathNode const * node = getNode(i);

		if(node == NULL) continue;
		
		if(getEdgeCount(i) == 0) continue;
		
		float dist2 = position_p.magnitudeBetweenSquared(node->getPosition_p());

		if(dist2 < range2)
		{
			results.push_back( const_cast<PathNode*>(node) );
		}
	}
}
Esempio n. 14
0
void TopologyDialog::getEdgeInfo()
{
    qDebug() << "get edge info";
    QByteArray datagram;
    QHostAddress address;

    QDataStream in(&datagram, QIODevice::ReadOnly);
    char msgRecv[256];
    char dstIp[256];
    char nextHop[256];
    int nread = 0;
    int count;

    do {
        datagram.resize(udpSocket->pendingDatagramSize());
        udpSocket->readDatagram(datagram.data(), datagram.size(), &address);

    } while(udpSocket->hasPendingDatagrams());
    nread = in.readRawData(msgRecv, 256);
    if (nread > 0) {
        msgRecv[nread ] = '\0';
        qDebug() << QString(msgRecv) << address.toString();
        count = getEdgeCount(msgRecv);
        for (int i = 0; i < count; i++) {
            sscanf(msgRecv + i*16, "%8s%8s", dstIp, nextHop);
            convertIp(dstIp);
            convertIp(nextHop);
            qDebug() << "dstIp:" << dstIp << "nextHop:" << nextHop;
            if (QString(nextHop) == "0.0.0.0") {
                addToGraph(address.toString(), dstIp);
            } else {
                addToGraph(address.toString(), nextHop);
            }

        }
    }
}
Esempio n. 15
0
void PathGraph::setPartTags ( void )
{
	static PathNodeStack unprocessed;

	int nodeCount = getNodeCount();

	for(int i = 0; i < nodeCount; i++)
	{
		PathNode * node = getNode(i);

		node->setPartId(-1);

		// ignore nodes without edges unless they're the only node in the cell

		int edgeCount = getEdgeCount(node->getIndex());

		if(edgeCount > 0)
		{
			unprocessed.push(node);
		}
		else
		{
			PathNodeType type = node->getType();

			if((nodeCount == 1) && (type == PNT_CellPortal))
			{
				unprocessed.push(node);
			}
		}
	}

	static PathNodeStack processing;

	int currentTag = 0;

	while(!unprocessed.empty())
	{
		PathNode * currentNode = unprocessed.top();
		unprocessed.pop();

		if(currentNode->getPartId() != -1) continue;

		processing.push(currentNode);

		while(!processing.empty())
		{
			PathNode * node = processing.top();
			processing.pop();

			if(node->getPartId() != -1) continue;

			node->setPartId(currentTag);

			int edgeCount = getEdgeCount(node->getIndex());

			for(int i = 0; i < edgeCount; i++)
			{
				int neighborIndex = getEdge(node->getIndex(),i)->getIndexB();

				PathNode * neighborNode = getNode(neighborIndex);

				processing.push(neighborNode);
			}
		}

		currentTag++;
	}

	m_partCount = currentTag;
}
Esempio n. 16
0
/* This test it to test my function getEdges, which is to extract edges 
 * from the 3rd party astar algorithm result */
char *test_astar_getedges() {
	ASPathNodeSource graph = {
		sizeof(Node),
		&buildNeighbors,
		&heuristic,
		NULL,
		NULL
	};

	NodeSet set;

	Node n0;
	Node n1;
	Node n2;
	Node n3;
	Node n4;
	Node *noderefs[5] = {&n0, &n1, &n2, &n3, &n4};

	Edge e0;
	Edge e1;
	Edge e2;
	Edge e3;
	Edge e4;
	Edge e5;

	Edge *oel0[2] = {&e0, &e1};
	Edge *oel1[2] = {&e4, &e5};
	Edge *oel2[1] = {&e2};
	Edge *oel3[1] = {&e3};
	Edge *oel4[1];

	Edge *iel0[1];
	Edge *iel1[1] = {&e0};
	Edge *iel2[2] = {&e1, &e5};
	Edge *iel3[1] = {&e4};
	Edge *iel4[2] = {&e2, &e3};

	ASPath path;

	n0.id = 0;
	n1.id = 1;
	n2.id = 2;
	n3.id = 3;
	n4.id = 4;

	n0.outdegree = 2;
	n1.outdegree = 2;
	n2.outdegree = 1;
	n3.outdegree = 1;
	n4.outdegree = 0;

	n0.indegree = 0;
	n1.indegree = 1;
	n2.indegree = 2;
	n3.indegree = 1;
	n4.indegree = 2;

	n0.outedges = oel0;
	n1.outedges = oel1;
	n2.outedges = oel2;
	n3.outedges = oel3;
	n4.outedges = oel4;

	n0.inedges = iel0;
	n1.inedges = iel1;
	n2.inedges = iel2;
	n3.inedges = iel3;
	n4.inedges = iel4;

	e0.id = 0;
	e1.id = 1;
	e2.id = 2;
	e3.id = 3;
	e4.id = 4;
	e5.id = 5;

	e0.length = 1;
	e1.length = 2;
	e2.length = 2;
	e3.length = 1;
	e4.length = 1;
	e5.length = 2;

	e0.head = &n0;
	e1.head = &n0;
	e2.head = &n2;
	e3.head = &n3;
	e4.head = &n1;
	e5.head = &n1;

	e0.tail = &n1;
	e1.tail = &n2;
	e2.tail = &n4;
	e3.tail = &n4;
	e4.tail = &n3;
	e5.tail = &n2;

	set.count = 5;
	set.refs = noderefs;

    path = ASPathCreate(&graph, &set, &n0, &n4);

	mu_assert("astar get edges: count incorrect ", getEdgeCount(path) == 3);
	mu_assert("astar get edges: total length incorrect", 
			equal(ASPathGetCost(path), 3));
	mu_assert("astar get edges: 3 incorrect ", getEdges(path)[2]->id == 3);

	path = ASPathCreate(&graph, &set, &n4, &n0);

	printf("%lf", ASPathGetCost(path));

    ASPathDestroy(path);

	return 0;

}
Esempio n. 17
0
double getGraphDensity(int ** M, int N) {
  return (double) ((2.0*getEdgeCount(M,N)) / (double)N);
}