• No results found

Geometric Predicates and Basic Algorithms

In document DiplThesisJena07 (Page 64-69)

Geometric primitives are the basic operations in geometric algorithms; they test properties of basic geometric objects. There is a small set of such operations which covers most of the computations needed in CG algorithms. This minimal set of geometric primitives is defined in [FH95] and [BH98]. These primitives are explained below and extended by the ones used in this implementation, which were in their majority adapted from those in the JTS.

Lexicographic comparison of two points. Some algorithms like the convex hull algorithmGraham’s scanneed to sort coordinates into a sequence, from left to right in the coordinate system. This is called the lexicographic order. Sorting a set of coordinates in lexicographic order is based on the comparison of two pointsp andq:

(p.x < q.x)OR((p.x=q.x)AN D(p.y < q.y))

The implementation of this algorithm is robust by its nature since it does not create new data.

Point-Line Orientation Test. Letsbe a line segment with bounding verticesp1

andp2, then it is represented by the point set{p1+λ·(p2−p1)|λ∈[0,1]}. And

let l(s) be the supporting straight line of s defined by {p1+λ·(p2−p1)|λ∈ <}.

The 2D orientation test determines whether a pointq lies on the left or right side of l(s) or on l(s) itself. Another way of understanding the orientation test is by testing whether a given sequence of three points(p1, p2, q)is clockwise orientated,

counter clockwise orientated or whether the points are collinear (i.e. whether they lie on the same straight line).

Let p1 be the starting point of sandp2 its finishing point. Through the trans-

lation of linesand pointq so that p1 lies on the origin of the coordinate system,

we have a new line with start point dp1 =

0 0

and end point dp2 = p2 −p1

and the new single pointdq = q−p1. Figure 6.2 illustrates this translation. The

determinant ofdp2anddqcan now be used to determine whetherdqlies on the left

or right side of the new line, because a translation does not change the topological properties or the relationsship between the line and point (i.e. whetherq lies on the left or the right side of lines).

Figure 6.2:Vector and point translation to the origin of the coordinate system for the ori- entation test of a point and a line segment

Let α be the angle between the two vectorsdp1 → dp2 anddp1 → dq (that is 0 0 → dp2 and 0 0

→ dq, becausedp1 lies on the origin). The following rules

hold for the evaluation of the sign of the determinantD:

D < 0: 0 < α < π, the vectors are positively (counter-clockwise) oriented,q lies on the left side ofs

D > 0: π < α <2π, the vectors are negatively (clockwise) oriented,qlies on the right side ofs

D = 0:qis collinear tos

The matrix of our example (figure 6.2) has a positive sign, since the pointqlies on the right side of the line segments.

Given that the algorithm is based on the robustevaluation of signs of determinants, it is also implemented in a robust manner. The Point-Line Orientation test has a constant runtime ofO(1)because the determinant evaluation works in constant time as well.

Ring Orientation Test. This algorithm tests whether a closed ring is clockwise (cw) oriented or counter-clockwise (ccw) oriented and is based on the above Point- Line Orientation Test. The proceedure is simple: the algorithm searches the highest point hp within the ring, as well as its predecessor prev and its successor next.

Figure 6.3:Ring orientation test: if the the highest point, its predecessor and its successor are ccw oriented, the whole ring is ccw oriented (a); in case of collinearity the x value order of its predecessor abd successor decides (b)

The whole ring has the same orientation as the curve defined by the sequence prev,hpandnext. Example(a)in figure 6.3 illustrates a ccw oriented ring. If all three points are highest points(i.e. they are are collinear), the ring is cw oriented if prev.x < next.xand ccw oriented ifprev.x > next.xas illustrated in example(b). Since the search of the highest point in a ring withnpoints may needniterations, the test is performed in linear timeO(n).

Evaluation of signs of determinants. Many geometric algorithms, for exam- ple the Point-Line Orientation Test, are based on the evaluation of determinants. Usually, this is one of the few parts that deal with numerical computation in such algorithms. As previously illustrated, as a result of rounding errors, floating-point arithmetic has serious shortcomings. Such errors may cause the sign evaluation of a determinant fail.

Because this basic algorithm holds significant importance, the study of exact determinant evaluation gained researchers’ attention. [BY] reviews the two prin- cipal algorithms of Clarkson and Avnaim et al. Clarkson’s algorithm [Cla92] evaluatesd×ddeterminants. The algorithm used in this implementation is the one presented by Avnaim et al. [ABD+97]. He presents algorithms for2×2and 3×3determinants. This is sufficient for common application in today’s GIS and for the Abstract Feature Geometry Specification which is limited to three dimen- sions.

The implementation of this work was adapted from the JTS and contains only the evaluation2×2matrixes. Its running time is sensitive to the numbers of bits of

the input data. For a2×2matrix withb-bit Integer entries, the algorithm requires at mostbiterations which operations are performed inO(1). Though its asymptotic worst-case runtime complexity is worse than the one of Clarkson, the constant runtime in O(1) is competitive in practise (see [ABD+95]) and the algorithm is much simpler than Clarkson’s.

Vertical relationship between two line segments. Let a and b be two line segments and letlbe a vertical straight line that intersectsaandb. This predicate determines they-order of the intersections pointalbetweenaandland the inter-

section pointblbetweenbandl:(al·y < bl·y).

The algorithm can be used in the same manner interchanging the axis. Here the test is against a horizontal line and will determine thex-order of the intersec- tion points between both the segments and the linel. This algorithm performs in constant time and is used by the Plane Sweep algorithm to sort the actual set of segments that intersect with the sweep line.

Point-In-Ring Test. This basic operation verifies whether a pointp lies within or outside a closed area, which may be a polygon whose boundary is described by a ring. This operator is used, for instance, within the overlay operation to determine whether isolated components1 lay within or outside another polygon in order to properly label it. The algorithm lays a horizontal straight line (parallel to the x-axis) frompto positive infinity and counts its intersections with the ring (see figure 6.4). The point lies within the area if the number of intersections is odd. In the event of an even number of intersections, the point will lie outside the area. The whole algorithm, as well as the special case when the horizontal line overlaps a line segment of the polygon’s boundary are explained in [PS85].

The implementation in this work iterates all segments that define the polygon and tests for intersection between the segment and the straight line2. The imple- mentation is robust due to the use of the Point-Line-Orientation test, which is a robust test used to determine whether the point lies on the right or left side of a segment. An intersection is counted only if the point lies on the right side and the segment points downwards or if the point lies on the left side and the segment

1Isolated components are points or lines which do not contain any intersections with the other

input geometry

2(segment.start.y > p.y and segment.end.y <= p.y) or (segment.start.y > p.y and segment.end.y <=p.y)

Figure 6.4:The Point-In-Ring Test counts the intersections of the ring segments with the straight line from the point into a direction: p1 and p4 lie outside the ring and have an even number of intersections. p2 and p3 lie within the ring and have an odd number of intersections.

points upwards.

Since the Point-Line-Orientation works in constant time, the Point-In-Ring algo- rithm works in linear time O(n) where nis the number of segments that define the ring.

Line Intersection Test. TheLine Intersection Testverifies whether two line seg- ments intersect or not. It uses the Point-Line Orientation test. Lets1ands2be two

line segments. s1 ands2 intersect, ifs1.startands1.endlie on opposite sides ofs2

(i.e.s1.startands1.enddo not lie on the same side ofs2) ands2.start s2.endlie on

opposite sides ofs1 (i.e. s2.startands2.enddo not lie on the same side ofs1). s1

ands2will not intersect if the conditions above are not satisfied.

Line Segment Intersection. This algorithm actually computes the intersection point between two line segments. Different techniques for line intersection com- putation are listed in [BGHV99], [vO94] and [BH98] in a broad overview. Some approaches address the robustness problem, for instance, the one of [BH98]. It is based on the idea of representing an intersection point between two line segments by the input data, i.e. the original line segments. However, most of the used techniques for line intersection are based on direct calculation of the intersection point in floating-point arithmetic.

The computation of an intersection point between two line segments in this implementation is based on homogeneous coordinates (seeExact Geometric Com-

putationin section 3.4.2). It uses floating-point arithmetic, but is robust as it avoids division.

In document DiplThesisJena07 (Page 64-69)