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); } } } }
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; } }