顶点坐标从General_polygon_2
Vertex coordinates from General_polygon_2
说明
、指针或示例,说明如何从General_Polygon_2
中提取顶点坐标将不胜感激。
用例是偏移(带有圆的闵可夫斯基和)一个简单的多边形。 施工精度并不重要。 我似乎可以构造源多边形以提供给approximated_offset_2
,但是一旦构造了偏移多边形,我就无法弄清楚如何从来自outer_boundary()
方法的General_polygon_2
中提取顶点。
简化的示例代码如下:
#include <vector>
#include <cmath>
#include <cstdio>
// CGAL includes
#include <CGAL/Cartesian.h>
#include <CGAL/Polygon_2.h>
#include <CGAL/approximated_offset_2.h>
#include <CGAL/offset_polygon_2.h>
#include <CGAL/Lazy_exact_nt.h>
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
// typedefs for CGAL items
typedef CGAL::Exact_predicates_exact_constructions_kernel Kernel;
typedef CGAL::Polygon_2<Kernel> Polygon_2;
typedef CGAL::Point_2<Kernel> Point_2;
typedef CGAL::Gps_circle_segment_traits_2<Kernel> Gps_traits_2;
typedef Gps_traits_2::Polygon_with_holes_2 Offset_polygon_with_holes_2;
typedef CGAL::Lazy_exact_nt<CGAL::Quotient<CGAL::MP_Float> > NT;
typedef struct {double x; double y;} vertex_2;
void init_regular_ngon_2(std::vector<vertex_2>& ngon_vertices, const double center_x, const double center_y, const double r)
{
unsigned int i, num_subdiv;
double theta;
num_subdiv = ngon_vertices.size();
for (i = 0; i < num_subdiv; i++)
{
theta = 2.0 * M_PI * (double)i / (double)num_subdiv;
ngon_vertices[i].y = center_y + (r * sin(theta));
ngon_vertices[i].x = center_x + (r * cos(theta));
}
}
void ngon_2_to_CGAL_poly(const std::vector<vertex_2>& ngon_vertices, Polygon_2& dest_poly)
{
unsigned int i;
for (i = 0; i < ngon_vertices.size(); i++)
{
dest_poly.push_back(Point_2(ngon_vertices[i].x, ngon_vertices[i].y));
}
}
int main(void)
{
CGAL::Lazy_exact_nt<NT>::set_relative_precision_of_to_double(1E-8);
Polygon_2 test_poly;
Offset_polygon_with_holes_2 offset_poly;
std::vector<vertex_2> ngon(8);
init_regular_ngon_2(ngon, 0.0, 0.0, 5.0);
ngon_2_to_CGAL_poly(ngon, test_poly);
offset_poly = approximated_offset_2(test_poly, 0.85, 1E-5);
printf("The offset polygon has %d verts and %d holesn", offset_poly.outer_boundary().size(), offset_poly.number_of_holes());
// How to obtain vertex coordinates comprising offset polygon boundary?? Goal is to to store these in a vector of vertex_2 for later manipulation.
return 0;
}
类似以下内容的内容应该可以完成这项工作:
typedef Gps_traits_2::General_polygon_2 General_polygon_2;
const General_polygon_2& outer_boundary = offset_poly.outer_boundary();
General_polygon_2::Curve_const_iterator cit=outer_boundary.curves_begin(),
cit_end=outer_boundary.curves_end();
for(;cit!=cit_end;++cit)
{
bool is_linear = cit->is_linear();
std::cout << cit->source() << " " << cit->target() << "n";
}
此处给出了所有成员函数的列表。
approximated_offset_2
返回一个Polygon_with_holes
,outer_boundary()
你可以得到General_polygon_2
,但你不能只通过它的曲线迭代它的顶点,然后你可以得到曲线的目标和源,正如 Sloriot 提到的。目标点和源点不是Kernel::Point_2
成员,因此您不能使用=
运算符,但您可以获得target.x()
和target.y()
,它们是其坐标并且是类 CoordNT
的成员,这不是双精度/浮点数,而是 3 个数字(alpha, beta, gamma)
,您可以从方程 x = a+b * sqrt(c) 计算出确切的值。
typedef Gps_traits_2::General_polygon_2 General_polygon_2;
const General_polygon_2& outer_boundary = offset_poly.outer_boundary();
General_polygon_2::Curve_const_iterator cit;
for(cit = outer_boundary.curves_begin();
cit !=outer_boundary.curves_end();++cit){
source = Point2((cit->source().x().alpha())+
(cit->source().x().beta()*sqrt(cit->source().x().gamma())),
(cit->source().y().alpha())+
(cit->source().y().beta()*sqrt(cit->source().y().gamma())));
target = Point2((cit->target().x().alpha())+
(cit->target().x().beta()*sqrt(cit->target().x().gamma())),
(cit->target().y().alpha())+
(cit->target().y().beta()*sqrt(cit->target().y().gamma())));
相关文章:
- 使用Boost Polygon的减法结果不正确
- Visual Studio 2015 中缺少"General"选项卡
- 从 2D C 列表创建 boost.geometry.model.polygon
- 使用C++拆分"[General Setting]"格式的部分字符串
- Android NDK / General JNI 问题:将对象/jobject 转换为 c++ 用户 defiend
- 如何从svg读取boost::geometry::model::polygon
- 视图在Boost Polygon库中使用
- #include < windows.h> 在全局命名空间中定义了许多符号,例如 Polygon
- 如何使用协议缓冲区中的扩展来维护'general'消息