how can you tell if two line drawings are the same?

3
COMPUTER GI~API-IICS AND II~LIGE PROCESSINO 6, 90-92 (1977) SHORT NOTE How Can You Tell if Two Line Drawings Are the Same?* JAMES F. I~.EEGAlg AND ARTI=[UR ~/~. L:ESK Fairleigh Dickinson University, Teaneclc, New Jersey 07600 Received November 17, 1975 Two drawings, each composed of line segments, may appear identical, even though the data sets by which they are sLored in a computer bear no obvious relationship to one another. An algorithmis presented by which any data set specifying a line drawing may be reduced to a canonicalform. Any two data sets that specifyline drawings that look the same will becomeidenticalwhen reduced to canonicalform. There are many ways to represent a line drawing in a computer program. One way, which is always possible, is to store the endpoints of every line segment of the drawing. If, however, the drawing contains a set of connected line segments, it is possible to store this portion of the drawing in polygonal form, interpreting suc- cessive data points in the sense: Set Pointer--Draw To--Draw To-- .... Other equivalent ways of representing the same drawing arise h'om the possibility of interpreting coordinate data as absolute, or relative to the current position of a pointer. But even if we choose a general form for representing the line segments of the drawing, for example, a set of ordered pairs of endpoints: (XI.(I), YI(I)), (X2(I), Y2(Z)), Z = 1, ..., N, there remains the possibility of considerable ambiguity. The indeterminacy may include one or more of the following variations : 1. The order of the line segments in the list may be different. 2. The endpoints of any particular line segment may be exchanged. 3. Any line segment is equivMent ~o two or more subsegments that together span the stone two endpoints. These subsegments may abut, or overlap to various extents. (We ignore the possible intensity differences that overlapping segments might produce on particular output devices.) 4. Any segment may be repeated one or more times. (This is a special ease of 3.) 5. The entire drawing may be translated or scaled. However, we regard two drawings ~ha% differ by only a rotation to be distinct, in order that the letters d and p, for example, not be regarded as identical. * Work supported by Research Grant DCR74-24390 from the Nationd Science Foundation. 90 Copyrlgh~ ~ 1977 by Aeademie Press, Ino. 3_11 right~ of reproduction in any form reserved,

Upload: james-f-keegan

Post on 02-Jul-2016

217 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: How can you tell if two line drawings are the same?

COMPUTER GI~API-IICS AND II~LIGE PROCESSINO 6, 90-92 (1977)

SHORT NOTE How Can You Tell if Two Line Drawings Are the Same?*

JAMES F . I~.EEGAlg AND ARTI=[UR ~/~. L:ESK

Fairleigh Dickinson University, Teaneclc, New Jersey 07600

Received November 17, 1975

Two drawings, each composed of line segments, may appear identical, even though the data sets by which they are sLored in a computer bear no obvious relationship to one another. An algorithm is presented by which any data set specifying a line drawing may be reduced to a canonical form. Any two data sets that specify line drawings that look the same will become identical when reduced to canonical form.

There are many ways to represent a line drawing in a computer program. One way, which is always possible, is to store the endpoints of every line segment of the drawing. If, however, the drawing contains a set of connected line segments, it is possible to store this portion of the drawing in polygonal form, interpreting suc- cessive data points in the sense: Set Pointer--Draw To--Draw T o - - . . . . Other equivalent ways of representing the same drawing arise h'om the possibility of interpreting coordinate data as absolute, or relative to the current position of a pointer.

But even if we choose a general form for representing the line segments of the drawing, for example, a set of ordered pairs of endpoints: (XI.(I), YI(I)), (X2(I), Y2(Z)), Z = 1, . . . , N, there remains the possibility of considerable ambiguity. The indeterminacy may include one or more of the following variations :

1. The order of the line segments in the list may be different. 2. The endpoints of any particular line segment may be exchanged. 3. Any line segment is equivMent ~o two or more subsegments that together

span the stone two endpoints. These subsegments may abut, or overlap to various extents. (We ignore the possible intensity differences that overlapping segments might produce on particular output devices.)

4. Any segment may be repeated one or more times. (This is a special ease of 3.)

5. The entire drawing may be translated or scaled.

However, we regard two drawings ~ha% differ by only a rotation to be distinct, in order that the letters d and p, for example, not be regarded as identical.

* Work supported by Research Grant DCR74-24390 from the Nationd Science Foundation.

90

Copyrlgh~ ~ 1977 by Aeademie Press, Ino. 3_11 right~ of reproduction in any form reserved,

Page 2: How can you tell if two line drawings are the same?

CANONICAL FORN'I FOR, COMPARING LINE DRAWINGS 91

I t is therefore not obvious, given two drawings specified by sets of endpoints of line segments, how to tell whether or not they are visually identical. The purpose of this report is to describe a canonical form to which any line drawing is reducible, such that the canonical forms of any two visually identical drawings will be the same.

To avoid problems arising from roundoff error in aritluuetie operations, we assume that all coordinates are stored as fixed-point numbers in the range - A _< X(I), Y(Z) _< + _4.

TIIANSFORMATION OF A TW0-DIMENSIONAL LINE DRAWING TO CANONICAL FORI~{

We assume i, hat the drawing is specified by an integer N, and line segment endpoints (XI(I) , YI(I)), (X2(I), Y2(I)), I = 1, . . . , N. The drawing may con- tain dots, represented in this form by segments having two identical endpoints.

Step 1. Exchange the endpoints of each line segment, if necessary, so that XI ( I ) < X2( I ) , I = 1, . . . , N. If XI(I) = X2(I), exchange the endpoints if necessary so that Yl(I) <: Y.2(I).

Step 2. Calctflate the norm of each line segment, norm(/) = (X2(/) - X1 (I)) 2 + ( r 2 ( I ) - Yl(I)?.

Step 3. For each segment, if X1 ([) ~ X2 (I), calculate the slope,

slope (I) -- FLOAT (Y2 (I) -- Y1 (I))/FLOAT (X2 (I) - X1 (I))

and intercept, intercept (I) = Y1 (I) -- X1 (I)*slope (I).

If XI( I ) ~ X 2 ( I ) , slope(/) and iniereept(I) are taken to be floating-point numbers. But whenever XI ( I ) -- X2(I), set slope(I) to the character string INFINITE, and intercept(I) to the character string NO.

Step 4. Move the dots to the end of the list : Sort the line segments in descending order of norm. Let M be the maximum index such that norm(M) > 0.

Step 5. Sort the first M line segments on Y1, ascending; then on X1, ascending; then on intercept, ascending, with segments with NO intercept ending up last; then on slope, ascending, with INFINITE slopes ending up last. All these sorting operations are to be performed stably, in the sense of Knuth. Note that only the segments with NO intercept have INFINITE slope.

The result of these sorting operations is that the lines of finite slope occur in order of increasing slope, followed by lines of INFINITE slope, followed by dots. Within classes of equal slope, segments appear in order of increasing intercept; within classes of equal finite slope and intercept, segments appear in order of increasing wdue of X1. Within the class of segments of INFINITE slope and equal X1, segments appear in order of increasing value of Y1.

All collinear segments are now adjacent in the lists and appear in order of in- creasing X coordinate of the left endpoint.

Step 6. Eliminate redundant line segments. (6A) Within collinear sets of segments of finite slope, check whether X2(I)

< X I ( I + 1). If so, the I th line segment is not redundant and must be kept.

Page 3: How can you tell if two line drawings are the same?

92 KEEGAN AND LESK

If X2 (l) > X1 (1 + 1), note that X1 (/) < X1 (Z + 1) in all o-~ses ~s a result of the sorting procedure in Step 5. If X2(I) < X2(I + 1), set X2(I) = X2( I + 1) and I"2(/) = Y2(I + 1), but if X2(I) > X2(I + 1) make no replacements. In both eases delete line seglnent / + 1 from the drawing.

(6B) For the set of segments of I N F I N I T E slope, cheek classes of equal X1 for redundancy. Whenever Y2(I) > Y I ( I + I ) , set YI(I) = Min{Yl(I) , Y I ( / + 1)} and Y2(/) = Max{Y2(/), Y2([ + 1)}; then delete line segment 1 + 1 .

(6Ci) For the dots (the segments of zero norm) check for duplicates by comparing X1 (I) with X1 (I + 1) and Y1 (I) with Y1 (I + 1). If both comparisons yield equalities, delete line segment I + 1.

(6Cii) Elimination of dots that lie on lines. For each dot (XX, YY) and each set of P of parallel line segments with a coin-

mou slope S(P), compute b(P) = Y Y - - S ( P ) * X X as a floating-poin~ value. Check the intercepts of the segments in P : If no intercept is equal to b (P) the dot (XX, YY) cannot be redundant..But if a line segment in P has in.tereept b(P), and the values of the abscissa at its endpoints are X1 and X2, delete the dot (XX, YY) if X1 _< X X < X2.

In the following step, N is the updated value of the number of line segmen.ts, that is, N is equal to the originM number of line segments minus the number deleted in Step 6.

Step 7. To fix translational and scale features, determine

XMAX = Max{X2(I), I = 1, . . . , N}

XMIN = 5fin{Xl(I), I = 1, . . . , N},

YMAX = Max{Max{Yl(/) , Y2(/)}, I = 1, . . . , N},

YMIN = Mi~{Min{YI(I), Y2(I)}, I = 1, . . . , N}, and then

and XOI{G = (XMAX -- XMIN)/2

YORG = ( Y M A X - YMIN)/2.

Translate the system of coordinates to a new origin at (XORG, YORG). To normalize the drawing, recall that the allowed range of coordinate values is

from - A to +A, in fixed point. Compute, in fixed point,

B = Max{ (XMAX -- XORGI, [XI~IN -- X01~G[,

I rMAX - YOeGI, I yMmr - Yo GI}.

If B = 0, do nothing. If B > 0, multiply each coordinate value by FLOAT(A)/ FLOAT (B) and store the result in fixed point.

The result of these steps is to reduce any line drawing expressed as a list of line segments to a unique canonical form. Two line drawings are visuallyequiva- lent if and only if the data sets defining their c~monical forms t~re the s~me.