首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >CGAL笛卡尔网格

CGAL笛卡尔网格
EN

Stack Overflow用户
提问于 2018-04-18 19:16:23
回答 1查看 134关注 0票数 2

在我的代码中,我将对象组织到一个普通的笛卡儿网格中(例如10x10)。通常给定一个点,我需要测试点是否与网格相交,如果是的话,哪个回收箱包含这个点。我已经有了自己的实现,但我不喜欢纠缠于精度问题。

那么,CGAL有二维正则笛卡儿网格吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-26 13:12:34

您可以使用CGAL::points_on_square_grid_2生成网格点。CGAL内核提供了Kernel::CompareXY_2函子,您可以使用这些函子来计算查询点在网格上的确切位置。例如,您可以对网格点进行排序,然后使用std::lower_bound,然后在范围的适当元素上使用CGAL::定位或CGAL::共线。你也可以建立一个安排,但这将是一个过头。

这是一个示例代码。

代码语言:javascript
运行
复制
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <CGAL/point_generators_2.h>
#include <CGAL/random_selection.h>
#include <CGAL/Polygon_2_algorithms.h>

using namespace CGAL;
using K= Exact_predicates_exact_constructions_kernel;
using Point =K::Point_2;
using Creator = Creator_uniform_2<double, Point>;
using Grid = std::vector<Point>;

const int gridSide = 3;

void locate_point (Point p, Grid grid);

int main ()
{
  Grid points;
  points_on_square_grid_2(gridSide * gridSide, gridSide * gridSide, std::back_inserter(points), Creator());

  std::sort(points.begin(), points.end(), K::Less_xy_2());

  std::cout << "Grid points:\n";
  for (auto& p:points)
    std::cout << p << '\n';

  std::cout << "\ncorner points:\n";
  Grid cornerPoints{points[0], points[gridSide - 1], points[gridSide * gridSide - 1],
                    points[gridSide * (gridSide - 1)]};
  for (auto& p:cornerPoints)
    std::cout << p << '\n';

  std::cout << '\n';

  Point p1{-8, -8};
  Point p2{-10, 3};
  Point p3{-9, -8};
  Point p4{0, 4};
  Point p5{1, 5};

  locate_point(p1, points);
  locate_point(p2, points);
  locate_point(p3, points);
  locate_point(p4, points);
  locate_point(p5, points);

}

void locate_point (Point p, Grid grid)
{
  if (grid.empty())
    {
      std::cout << "Point " << p << " not in grid";
      return;
    }

  // check if point is in grid
  Grid cornerPoints{grid[0], grid[gridSide - 1], grid[gridSide * gridSide - 1], grid[gridSide * (gridSide - 1)]};
  auto point_is = CGAL::bounded_side_2(cornerPoints.begin(), cornerPoints.end(), p);
  switch (point_is)
    {
  case CGAL::ON_UNBOUNDED_SIDE:
    std::cout << "Point " << p << " not in grid\n";
      return;
  case CGAL::ON_BOUNDARY:
    std::cout << "Point " << p << " on grid boundary\n";
      return;
  case CGAL::ON_BOUNDED_SIDE:
    std::cout << "Point " << p << " is in grid\n";
    }

  auto f = std::lower_bound(grid.begin(), grid.end(), p, K::Less_xy_2());
  auto g = std::find_if(f, grid.end(), [&p] (const Point& gridpoint)
  { return K::Less_y_2()(p, gridpoint); });

  if (CGAL::collinear(p, *g, *(g - 1)))
    {
      std::cout << "Point " << p << " on grid side between points " << *(g - 1) << " and " << *g << '\n';
      return;
    }

  std::cout << "Point " << p << " in bin whose upper right point is " << *g << '\n';
  return;

}

输出:

代码语言:javascript
运行
复制
Grid points:
-9 -9
-9 0
-9 9
0 -9
0 0
0 9
9 -9
9 0
9 9

corner points:
-9 -9
-9 9
9 9
9 -9

Point -8 -8 is in grid
Point -8 -8 in bin whose upper right point is 0 0
Point -10 3 not in grid
Point -9 -8 on grid boundary
Point 0 4 is in grid
Point 0 4 on grid side between points 0 0 and 0 9
Point 1 5 is in grid
Point 1 5 in bin whose upper right point is 9 9
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49907425

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档