Graph.cpp

Go to the documentation of this file.
00001 //------------------------------------------------------------------------------ 00004 00005 // Last Change: 2004/06/14 00006 // Version: 0.5 00007 00008 //---------------------------------------------------------------------includes- 00009 #include <math.h> 00010 #include "../src/Graph.h" 00011 #include "../src/main.h" 00012 #include "../src/Vector3D.h" 00013 00014 #define PI 3.14159265358979323846 00015 00016 //------------------------------------------------------------------createGraph- 00017 // 00018 // This creates a graph from a file. 00019 // @param file_name The file name of the file which contains the graph to be 00020 // build. 00021 // @pre file_name != "" 00022 // 00023 void Graph::createGraph(char *file_name) 00024 { 00025 //=========================================================================TEST= 00026 // 00027 // It's just a test proposal. 00028 // - Here we create a static Graph and test for displaying and other 00029 // calculations. 00030 00031 // // the test graph should have 6 nodes 00032 // Node *node_01 = new Node(); 00033 // Node *node_02 = new Node(); 00034 // Node *node_03 = new Node(); 00035 // Node *node_04 = new Node(); 00036 // Node *node_05 = new Node(); 00037 // Node *node_06 = new Node(); 00038 // Node *node_07 = new Node(); 00039 // Node *node_08 = new Node(); 00040 // Node *node_09 = new Node(); 00041 // Node *node_10 = new Node(); 00042 // Node *node_11 = new Node(); 00047 // // set the node id's 00048 // 00049 // node_01->setID(1); 00050 // node_02->setID(2); 00051 // node_03->setID(3); 00052 // node_04->setID(4); 00053 // node_05->setID(5); 00054 // node_06->setID(6); 00055 // node_07->setID(7); 00056 // node_08->setID(8); 00057 // node_09->setID(9); 00058 // node_10->setID(10); 00059 // node_11->setID(11); 00064 // 00065 // // set caption of the nodes 00066 // node_01->setCaption(&string("1")); 00067 // node_02->setCaption(&string("2")); 00068 // node_03->setCaption(&string("3")); 00069 // node_04->setCaption(&string("4")); 00070 // node_05->setCaption(&string("5")); 00071 // node_06->setCaption(&string("6")); 00072 // node_07->setCaption(&string("7")); 00073 // node_08->setCaption(&string("8")); 00074 // node_09->setCaption(&string("9")); 00075 // node_10->setCaption(&string("10")); 00076 // node_11->setCaption(&string("11")); 00081 // 00082 // // set the adjacents of the graph 00083 // node_01->setOneAdjacentNode(node_02); 00084 // node_02->setOneAdjacentNode(node_01); 00085 // node_02->setOneAdjacentNode(node_03); 00086 // node_03->setOneAdjacentNode(node_02); 00087 // node_03->setOneAdjacentNode(node_04); 00088 // node_04->setOneAdjacentNode(node_03); 00089 // node_04->setOneAdjacentNode(node_05); 00090 // node_04->setOneAdjacentNode(node_06); 00091 // node_05->setOneAdjacentNode(node_04); 00092 // node_06->setOneAdjacentNode(node_04); 00093 // 00094 // node_07->setOneAdjacentNode(node_08); 00095 // node_07->setOneAdjacentNode(node_01); 00096 // node_01->setOneAdjacentNode(node_07); 00097 // node_08->setOneAdjacentNode(node_07); 00098 // node_08->setOneAdjacentNode(node_09); 00099 // node_09->setOneAdjacentNode(node_08); 00100 // node_10->setOneAdjacentNode(node_03); 00101 // node_03->setOneAdjacentNode(node_10); 00102 // 00103 // node_09->setOneAdjacentNode(node_11); 00104 // node_11->setOneAdjacentNode(node_09); 00107 // 00108 // // insert all nodes into nodes list 00109 // setOneNodeIntoGraph(node_01); 00110 // setOneNodeIntoGraph(node_02); 00111 // setOneNodeIntoGraph(node_03); 00112 // setOneNodeIntoGraph(node_04); 00113 // setOneNodeIntoGraph(node_05); 00114 // setOneNodeIntoGraph(node_06); 00115 // setOneNodeIntoGraph(node_07); 00116 // setOneNodeIntoGraph(node_08); 00117 // setOneNodeIntoGraph(node_09); 00118 // setOneNodeIntoGraph(node_10); 00119 // setOneNodeIntoGraph(node_11); 00121 00122 // 00123 //=========================================================================TEST= 00124 00125 //========================================================================TEST2= 00126 // 00127 00128 // the test graph should have 68 nodes 00129 Node *node_01 = new Node(); 00130 Node *node_02 = new Node(); 00131 Node *node_03 = new Node(); 00132 Node *node_04 = new Node(); 00133 Node *node_05 = new Node(); 00134 Node *node_06 = new Node(); 00135 Node *node_07 = new Node(); 00136 Node *node_08 = new Node(); 00137 Node *node_09 = new Node(); 00138 Node *node_10 = new Node(); 00139 Node *node_11 = new Node(); 00140 Node *node_12 = new Node(); 00141 Node *node_13 = new Node(); 00142 Node *node_14 = new Node(); 00143 Node *node_15 = new Node(); 00144 Node *node_16 = new Node(); 00145 Node *node_17 = new Node(); 00146 Node *node_18 = new Node(); 00147 Node *node_19 = new Node(); 00148 Node *node_20 = new Node(); 00149 Node *node_21 = new Node(); 00150 Node *node_22 = new Node(); 00151 Node *node_23 = new Node(); 00152 Node *node_24 = new Node(); 00153 Node *node_25 = new Node(); 00154 Node *node_26 = new Node(); 00155 Node *node_27 = new Node(); 00156 00157 // set caption of the nodes 00158 node_01->setCaption("1"); 00159 node_02->setCaption("2"); 00160 node_03->setCaption("3"); 00161 node_04->setCaption("4"); 00162 node_05->setCaption("5"); 00163 node_06->setCaption("6"); 00164 node_07->setCaption("7"); 00165 node_08->setCaption("8"); 00166 node_09->setCaption("9"); 00167 node_10->setCaption("10"); 00168 node_11->setCaption("11"); 00169 node_12->setCaption("12"); 00170 node_13->setCaption("13"); 00171 node_14->setCaption("14"); 00172 node_15->setCaption("15"); 00173 node_16->setCaption("16"); 00174 node_17->setCaption("17"); 00175 node_18->setCaption("18"); 00176 node_19->setCaption("19"); 00177 node_20->setCaption("20"); 00178 node_21->setCaption("21"); 00179 node_22->setCaption("22"); 00180 node_23->setCaption("23"); 00181 node_24->setCaption("24"); 00182 node_25->setCaption("25"); 00183 node_26->setCaption("26"); 00184 node_27->setCaption("27"); 00185 00186 // set ID of the nodes 00187 node_01->setID(1); 00188 node_02->setID(2); 00189 node_03->setID(3); 00190 node_04->setID(4); 00191 node_05->setID(5); 00192 node_06->setID(6); 00193 node_07->setID(7); 00194 node_08->setID(8); 00195 node_09->setID(9); 00196 node_10->setID(10); 00197 node_11->setID(11); 00198 node_12->setID(12); 00199 node_13->setID(13); 00200 node_14->setID(14); 00201 node_15->setID(15); 00202 node_16->setID(16); 00203 node_17->setID(17); 00204 node_18->setID(18); 00205 node_19->setID(19); 00206 node_20->setID(20); 00207 node_21->setID(21); 00208 node_22->setID(22); 00209 node_23->setID(23); 00210 node_24->setID(24); 00211 node_25->setID(25); 00212 node_26->setID(26); 00213 node_27->setID(27); 00214 00215 // insert all nodes into nodes list 00216 setOneNodeIntoGraph(node_01); 00217 setOneNodeIntoGraph(node_02); 00218 setOneNodeIntoGraph(node_03); 00219 setOneNodeIntoGraph(node_04); 00220 setOneNodeIntoGraph(node_05); 00221 setOneNodeIntoGraph(node_06); 00222 setOneNodeIntoGraph(node_07); 00223 setOneNodeIntoGraph(node_08); 00224 setOneNodeIntoGraph(node_09); 00225 setOneNodeIntoGraph(node_10); 00226 setOneNodeIntoGraph(node_11); 00227 setOneNodeIntoGraph(node_12); 00228 setOneNodeIntoGraph(node_13); 00229 setOneNodeIntoGraph(node_14); 00230 setOneNodeIntoGraph(node_15); 00231 setOneNodeIntoGraph(node_16); 00232 setOneNodeIntoGraph(node_17); 00233 setOneNodeIntoGraph(node_18); 00234 setOneNodeIntoGraph(node_19); 00235 setOneNodeIntoGraph(node_20); 00236 setOneNodeIntoGraph(node_21); 00237 setOneNodeIntoGraph(node_22); 00238 setOneNodeIntoGraph(node_23); 00239 setOneNodeIntoGraph(node_24); 00240 setOneNodeIntoGraph(node_25); 00241 setOneNodeIntoGraph(node_26); 00242 setOneNodeIntoGraph(node_27); 00243 00244 // set the adjacents of the graph 00245 node_01->setOneAdjacentNode(node_08); 00246 node_08->setOneAdjacentNode(node_01); 00247 node_02->setOneAdjacentNode(node_10); 00248 node_10->setOneAdjacentNode(node_02); 00249 node_03->setOneAdjacentNode(node_10); 00250 node_10->setOneAdjacentNode(node_03); 00251 node_04->setOneAdjacentNode(node_13); 00252 node_13->setOneAdjacentNode(node_04); 00253 node_05->setOneAdjacentNode(node_13); 00254 node_13->setOneAdjacentNode(node_05); 00255 node_06->setOneAdjacentNode(node_13); 00256 node_13->setOneAdjacentNode(node_06); 00257 00258 node_07->setOneAdjacentNode(node_18); 00259 node_18->setOneAdjacentNode(node_07); 00260 node_08->setOneAdjacentNode(node_18); 00261 node_18->setOneAdjacentNode(node_08); 00262 node_09->setOneAdjacentNode(node_19); 00263 node_19->setOneAdjacentNode(node_09); 00264 node_10->setOneAdjacentNode(node_19); 00265 node_19->setOneAdjacentNode(node_10); 00266 node_11->setOneAdjacentNode(node_19); 00267 node_19->setOneAdjacentNode(node_11); 00268 node_12->setOneAdjacentNode(node_20); 00269 node_20->setOneAdjacentNode(node_12); 00270 node_13->setOneAdjacentNode(node_20); 00271 node_20->setOneAdjacentNode(node_13); 00272 node_14->setOneAdjacentNode(node_22); 00273 node_22->setOneAdjacentNode(node_14); 00274 node_15->setOneAdjacentNode(node_22); 00275 node_22->setOneAdjacentNode(node_15); 00276 node_16->setOneAdjacentNode(node_22); 00277 node_22->setOneAdjacentNode(node_16); 00278 node_17->setOneAdjacentNode(node_22); 00279 node_22->setOneAdjacentNode(node_17); 00280 00281 node_18->setOneAdjacentNode(node_24); 00282 node_24->setOneAdjacentNode(node_18); 00283 node_19->setOneAdjacentNode(node_24); 00284 node_24->setOneAdjacentNode(node_19); 00285 node_20->setOneAdjacentNode(node_25); 00286 node_25->setOneAdjacentNode(node_20); 00287 node_21->setOneAdjacentNode(node_25); 00288 node_25->setOneAdjacentNode(node_21); 00289 node_22->setOneAdjacentNode(node_25); 00290 node_25->setOneAdjacentNode(node_22); 00291 node_23->setOneAdjacentNode(node_26); 00292 node_26->setOneAdjacentNode(node_23); 00293 node_24->setOneAdjacentNode(node_27); 00294 00295 node_27->setOneAdjacentNode(node_24); 00296 node_25->setOneAdjacentNode(node_27); 00297 node_27->setOneAdjacentNode(node_25); 00298 node_26->setOneAdjacentNode(node_27); 00299 node_27->setOneAdjacentNode(node_26); 00300 00301 // 00302 //========================================================================TEST2= 00303 // make a deep copy of the graph 00304 makeDeepCopyOfGraph(&nodes_copy_); 00305 00306 makeDeepCopyOfGraph(&nodes_end_pos_); 00307 // clear all nodes 00308 getCentreNodeOfGraph(&nodes_copy_); 00309 00310 vector<Node*>::iterator iter = nodes_.begin(); 00311 int count = 0; 00312 while (iter != nodes_.end()) 00313 { 00314 // cout << count << endl; 00315 count++; 00316 if ((*iter)->getID() == nodes_copy_.at(0)->getID()) 00317 { 00318 break; 00319 } 00320 ++iter; 00321 } 00322 // the real graph centre!! 00323 centroid_ = (*iter); 00324 // cout << "GRAPH-CENTRE: " << centroid_->getID() << endl; 00325 // set the start position of the centroid and the node wich has the most 00326 // number of leafs in it's sub-tree 00327 (*iter)->setPointVector(new Vector3D(0.0f, 0.0f, 0.0f, 0.0f)); 00328 00329 // the centroid will recieve the whole angular width available 00330 (*iter)->setAngularWidth(360.0f); 00331 00332 // calcualte the angular widths for all nodes in the graph: firstly we take 00333 // all nodes with degree 1 and propagate +1 to other adjacent nodes 00334 //calcAngularWidths(&nodes_); 00335 updateNumOfLeafs(&nodes_); 00336 00337 radial_layout_->initRadiusOfRings(10); 00338 initStartPositions(*iter, *iter); 00339 00340 calcCartesianCoordinates(&nodes_); 00341 } 00342 00343 //----------------------------------------------------------makeDeepCopyOfGraph- 00344 // 00345 // This copies (really) all elemens of a graph. 00346 // 00347 void Graph::makeDeepCopyOfGraph(vector<Node*> *nodes_copy) 00348 { 00349 vector<Node*>::iterator iter_old = nodes_.begin(); 00350 // go through the old graph and assign the "same" Nodes (i.e. copies) to the 00351 // copied graph 00352 while (iter_old != nodes_.end()) 00353 { 00354 nodes_copy->push_back(new Node(*iter_old)); 00355 ++iter_old; 00356 } 00357 00358 // go through the adjacent list and link each adjacent node with the one 00359 iter_old = nodes_.begin(); 00360 vector<Node*>::iterator iter_new = nodes_copy->begin(); 00361 while (iter_new != nodes_copy->end()) 00362 { 00363 vector<Node*>::iterator iter_adj = 00364 (*iter_old)->getListOfAdjacentNodes()->begin(); 00365 // copy the adjacent list 00366 while (iter_adj != (*iter_old)->getListOfAdjacentNodes()->end()) 00367 { 00368 // this loop goes through the copied nodes vector and searches for the 00369 // right adjacent node 00370 vector<Node*>::iterator iter_new_2 = nodes_copy->begin(); 00371 while (iter_new_2 != nodes_copy->end()) 00372 { 00373 if ((*iter_adj)->getID() == (*iter_new_2)->getID()) 00374 { 00375 (*iter_new)->setOneAdjacentNode(*iter_new_2); 00376 } 00377 ++iter_new_2; 00378 } 00379 ++iter_adj; 00380 } 00381 ++iter_old; 00382 ++iter_new; 00383 } 00384 00385 vector<Node*>::iterator iter_new_3 = nodes_copy->begin(); 00386 while (iter_new_3 != nodes_copy->end()) 00387 { 00388 vector<Node*>::iterator iter_adj_2 = 00389 (*iter_new_3)->getListOfAdjacentNodes()->begin(); 00390 while (iter_adj_2 != (*iter_new_3)->getListOfAdjacentNodes()->end()) 00391 { 00392 ++iter_adj_2; 00393 } 00394 cout << endl; 00395 ++iter_new_3; 00396 } 00397 } 00398 00399 //---------------------------------------------------------getCentreNodeOfGraph- 00400 // 00401 // This finds the longest path of the graph and returns the centred node. We are 00402 // using the simple algorithm on @link 00403 // http://www.personal.kent.edu/~rmuhamma/GraphTheory/MyGraphTheory/trees.htm 00404 // @param nodes The list of nodes. 00405 // @pre nodes_ != empty 00406 // 00407 void Graph::getCentreNodeOfGraph(vector<Node*> *nodes) 00408 { 00409 // We go through the adjacents list and check whether a node has a degree 00410 // 1. If so we delete him. 00411 vector<Node*>::iterator iter_1 = nodes->begin(); 00412 vector<Node*> temp_nodes; 00413 // this loop findes all nodes in current 'nodes'-vector with degree 1 00414 // which will be deleted 00415 while (iter_1 != nodes->end()) 00416 { 00417 if ((*iter_1)->getDegree() == 1) 00418 { 00419 // store the nodes with degree 1 at current iteration 00420 temp_nodes.push_back(*iter_1); 00421 } 00422 ++iter_1; 00423 } 00424 vector<Node*>::iterator iter_2 = temp_nodes.begin(); 00425 // this loop deletes all nodes with degree 1 for one iteration 00426 while (iter_2 != temp_nodes.end()) 00427 { 00428 iter_1 = nodes->begin(); 00429 while (iter_1 != nodes->end()) 00430 { 00431 // leave at least one node in array 'nodes' 00432 if ((*iter_1 == *iter_2) & (nodes->size() != 1)) 00433 { 00434 // erase the current node out of the graph (i.e. so that there will be 00435 // no other link to this node) 00436 (*iter_1)->eraseNode(*iter_1); 00437 // erase the current node with degree 1 00438 nodes->erase(iter_1); 00439 --iter_1; 00440 } 00441 ++iter_1; 00442 } 00443 ++iter_2; 00444 } 00445 00446 if (nodes->size() != 1) 00447 { 00448 getCentreNodeOfGraph(nodes); 00449 } 00450 } 00451 00452 //----------------------------------------------------------setOneNodeIntoGraph- 00453 // 00454 // This sets one node into the graph. 00455 // @param node The node to set. 00456 // 00457 void Graph::setOneNodeIntoGraph(Node *node) 00458 { 00459 nodes_.push_back(node); 00460 } 00461 00462 //-----------------------------------------------------------initStartPositions- 00463 // 00464 // This initializes the start positions of the nodes. 00465 // @param centroid The centroid of the graph. 00466 // 00467 void Graph::initStartPositions(Node *centroid, Node *predecessor) 00468 { 00469 // 90 00470 // --|-- 00471 // / | \ 00472 // 180 |-------| 0=360 00473 // \ | / 00474 // --|-- 00475 // 270 00476 00477 // sum the number of leafs which are reachable from the root 00478 GLuint overall_num_leafs = 0; 00479 vector<GLuint>::iterator iter_leaf = 00480 (centroid->getListOfLeafeNodes())->begin(); 00481 vector<Node*>::iterator iter = (centroid->getListOfAdjacentNodes())->begin(); 00482 while (iter_leaf != (centroid->getListOfLeafeNodes())->end()) 00483 { 00484 if ((*iter)->getID() != predecessor->getID()) 00485 { 00486 overall_num_leafs += (*iter)->getNumLeafs(centroid); 00487 } 00488 ++iter_leaf; 00489 ++iter; 00490 } 00491 00492 // set radius of the ring to the centroid 00493 centroid->setRing(num_ring_); 00494 GLfloat radius = radial_layout_->getRadiusOfRing(num_ring_); 00495 centroid->setRadius(radius); 00496 00497 // go through the adjacent list and assign the angular width to each 00498 // neighbour 00499 // find the node with the most number of leafs in it's sub-tree and 00500 // re-orientate others on that node 00501 vector<Node*>::iterator iter_greatest_angular_width = 00502 (centroid->getListOfAdjacentNodes())->begin(); 00503 00504 // initialize one element so that the following elements do have an starting 00505 // orientation 00506 iter = (centroid->getListOfAdjacentNodes())->begin(); 00507 iter_leaf = (centroid->getListOfLeafeNodes())->begin(); 00508 while (iter != (centroid->getListOfAdjacentNodes())->end()) 00509 { 00510 if ((*iter)->getID() != predecessor->getID()) 00511 { 00512 GLfloat temp = (*iter)->getNumLeafs(centroid); // DON'T FORGET!! 00513 GLfloat ang_width = (temp / overall_num_leafs) * 00514 (centroid->getAngularWidth()); 00515 (*iter)->setAngularWidth(ang_width); 00516 00517 if (ang_width > (*iter_greatest_angular_width)->getAngularWidth()) 00518 { 00519 ++iter_greatest_angular_width; 00520 } 00521 } 00522 ++iter_leaf; 00523 ++iter; 00524 } 00525 00526 // set the angle of the centroid 00527 centroid->setAngle((centroid->getAngularWidth() / 2) 00528 + centroid->getAngleOffset()); 00529 00530 // go through the adjacent list and assignt the angle values to each node 00531 iter = (centroid->getListOfAdjacentNodes())->begin(); 00532 GLfloat curr_angle = 0; 00533 while (iter != (centroid->getListOfAdjacentNodes())->end()) 00534 { 00535 if ((*iter)->getID() != predecessor->getID()) 00536 { 00537 (*iter)->addAngleOffset(centroid->getAngleOffset()); 00538 ++num_ring_; 00539 initStartPositions(*iter, centroid); 00540 --num_ring_; 00541 } 00542 ++iter; 00543 } 00544 predecessor->addAngleOffset(centroid->getAngularWidth()); 00545 00546 if (centroid->getID() == predecessor->getID()) 00547 { 00548 centroid->setAngle(0.0f); 00549 return; 00550 } 00551 } 00552 00553 //-------------------------------------------------------------updateNumOfLeafs- 00554 // 00555 // This assigns the right number of the leafs which are reachable from a given 00556 // node. 00557 // @param node The node from which we want to reach the leafs. 00558 // 00559 void Graph::updateNumOfLeafs(vector<Node*> *nodes) 00560 { 00561 // the number of leafs for current subtree: find the number of nodes with 00562 // degree 1 (i.e. a leaf) 00563 vector<Node*>::iterator iter = nodes->begin(); 00564 while (iter != nodes->end()) 00565 { 00566 // update the angular width 00567 if ((*iter)->getDegree() == 1) 00568 { 00569 (*iter)->incrNumOfLeafs(*iter); 00570 } 00571 ++iter; 00572 } 00573 } 00574 //-----------------------------------------------------calcCartesianCoordinates- 00575 // 00576 // This calculates the Cartesian coordinates. 00577 // @param nodes The nodes to calculate the Cartesian coordinates. 00578 // 00579 void Graph::calcCartesianCoordinates(vector<Node*> *nodes) 00580 { 00581 // convert polar coordinates into cartesian coordinates 00582 vector<Node*>::iterator iter = nodes->begin(); 00583 while (iter != nodes->end()) 00584 { 00585 GLfloat x_coord = 0; 00586 GLfloat y_coord = 0; 00587 x_coord = (*iter)->getRadius() * cos((PI / 180) * (*iter)->getAngle()); 00588 y_coord = (*iter)->getRadius() * sin((PI / 180) * (*iter)->getAngle()); 00589 00590 (*iter)->setPointVector(new Vector3D(x_coord, y_coord, 0.0f, 0.0f)); 00591 ++iter; 00592 } 00593 } 00594 00595 //---------------------------------------------------------------------drawNode- 00596 // 00597 // This draws the graph into an OpenGL viewport. 00598 // @param quadric The quadric which represents the nodes for drawing. 00599 // 00600 void Graph::drawGraph(GLUquadricObj *quadric) 00601 { 00602 calcCartesianCoordinates(&nodes_); 00603 // convert to cartesian coordinates 00604 GLfloat mat_ambient[] = {0.0f, 0.0f, 1.0f, 1.0f}; 00605 GLfloat mat_diffuse[] = {0.0f, 0.0f, 0.8f, 1.0f}; 00606 GLfloat light_position[] = {0.0f, -1.0f, -1.0f, 1.0}; 00607 GLfloat shininess[] = {0.5f}; 00608 00609 glMaterialfv( GL_FRONT, GL_AMBIENT, mat_ambient); 00610 glMaterialfv( GL_FRONT, GL_DIFFUSE, mat_diffuse); 00611 glMaterialfv( GL_FRONT, GL_SHININESS, shininess); 00612 glLightfv(GL_LIGHT0, GL_POSITION, light_position); 00613 00614 glEnable(GL_LIGHTING); 00615 glEnable(GL_LIGHT0); 00616 glEnable(GL_COLOR_MATERIAL); 00617 00618 // draw lines first 00619 glPushMatrix(); 00620 drawLine(); 00621 glPopMatrix(); 00622 00623 if (last_animation_speed_ - (*animation_speed_) != 0) 00624 { 00625 node_animation_->setNodeAnimationSpeed(animation_speed_); 00626 last_animation_speed_ = (*animation_speed_); 00627 num_iterations_ = 1; 00628 } 00629 00630 vector<Node*>::iterator iter = nodes_.begin(); 00631 vector<Node*>::iterator iter_2 = nodes_end_pos_.begin(); 00632 while (iter != nodes_.end()) 00633 { 00634 glLoadName((*iter)->getID()); 00635 glBegin(GL_LINES); 00636 glEnd(); 00637 (*iter)->setSceneRotation(x_rotation_, y_rotation_, z_rotation_); 00638 if (is_animation_running_ == true) 00639 { 00640 // getNewEndRadius, getNewEndAngle 00641 node_animation_->interpolatePolarCoord((*iter), (*iter_2)->getRadius(), 00642 (*iter_2)->getAngle(), 00643 num_iterations_); 00644 node_animation_->calculateNodeRadiusAndColor((*iter), 00645 (*iter)->getRadius()); 00646 } 00647 else 00648 { 00649 node_animation_->calculateNodeRadiusAndColor((*iter), 00650 (*iter)->getRadius()); 00651 } 00652 glPushMatrix(); 00653 (*iter)->drawNode(quadric, render_mode_, caption_mode_); 00654 glPopMatrix(); 00655 glEnd(); 00656 ++iter; 00657 ++iter_2; 00658 } 00659 } 00660 00661 //---------------------------------------------------------------------drawLine- 00662 // 00663 // This draws the Lines between the adjacent nodes. 00664 // 00665 void Graph::drawLine() 00666 { 00667 vector<Node*>::iterator iter_1 = nodes_.begin(); 00668 while (iter_1 != nodes_.end()) 00669 { 00670 GLfloat x = (*iter_1)->getPointVector()->getXCoord(); 00671 GLfloat y = (*iter_1)->getPointVector()->getYCoord(); 00672 00673 vector<Node*>::iterator iter_2 = 00674 (*iter_1 )->getListOfAdjacentNodes()->begin(); 00675 while (iter_2 != (*iter_1 )->getListOfAdjacentNodes()->end()) 00676 { 00677 GLfloat x_adj = (*iter_2)->getPointVector()->getXCoord(); 00678 GLfloat y_adj = (*iter_2)->getPointVector()->getYCoord(); 00679 glColor4f(0.6f, 0.8f, 1.0f, 0.5f); 00680 glEnable(GL_BLEND); 00681 glLineWidth(1.0f); 00682 00683 glBegin(GL_LINES); 00684 glVertex3f(x, y, 0.0); 00685 glVertex3f(x_adj, y_adj, 0.0); 00686 glEnd(); 00687 glLineWidth(1.0f); 00688 glDisable(GL_BLEND); 00689 glColor3f(0.0f, 0.0f, 0.0f); 00690 ++iter_2; 00691 } 00692 ++iter_1; 00693 } 00694 } 00695 00696 //----------------------------------------------------------------setCenterNode- 00697 // 00698 // This sets the selected node as the new focud node. 00699 // @param center_node The node which was select from the user to be the center 00700 // of the graph. 00701 // 00702 void Graph::setCenterNode(GLuint id) 00703 { 00704 vector<Node*>::iterator iter = nodes_.begin(); 00705 while (iter != nodes_.end()) 00706 { 00707 if ((*iter)->getID() == id) 00708 { 00709 centroid_ = (*iter); 00710 } 00711 ++iter; 00712 } 00713 } 00714 00715 //----------------------------------------------------------------isNodeClicked- 00716 // 00717 // Checks wether a graph's node has been selected. 00718 // @return Returns true if a node of the graph has been selected. 00719 // 00720 bool Graph::isANodeClicked(GLuint object_id) 00721 { 00722 bool return_flag = false; 00723 vector<Node*>::iterator iter = nodes_.begin(); 00724 00725 iter = nodes_end_pos_.begin(); 00726 while (iter != nodes_end_pos_.end()) 00727 { 00728 if ((*iter)->getID() == object_id) 00729 { 00730 // reset all relevant member variables of each node to default values 00731 vector<Node*>::iterator iter_2 = nodes_end_pos_.begin(); 00732 while (iter_2 != nodes_end_pos_.end()) 00733 { 00734 (*iter_2)->resetMembers(); 00735 ++iter_2; 00736 } 00737 00738 (*iter)->setPointVector(new Vector3D(0.0f, 0.0f, 0.0f, 0.0f)); 00739 (*iter)->setAngularWidth(360.0f); 00740 updateNumOfLeafs(&nodes_end_pos_); 00741 num_ring_ = 0; 00742 00743 initStartPositions((*iter), (*iter)); 00744 calcCartesianCoordinates(&nodes_end_pos_); 00745 return_flag = true; 00746 break; 00747 } 00748 ++iter; 00749 } 00750 00751 if (return_flag == true) 00752 { 00753 return(true); 00754 } 00755 else 00756 { 00757 return(false); 00758 } 00759 } 00760 00761 //-------------------------------------------------------------resetAngleOffset- 00762 // 00763 // Resets the angle offset of each node to the specified value. 00764 // @param nodes The graph which nodes' angle offset has to be set. 00765 // @param angle_offset The angle offset to set for each node of the graph. 00766 // 00767 void Graph::resetAngleOffsetTo(vector<Node*> *nodes, GLfloat angle_offset) 00768 { 00769 vector<Node*>::iterator iter = nodes->begin(); 00770 while (iter != nodes->end()) 00771 { 00772 GLfloat curr_angle_offset = (*iter)->getAngleOffset(); 00773 (*iter)->addAngleOffset(-curr_angle_offset + angle_offset); 00774 ++iter; 00775 } 00776 } 00777 00778 //------------------------------------------------------highlightNodeIfSelected- 00779 // 00780 // Highlights the node with the given ID if the node has been selected. 00781 // @param node_id The ID of the node. 00782 // 00783 void Graph::highlightNodeIfSelected(GLuint node_id) 00784 { 00785 vector<Node*>::iterator iter = nodes_.begin(); 00786 while (iter != nodes_.end()) 00787 { 00788 // set all other nodes not-selected 00789 (*iter)->setSelected(false); 00790 if ((*iter)->getID() == node_id) 00791 { 00792 (*iter)->setSelected(true); 00793 } 00794 ++iter; 00795 } 00796 } 00797 00798 //-------------------------------------------------------------setAnimationMode- 00803 void Graph::setAnimationMode(bool animation_mode) 00804 { 00805 animation_mode_ = animation_mode; 00806 if (animation_mode_ == true) 00807 { 00808 GLint random_center = (GLint)(rand()% nodes_.size()); 00809 if (random_center != 0) 00810 { 00811 cout <<"New Centroid: " << random_center << endl; 00812 highlightNodeIfSelected(random_center); 00813 isANodeClicked(random_center); 00814 setCenterNode(random_center); 00815 } 00816 } 00817 } 00818 00819 //------------------------------------------------------------toggleRenderMode- 00820 // 00821 // This sets the render mode. 00822 // 00823 void Graph::toggleRenderMode() 00824 { 00825 render_mode_ == true ? render_mode_ = false : render_mode_ = true; 00826 } 00827 00828 //------------------------------------------------------------toggleCaptionMode- 00832 void Graph::toggleCaptionMode() 00833 { 00834 caption_mode_ == true ? caption_mode_ = false : caption_mode_ = true; 00835 } 00836 00837 //-----------------------------------------------------------setAnimationSpeed- 00838 // 00839 // This sets the animation speed. 00840 // @paran animation_speed The speed of the node animation. 00841 // 00842 void Graph::setAnimationSpeed(GLuint *animation_speed) 00843 { 00844 animation_speed_ = animation_speed; 00845 } 00846 00847 //------------------------------------------------------------setRotaionOfScene- 00848 // 00849 // This sets the current rotation of the entire scene. 00850 // @param x_rotation The rotation along the x axis. 00851 // @param y_rotation The rotation along the y axis. 00852 // @param z_rotation The rotation along the z axis. 00853 // 00854 void Graph::setSceneRotation(GLfloat x_rotation, GLfloat y_rotation, 00855 GLfloat z_rotation) 00856 { 00857 x_rotation_ = x_rotation; 00858 y_rotation_ = y_rotation; 00859 z_rotation_ = z_rotation; 00860 } 00861 00862 // eof

Generated on Mon Jun 14 12:48:56 2004 for InfoVis by doxygen 1.3.7