3 About Kernels and Traits Classes
If you look at the manual page of the function convex_hull_2()
and
the other 2D convex hull algorithms, you see that they come in two versions. In the examples we have seen so far the function that takes two iterators for the range of input points and an output iterator for writing the result to. The second version has an
additional template parameter Traits
, and an additional parameter of this type.
template<class InputIterator , class OutputIterator , class Traits >
OutputIterator
convex_hull_2(InputIterator first,
InputIterator beyond,
OutputIterator result,
const Traits & ch_traits)
What are the geometric primitives a typical convex hull algorithm uses? Of course, this depends on the algorithm,
so let us consider what is probably the simplest efficient algorithm, the so-called "Graham/Andrew Scan". This algorithm first sorts the points from left to right, and then builds the convex hull incrementally by adding one point after another from the sorted
list. To do this, it must at least know about some point type, it should have some idea how to sort those points, and it must be able to evaluate the orientation of a triple of points.
And that is where the template parameter Traits
comes in. For ch_graham_andrew()
it
must provide the following nested types:
Traits::Point_2
Traits::Less_xy_2
Traits::Left_turn_2
Traits::Equal_2
Left_turn_2
is responsible for the orientation test, while Less_xy_2
is
used for sorting the points. The requirements these types have to satisfy are documented in full with the concept ConvexHullTraits_2
.
template <class InputIterator,
class OutputIterator,
class Point_2,
class Less_xy_2,
class Left_turn_2, class Equal_2>
ch_graham_andrew(
InputIterator first,
InputIterator beyond,
OutputIterator result);
#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 CGAL::Exact_predicates_inexact_constructions_kernel K3;
typedef CGAL::Projection_traits_yz_3<K3> K;
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;
}
相關文章
- Python高階特性(3): Classes和MetaclassesPython
- 理解 traitsAI
- Python3.4 Tutorial9 - Classes (Part 3)Python
- Type classes in Scala
- 【Scala】Scala之TraitsAI
- 探索Scala(2)-- TraitsAI
- About Enqueue:P1/P2/P3ENQ
- About HTMLHTML
- About interviewView
- About Personality
- About IndexDBIndex
- about bapiAPI
- about me
- PHP 物件導向 (十)TraitsPHP物件AI
- 從 Java 到 Scala(四):TraitsJavaAI
- C++ 萃取機 Iterator TraitsC++AI
- about datapump parallelParallel
- About Oracle WITH clauseOracle
- About Oracle LockOracle
- About post and get
- about histogram(2)Histogram
- about histogram(1)Histogram
- PHP中的traits使用詳解PHPAI
- Linux_java_呼叫classesLinuxJava
- DBReader/Classes/LogonGo
- STL迭代器的"特性萃取機"-----TraitsAI
- 《STL原始碼剖析》-- type_traits.h原始碼AI
- An example about git hookGitHook
- About Oracle HanganalyzeOracle
- All About PythonPython
- os staroge:about stripe
- error log about AIXErrorAI
- About SAP currency type
- about raw device[zt]dev
- About My Blog
- Trivia about pythonPython
- 【OCP最新題庫解析(052)--題3】Which two are true about external tables?
- PostgreSQL DBA(46) - PG Operator classes and familiesSQL