The Arithmetic Toolbox in...• Key design goals : –Genericity (app. domain independent...

31
Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR 2018-04-18/20 Computational Geometry Algorithms Library Sylvain Pion Mini-workshop on iRRAM/MPC/MPFR Schloß Dagstuhl April 18-20, 2018 The Arithmetic Toolbox in

Transcript of The Arithmetic Toolbox in...• Key design goals : –Genericity (app. domain independent...

Page 1: The Arithmetic Toolbox in...• Key design goals : –Genericity (app. domain independent algorithms) –Robustness –Efficiency Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR

Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR 2018-04-18/20

Computational Geometry Algorithms Library

Sylvain PionMini-workshop on iRRAM/MPC/MPFR Schloß Dagstuhl April 18-20, 2018

The Arithmetic Toolbox in

Page 2: The Arithmetic Toolbox in...• Key design goals : –Genericity (app. domain independent algorithms) –Robustness –Efficiency Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR

Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR 2018-04-18/20

Talk outline

• Brief overview of CGAL

• Robustness issues

• Arithmetic toolbox

Page 3: The Arithmetic Toolbox in...• Key design goals : –Genericity (app. domain independent algorithms) –Robustness –Efficiency Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR

Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR 2018-04-18/20

Brief overview ofCGAL

Page 4: The Arithmetic Toolbox in...• Key design goals : –Genericity (app. domain independent algorithms) –Robustness –Efficiency Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR

Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR 2018-04-18/20

CGAL project• 20 years old project started in European academia• 1M C++ template lines of geometric algorithms

• Open Source (GPL) and Commercial licenses

• >100 of commercial customers from :– CAD/CAM, VLSI, Digital maps, GIS, Medical,

Telecom, Geophysics (Oil&Gas)...• Key design goals :

– Genericity (app. domain independent algorithms)– Robustness– Efficiency

Page 5: The Arithmetic Toolbox in...• Key design goals : –Genericity (app. domain independent algorithms) –Robustness –Efficiency Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR

Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR 2018-04-18/20

Triangulations of Polygons

Page 6: The Arithmetic Toolbox in...• Key design goals : –Genericity (app. domain independent algorithms) –Robustness –Efficiency Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR

Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR 2018-04-18/20

Triangulations of Polygons

Page 7: The Arithmetic Toolbox in...• Key design goals : –Genericity (app. domain independent algorithms) –Robustness –Efficiency Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR

Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR 2018-04-18/20

Parametrizable Meshes

Without constraint Uniform Size function

Page 8: The Arithmetic Toolbox in...• Key design goals : –Genericity (app. domain independent algorithms) –Robustness –Efficiency Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR

Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR 2018-04-18/20

Union Intersection Complement

Support for :

Line segments, Circular arcs, Bezier curves

Boolean Operations

Page 9: The Arithmetic Toolbox in...• Key design goals : –Genericity (app. domain independent algorithms) –Robustness –Efficiency Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR

Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR 2018-04-18/20

Voronoi diagrams of line segments

Page 10: The Arithmetic Toolbox in...• Key design goals : –Genericity (app. domain independent algorithms) –Robustness –Efficiency Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR

Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR 2018-04-18/20

3D Triangulations • Delaunay and weighted

• Dynamic structure

• 1M points in 16s

Page 11: The Arithmetic Toolbox in...• Key design goals : –Genericity (app. domain independent algorithms) –Robustness –Efficiency Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR

Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR 2018-04-18/20

Volumic mesh (segmented image)

Page 12: The Arithmetic Toolbox in...• Key design goals : –Genericity (app. domain independent algorithms) –Robustness –Efficiency Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR

Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR 2018-04-18/20

Robustness-

ExactGeometricComputing

Page 13: The Arithmetic Toolbox in...• Key design goals : –Genericity (app. domain independent algorithms) –Robustness –Efficiency Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR

Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR 2018-04-18/20

Predicates and Constructions

Predicates Constructions

orientation in_circle intersection circumcenter

Page 14: The Arithmetic Toolbox in...• Key design goals : –Genericity (app. domain independent algorithms) –Robustness –Efficiency Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR

Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR 2018-04-18/20

Floating-point direct evaluation orientation(p,q,r) = sign((px-rx)(qy-ry)-(py-ry)(qx-rx))

negative zero positive

Page 15: The Arithmetic Toolbox in...• Key design goals : –Genericity (app. domain independent algorithms) –Robustness –Efficiency Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR

Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR 2018-04-18/20

Non-robustness problems

• Direct use of FP arithmetic causesalgorithms to– Produce [slightly] wrong output– Crash on invariant violation– Infinite loop

• Geometry is different – in theory– using FP arithmetic

Page 16: The Arithmetic Toolbox in...• Key design goals : –Genericity (app. domain independent algorithms) –Robustness –Efficiency Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR

Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR 2018-04-18/20

Partial solution : ε heuristics

« If value < ε, consider value == 0 »– Transitivity: p = q, q = r, p != r

– Does not generalize well– Not beautiful / mathematically appealing

Page 17: The Arithmetic Toolbox in...• Key design goals : –Genericity (app. domain independent algorithms) –Robustness –Efficiency Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR

Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR 2018-04-18/20

Better solution : Exact GeometricComputing paradigm • « Make sure decisions are taken as if real

arithmetic is used »– Theoretical proofs made in the Real-RAM

model therefore apply

Page 18: The Arithmetic Toolbox in...• Key design goals : –Genericity (app. domain independent algorithms) –Robustness –Efficiency Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR

Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR 2018-04-18/20

Exact Geometric ComputingFor the simple but common cases of rational expressions

• Use Multiple Precision instead of FP– Works, but can be >100 times slower

• Increase precision as needed (filtering) – Try Interval Arithmetic before MP

• 10 times slower than FP

– Try Static Error Analysis before IA• <2 times slower than FP

Page 19: The Arithmetic Toolbox in...• Key design goals : –Genericity (app. domain independent algorithms) –Robustness –Efficiency Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR

Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR 2018-04-18/20

Arithmetictoolbox

Page 20: The Arithmetic Toolbox in...• Key design goals : –Genericity (app. domain independent algorithms) –Robustness –Efficiency Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR

Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR 2018-04-18/20

Multiple precision• Integers, Dyadic (m.2ˆe), Rationals

• Implementations– LEDA, GMP– CGAL's own MP_Float, Quotient, Gmpzf

• MP_Float– Dyadic using signed limbs (no sign bit)– Quick implementation (O(n^2) mult...)

Page 21: The Arithmetic Toolbox in...• Key design goals : –Genericity (app. domain independent algorithms) –Robustness –Efficiency Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR

Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR 2018-04-18/20

Interval Arithmetic

• Double precision mostly

• CGAL's own Interval_nt– For portability, integrability, efficiency– Decreasing number of rounding mode

changes

• Gmpfi too (intervals based on MPFR)

Page 22: The Arithmetic Toolbox in...• Key design goals : –Genericity (app. domain independent algorithms) –Robustness –Efficiency Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR

Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR 2018-04-18/20

Static Error Analysis• For predicates that are signs of

polynomial expressions, like orientation()• Start from a bound on the input (known

or computed), quickly deduce themaximum error on the final value

• Tradeoffs precision/speed• Automatization (FPG, C++ ?)• Formal proofs (COQ+Gappa)

Page 23: The Arithmetic Toolbox in...• Key design goals : –Genericity (app. domain independent algorithms) –Robustness –Efficiency Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR

Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR 2018-04-18/20

Algebraic numbers• Example : sign(sqrt(2)ˆ2-2) ?

• Repeated refined numeric approximationwith separation bounds [LEDA, CORE]

• More demanding problems : algebraicmethods for polynomial systems (notvery functional in CGAL) [RS]

• Root_of_2

Page 24: The Arithmetic Toolbox in...• Key design goals : –Genericity (app. domain independent algorithms) –Robustness –Efficiency Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR

Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR 2018-04-18/20

Side note : Modular arithmetic

• Used for quick non-zero testing

Page 25: The Arithmetic Toolbox in...• Key design goals : –Genericity (app. domain independent algorithms) –Robustness –Efficiency Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR

Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR 2018-04-18/20

Generic programming• Layers : Arithmetic, Algebra, Geometry

Page 26: The Arithmetic Toolbox in...• Key design goals : –Genericity (app. domain independent algorithms) –Robustness –Efficiency Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR

Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR 2018-04-18/20

Concrete classes• Number Types

– double, float

– CGAL::Gmpq or leda::rational (rational)

– Core or leda::real (algebraic)

– CGAL::Lazy_exact_nt<ExactNT>

• Geometry Kernels– CGAL::Simple_cartesian<NT>

– CGAL::Filtered_kernel<Kernel>

– CGAL::Lazy_kernel<NT>

• Convenience Kernels– Exact_predicates_inexact_constructions_kernel– Exact_predicates_exact_constructions_kernel– Exact_predicates_exact_constructions_kernel_with_sqrt

Page 27: The Arithmetic Toolbox in...• Key design goals : –Genericity (app. domain independent algorithms) –Robustness –Efficiency Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR

Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR 2018-04-18/20

Filtered predicatesGeneric functor adaptor Filtered_predicate<P>

try {

evaluate predicate P<interval arithmetic>;

} catch(UncertaintyException e) {

evaluate predicate P<exact arithmetic>;

}

Page 28: The Arithmetic Toolbox in...• Key design goals : –Genericity (app. domain independent algorithms) –Robustness –Efficiency Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR

Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR 2018-04-18/20

Filtered constructions

[ ]

+

*

[ ]

3.2 1.5

13

if (collinear(a',m',b'))if ( n' < m' )

midpoint

intersect

projection

)[ ] ,[ ](

)[ ] ,[ ]( )[ ] ,[ ](

bs1 s2 l

m'

i' p'

p

a

s1

s2

l

mi

b

n'

Lazy numbers = interval + expression tree

Lazy object = approximate object + geometric operations tree

Test which can trigger an exact re-evaluation :

)3.2 + 1.5( * 13

Page 29: The Arithmetic Toolbox in...• Key design goals : –Genericity (app. domain independent algorithms) –Robustness –Efficiency Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR

Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR 2018-04-18/20

External libraries vs internal

• Reasons/arguments to decide when to use external librariesversus implement internally– Availability, portability, ease of installation– Efficiency– Stability, maturity– License, standardizability– Inertia, available time/motivation for change,

procrastination, whatever...

Page 30: The Arithmetic Toolbox in...• Key design goals : –Genericity (app. domain independent algorithms) –Robustness –Efficiency Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR

Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR 2018-04-18/20

Conclusion & Open issues

• A mix of FP and EGC works well for most CGAL algorithms

• Some open issues :– Depth of cascaded constructions needs to stay

reasonable– Topology preserving geometric rounding to the FP grid– Non-algebraic numbers– Static error analysis within C++ (machine precision ball

arithmetic?)– Formal proofs

Page 31: The Arithmetic Toolbox in...• Key design goals : –Genericity (app. domain independent algorithms) –Robustness –Efficiency Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR

Sylvain Pion Dagstuhl mini-workshop on iRRAM/MPC/MPFR 2018-04-18/20

Acknowledgements• Contributors to the CGAL arithmetic toolbox

– Olivier Devillers, Marc Glisse– Michael Hemmer, Luis Peñaranda– Monique Teillaud– CGAL editors and developers

• Special Thanks– Mariette Yvinec, Jean-Daniel Boissonnat– Andreas Fabri & GeometryFactory

• Arithmetic libraries : LEDA, GMP, CORE, MPFR, MPFI

• Standards : C++, IEEE-754, IEEE-1788