Example #1
0
void draw_it(const world::Line& line)
{
	glPushMatrix();
	glRotatef(40, 1, 0, 0);
	glTranslatef(-300, 0, -300);

	::glDisable(GL_DEPTH_TEST);
	glColor3f(0, 1, 0);
	glBegin(GL_TRIANGLES);

	world::tri_iterator c, e = qt.end();
	for (c = qt.begin(); c != e; ++c) {
		glVertex3fv(c->a.get());
		glVertex3fv(c->b.get());
		glVertex3fv(c->c.get());
	}
	glEnd();

	glColor3f(0, 0, 1);
	glBegin(GL_TRIANGLES);
	for (c = qt.find(line); c != e; ++c) {
		glVertex3fv(c->a.get());
		glVertex3fv(c->b.get());
		glVertex3fv(c->c.get());
	}
	glEnd();

	::glEnable(GL_DEPTH_TEST);
	glColor3f(1,1,1);
	glBegin(GL_LINES);
	glVertex3fv(line.p1.get());
	glVertex3fv(line.p2.get());
	glEnd();

	glPopMatrix();

	glColor3f(1,1,1);
}
Example #2
0
void simple_test() {

  std::cout << "Beginning simple_test()..." << std::endl;


  // --------------------------------------------------------
  // a collection of 21 points that make a nice sample tree
  std::vector< std::pair<Point<int>,char> > simple_points;
  simple_points.push_back(std::make_pair(Point<int>(20,10), 'A'));
  simple_points.push_back(std::make_pair(Point<int>(10,5), 'B'));
  simple_points.push_back(std::make_pair(Point<int>(30,4), 'C'));
  simple_points.push_back(std::make_pair(Point<int>(11,15), 'D'));
  simple_points.push_back(std::make_pair(Point<int>(31,16), 'E'));
  simple_points.push_back(std::make_pair(Point<int>(5,3), 'F'));
  simple_points.push_back(std::make_pair(Point<int>(15,2), 'G'));
  simple_points.push_back(std::make_pair(Point<int>(4,7), 'H'));
  simple_points.push_back(std::make_pair(Point<int>(14,8), 'I'));
  simple_points.push_back(std::make_pair(Point<int>(25,1), 'J'));
  simple_points.push_back(std::make_pair(Point<int>(35,2), 'K'));
  simple_points.push_back(std::make_pair(Point<int>(26,7), 'L'));
  simple_points.push_back(std::make_pair(Point<int>(36,6), 'M'));
  simple_points.push_back(std::make_pair(Point<int>(3,13), 'N'));
  simple_points.push_back(std::make_pair(Point<int>(16,12), 'O'));
  simple_points.push_back(std::make_pair(Point<int>(4,17), 'P'));
  simple_points.push_back(std::make_pair(Point<int>(15,18), 'Q'));
  simple_points.push_back(std::make_pair(Point<int>(25,13), 'R'));
  simple_points.push_back(std::make_pair(Point<int>(37,14), 'S'));
  simple_points.push_back(std::make_pair(Point<int>(24,19), 'T'));
  simple_points.push_back(std::make_pair(Point<int>(36,18), 'U'));


  // --------------------------------------------------------
  // the quad tree data structure starts out empty
  QuadTree<int,char> simple;
  assert (simple.size() == 0);
  // an empty tree has height == -1
  assert (simple.height() == -1); 
  // plot the structure with with these dimensions (width=40,height=20)
  std::cout << "\nan empty tree:" << std::endl;
  simple.plot(40,20);

  

  // --------------------------------------------------------
  for (int i = 0; i < simple_points.size(); i++) {

    // add each point from the collection
    //cout << "hello"<< endl;
    simple.insert(simple_points[i].first,simple_points[i].second);
    // verify the size (total # of points in the tree)
     //cout << "hello"<< endl;
    assert (simple.size() == i+1);

    // a few some specific checks along the way
    if (i == 0) { 
      std::cout << "\nafter inserting first data point:" << std::endl;
      simple.plot(40,20);
      // a tree with 1 node has height == 0
      //cout << simple.height() << endl;
      assert (simple.height() == 0); 
      // check that the newly inserted element can be found
      //cout << "hello"<< endl;
      QuadTree<int,char>::iterator itr = simple.find(20,10);
      //cout << itr.getLabel() << endl;
      //cout << "hello"<< endl;
      assert (itr != simple.end());
      // read the label & coordinates from the iterator
      assert (itr.getLabel() == 'A');
      // dereference the iterator to get the point
      const Point<int> &pt = *itr;
      assert (pt.x == 20);
      assert (pt.y == 10);
      //cout << "hello"<< endl;
    } else if (i <= 4) {
      std::cout << "\nafter inserting " << i+1 << " data points:" << std::endl;
      simple.plot(40,20);
      // the next 4 additions for this simple all happen at the
      // second level, tree has height = 1
      assert (simple.height() == 1);
    } else if (i == 8) {
      std::cout << "\nafter inserting " << i+1 << " data points:" << std::endl;
      simple.plot(40,20);
      assert (simple.height() == 2);
      // check for an element that exists
      QuadTree<int,char>::iterator itr = simple.find(4,7);
      assert (itr != simple.end());
      assert (itr.getLabel() == 'H');
      assert ((*itr).x == 4);
      assert ((*itr).y == 7);
      // check for a couple elements that aren't in the tree
      itr = simple.find(14,14);
      assert (itr == simple.end());
      itr = simple.find(15,18);
      assert (itr == simple.end());
      // another visualization of the tree structure
      // note: this is a pre-order traversal of the data (print the node, then recurse on each child)
      std::cout << "\na 'sideways' printing of the tree structure with 9 nodes:" << std::endl;
      simple.print_sideways();
    }
  }


  // --------------------------------------------------------
  // a few more checks
  std::cout << "\nafter inserting all 21 data points:" << std::endl;
  simple.plot(40,20);
  assert (simple.size() == 21);
  assert (simple.height() == 2);
  QuadTree<int,char>::iterator itr = simple.find(15,18);
  assert (itr != simple.end());
  assert (itr.getLabel() == 'Q');
  assert ((*itr).x == 15);
  assert ((*itr).y == 18);

  // plot the data without the lines
  std::cout << "\na plot of the point data without the lines:" << std::endl;
  simple.plot(40,20,false);


  // --------------------------------------------------------
  // another visualization of the tree structure
  // note: this is a pre-order traversal of the data (print the node, then recurse on each child)
  std::cout << "\na 'sideways' printing of the finished tree structure:" << std::endl;
  simple.print_sideways();


  // --------------------------------------------------------
  // use the primary (depth-first) iterator to traverse the tree structure
  // note: this is a pre-order traversal, the same order as the 'sideways' tree above!!
  std::cout << "\nA depth-first traversal of the simple tree (should match sideways output!):" << std::endl;
  QuadTree<int,char>::iterator df_itr = simple.begin();
  char expected_depth_first_order[21] = 
    { 'A','B','F','G','H','I','C','J','K','L','M','D','N','O','P','Q','E','R','S','T','U' };
  for (int i = 0; i < 21; i++) {
    assert (df_itr != simple.end());
    // get the depth/level of this element in the tree (distance from root node!)
    int depth = df_itr.getDepth();
    // use the depth to indent the output (& match the sideways tree output above)
    std::cout << std::string(depth*2,' ') << df_itr.getLabel() << " " << *df_itr << std::endl;
    // check that the output is in the correct order!
    assert (df_itr.getLabel() == expected_depth_first_order[i]);
    // test the pre-increment operator++ 
    ++df_itr;  
  }
  // after 21 increments, we better be at the end!
  assert (df_itr == simple.end());


  // --------------------------------------------------------
  // using the breadth-first iterator to traverse the data by level
  std::cout << "\nA breadth first traversal of the simple tree:";
  QuadTree<int,char>::bf_iterator bf_itr = simple.bf_begin();
  char expected_breadth_first_order[21] = 
    { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U' };
  int level = -1;
  for (int i = 0; i < 21; i++) {
    assert (bf_itr != simple.bf_end());
    // get the depth/level of this element in the tree (distance from root node!)
    int depth = bf_itr.getDepth();
    if (level != depth) {
      level = depth;
      // starting a new level!
      std::cout << std::endl << "   level " << level << ":";
    }
    // print out this data point
    std::cout << " " << bf_itr.getLabel() << *bf_itr;
    // check that the output is in the correct order!
    assert (bf_itr.getLabel() == expected_breadth_first_order[i]);
    // test the pre-increment operator++
    ++bf_itr;
  }
  // after 21 increments, we better be at the end!
  assert (bf_itr == simple.bf_end());
  std::cout << std::endl;

  // --------------------------------------------------------
  std::cout << "\nFinished with simple_test().\n" << std::endl;
  

  // Note: the destructor for the QuadTree object 'simple' is
  // automatically called when we leave this function and the variable
  // goes out of scope!
}