CGAL - the Computational Geometry Algorithm Library


History


Architecture


Flexibility


Correctness


Ease of use


Efficiency


Geometric Kernel Classification


Geometric Kernel (easy) Choices

CGAL::Exact_predicates_inexact_constructions_kernel
CGAL::Exact_predicates_exact_constructions_kernel
CGAL::Exact_predicates_exact_constructions_kernel_with_sqrt

Orientation of a triple: Version 1

#include <CGAL/MP_Float.h>
#include <CGAL/Homogeneous.h>

typedef CGAL::Homogeneous<CGAL::MP_Float>  Kernel
typedef Kernel::Point_2                    Point_2

int main()
{
  Point_2 p(0,0), q(10,3), r(12,19);
  if (CGAL::orientation(q,p,r) == CGAL::LEFT_TURN)
    return 0;
  return 1
}

Orientation of a triple: Version 2

#include <CGAL/MP_Float.h>
#include <CGAL/Homogeneous.h>

typedef CGAL::Homogeneous<CGAL::MP_Float>  Kernel
typedef Kernel::Point_2                    Point_2
typedef Kernel::Orientation_2              Orientation_2;

int main()
{
  Kernel kernel;
  Orientation_2 orientation = Kernel.orientation_2_object();

  Point_2 p(0,0), q(10,3), r(12,19);
  if (orientation(q,p,r) == CGAL::LEFT_TURN)
    return 0;
  return 1
}

Basic Library


Convex Hull Definition

A subset S 2 is convex if for any two points p and q in the set the line segment with endpoints p and q is contained in S. The convex hull of a set S is the smallest convex set containing S. The convex hull of a set of points P is a convex polygon with vertices in P.

Upper Convex Hull: Version 1

template <class Iterator, class Output, class Traits>
Output
upper_hull1(Iterator first, Iterator beyond, Output result, Traits traits)
{ 
  typename Traits::Less_xy_2 less_xy = traits.less_xy_2_object();
  typename Traits::Left_turn_2 left_turn = traits.left_turn_2_object();
  
  if (first == beyond)                  // empty sequence
    return result;
  std::sort(first, beyond, less_xy);    // lexicographic sorting
  std::vector<typename Traits::Point_2> hull;
  hull.push_back(*first);               // sentinel
  hull.push_back(*first);

  // upper convex hull (left to right)
  for (++first; first != beyond; ++first) {
    while (left_turn(hull.end()[-2], hull.back(), *first))
      hull.pop_back();
    hull.push_back(*first);
  }
  return std::copy(hull.begin() + 1, hull.end(), result); // return result
}
upper_hull_2.C

Upper Convex Hull: Version 2

template <class Iterator, class Output, class Traits>
Output
upper_hull2(Iterator first, Iterator beyond, Output result, Traits traits)
{ 
  typename Traits::Less_xy_2 less_xy = traits.less_xy_2_object();
  typename Traits::Left_turn_2 left_turn = traits.left_turn_2_object();
    
  if (first == beyond)                  // empty sequence
    return result;
  std::sort(first, beyond, less_xy);    // lexicographic sorting
  std::vector<typename Traits::Point_2> hull;
  hull.push_back(*first);               // sentinel
  typename Traits::Point_2 p = *first;  // last point on the hull
  ++first;                              // next candidate for hull point
  // scan all points from left to right for upper convex hull
  for (; first != beyond; ++first) {
    while (left_turn(hull.back(), p, *first)) {
      p = hull.back();
      hull.pop_back();
    }
    hull.push_back(p);
    p = *first;
  }
  hull.push_back(p);
  return std::copy(hull.begin() + 1, hull.end(), result); // return result
}
upper_hull_2.C

Upper Convex Hull: Version 3

template <class Iterator, class Output, class Traits>
Output
upper_hull3(Iterator first, Iterator beyond, Output result, Traits traits)
{ 
  typename Traits::Less_xy_2 less_xy = traits.less_xy_2_object();
  typename Traits::Left_turn_2 left_turn = traits.left_turn_2_object();
    
  if (first == beyond)                  // empty sequence
    return result;
  std::sort(first, beyond, less_xy);    // lexicographic sorting
  std::vector<Iterator> hull;
  hull.push_back(first);                // sentinel
  Iterator p = first;                   // last point on the hull
  ++first;                              // next candidate for hull point
  // scan all points from left to right for upper convex hull
  for (; first != beyond; ++first) {
    while (left_turn(*(hull.back()), *p, *first)) {
      p = hull.back();
      hull.pop_back();
    }
    hull.push_back(p);
    p = first;
  }
  typename std::vector<Iterator>::iterator i = hull.begin();
  ++i;                                  // skip sentinel
  while (i != hull.end())
    *result++ = **i++;
  return result;
}
upper_hull_2.C

Upper Convex Hull: Version 4

template <class Iterator, class Pred>   // bidirectional iterator
Iterator remove_if_triple(Iterator first, Iterator beyond, Pred pred)
{ 
  if (first == beyond)
    return first;
  // setup iterators i,j,k for scanning triples
  Iterator i = first, j = first;
  if (++j == beyond)    // i,j mark two elements on top of stack
    return j;
  Iterator k = j;       // k marks the third value in the sequence
  while (++k != beyond) {
    while ((pred(*i, *j, *k))) { 
      j = i;            // remove one element from stack
      if (i == first)   // explicit test for stack underflow
        break;
      --i;              // remove one element from the stack, part 2
    }
    i = j;              // push new element from k on stack
    ++j;
    *j = *k;
  }
  return ++j;
}

template <class Iterator, class Output, class Traits>
Output upper_hull(Iterator first, Iterator beyond, Output result, Traits traits)
{ 
  typename Traits::Less_xy_2 less_xy = traits.less_xy_2_object();
  typename Traits::Left_turn_2 left_turn = traits.left_turn_2_object();
    
  if (first == beyond)              // empty sequence
    return result;
  std::sort(first, beyond, less_xy); // lexicographic sorting
  Iterator hull_end = remove_if_triple(first, beyond, left_turn);
  return std::copy(first, hull_end, result);
}
upper_hull_2.C

Exercise Notes


Installing CGAL


Functions, Classes, and Libraries

top prev


Last modified: November 20 2006.