bool NGRandomNetBuilder::createNewNode(NGNode* baseNode) { // calculate position of new node based on BaseNode SUMOReal dist = RandHelper::rand(myMinDistance, myMaxDistance); SUMOReal angle = RandHelper::rand((SUMOReal)(2 * M_PI)); SUMOReal x = baseNode->getPosition().x() + dist * cos(angle); SUMOReal y = baseNode->getPosition().y() + dist * sin(angle); NGNode* newNode = new NGNode(myNet.getNextFreeID()); newNode->setX(x); newNode->setY(y); newNode->setMaxNeighbours((SUMOReal) myNeighbourDistribution.num()); NGEdge* newLink = new NGEdge(myNet.getNextFreeID(), baseNode, newNode); if (canConnect(baseNode, newNode)) { // add node myNet.add(newNode); myOuterNodes.push_front(newNode); // add link myNet.add(newLink); myOuterLinks.push_back(newLink); // check basenode for being outer node if (baseNode->LinkList.size() >= baseNode->getMaxNeighbours()) { removeOuterNode(baseNode); } return true; } else { delete newNode; return false; } }
void NGNet::createSpiderWeb(int numRadDiv, int numCircles, SUMOReal spaceRad, bool hasCenter) { if (numRadDiv < 3) { numRadDiv = 3; } if (numCircles < 1) { numCircles = 1; } int ir, ic; SUMOReal angle = (SUMOReal)(2 * M_PI / numRadDiv); // angle between radial divisions NGNode* Node; for (ir = 1; ir < numRadDiv + 1; ir++) { for (ic = 1; ic < numCircles + 1; ic++) { // create Node Node = new NGNode( toString<int>(ir) + "/" + toString<int>(ic), ir, ic); Node->setX(radialToX((ic) * spaceRad, (ir - 1) * angle)); Node->setY(radialToY((ic) * spaceRad, (ir - 1) * angle)); myNodeList.push_back(Node); // create Links if (ir > 1) { connect(Node, findNode(ir - 1, ic)); } if (ic > 1) { connect(Node, findNode(ir, ic - 1)); } if (ir == numRadDiv) { connect(Node, findNode(1, ic)); } } } if (hasCenter) { // node Node = new NGNode(getNextFreeID(), 0, 0, true); Node->setX(0); Node->setY(0); myNodeList.push_back(Node); // links for (ir = 1; ir < numRadDiv + 1; ir++) { connect(Node, findNode(ir, 1)); } } }
bool NGRandomNetBuilder::checkAngles(NGNode* node) { bool check = true; if (node->LinkList.size() > 1) { // loop over all links NGEdgeList::iterator li; NGNode* ni; for (li = node->LinkList.begin(); li != node->LinkList.end(); ++li) { // calc vector of currentnode if ((*li)->getStartNode() == node) { ni = (*li)->getEndNode(); } else { ni = (*li)->getStartNode(); } Position v1( ni->getPosition().x() - node->getPosition().x(), ni->getPosition().y() - node->getPosition().y()); // loop over all links NGEdgeList::iterator lj; for (lj = node->LinkList.begin(); lj != node->LinkList.end(); ++lj) { if (li != lj) { if ((*lj)->getStartNode() == node) { ni = (*lj)->getEndNode(); } else { ni = (*lj)->getStartNode(); } Position v2( ni->getPosition().x() - node->getPosition().x(), ni->getPosition().y() - node->getPosition().y()); if (fabs(GeomHelper::angle2D(v1, v2)) < myMinLinkAngle) { check = false; } } } } } return check; }
void NGRandomNetBuilder::createNet(int numNodes) { myNumNodes = numNodes; NGNode* outerNode = new NGNode(myNet.getNextFreeID()); outerNode->setX(0); outerNode->setY(0); outerNode->setMaxNeighbours(4); myNet.add(outerNode); myOuterNodes.push_back(outerNode); bool created = true; while (((int) myNet.nodeNo() < numNodes) && (myOuterNodes.size() > 0)) { // brings last element to front if (!created) { myOuterNodes.push_front(myOuterNodes.back()); myOuterNodes.pop_back(); } outerNode = myOuterNodes.back(); findPossibleOuterNodes(outerNode); created = false; if ((myConNodes.size() > 0) && (RandHelper::rand() < myConnectivity)) { // create link NGEdge* newLink = new NGEdge(myNet.getNextFreeID(), outerNode, myConNodes.back()); if (canConnect(outerNode, myConNodes.back())) { // add link myNet.add(newLink); myOuterLinks.push_back(newLink); // check nodes for being outer node if (outerNode->LinkList.size() >= outerNode->getMaxNeighbours()) { removeOuterNode(outerNode); } if (myConNodes.back()->LinkList.size() >= myConNodes.back()->getMaxNeighbours()) { removeOuterNode(myConNodes.back()); } created = true; } else { delete newLink; } } else { int count = 0; do { created = createNewNode(outerNode); count++; } while ((count <= myNumTries) && !created); if (!created) { outerNode->setMaxNeighbours((SUMOReal) outerNode->LinkList.size()); myOuterNodes.remove(outerNode); } } } }
void NGNet::createChequerBoard(int numX, int numY, SUMOReal spaceX, SUMOReal spaceY, SUMOReal attachLength) { for (int ix = 0; ix < numX; ix++) { for (int iy = 0; iy < numY; iy++) { // create Node std::string nodeID = toString<int>(ix) + "/" + toString<int>(iy); NGNode* node = new NGNode(nodeID, ix, iy); node->setX(ix * spaceX + attachLength); node->setY(iy * spaceY + attachLength); myNodeList.push_back(node); // create Links if (ix > 0) { connect(node, findNode(ix - 1, iy)); } if (iy > 0) { connect(node, findNode(ix, iy - 1)); } } } if (attachLength > 0.0) { for (int ix = 0; ix < numX; ix++) { // create nodes NGNode* topNode = new NGNode("top" + toString<int>(ix), ix, numY); NGNode* bottomNode = new NGNode("bottom" + toString<int>(ix), ix, numY + 1); topNode->setX(ix * spaceX + attachLength); bottomNode->setX(ix * spaceX + attachLength); topNode->setY((numY - 1) * spaceY + 2 * attachLength); bottomNode->setY(0); myNodeList.push_back(topNode); myNodeList.push_back(bottomNode); // create links connect(topNode, findNode(ix, numY - 1)); connect(bottomNode, findNode(ix, 0)); } for (int iy = 0; iy < numY; iy++) { // create nodes NGNode* leftNode = new NGNode("left" + toString<int>(iy), numX, iy); NGNode* rightNode = new NGNode("right" + toString<int>(iy), numX + 1, iy); leftNode->setX(0); rightNode->setX((numX - 1) * spaceX + 2 * attachLength); leftNode->setY(iy * spaceY + attachLength); rightNode->setY(iy * spaceY + attachLength); myNodeList.push_back(leftNode); myNodeList.push_back(rightNode); // create links connect(leftNode, findNode(0, iy)); connect(rightNode, findNode(numX - 1, iy)); } } }