Geometric Computing: Introduction to...

52
Geometric Computing: Introduction to CGAL Panos Giannopoulos, Dror Atariah AG TI WS 2012/13

Transcript of Geometric Computing: Introduction to...

Geometric Computing: Introduction to CGAL

Panos Giannopoulos, Dror AtariahAG TI

WS 2012/13

!! Register in Campus Management !!

Outline

What you need to know

What is the course about?

Course structure

Let’s start!

Exercise 1

,FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 3

You need to know (or learn quickly)

É C++ (templates, STL iterators, containers, generic algorithms,etc.)

template< class InputIterator, class T >InputIterator find( InputIterator first, InputIterator last, const T& value );

or

std::vector<int> v;...for(std::vector<int>::iterator it = v.begin(); it != v.end(); ++it) {...}

É Algorithms (run-time analysis, O-notation, basic algorithmictechniques and data structures, etc.)

É Basic geometry

,FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 4

This course is about

É Computing with geometric objects (points, lines, segments,curves, planes, etc.)

É Implementing and using geometric algorithms and datastructures (efficiently and correctly)

É Using the Computational Geometry Algorithms Library (CGAL)(together with STL, Boost, Qt, etc.)É Basic packages: Convex Hull, Arrangements,

Triangulations, Voronoi Diagrams, etc.É Having Fun!

,FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 5

This course is about (for example)intersection point

Arrangement cell

query point

,FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 6

This course is about (for example)

É Bounding VolumesÉ TriangulationsÉ Surface Reconstruction

,FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 7

Course structure

É Lectures: 19, 26 Oct., 2 Nov., then every second weekÉ Be present in the lectures!É Bring your laptopÉ One concept, package, algorithm per lecture (approximately).

Then we’ll work on exercisesÉ Exercises will gradually become more involved.

By the end of Nov. - beginning Dec. each team should choosesome project

É Make teams of 1-3 peopleÉ Suggestion of projects also welcome!É You’ll be graded for the project

,FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 8

Let’s start!

Let’s start!(Many thanks to Efi Fogel from TAU, IL, for sharing the

next slides)

,FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 9

Geometric computing ([E. Fogel, TAU, IL, 2012])

Geometric Computing: The Goal

(Re)design and implement geometric algorithms and data structures thatare at once certified and efficient in practice.

Computational Geometry Algorithm Library 3,

FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 10

Geometric computing ([E. Fogel, TAU, IL, 2012])

Geometric Computing: The Assumptions

Input data is in general position

Degenerate input, e.g., three curves intersecting at a common point, isprecluded.

Computational model: the real RAM

Operations on real numbers yield accurate results.

Each basic operation on a small (constant-size) set of simple objectstakes unit time.

Computational Geometry Algorithm Library 4,

FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 10

Geometric computing ([E. Fogel, TAU, IL, 2012])

Geometric Computing: The Problems

These assumptions often do not hold in practice

Degenerate input is commonplace in practical applications.

Numerical errors are inevitable while using standard computerarithmetic.

Naive use of floating-point arithmetic causes geometric programs to:

Crash after invariant violationEnter an infinite loopProduce wrong output

There is a gap between Geometry in theory and Geometry withfloating-point arithmetic.

Standard cs-theory asymptotic performance measures many times poorpredictors of practical performance.

Computational Geometry Algorithm Library 5,

FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 10

Geometric computing ([E. Fogel, TAU, IL, 2012])

Geometry in Theory

orientation(p, q, r) = sign

det

px py 1qx qy 1rx ry 1

=

< 0 cw(p, q, r)= 0 colinear(p, q, r)> 0 ccw(p, q, r)

= sign((qx − px)(ry − py )− (qy − py )(rx − px))

ccw(p, q, s) ∩ ccw(s, q, r) ∩ ccw(p, s, r)⇒ ccw(p, q, r)

s

rp

q

Computational Geometry Algorithm Library 6,

FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 10

Geometric computing ([E. Fogel, TAU, IL, 2012])

Geometry in Practice: Trouble with Double

orientation(p, q, r) = sign((qx − px)(ry − py )− (qy − py )(rx − px))

256× 256 pixel image r(24, 24)

q(12, 12)

x=y

(0.5, 0.5) p(0.5 + x · ǫ, 0.5 + y · ǫ)0 ≤ x , y ≤ 256, ǫ = 2−53

Positive Zero Negative

[KMP+08]

Computational Geometry Algorithm Library 7,

FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 10

Geometric computing ([E. Fogel, TAU, IL, 2012])

The Naive Solution: Exact Multi-Precision Arithmetic

Implemented for several number types:

Integers, rational (e.g., GMP, CORE, and LEDA)Even algebraic numbers (e.g., CORE and LEDA)No solution for transcendental numbers!

Exact up to memory limit.

Slow running time.

Computational Geometry Algorithm Library 8,

FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 10

Geometric computing ([E. Fogel, TAU, IL, 2012])

The Efficient Solution: Exact Geometric Computation

Ensure that the control flow in the implementation corresponds to thecontrol flow with exact arithmetic. [Yap04]

Evaluate predicate instantiated with limited precision.

If uncertain =⇒ evaluate predicate instantiated with multipleprecision.

orientation(p, q, r)orientation(p, q, r)

< 0 = 0 > 0

Computational Geometry Algorithm Library 9,

FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 10

Geometric computing ([E. Fogel, TAU, IL, 2012])

Floating-Point Arithmetic

A double float f uses 64 bits

1 bit for the sign s.52 bits for the mantissa m = m1 . . .m52.11 bits for the exponent e = e1 . . . e52.

f = −1s · (1 + Σ1≤i≤52mi2−i ) · 2e−2013, if 0 < e < 211 − 1

. . .

Notation

For a ∈ R, let fl(a) denote the closest float to a.For a ∈ Z, |a − fl(a)| ≤ ǫ|fl(a)|, where ǫ = 2−53.For o ∈ {+,−,×}, |f1of2| ≤ ǫ|f1of2|.

Floating-point arithmetic is monotone.

e.g., b ≤ c ⇒ a⊕ b ≤ a⊕ c .

Computational Geometry Algorithm Library 11

,FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 10

Geometric computing ([E. Fogel, TAU, IL, 2012])

Geometric-Computing Bibliography

Lutz Kettner, Kurt Mehlhorn, Sylvain Pion, Stefan Schirra, and Chee K. Yap.

Classroom Examples of Robustness Problems in Geometric Computations.Computational Geometry: Theory and Applications, 40(1):61–78, 2008.

Chee K. Yap.

Robust geomtric computation.In Jacob E. Goodman and Joseph O’Rourke, editors, Handbook of Discrete and Computational Geometry, chapter 41,pages 927–952. Chapman & Hall/CRC, Boca Raton, FL, 2nd edition, 2004.

Kurt Mehlhorn and Stefan Naher.

Leda: A Platform for Combinatorial and Geometric Computing.Cambridge University Press, Cambridge, UK, 2000.

Computational Geometry Algorithm Library 13,

FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 10

Generic programming ([E. Fogel, TAU, IL, 2012])

Generic Programming Paradigm

Definition (Generic Programming)

A discipline that consists of the gradual lifting of concrete algorithmsabstracting over details, while retaining the algorithm semantics andefficiency.

[MS88]

Computational Geometry Algorithm Library 14,

FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 11

Generic programming ([E. Fogel, TAU, IL, 2012])

Generic Programming Paradigm

Definition (Generic Programming)

A discipline that consists of the gradual lifting of concrete algorithmsabstracting over details, while retaining the algorithm semantics andefficiency.

[MS88]

Translation:

You do not want to write the same algorithm again and again !

Computational Geometry Algorithm Library 15,

FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 11

Generic programming ([E. Fogel, TAU, IL, 2012])

Generic Programming Paradigm

Definition (Generic Programming)

A discipline that consists of the gradual lifting of concrete algorithmsabstracting over details, while retaining the algorithm semantics andefficiency.

[MS88]

Translation:

You do not want to write the same algorithm again and again !

⇒ You even want to make it independent from the used types.

See also: http://en.wikipedia.org/wiki/Generic programming

Computational Geometry Algorithm Library 16,

FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 11

Generic programming ([E. Fogel, TAU, IL, 2012])

Terms and Definitions

Class Template A specification for generating (instantiating) classes basedon parameters.

Function Template A specification for generating (instantiating) functionsbased on parameters.

Template Parameter

Specialization A particular instantiation from a template for for a givenset of template parameters.

Computational Geometry Algorithm Library 17,

FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 11

Generic programming ([E. Fogel, TAU, IL, 2012])

Generic Programming Dictionary

Concept A set of requirements that a class must fulfill.

Model A class that fulfills the requirements of a concept.

Traits Models that describe behaviors.

Refinement An extension of the requirements of another concept.

Generalization A reduction of the requirements of another concept.

Computational Geometry Algorithm Library 18,

FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 11

Generic programming ([E. Fogel, TAU, IL, 2012])

Some Generic Programming Libraries

Stl The C++ Standard Template Library.

Boost A large set of portable and high quality C++ libraries thatwork well with, and are in the same spirit as, the C++ Stl.

Leda The Library of efficient data types and algorithms.

Cgal The computational geometry algorithms and data structureslibrary.

Computational Geometry Algorithm Library 19,

FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 11

Generic programming ([E. Fogel, TAU, IL, 2012])

Stl Components

Container A class template, an instance of which stores collection ofobjects.

Iterator Generalization of pointers; an object that points to anotherobject.

Algorithm

Function Object (Functor) A computer programming construct invoked asthough it were an ordinary function.

Adaptor A type that transforms the interface of other types.

Allocator An objects for allocating space.

Computational Geometry Algorithm Library 20,

FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 11

Generic programming

template< class InputIterator, class T >InputIterator find( InputIterator first, InputIterator last, const T& value );

or

std::vector<int> v;...for(std::vector<int>::iterator it = v.begin(); it != v.end(); ++it) {...}

,FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 12

Generic programming ([E. Fogel, TAU, IL, 2012])

Generic Algorithms

A generic algorithm has 2 parts:

The actual instructions that describe the steps of the algorithm.A set of requirements that specify which properties its argument typesmust satisfy.

Computational Geometry Algorithm Library 21

,FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 13

Generic programming ([E. Fogel, TAU, IL, 2012])

A Trivial Example: swap ( )✞ ☎t emp la t e <typename T> vo i d swap (T& a , T& b ){ T tmp( a ) ; a = b ; b = tmp ; }✝ ✆

When a function call is compiled the function template is instantiated.

The template parameter T is substituted with a data type.

The data type must have1 a copy constructor, and2 an assignment operator.

Computational Geometry Algorithm Library 22,

FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 13

Generic programming ([E. Fogel, TAU, IL, 2012])

A Trivial Example: swap ( )✞ ☎t emp la t e <typename T> vo i d swap (T& a , T& b ){ T tmp( a ) ; a = b ; b = tmp ; }✝ ✆

When a function call is compiled the function template is instantiated.

The template parameter T is substituted with a data type.

The data type must have1 a copy constructor, and2 an assignment operator.

In formal words:

T is a model of the concept CopyConstructible.T is a model of the concept Assignable.

Computational Geometry Algorithm Library 23,

FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 13

Generic programming ([E. Fogel, TAU, IL, 2012])

A Trivial Example: swap ( )✞ ☎t emp la t e <typename T> vo i d swap (T& a , T& b ){ T tmp( a ) ; a = b ; b = tmp ; }✝ ✆

When a function call is compiled the function template is instantiated.

The template parameter T is substituted with a data type.

The data type must have1 a copy constructor, and2 an assignment operator.

In formal words:

T is a model of the concept CopyConstructible.T is a model of the concept Assignable.

The i n t data type is a model of the 2 concepts.✞ ☎i n t a = 2 , b = 4 ; s t d : : swap ( a , b ) ;✝ ✆

Computational Geometry Algorithm Library 24,

FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 13

Generic programming ([E. Fogel, TAU, IL, 2012])

Concept

A concept is a set of requirements divided into four categories:

Computational Geometry Algorithm Library 25,

FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 13

Generic programming ([E. Fogel, TAU, IL, 2012])

Concept

A concept is a set of requirements divided into four categories:

Associated Types — auxiliary types, for example

Po i n t 2 — a type that represents a two-dimensional point.

Computational Geometry Algorithm Library 26,

FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 13

Generic programming ([E. Fogel, TAU, IL, 2012])

Concept

A concept is a set of requirements divided into four categories:

Associated Types — auxiliary types, for example

Po i n t 2 — a type that represents a two-dimensional point.

Valid Expressions — C++ expressions that must compile successfully, forexample

p = q, where p and q are objects of type Po i n t 2 .

Computational Geometry Algorithm Library 27,

FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 13

Generic programming ([E. Fogel, TAU, IL, 2012])

Concept

A concept is a set of requirements divided into four categories:

Associated Types — auxiliary types, for example

Po i n t 2 — a type that represents a two-dimensional point.

Valid Expressions — C++ expressions that must compile successfully, forexample

p = q, where p and q are objects of type Po i n t 2 .

Runtime Characteristics — characteristics of the variables involved inthe valid expressions that apply during the variables’ lifespans,

pre/post-conditions.

Computational Geometry Algorithm Library 28,

FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 13

Generic programming ([E. Fogel, TAU, IL, 2012])

Concept

A concept is a set of requirements divided into four categories:

Associated Types — auxiliary types, for example

Po i n t 2 — a type that represents a two-dimensional point.

Valid Expressions — C++ expressions that must compile successfully, forexample

p = q, where p and q are objects of type Po i n t 2 .

Runtime Characteristics — characteristics of the variables involved inthe valid expressions that apply during the variables’ lifespans,

pre/post-conditions.

Complexity Guarantees — maximum limits on the computing resourcesconsumed by the valid expressions.

Computational Geometry Algorithm Library 29,

FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 13

Generic programming ([E. Fogel, TAU, IL, 2012])

Generic Programming & the Stl BibliographyAndrei Alexandrescu.Modern C++ Design: Generic Programming And Design Patterns Applied.Addison-Wesley, Boston, MA, USA, 2001.

Matthew H. Austern.Generic Programming and the Stl.Addison-Wesley, Boston, MA, USA, 1999.

Erich Gamma, Richard Helm, Ralph Johnson, and John M. Vlissides.Design Patterns — Elements of Reusable Object-Oriented Software.Addison-Wesley, Boston, MA, USA, 1995.

David R. Musser, Gillmer J. Derge, and Atul Saini.Stl tutorial and reference guide: C++ programming with the standard template library.

Addison-Wesley, Boston, MA, USA, 2nd edition, Professional Computing Series, 2001.

David Vandevoorde and Nicolai M. Josuttis.C++ Templates: The Complete Guide,Addison-Wesley, Boston, MA, USA, 2002.

Jeremy G. Siek, Lie-Quan Lee, and Andrew Lumsdaine.The Boost Graph Library,Addison-Wesley, Boston, MA, USA, 2002.

David A. Musser and Alexander A. Stepanov.Generic programming.In Proceedings of International Conference on Symbolic and Algebraic Computation, volume 358 of LNCS, pages 13–25.Springer, 1988.

The planned new standard for the C++ programming language.http://en.wikipedia.org/wiki/C++0x#References .

The Sgi STL.http://www.sgi.com/tech/stl/ .

Computational Geometry Algorithm Library 30,

FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 13

CGAL ([E. Fogel, TAU, IL, 2012])

Cgal: Mission

“Make the large body of geometric algorithms developed in the field ofcomputational geometry available for industrial applications”

Cgal Project Proposal, 1996

Computational Geometry Algorithm Library 31 ,FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 14

CGAL ([E. Fogel, TAU, IL, 2012])

Some of Cgal Content

Bounding Volumes Polyhedral Surfaces Boolean Operations

Triangulations Voronoi Diagrams Mesh Generation

Subdivision Simplification Parametrisation Streamlines Ridge Detection Neighbor Search Kinetic Data Structures

Envelopes Arrangements Intersection Detection Minkowski Sums PCA Polytope Distance QP Solver

Computational Geometry Algorithm Library 32 ,FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 14

CGAL ([E. Fogel, TAU, IL, 2012])

Cgal Facts

Written in C++

Follows the generic programming paradigm

Development started in 1995

Active European sites:

1 INRIA Sophia Antipolis

2 MPII Saarbrucken

3 Tel Aviv University

4 ETH Zurich (Plageo)

5 University of Crete and FO.R.T.H.

6 INRIA Nancy

7 Universite Claude Bernard de Lyon

8 ENS Paris

9 University of Eindhoven

10 University of California, San Francisco

11 University of Athens

Computational Geometry Algorithm Library 34 ,FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 14

CGAL ([E. Fogel, TAU, IL, 2012])

Cgal in Numbers

900,000 lines of C++ code10,000 downloads per year not including Linux distributions3,500 manual pages3,000 subscribers to cgal-announce list1,000 subscribers to cgal-discuss list120 packages60 commercial users25 active developers6 months release cycle7 Google’s page rank for cgal.org.com2 licenses: Open Source and commercial

Computational Geometry Algorithm Library 36 ,FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 14

CGAL ([E. Fogel, TAU, IL, 2012])Cgal Properties

Reliability

Explicitly handles degeneraciesFollows the Exact Geometric Computation (EGC) paradigm

Flexibility

Is an open libraryDepends on other libraries (e.g., Boost, Gmp, Mpfr, Qt, & Core)Has a modular structure, e.g., geometry and topology are separatedIs adaptable to user codeIs extensible, e.g., data structures can be extended

Ease of Use

Has didactic and exhaustive ManualsFollows standard concepts (e.g., C++ and Stl)Characterizes with a smooth learning-curve

EfficiencyAdheres to the generic-programming paradigm

⋆ Polymorphism is resolved at compile time

Computational Geometry Algorithm Library 38,

FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 14

CGAL ([E. Fogel, TAU, IL, 2012])

Cgal Structure

Basic Library

Algorithms and Data Structurese.g., Triangulations, Surfaces, and Arrangements

Kernel

Elementary geometric objectsElementary geometric computations on them

Support Library

Configurations, Assertions,...

Visualization

Files

I/O

Number Types

Generators

...

Computational Geometry Algorithm Library 39

,FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 14

CGAL ([E. Fogel, TAU, IL, 2012])

Cgal Kernel Concept

Geometric objects of constant size.

Geometric operations on object of constant size.

Primitives 2D, 3D, dDOperations

Predicates Constructions

point comparison intersectionvector orientation squared distancetriangle containment . . .iso rectangle . . .circle

. . .

Computational Geometry Algorithm Library 40

,FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 15

CGAL ([E. Fogel, TAU, IL, 2012])

Cgal Kernel Affine Geometry

point - origin → vectorpoint - point → vectorpoint + vector → point

point + point ← Illegalmidpoint(a, b) = a+ 1/2× (b − a)

Computational Geometry Algorithm Library 41

,FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 16

CGAL ([E. Fogel, TAU, IL, 2012])

Cgal Kernel Classification

Dimension: 2, 3, arbitrary

Number types:

Ring: +,−,×Euclidean ring (adds integer division and gcd) (e.g., CGAL : : Gmpz).Field: +,−,×,/ (e.g., CGAL : : Quot i en t (CGAL : : Gmpz)).Exact sign evaluation for expressions with roots ( F i e l d w i t h s q r ).

Coordinate representation

Cartesian — requires a field number type or Euclidean ring if noconstructions are performed.Homegeneous — requires Euclidean ring.

Reference counting

Exact, Filtered

Computational Geometry Algorithm Library 42

,FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 16

CGAL ([E. Fogel, TAU, IL, 2012])

Cgal Kernels and Number Types

Cartesian representation Homogeneous representation

point

∣∣∣∣x = hx

hw

y = hyhw

point

∣∣∣∣∣∣

hxhyhw

Intersection of two lines{a1x + b1y + c1 = 0a2x + b2y + c2 = 0

{a1hx + b1hy + c1hw = 0a2hx + b2hy + c2hw = 0

(x , y) = (hx , hy , hw) =

∣∣∣∣∣∣b1 c1b2 c2

∣∣∣∣∣∣∣∣∣∣∣∣a1 b1a2 b2

∣∣∣∣∣∣

,−

∣∣∣∣∣∣a1 c1a2 c2

∣∣∣∣∣∣∣∣∣∣∣∣a1 b1a2 b2

∣∣∣∣∣∣

(∣∣∣∣b1 c1b2 c2

∣∣∣∣ ,−∣∣∣∣a1 c1a2 c2

∣∣∣∣ ,∣∣∣∣a1 b1a2 b2

∣∣∣∣)

Field operations Ring operations

Computational Geometry Algorithm Library 43

,FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 16

CGAL ([E. Fogel, TAU, IL, 2012])

Example: Ke rne l s<NumberType>

Ca r t e s i a n<FieldNumberType>

t y p ed e f CGAL : : Ca r t e s i a n<Gmpq> Kerne l ;t y p ed e f CGAL : : S imp l e c a r t e s i a n<double> Kerne l ;

⋆ No reference-counting, inexact instantiation

Homogeneous<RingNumberType>

t y pd e f CGAL : : Homogeneous<Core : : B i g In t> Kerne l ;

d-dimensional C a r t e s i a n d and Homogeneous d

Types + Operations

Kerne l : : Po i n t 2 , Ke rne l : : Segment 3Ke rne l : : L e s s x y 2 , Ke rne l : : C o n s t r u c t b i s e c t o r 3

Computational Geometry Algorithm Library 44

,FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 16

CGAL ([E. Fogel, TAU, IL, 2012])

Cgal Numerical Issues

✞ ☎t y p ed e f CGAL : : Ca r t e s i a n<NT> Kerne l ;NT sq r t 2 = s q r t (NT( 2 ) ) ;

Ke rne l : : Po i n t 2 p ( 0 , 0 ) , q ( sq r t2 , s q r t 2 ) ;Ke rne l : : C i r c l e 2 C(p , 4 ) ;

a s s e r t (C . has on bounda ry ( q ) ) ;✝ ✆OK if NT supports exact sqrt.

Assertion violation otherwise.

Computational Geometry Algorithm Library 45

,FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 16

CGAL ([E. Fogel, TAU, IL, 2012])

Computing the Intersection

✞ ☎t y p ed e f Ke rne l : : L i n e 2 L i n e 2 ;

i n t main ( ) {Kerne l k e r n e l ;Po i n t 2 p (1 , 1 ) , q ( 2 , 3 ) , r (−12 ,19);L i n e 2 l 1 (p , q ) , l 2 ( r , p ) ;i f ( d o i n t e r s e c t ( l1 , l 2 ) ) {

CGAL : : Objec t ob j = CGAL : : i n t e r s e c t i o n ( l1 , l 2 ) ;i f ( con s t Po i n t 2∗ po i n t = ob j e c t c a s t<Point 2>(&ob j ) ) {

/∗ do someth ing wi th po i n t ∗/} e l s e i f ( con s t Segment 2∗ segment = ob j e c t c a s t<Segment 2>(&ob j ) ) {

/∗ do someth ing wi th segment ∗/}

}r e t u r n 0 ;

}✝ ✆

Computational Geometry Algorithm Library 49

,FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 16

CGAL ([E. Fogel, TAU, IL, 2012])

Cgal Basic Library

Generic data structures are parameterized with Traits

Separates algorithms and data structures from the geometric kernel.

Generic algorithms are parameterized with iterator ranges

Decouples the algorithm from the data structure.

Computational Geometry Algorithm Library 50

,FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 16

CGAL ([E. Fogel, TAU, IL, 2012])

Cgal Bibliography

A. Fabri, G.-J. Giezeman, L. Kettner, S. Schirra, and S. Schonherr.

On the design of Cgal a computational geometry algorithms library.Software — Practice and Experience, 30(11):1167–1202, 2000. Special Issue on Discrete Algorithm Engineering.

A. Fabri and S. Pion.

A generic lazy evaluation scheme for exact geometric computations.

In 2nd Library-Centric Software Design Workshop, 2006.

M. H. Overmars.

Designing the computational geometry algorithms library Cgal.In Proceedings of ACM Workshop on Applied Computational Geometry, Towards Geometric Engineering, volume 1148,pages 53–58, London, UK, 1996. Springer.

The Cgal Project.

Cgal User and Reference Manual.Cgal Editorial Board, 3.9 edition, 2010. http://www.cgal.org/Manual/3.9/doc_html/cgal_manual/contents.html .

Efi Fogel, Ron Wein, and Dan Halperin.

Cgal Arrangements and Their Applications, A Step-by-Step Guide.Springer, 2012.

Computational Geometry Algorithm Library 51

,FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 16

Exercise 1

1. Install CGAL: www.cgal.org(Installation tips also on the course’s website)

2. Read CGAL’s manual: Chapters 1 (Introduction), 2 (Preliminaries),3 (Installation), 5 (Number Types), 11 (2D and 3D Geometry Kernel)

3. For the following use the Cartesian kernel with number type double.Use only geometric primitives from the kernel. Do not use any kerneloperation (predicate or construction).É Write a function that takes two segments in the plane as

arguments and returns: 1 if the segments intersect, 0 if theydon’t.

É Write a function that takes two segments in the plane asarguments and computes their intersection (if it exists).

É Write a function that takes a point and a segment in the plane asarguments and returns 1 if the point lies on the segment and 0otherwise.

É Test your functions.

4. Suggested reading: Computational Geometry in C (Second Edition), J.O’Rourke. Cambridge University Press, 1998.

,FU Berlin, Geometric Computing: Introduction to CGAL, WS 2012/13 17