CGAL有神秘的面纱,让我不断想看清其真面目。开始吧!

1 Three Points and One Segment

第一个例子是创建3个点和一条线段,并且在其上进行一些操作。

所有的CGAL头文件都在CGAL目录下。所有的CGAL类和函数都在CGAL的命名空间。类以大写字母开头,常量全大写,全局函数名小写。对象的空间维度由后缀给出。

几何元,如点,在一个kernel中定义。第一个例子中我们选择的kernel采用double精度的浮点数作为笛卡尔空间坐标。

另外,我们有predicate(断言),如位置测试断言,我们有construction(构建),如距离和中点的计算,都是construction。一个predicate的结果是一个离散集,一个construction产生一个值,也可能产生一个新的几何实体。

File Kernel_23/points_and_segment.cpp

#include <iostream>
#include <CGAL/Simple_cartesian.h>
 
typedef Kernel::Point_2 Point_2;
typedef Kernel::Segment_2 Segment_2;
 
int main()
{
Point_2 p(1,1), q(10,10);
 
std::cout << "p = " << p << std::endl;
std::cout << "q = " << q.x() << " " << q.y() << std::endl;
 
std::cout << "sqdist(p,q) = "
<< CGAL::squared_distance(p,q) << std::endl;
 
Segment_2 s(p,q);
Point_2 m(5, 9);
 
std::cout << "m = " << m << std::endl;
std::cout << "sqdist(Segment_2(p,q), m) = "
<< CGAL::squared_distance(s,m) << std::endl;
 
std::cout << "p, q, and m ";
switch (CGAL::orientation(p,q,m)){
std::cout << "are collinear\n";
break;
std::cout << "make a left turn\n";
break;
std::cout << "make a right turn\n";
break;
}
 
std::cout << " midpoint(p,q) = " << CGAL::midpoint(p,q) << std::endl;
return 0;
}
 
下面采用浮点数进行的几何运行让我们吃惊。

File Kernel_23/surprising.cpp

#include <iostream>
#include <CGAL/Simple_cartesian.h>
 
typedef Kernel::Point_2 Point_2;
 
int main()
{
{
Point_2 p(0, 0.3), q(1, 0.6), r(2, 0.9);
std::cout << (CGAL::collinear(p,q,r) ? "collinear\n" : "not collinear\n");
}
{
Point_2 p(0, 1.0/3.0), q(1, 2.0/3.0), r(2, 1);
std::cout << (CGAL::collinear(p,q,r) ? "collinear\n" : "not collinear\n");
}
{
Point_2 p(0,0), q(1, 1), r(2, 2);
std::cout << (CGAL::collinear(p,q,r) ? "collinear\n" : "not collinear\n");
}
return 0;
}

如果只从代码上看,我们会发现前两种情况也应当是共线的,但实际的结果是:

not collinear
not collinear
collinear
因为分数作为双精度数是不可被描述的,共线测试内部的计算是一个3X3行列式(determinant),它可以得到近似值,但不能得到误差为0的精确值。所以得出前两种情况为不花线的结论。
其他的predicate也会有同样的问题,如CGAL::orientation(p,q,m)运算可能会由于舍入误差,可能得出不同实际的结论。
如果你需要使数被全精度解析,你可以使用精确断言和精确构建的CGAL kernel。 

File Kernel_23/exact.cpp

#include <iostream>
#include <CGAL/Exact_predicates_exact_constructions_kernel.h>
#include <sstream>
 
typedef Kernel::Point_2 Point_2;
 
int main()
{
Point_2 p(0, 0.3), q, r(2, 0.9);
{
q = Point_2(1, 0.6);
std::cout << (CGAL::collinear(p,q,r) ? "collinear\n" : "not collinear\n");
}
 
{
std::istringstream input("0 0.3 1 0.6 2 0.9");
input >> p >> q >> r;
std::cout << (CGAL::collinear(p,q,r) ? "collinear\n" : "not collinear\n");
}
 
{
q = CGAL::midpoint(p,r);
std::cout << (CGAL::collinear(p,q,r) ? "collinear\n" : "not collinear\n");
}
 
return 0;
}

这里的结果仍然让我们吃惊:

not collinear
collinear
collinear

第一个结果仍然是错的,原因与前面相同,它们仍然是浮点运算。第二个结果不同,它由字符串生成(construct),则精确地代表了字符串所表示的数。第三个结果通过构建(construct)中点得到第三个点,构建操作是精确的,所以结果也是正确的。

在很多情况下,你操作“精确”浮点数据,认为它们是由应用计算得到或由传感器得到的。它们不是象“0.1”这样的字符串,也不是象"1.0/10.0"这样动态( on the fly)生成的,它是一个全精度的浮点数。如果它们只是被传递入某个算法并且没有构建(construct)操作时,你可以使用支持精确断言(predicate)和非精确构建(construct)的kernel。这样的例子包括下一节我们看到的“凸包”算法。它的输出是输入的一个子集,这个算法只进行坐标比较和位置测试。

由于高精度的计算需要消耗比普通计算多的资源,内存、时间等,所以使用时需要考虑。

大部分CGAL包说明它们需要或支持哪种kernel。

2 The Convex Hull of a Sequence of Points

本节所有例子都是凸包算法。输入一个点序列,输出所有凸包边界上的点序列。

下面的例子输入和输出的都是一个坐标数组。

File Convex_hull_2/array_convex_hull_2.cpp

#include <iostream>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/convex_hull_2.h>
 
typedef K::Point_2 Point_2;
 
int main()
{
Point_2 points[5] = { Point_2(0,0), Point_2(10,0), Point_2(10,10), Point_2(6,5), Point_2(4,1) };
Point_2 result[5];
 
Point_2 *ptr = CGAL::convex_hull_2( points, points+5, result );
std::cout << ptr - result << " points on the convex hull:" << std::endl;
for(int i = 0; i < ptr - result; i++){
std::cout << result[i] << std::endl;
}
return 0;
}
如同上节所说,我们采用了精确断言和非精确构建的kernel来生成程序。
下面的例子则采用了标准库中的vector类来进行输入和输出。

File Convex_hull_2/vector_convex_hull_2.cpp

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/convex_hull_2.h>
 
#include <vector>
 
typedef K::Point_2 Point_2;
typedef std::vector<Point_2> Points;
 
int main()
{
Points points, result;
points.push_back(Point_2(0,0));
points.push_back(Point_2(10,0));
points.push_back(Point_2(10,10));
points.push_back(Point_2(6,5));
points.push_back(Point_2(4,1));
 
 
CGAL::convex_hull_2( points.begin(), points.end(), std::back_inserter(result) );
std::cout << result.size() << " points on the convex hull" << std::endl;
return 0;
}

3 About Kernels and Traits Classes

本节简介traits的内涵和意义.

在上个例子中,如果我们阅读convex_hull_2()的手册时,会发现它及其他2D convex_hull_2()算法都有两个版本。其中一个版本包含了traits参数。

template<class InputIterator , class OutputIterator , class Traits >
const Traits & ch_traits)

什么原因要我们使用traits呢?泛型编程需要使用抽象元语对算法进行抽象,而在实现中将元语变为实际的类型和算法。那么convex hull算法需要哪些元语(primitive)呢?最简单的"Graham/Andrew Scan"算法过程是:(1)将所有输入的点进行从左到右排序;(2)从左向右顺序加入,逐步形成convex hull。这个过程(ch_graham_andrew())需要的元语包括:

  • Traits::Point_2
  • Traits::Less_xy_2
  • Traits::Left_turn_2
  • Traits::Equal_2

可以看出,Left_turn_2负责位置测试,Less_xy_2用于点的排序(这些类型需要满足的要求在概论念ConvexHullTraits_2中进行详细说明)。

从泛型概念的要求出发,这些元语需要抽象形成模板,所以下面是新的算法形式:

template <class InputIterator, class OutputIterator, class Point_2, class Less_xy_2, class Left_turn_2, class Equal_2>
每一种类型必须对模板中的所有类型进行定义。可以看出,这个模板参数有一点复杂。
有两个问题需要我们回答:(1)哪些类型需要进入模板参数列表?(2)我们为什么要用这些模板参数?
对第一个问题:ConvexHullTraits_2所要求的任何模型,这些模型由CGAL概念Kernel提供。
对第二个问题:如果我们将来需要计算投影到yz平面上的的3D点集的convex hull时,我们设计一个新的traits——Projection_traits_yz_3,这样前面的例子就不需要进行大的修改。

File Convex_hull_2/convex_hull_yz.cpp

#include <iostream>
#include <iterator>
#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/Projection_traits_yz_3.h>
#include <CGAL/convex_hull_2.h>
 
typedef K::Point_2 Point_2;
 
int main()
{
std::istream_iterator< Point_2 > input_begin( std::cin );
std::istream_iterator< Point_2 > input_end;
std::ostream_iterator< Point_2 > output( std::cout, "\n" );
CGAL::convex_hull_2( input_begin, input_end, output, K() );
return 0;
}
另一个例子是关于使用已经定义的空间点类型,或者来自非CGAL库中的点类型,将这些点类型及其相应的断言(predicates)加入类范围,然后你就可以基于新的点类型运行convex_hull_2。
最后,为什么需要将一个traits对象作为参数传入该方法呢?主要原因在于我们可以用一个更加一般的投影特征对象(projection trait)来保存状态。例子:如果这个投影平面是由一个向量给出的方向,而且是通过硬编码的方式加入Projection_traits_yz_3。
 

4 Concepts and Models

一个概念(concept)是一个类型的一个需求集(requirment set),它包括一些内嵌的类型,成员函数或一些处理该类型自由函数。

一个概念的模型(model of a concept)是一个用于实现概念所有需求的一个类。

下面有一个函数:

template <typename T>
T
duplicate(T t)
{
return t;
}

如果你用一个类C来实例化该函数,则C必须提供一个复制构造函数(copy constructor),我们称类C必须是“可复制构造的”(CopyConstructible)。

另一个例子:

template <typename T>
T& std::min(const T& a, const T& b)
{
return (a<b)?a:b;
}

这个函数只有在类型T的operator<(..)有定义时才能编译。我们称类C必须是“小于关系可比较的”(LessThanComparable)

关于自由函数的一个例子:CGAL包和Boost Graph库中的HalfedgeListGraph概念。如果一个类G要成为HalfedgeListGraph的一个模型,必须有一个全局函数halfedges(const G&)处理该类。

关于带有traits需求的概念的一个例子是InputIterator。对于一个InputIterator的模型,一个特化的std::iterator_traits类必须存在(或其通用的模板必须可用)。

5 Further Reading

阅读:

"The C++ Standard Library, A Tutorial and Reference" by Nicolai M. Josuttis from Addison-Wesley, or "Generic Programming and the STL" by Matthew H. Austern for the STL and its notion of concepts and models.

Other resources for CGAL are the rest of the tutorials and the user support page at https://www.cgal.org/.

 

Hello World 之 CGAL的更多相关文章

  1. QT特供 CGAL配置流程(基于QT5+VS2015)

    最近做的QT项目涉及计算几何库,需要用到CGAL,其配置着实麻烦,而且相互关联的软件也存在版本兼容一类的问题,在这里就对其配置流程做一些整理说明,以便后来者能够少些烦恼.(注:以下使用Win10作说明 ...

  2. 最近在 OS-10.9下配置opencv, cgal, latex, qt, pillow

    其实我之前使用的Mac os的版本是10.8的雪豹,可是最近想体验一下Mac os10.9新版本,于是就开始更新Mac os,经过10多个小时的下载和成功安装后,发现之前的配置全乱了,首先是发现lat ...

  3. 《zw版·Halcon-delphi系列原创教程》cgal与opencv,Halcon

    <zw版·Halcon-delphi系列原创教程>cgal与opencv,Halcon opencv作为少有的专业开源图像软件,虽然功能,特别是几何计算方面,不如Halcon,不过因为开源 ...

  4. CGAL Manual/tutorial_hello_world.html

    Hello World Author CGAL Editorial Board 本教程是为知道C++和几何算法的基本知识的CGAL新手准备的.第一节展示了如何特化点和段CGAL类,以及如何应用几何谓词 ...

  5. CGAL 介绍

    CGAL组织 内核 数值健壮 基础库 扩展性 2.4 命名约定 Naming In order to make it easier to remember what kind of entity a ...

  6. [CGAL]带岛多边形三角化

    CGAL带岛多边形三角化,并输出(*.ply)格式的模型 模型输出的关键是节点和索引 #include <CGAL/Triangulation_vertex_base_with_id_2.h&g ...

  7. CGAL学习:数据类型

    CGAL 4.13 - Number Types 1 Introduction(介绍:略) 涉及到的数大致有3种:一是整数,二是有理数,三是浮点数.有理数可以用2个整数表示.精度上可分为任意精度和固定 ...

  8. 2D Polygons( Poygon) CGAL 4.13 -User Manual

    1 Introduction A polygon is a closed chain of edges. Several algorithms are available for polygons. ...

  9. 2D Convex Hulls and Extreme Points( Convex Hull Algorithms) CGAL 4.13 -User Manual

    1 Introduction A subset S⊆R2 is convex if for any two points p and q in the set the line segment wit ...

  10. Linear and Quadratic Programming Solver ( Arithmetic and Algebra) CGAL 4.13 -User Manual

    1 Which Programs can be Solved? This package lets you solve convex quadratic programs of the general ...

随机推荐

  1. hex文件和bin文件区别

    HEX文件和BIN文件是我们经常碰到的2种文件格式.因为自己也是新手,所以一直对这两个文件懵懵懂懂,不甚了解,最近在做STM32单片机的IAP更新,其中要考虑HEX文件和BIN文件,所以需要学习下这两 ...

  2. mysql 获取一段时间的数据

    用 sql 获取一段时间内的数据: SELECT * FROM EDI.edi_history WHERE timestampdiff(day, SYSDATE(), create_time_loc) ...

  3. 跟我学算法-吴恩达老师的logsitic回归

    logistics回归是一种二分类问题,采用的激活函数是sigmoid函数,使得输出值转换为(0,1)之间的概率 A = sigmoid(np.dot(w.T, X) + b ) 表示预测函数 dz ...

  4. Cardboard Talk01 HeadTracker

    操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Android studio 3.0.0 | Cardboard 1.0 使用 Google 的 Cardboard开发V ...

  5. helm 安装 spinnaker

    $ curl -Lo values.yaml https://raw.githubusercontent.com/kubernetes/charts/master/stable/spinnaker/v ...

  6. Daylight Saving Time

    [Daylight Saving Time] 夏时制,又称日光节约时制.日光節約時間(英语:Daylight saving time)或夏令时间(英语:Summer time),是一种为节约能源而人为 ...

  7. ora-01045错误的解决办法

    问题: 在用PL/SQL进行登录时,出现:”ora-01045 :user system lacks create session privilege; logon denied”. 原因:该用户没有 ...

  8. 03-SSH综合案例:商城表关系分析

    1.2   设计页面: 静态页面: 1.3    数据库分析与设计: 一般是一个Java类就对应一个表,有哪些类?那看你有哪些实体啊?一般一个模块对应一个实体 有用户模块就会有用户的一个实体,就会有用 ...

  9. 流形学习 (Manifold Learning)

    流形学习 (manifold learning) zz from prfans............................... dodo:流形学习 (manifold learning) ...

  10. 38.Count and Say 报数

    [抄题]: The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 1 ...