triangle_contains_point_viewer():
     t_(point_2(0, 0),
        point_2(100, 100),
        point_2(200, 0)),
     rbutton_pressed_(false)
 {
 }
Exemple #2
0
 bool on_double_click(const point_2f & p)
 {
    chain.clear();
    chain = {point_2(0, 0), point_2(20, 20), point_2(40, 40)};
    current_vertex_.reset();
    return true;
 }
TEST(Triangulation, one_line)
{
    using cg::point_2;
    std::vector<point_2> pts = boost::assign::list_of(point_2(0, 0))
                                                     (point_2(10, 0))
                                                     (point_2(20, 0))
                                                     (point_2(10, -10));

    cg::cell<double> cell;
    for (int i = 0; i < pts.size(); i++) {
        auto ans = cell.get_triangulation();

        for (const cg::triangle_2 & tr : ans) {
            std::cout << tr[0] << tr[1] << tr[2] << std::endl;
        }
        cell.add_point(pts[i]);
    }
    std::cout << "Ans" << std::endl;
    auto ans = cell.get_triangulation();

    for (const cg::triangle_2 & tr : ans) {
        std::cout << tr[0] << tr[1] << tr[2] << std::endl;
    }
    EXPECT_TRUE(true == true);
}
Exemple #4
0
TEST(convex_hull, simple_andrew4)
{
   using cg::point_2;

   std::vector<point_2> pts = boost::assign::list_of(point_2(1, 0))
                                                    (point_2(0, 1));

   EXPECT_TRUE(is_convex_hull(pts.begin(), cg::andrew_hull(pts.begin(), pts.end()), pts.end()));
}
Exemple #5
0
TEST(orientation, counterclockwise2)
{
   using cg::point_2;

   std::vector<point_2> a = boost::assign::list_of(point_2(1, 0))
                                                  (point_2(3, 0))
                                                  (point_2(0, 2));

   EXPECT_TRUE(cg::counterclockwise(cg::contour_2(a)));
}
Exemple #6
0
TEST(quick_hull, simple2)
{
   using cg::point_2;

   std::vector<point_2> pts = boost::assign::list_of(point_2(0, 0))
                                                    (point_2(5, 0))
                                                    (point_2(5, 5))
                                                    (point_2(4, 3));

   EXPECT_TRUE(is_convex_hull(pts.begin(), cg::quick_hull(pts.begin(), pts.end()), pts.end()));
}
Exemple #7
0
TEST(convex_hull, simple_quick_hull0)
{
   using cg::point_2;

   std::vector<point_2> pts = boost::assign::list_of(point_2(-1, 0))
                                                    (point_2(1, 0))
                                                    (point_2(0, 1))
                                                    (point_2(0, -1));

   EXPECT_FALSE(is_convex_hull(pts.begin(), cg::quick_hull(pts.begin(), pts.end()), pts.end()));
}
Exemple #8
0
TEST(quick_hull, line)
{
    using cg::point_2;

    std::vector<point_2> pts = boost::assign::list_of(point_2(0, 0))
                                                     (point_2(1, 1))
                                                     (point_2(3, 3))
                                                     (point_2(-1, -1))
                                                     (point_2(4, 4));

    EXPECT_TRUE(is_convex_hull(pts.begin(), cg::quick_hull(pts.begin(), pts.end()), pts.end()));
}
Exemple #9
0
TEST(quick_hull, same_points)
{
    using cg::point_2;

    std::vector<point_2> pts = boost::assign::list_of(point_2(1, 1))
                                                     (point_2(1, 1))
                                                     (point_2(1, 1))
                                                     (point_2(1, 1))
                                                     (point_2(1, 1));

    EXPECT_TRUE(is_convex_hull(pts.begin(), cg::quick_hull(pts.begin(), pts.end()), pts.end()));
}
Exemple #10
0
void main_window_t::wheelEvent(QWheelEvent * e)
{
   double old_zoom = zoom_;

   int delta = e->delta() / 8 / 15;
   if (delta > 0)
   {
      for (int i = 0; i != delta; ++i)
         zoom_ *= 1.1;
   }
   else if (delta < 0)
   {
      for (int i = 0; i != delta; --i)
         zoom_ /= 1.1;
   }

   point_2f pos(e->pos().x(), e->pos().y());
   point_2f sz(size().width() / 2, size().height() / 2);

   vector_2f diff = pos - sz;

   center_ += (old_zoom - zoom_) * vector_2f(diff.x, -diff.y);
   center_ = limit(center_);

   e->accept();

   viewer_->on_move(limit(screen_to_global(e->pos())));

   resize_impl(size().width(), size().height());
   updateGL();
}
Exemple #11
0
TEST(jarvis_hull, same_line)
{
   using cg::point_2;

   std::vector<point_2> pts;
   int sz = 10;
   for (int i = 0; i < sz; i++) {
      pts.push_back(point_2(i, 0));
      pts.push_back(point_2(sz, i));
      pts.push_back(point_2(0, i + 1));
      pts.push_back(point_2(i + 1, sz));
   }

   for (int it = 0; it < 10; ++it)
   {
      EXPECT_TRUE(is_convex_hull(pts.begin(), cg::jarvis_hull(pts.begin(), pts.end()), pts.end()));
      std::random_shuffle(pts.begin(), pts.end());
   }
}
TEST(dynamic_convex_hull, without_deleting4)
{
   using cg::point_2;

   std::vector<point_2> pts = boost::assign::list_of(point_2(92.5535, 63.9831))
                              (point_2(48.2666, -2.07253))
                              (point_2(-87.6796, 74.2083))
                              (point_2(-41.6394, 42.1274))
                              (point_2(4.02735, 3.24309));

   cg::dynamic_hull dh;

   for (point_2 p : pts)
   {
      dh.add_point(p);
   }

   EXPECT_TRUE(is_convex_hull(pts.begin(), pts.end(), dh.get_hull().first, dh.get_hull().second));
}
Exemple #13
0
TEST(contains, triangle_point)
{
   using cg::point_2;

   cg::triangle_2 t(point_2(0, 0), point_2(1, 1), point_2(2, 0));

   for (size_t l = 0; l != 3; ++l)
      EXPECT_TRUE(cg::contains(t, t[l]));

   EXPECT_TRUE(cg::contains(t, point_2(1, 0.5)));

   EXPECT_TRUE(cg::contains(t, point_2(1, 0)));
   EXPECT_TRUE(cg::contains(t, point_2(0.5, 0.5)));
   EXPECT_TRUE(cg::contains(t, point_2(1.5, 0.5)));

   EXPECT_FALSE(cg::contains(t, point_2(0, 1)));
   EXPECT_FALSE(cg::contains(t, point_2(2, 1)));
   EXPECT_FALSE(cg::contains(t, point_2(1, -1)));
}
TEST(dynamic_convex_hull, without_deleting5)
{
   using cg::point_2;

   std::vector<point_2> pts = boost::assign::list_of(point_2(-55, -55))
                              (point_2(53, 65))
                              (point_2(67, -27))
                              (point_2(-10, -46))
                              (point_2(12, -30));

   cg::dynamic_hull dh;

   for (point_2 p : pts)
   {
      dh.add_point(p);
   }

   EXPECT_TRUE(is_convex_hull(pts.begin(), pts.end(), dh.get_hull().first, dh.get_hull().second));
}
TEST(dynamic_convex_hull, without_deleting3)
{
   using cg::point_2;

   std::vector<point_2> pts = boost::assign::list_of(point_2(-15.2939, -91.2349))
                              (point_2(73.6514, -84.1031))
                              (point_2(46.0487, -42.9297))
                              (point_2(21.95, 48.3017))
                              (point_2(-6.31702, -43.6781));

   cg::dynamic_hull dh;

   for (point_2 p : pts)
   {
      dh.add_point(p);
   }

   EXPECT_TRUE(is_convex_hull(pts.begin(), pts.end(), dh.get_hull().first, dh.get_hull().second));
}
Exemple #16
0
TEST(orientation, uniform0)
{
   using cg::point_2;
   using cg::contour_2;


   for (size_t cnt_points = 3; cnt_points < 1000; cnt_points++)
   {
      std::vector<point_2> pts = uniform_points(cnt_points);

      auto it = cg::graham_hull(pts.begin(), pts.end());
      pts.resize(std::distance(pts.begin(), it));

      EXPECT_TRUE(cg::counterclockwise(contour_2(pts)));

      std::reverse(pts.begin(), pts.end());
      EXPECT_FALSE(cg::counterclockwise(contour_2(pts)));
   }
}
 circle_2 get_circumcircle(triangle_2 const &t) const
 {
     if (t[0] == t[1] && t[1] == t[2])
         return circle_2(t[0], 0);
     if (orientation(t[0], t[1], t[2]) == cg::CG_COLLINEAR)
     {
         auto minmax = std::minmax({t[0], t[1], t[2]});
         point_2 p1 = minmax.first;
         point_2 p2 = minmax.second;
         if (t[0] != t[1])
             p2 = t[1];
         else
             p2 = t[2];
         point_2 center = p1 + vector_2((p2.x - p1.x) / 2, (p2.y - p1.y) / 2);
         double radius = segment_2(p1, p2).length() / 2;
         return circle_2(center, radius);
     }
     double A1 = t[1].x - t[0].x;
     double B1 = t[1].y - t[0].y;
     double C1 = (t[1].x - t[0].x) * (t[1].x + t[0].x) / 2 + (t[1].y - t[0].y) * (t[1].y + t[0].y) / 2;
     double A2 = t[2].x - t[0].x;
     double B2 = t[2].y - t[0].y;
     double C2 = (t[2].x - t[0].x) * (t[2].x + t[0].x) / 2 + (t[2].y - t[0].y) * (t[2].y + t[0].y) / 2;
     point_2 center((C1 * B2 - C2 * B1) / (A1 * B2 - A2 * B1),
                    (A1 * C2 - A2 * C1) / (A1 * B2 - A2 * B1));
     double radius = segment_2(center, t[0]).length();
     return circle_2(center, radius);
 }
Exemple #18
0
void main_window_t::resize_impl(int screen_w, int screen_h)
{
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();

   vector_2f size = (zoom_ / 2) * vector_2f(screen_w, screen_h);

   point_2f left_bottom = center_ + (-size);
   point_2f right_top   = center_ + size;

   glOrtho(left_bottom.x, right_top.x, left_bottom.y, right_top.y, -1.0, 1.0);
   glViewport(0, 0, screen_w, screen_h);
}
Exemple #19
0
   void print(cg::visualization::printer_type & p) const
   {
      p.corner_stream() << "double-click to clear." << cg::visualization::endl
                        << "press mouse rbutton for add vertex (click to first point to complete contour)" << cg::visualization::endl
                        << "move vertex with rbutton" << cg::visualization::endl
                        << "eps:" << eps << cg::visualization::endl;


      for (size_t i = 0; i < points_.size(); ++i)
      {
         p.global_stream((point_2f)points_[i] + vector_2f(5, 0)) << i;
      }
   }
Exemple #20
0
 triangle_intersects_triangle_viewer()
     : s_(point_2(-100, -100), point_2(50, 50), point_2(0, 50)),
      t_(point_2(-50, -50), point_2(50, -50), point_2(-50, 50)),
       rbutton_pressed_(false),
       int_({})
 {
     int_ = cg::intersection(s_, t_);
 }
   void print(cg::visualization::printer_type & p) const
   {
      p.corner_stream() << "double-click to clear." << cg::visualization::endl
                        << "press mouse rbutton for add vertex (click to first point to complete contour)" << cg::visualization::endl
                        << "move vertex with rbutton" << cg::visualization::endl
                        << "yellow contour -- not ccw or convex" << cg::visualization::endl
                        << "green contour -- contains cursor" << cg::visualization::endl
                        << "red contour -- don't contains cursor" << cg::visualization::endl;

      for (size_t i = 0; i < points_.size(); ++i)
      {
         p.global_stream((point_2f)points_[i] + vector_2f(5, 0)) << i;
      }
   }
Exemple #22
0
   void draw(cg::visualization::drawer_type & drawer) const
   {
       for (size_t idx = 0; idx < cur_contour; idx++)
       {
           contour const & cont = conts[idx];
           drawer.set_color(Qt::red);
           if (cg::counterclockwise(cont))
              drawer.set_color(Qt::green);

           for (size_t i = 0; i < cont.size(); i++)
           {
              size_t j = (i + 1) % cont.size();
              cg::point_2 p1 = cont[i], p2 = cont[j];
              drawer.draw_line(p1, p2);
           }
       }
      if (in_building_)
      {
         drawer.set_color(Qt::white);

         for (size_t i = 1; i < conts[cur_contour].size(); ++i)
         {
            drawer.draw_line(conts[cur_contour][i - 1], conts[cur_contour][i]);
         }
      } else
      {
          drawer.set_color(Qt::blue);
          drawer.draw_point(center, 3);
          contour_2 result = minkowski_convex_sum(contour_2(conts[0]), contour_2(reversed));

          for (size_t i = 0; i < result.size(); ++i)
          {
              size_t j = (i + 1) % result.size();
             drawer.draw_line(result[i], result[j]);
          }
      }
   }
Exemple #23
0
   bool on_press(const point_2f & p)
   {
      for (size_t i = 0; i < chain.size(); ++i)
      {
         if (fabs(chain[i].x - p.x) < 15 && fabs(chain[i].y - p.y) < 15)
         {
            current_vertex_ = i;
            return true;
         }
      }

      if(!current_vertex_)
         chain.push_back(point_2(p.x, p.y));

      return true;
   }
Exemple #24
0
TEST(contains, segment_point)
{
   using cg::point_2;

   cg::segment_2 s(point_2(0, 0), point_2(2, 2));
   for (size_t l = 0; l != 2; ++l)
      EXPECT_TRUE(cg::contains(s, s[l]));

   EXPECT_TRUE(cg::contains(s, point_2(1, 1)));

   EXPECT_FALSE(cg::contains(s, point_2(-1, -1)));
   EXPECT_FALSE(cg::contains(s, point_2(4, 4)));

   EXPECT_FALSE(cg::contains(s, point_2(1, 0)));
   EXPECT_FALSE(cg::contains(s, point_2(0, 1)));
}
Exemple #25
0
TEST(jarvis_hull, simple)
{
   using cg::point_2;

   std::vector<point_2> pts = boost::assign::list_of(point_2(0, 0))
                                                    (point_2(1, 0))
                                                    (point_2(0, 1))
                                                    (point_2(2, 0))
                                                    (point_2(0, 2))
                                                    (point_2(3, 0));

   EXPECT_TRUE(is_convex_hull(pts.begin(), cg::jarvis_hull(pts.begin(), pts.end()), pts.end()));
}
TEST(dynamic_convex_hull, with_deleting2)
{
   using cg::point_2;

   std::vector<point_2> pts = boost::assign::list_of(point_2(81, 80))
                              (point_2(-80, -41))
                              (point_2(71, 90))
                              (point_2(-64, 17));

   std::vector<point_2> not_removed = boost::assign::list_of(point_2(81, 80))
                              (point_2(71, 90))
                              (point_2(-64, 17));
   cg::dynamic_hull dh;

   for (point_2 p : pts)
   {
      dh.add_point(p);
   }

   dh.remove_point(point_2(-80, -41));
   EXPECT_TRUE(is_convex_hull(not_removed.begin(), not_removed.end(), dh.get_hull().first, dh.get_hull().second));
}
Exemple #27
0
 void calc_visibility_graph()
 {
     int n = 0;
     for (contour_2 &c : contours)
     {
         if (!counterclockwise(c))
         {
             c.reverse();
             std::reverse(points.begin() + n, points.begin() + n + c.size());
         }
         n += c.size();
     }
     graph g = visibility_graph(contours.begin(), contours.end(), true);
     visibility_segments.clear();
     for (int i = 0; i < (int) points.size(); ++i)
     {
         std::vector<int> const &edges = g.get_edges(i);
         for (int j : edges)
             visibility_segments.push_back(segment_2(points[i], points[j]));
     }
 }
TEST(dynamic_convex_hull, without_deleting1)
{
   using cg::point_2;

   std::vector<point_2> pts = boost::assign::list_of(point_2(0, 0))
                              (point_2(1, 0))
                              (point_2(0, 1))
                              (point_2(2, 0))
                              (point_2(0, 2))
                              (point_2(3, 0));
   cg::dynamic_hull dh;

   for (point_2 p : pts)
   {
      dh.add_point(p);
   }

   EXPECT_TRUE(is_convex_hull(pts.begin(), pts.end(), dh.get_hull().first, dh.get_hull().second));
}
TEST(dynamic_convex_hull, without_deleting6)
{
   using cg::point_2;

   std::vector<point_2> pts = boost::assign::list_of(point_2(17, -19))
                              (point_2(61, 94))
                              (point_2(-29, 38))
                              (point_2(-9, -42))
                              (point_2(-85, 40))
                              (point_2(-24, -7))
                              (point_2(-78, 95));
   cg::dynamic_hull dh;

   for (point_2 p : pts)
   {
      dh.add_point(p);
   }
   auto st_h = dh.get_hull().first;
   auto en_h = dh.get_hull().second;
   bool res = is_convex_hull(pts.begin(), pts.end(), st_h, en_h);

   EXPECT_TRUE(is_convex_hull(pts.begin(), pts.end(), dh.get_hull().first, dh.get_hull().second));
}
Exemple #30
0
 void new_contour()
 {
     contours.push_back(contour_2(std::vector<point_2>()));
 }