L2C ComputationalGeometry
L2C ComputationalGeometry
for Engineering
Numerical simulation
in technical sciences
Computational Geometry
Graz, Austria
June 2014
Contents
• References and sources
• Introduction and scope
• The need for data structures
• Definitions and notations
• Oriented Area of Polygons
• Polygons Tessellation
• Geometric Predicates
• Exact and Adaptive Arithmetic
• Closest Point at a Straight Segment
• Segment Intersection
• Point in Polygon Verification
2
References and Sources
References and Sources
[OROURKE98]
Joseph O’Rourke
Computational Geometry in C
Cambridge University Press, 1998
[BERG97]
M. de Berg, M. van Kreveld, M. Overmars, O. Schwarzkopf
Computational Geometry - Algorithms and Applications
Springer, 1997
[PREPARATA85]
Franco P. Preparata, Michael Ian Shamos
Computational Geometry – An Introduction
Springer-Verlag, 1985
4
References and Sources
[SCHNEIDER03]
Philip Schneider and David Eberly
Geometric Tools for Computer Graphics
Elsevier, 2003
[SKIENA02]
Steven S. Skiena, Miguel A. Revilla
Programming Challenges
Springer, 2002
[GHALI08] [VINCE05]
Sherif Ghali John Vince
Introduction to Geometric Computing Geometry for Computer Graphics
Springer, 2008 Springer, 2005
5
Introduction and Scope
Introduction and Scope
Fonte: [OROURKE98]
7
Introduction and Scope
Fonte: [BERG97]
The field of computational geometry is quite large and is one of the most
rapidly advancing fields in recent times. This chapter is by no means
comprehensive. The general topics covered are binary space-partitioning (BSP)
trees in two and three dimensions, point-in-polygon and point-in-polyhedron
tests, convex hulls of finite point sets, Delaunay triangulation in two and three
dimensions, partitioning of polygons into convex pieces or triangles, containment
of point sets by circles or oriented boxes in two dimensions and by spheres or
oriented boxes in three dimensions, area calculations of polygons, and volume
calculations of polyhedra.
The emphasis is, of course, on algorithms to implement the various ideas.
However, attention is given to the issues of computation when done within a
floating point number system. Particular themes arising again and again are
determining when points are collinear, coplanar, cocircular, or cospherical. This is
easy to do when the underlying computational system is based on integer
arithmetic, but quite problematic when floating-point arithmetic is used.
10
The Need for Data Structures
Data structures can be classified on the basis of the operations they support
(regardless of efficiency). Thus for ordered sets we have the following table.
17
For efficiency, each of these data structures is normally realized as a height-
balanced binary search tree (often an A VL or a 2-3-tree). With this realization,
each of the above operations is performed in time proportional to the logarithm
of the number of elements stored in the data structure; the storage is
proportional to the set size.
18
The above data structures can be viewed abstractly as a linear array of
elements (a list), so that insertions and deletions can be performed in an
arbitrary position of the array. In some cases, some more restrictive modes of
access are adequate for some applications, with the ensuing simplifications.
Such structures are: Queues, where insertions occur at one end and
deletions at the other; Stacks, where both insertions and deletions occur at one
end (the stack-top). Clearly, one and two pointers are all that is needed for
managing a stack or a queue, respectively.
Unordered sets can always be handled as ordered sets by artificially imposing
an order upon the elements (for example, by giving "names" to the elements
and using the alphabetical order). A typical data structure for this situation is the
following.
19
Definitions and Notations
Definitions and Notations
The objects considered in Computational Geometry are normally sets of
points in Euclidean space. 3 A coordinate system of reference is assumed, so that
each point is represented as a vector of cartesian coordinates of the appropriate
dimension. The geometric objects do not necessarily consist of finite sets of
points, but must comply with the convention to be finitely specifiable (typically,
as finite strings of parameters). So we shall consider, besides individual points,
the straight line containing two given points, the straight line segment defined by
its two extreme points, the plane containing three given points, the polygon
defined by an (ordered) sequence or points, etc.
This section has no pretence of providing formal definitions of the geometric
concepts used in this book; it has just the objectives of refreshing notions that
are certainly known to the reader and of introducing the adopted notation.
By Ed we denote the d-dimensional Euclidean space, i.e., the space of the d-
tuples (x1,...,xd) of real numbers x1, i = 1,..., d with metric (Σdi=1 xi2)1/2.
We shall now review the definition of the principal objects considered by
Computational Geometry.
Fonte: [PREPARATA85]
Point. A d-tuple (x1,...,xd) denotes a point p of Ed; this point may be also
interpreted as a d-component vector applied to the origin of Ed, whose free
terminus is the point p.
Line, plane, linear variety. Given two distinct points q1 and q2 in Ed, the linear
combination
α q1 + (1 – α) q2 (α ∈ R)
is a line in Ed. More generally, given k linearly independent points q1, . . . , qk in Ed
(k ≤ d), the linear combination
α1 q1 + α2 q2 + ... + α k-1qk-1 + (1 – α1 – ... – αk-1) qk
(αj ∈ R , j = 1, ... , k – 1)
is a linear variety of dimension (k – 1) in Ed.
Line segment. Given two distinct points q1 and q2 in Ed, if in the expression
αq1+(1 – α)q2 we add the condition 0 ≤ α ≤ 1, we obtain the convex combination
of q1 and q2, i.e.,
α q1 + (1 – α) q2 (α ∈ R , 0 ≤ α ≤ 1).
This convex combination describes the straight line segment joining the two
points q1 and q2. Normally this segment is denoted as q1q2 (unordered pair).
22
Convex set. A domain D in Ed is convex if, for any two points q1 and q2 in D,
the segment q1q2 is entirely contained in D. It can be shown that the intersection
of convex domains is a convex domain.
Convex hull. The convex hull of a set of points S in Ed is the boundary of the
smallest convex domain in Ed containing S.
23
Polygon. In E2 a polygon is defined by a finite set of segments such that every
segment extreme is shared by exactly two edges and no subset of edges has the
same property. The segments are the edges and their extremes are the vertices
of the polygon. (Note that the number of vertices and edges are identical.) An n-
vertex polygon is called an n-gon.
A polygon is simple if there is no pair of nonconsecutive edges sharing a
point. A simple polygon partitions the plane into two disjoint regions, the
interior (bounded) and the exterior (unbounded) that are separated by the
polygon (Jordan curve theorem). (Note: in common parlance, the term polygon
is frequently used to denote the union of the boundary and of the interior.)
A simple polygon P is convex if its interior is a convex set.
A simple polygon P is star-shaped if there exists a point z not external to P
such that for all points p of P the line segment zp lies entirely within P. (Thus,
each convex polygon is also star-shaped.) The locus of the points z having the
above property is the kernel of P. (Thus, a convex polygon coincides with its own
kernel.)
24
Planar graph. A graph G = (V, E) (vertex set V, edge set E) is planar if it can be
embedded in the plane without crossings. A straight line planar embedding of a
planar graph determines a partition of the plane called planar subdivision or
map. Let v, e, and f denote respectively the numbers of vertices, edges, and
regions (including the single unbounded region) of the subdivision. These three
parameters are related by the classical Euler's formula
v - e + f = 2.
If we have the additional property that each vertex has degree ≥ 3, then it is
a simple exercise to prove the following inequalities
v ≤ 2/3 e, e ≤ 3v - 6
e ≤ 3f - 6, f ≤ 2/3 e
v ≤ 2f - 4, f ≤ 2v - 4
which show that v, e and f are pairwise proportional. (Note that the three
rightmost inequalities are unconditionally valid.)
25
Triangulation. A planar subdivision is a triangulation if all its bounded regions
are triangles. A triangulation of a finite set S of points is a planar graph on S with
the maximum number of edges (this is equivalent to saying that the
triangulation of S is obtained by joining the points of S by nonintersecting
straight line segments so that every region internal to the convex hull if S is a
triangle).
26
Polyhedron. In E3 a polyhedron is defined by a finite set of plane polygons
such that every edge of a polygon is shared by exactly one other polygon
(adjacent polygons) and no subset of polygons has the same property. The
vertices and the edges of the polygons are the vertices and the edges of the
polyhedron; the polygons are the facets of the polyhedron.
A polyhedron is simple if there is no pair of nonadjacent facets sharing a
point. A simple polyhedron partitions the space into two disjoint domains, the
interior (bounded) and the exterior (unbounded). (Again, in common parlance
the term polyhedron is frequently used to denote the union of the boundary and
of the interior.)
The surface of a polyhedron (of genus zero) is isomorphic to a planar
subdivision. Thus the numbers v, e, and f of its vertices, edges, and facets obey
Euler's formula.
A simple polyhedron is convex if its interior is a convex set.
27
Oriented Area of Polygons
28
Area Computations Fonte: [SKIENA02]
We can calculate the area of a triangle, from the coordinates of its vertices,
evaluating the cross product defined below. Note that this calculation is easily
implementable..
double signed_triangle_area(const Point* _A, const Point* _B, const Point* _C)
{
return( (_A[X]*_B[Y] - _A[Y]*_B[X] + _A[Y]*_C[X]
- _A[X]*_C[Y] + _B[X]*_C[Y] - _C[X]*_B[Y]) / 2.0 );
}
29
Area Computations Fonte: [SKIENA02]
We can compute the area of any triangulated polygon by summing the area of all
triangles. This is easy to implement using the routines we have already developed.
However, there is an even slicker algorithm based on the notion of signed areas for
triangles, which we used as the basis for our ccw routine. By properly summing the
signed areas of the triangles defined by an arbitrary point p with each segment of
polygon P we get the area of P, because the negatively signed triangles cancel the area
outside the polygon. This computation simplifies to the equation
where all indices are taken modulo the number of vertices. See [O’R00] for an exposition
of why this works, but it certainly leads to a simple solution:
double area(polygon *p)
{
double total = 0.0; /* total area so far */
int i, j; /* counters */
for (i=0; i<p->n; i++) {
j = (i+1) % p->n;
total += (p->p[i][X]*p->p[j][Y]) - (p->p[j][X]*p->p[i][Y]);
}
return(total / 2.0);
}
30
Algorithm for
Polygons Tessellation
31
How to tessellate a face which is not convex?
Solution from SKIENA & REVILLA, 2002, Programming Challenges, p.319
First EAR found starting from V1 in
the CCW direction
v4 v3 v8 v7 v4 v3 v8 v7
33
Introduction to Predicates
Now-ancient books on computing frequently use flow charts, which conveniently
introduce predicates. At the time when FORTRAN in particular, and imperative
programming in general, were at the forefront of computing, the use of flow charts was
widespread. A flow chart illustrates rather pointedly the path that control may take
during computation. This path is sketched using straight lines that connect rectangles and
diamonds. Assignment statements appear inside rectangles and if-statements appear
inside diamonds. Other elements also exist, but we concentrate here on the parts where
the linear path of program control is broken, or branches. The functions that are
evaluated and that decide the path taken at such branches are called predicates. Flow
charts have since been replaced by pseudo-code, where changing the linear program
control appears in the form of an indentation.
System design has gone back to schematics with the advance of techniques for
object-oriented design. One such popular visual language and accompanying
methodology, the Unified Modeling Language, promotes that system design should be
tackled at a higher granularity. Objects and the messages they pass to each other are
identified, but the advance of UML did not supplant—it merely enlarged—pseudo-code
and the algorithm design that it captures.
The objective of this section is to argue that crafting good geometric predicates and
using them properly is at the center of geometric computing.
34
Return Type of a Predicate
We generally think of predicates as functions with a Boolean return type. The
Boolean type might identify whether a counter has reached some bound, whether a
predetermined tolerance has been satisfied, or whether the end of a list has been
reached. Such predicates arise in geometric computing, but an additional type of test is
frequently needed. Because this geometric test has three possible outcomes, we refer
to it as a ternary branching test. Yet most often, we are interested in forming a binary
predicate from the three possible outcomes.
The need for three branches in a test can be seen when we consider an oriented
line splitting the plane. The plane is split into the points that lie on the positive
halfplane, the points that lie on the negative halfplane, as well as those that lie on the
line itself. A geometric library will offer such ternary outcomes to clients, and the
application programmer will decide how the predicate should be formed.
35
An application might quite suitably need to capture only two cases, the set of points
lying on the positive halfplane or the line and the set of points lying in the negative
halfplane, for example. But the geometric tests should be offered in such a way that if
the application programmer wishes to provide different handling for each of the three
cases, it is possible to do so.
Just as we refer to an interval being open if it does not include its extremities and
refer to it as closed if it does, we can also talk about either open or closed halfspaces. A
left open halfspace consists of the points lying to the left of the line, not including the
points on the line itself. A left closed halfspace does include the points on the line.
Whether open or closed, we define the boundary of the halfspace as the points on the
line. Thus, a closed halfspace includes its boundary and an open halfspace does not. The
interior of an interval is the corresponding open interval. A set is termed regular if it is
equal to the closure of its interior—an interval is regular if it is closed. By thinking of the
predicate as a ternary rather than as a binary predicate we simplify the design of a
predicate and leave the decision of choosing among the different representable sets to
the client.
36
The Turn Predicate (Plane Orientation)
Determining the orientation of a point with respect to the line defined by two other
points is easily defined by appealing to a function that will take us momentarily to a third
dimension.
37
The Design of an Orientation 2D Predicate
SIGN orient2d(const Point* _p1, const Point* _p2, const Point* _p3);
enum SIGN {
NEGATIVE = -1,
ZERO = 0,
POSITIVE = 1
};
bool isLeftSide(const Point* _p1, const Point* _p2, const Point* _p3)
{
return orient2d(_p1,_p2,_p3) == POSITIVE;
}
bool areColinear(const Point* _p1, const Point* _p2, const Point* _p3)
{
return orient2d(_p1,_p2,_p3) == ZERO;
}
bool isRightSide(const Point* _p1, const Point* _p2, const Point* _p3)
{
return orient2d(_p1,_p2,_p3) == NEGATIVE;
} 38
Matrix Form of the Orient2D Predicate
39
Side of Circle Predicate
40
41
Numerical Precision
Exact and Adaptive Arithmetic
42
Why using Exact Arithmetic?
Source: Ricardo Marques
45
Max K. Agoston
Computer Graphics and Geometric Modeling
Springer 2004
46
Sherif Ghali
Introduction to Geometric Computing
Springer
2008
But how do we know, when starting the design of a system, whether floating
point numbers are adequate? The answer is sometimes easy. It is clear that
interactive computer games, or systems that need to run in real time in general,
cannot afford to use data types not provided by the hardware. This restricts the
usable data types to int, long, float, and/or double. It is also clear that systems that
perform Boolean operations on polygons or solids, such as the ones discussed in
Chapter 28, will need to use an exact number type. In general, however, this is an
important decision that needs to be made for each individual system. Genericity is a
powerful device at our disposal to attempt to delay the choice of number type as
long as possible, but to generate one executable or program, the various
compromises have to be weighed and the decision has to be made.
47
Sherif Ghali
Introduction to Geometric Computing
Springer
2008
48
Sherif Ghali
Introduction to Geometric Computing
Springer
2008
49
Sherif Ghali
Introduction to Geometric Computing
Springer
2008
50
Closest Point at a
Straight Segment
51
NEAREST POINT IN A STRAIGHT SEGMENT USING INTERNAL PRODUCT
D′ = A + tD′ ( D − C ) D
Parametric value of D’ point at AB segment:
AB AD
tD′ = 2 Closest point of D on AB segment:
AB tD′ > 1
P=B
52
Algorithms for
Segment- Segment Intersection
53
Line Segments and Intersection Fonte: [SKIENA02]
A line segment s is the portion of a line l which lies between two given points
inclusive. Thus line segments are most naturally represented by pairs of endpoints.
The most important geometric primitive on segments, testing whether a given
pair of them intersect, proves surprisingly complicated because of tricky special
cases that arise. Two segments may lie on parallel lines, meaning they do not
intersect at all. One segment may intersect at another’s endpoint, or the two
segments may lie on top of each other so they intersect in a segment instead of a
single point.
This problem of geometric special cases, or degeneracy, seriously complicates
the problem of building robust implementations of computational geometry
algorithms. Degeneracy can be a real pain in the neck to deal with. Read any
problem specification carefully to see if it promises no parallel lines or overlapping
segments. Without such guarantees, however, you had better program defensively
and deal with them.
The right way to deal with degeneracy is to base all computation on a small
number of carefully crafted geometric primitives.
54
HOW TO TREAT THE INTERSECTION OF STRAIGHT LINES IN ROBUST AND EFFICIENT WAY?
C B
A D
55
Fonte [VINCE05]
56
57
INTERSECTION BASED ON PARAMETRIC REPRESENTATION OF SEGMENTS
C
P = A + t AB ( B − A ) tCD
dC
P = C + tCD ( D − C )
A P D’ B
C’
dC dD
dC dC
tCD = tCD = 0 ≤ tCD ≤ 1
dC + dD dC − dD
58
INTERSECTION BASED ON PARAMETRIC REPRESENTATION OF SEGMENTS
C B C B
dC
C’ Area
A A
AB ⋅ dC AB × AC ( B − A) × (C − A)
Area = Area = Area =
2 2 2
C B dC
P tCD =
dC − dD
D
orient 2 d( A, B, C ) orient 2 d( A, B, D)
dC = dD =
A AB AB
orient 2 d( A, B, C )
Therefore: tCD = P = C + tCD ( D − C )
orient 2 d( A, B, C ) − orient 2 d( A, B, D)
orient 2 d(C , D, A)
Analogously : t AB = P = A + t AB ( B − A )
orient 2 d(C , D, A) − orient 2 d(C , D, B)
60
Segment_Segment_Intersection(u, v): B
if both endpoints of u are over v then
return false orient 2 d(C , D, A) > 0
end if
orient 2 d(C , D, B) > 0 A
if both endpoints of u are under v then
return false orient 2 d(C , D, A) < 0 orient 2 d(C , D, B) < 0 C
end if
if both endpoints of v are over u then
return false orient 2 d( A , B, C ) > 0 D
orient 2 d( A, B, D) > 0 B
end if
if both endpoints of v are under u then B
return false orient 2 d( A , B, C ) < 0
end if
orient 2 d( A, B, D) < 0 A C
if u and v are collinear then A
return false orient 2 d(C , D, A) = 0 orient 2 d(C , D, B) = 0
D
end if
C
ou orient 2 d( A, B, C ) = 0 orient 2 d( A, B, C ) = 0
D
if u and v are parallel then orient 2 d(C , D, A) = orient 2 d(C , D, B) A
return false ou C
end if orient 2 d( A , B, C ) = orient 2 d( A , B, D)
62
Ray Algorithm (or shot)
Philip Schneider and David Eberly Geometric Tools for Computer Graphics, 2003, p.70
A ray that part of any point within the polygon in one direction will cut any curves
on the edge of the polygon an odd number of times. If the ray cut the boundary
of the polygon an even number of times, the point is outside the polygon.
63
Criteria for counting intersections of the ray with a boundary edge