Computers if StrtutwesVol.26.No. 4, pi, 709-717,1987 Printedin Chnt Britain.
00457949/8753.00+ 0.00 8 1987Pcr~unonJournalsLtd
TECJSNICAL NOTE ON THE HIDDEN WILLIS
LINE REMOVAL
PROBLEM
R. SPILLERS and KINCHO H. LAW
Department of Civil Engineering, Rensselaer Polytechnic Institute, Troy, NY 12180, U.S.A. (Received 28 October 1986)
Al&me-It is argued that the hidden line removal algorithm of Janssen can be simplified using the parametric representation of a Iine rather than area1 coordinates to determine the geomettical relationship between a triangular facet and a line segment. A FORTRAN subroutine for the hidden line removal algorithm is included. INTRODUCTION
While the problem of hidden line removal is basic to three-dimensional computer graphics, it has remained strangely elusive to the casual graphics user who would simply Iike to add this type of feature to his existing displays. All that changed with the paper of Janssen [1] who described an effective hidden line removal algorithm and gave its FORTRAN listing. Janssen considers the case in which a surface is represented by fiat triangular facets. Central to his algorithm is his method for determining the portion of a given line segment which is ‘covered’ by a triangular facet. He does so by using the area1 coordinates of the end points bf the line \-point
to determine its relationship to the triangular facet. It is suggested here that Janssen’s algorithm can be simplified by using the parametric representations of a tine instead of areal coordinates. In terms of convex analysis these techniques are of course related since the parametric representation of a line is a variation of the problem of representing a l-simplex, while area1 coordinates are a variation of the representation of a 2-simplex [2]. OVERVIEW OF THE ALGORITHM A general discussion of the hidden line removal problem is not the purpose of this note. Such discussions are not
E
triangular
facet
segment
Fig. i. Schematic of line segment and triangular facet.
triangular
Fig. 2. x-y projection of line segment and triangular facet. 709 C.A.S. 26/4.-L
facet
Technical Note
710
Loop for
coah
triangle
an the node!
4 s tha
trionglci
to
tha
view
plme
no
I
Find
intersecting
of
points
line
and edges
of
tricmgic
I
J m ttw numberOFedga
of
triangle
Intawctcd
with
lim
< 2
no IDatemtioo
hidda
ngmnt
lecr
\z 6 hldden segnent $ Sot-t and oerga
hiddan
“g
thla
41
Found
Yes
a-t
with
(aem Figure
C.ll
Five
previous
hidden
?w$wnts
51
J thm last
triangle\
Ye= Plot
Is
thls
visible
the
\1, lost
portIons
of
line
In the
I~ne
model
Fig. 3. Description of the hidden line removal algorithm.
Fig. 4. Determination of hidden segment.
Technical Note available and clearly involve many practical issues such as preliminary sorting. But at some point, as Janssen indicates, it is necessary to deal with the question of the fraction of a line segment which is covered by a triangular facet. To do so only requires the computation of the intersection points of the projections on the x-y plane of the line with two sides of the trianguhu facet (see Figs 1 and 2). These intersections provide the values ZHTI and ZHT2 from which it is possible to compute the faction of the line segment is covered. Let R, and RF represent the position vectors of two given points on a line. Any point R along this line can then be represented parametrically as R(a)=aR,+(l-a)R,
(1)
711
using the scalar parameter a. This parameter has the useful property that 0 < a < 1 for points between the end points E and F. Similarly a second line can be represented as R(a’)=a’R;+(I
i
START
-
--
-
--
-
--
-
--
-
--
EN0
segwnt
[al
New hidden
Prev I ous hodden segwnts
segment
not
overlopped
with
i-l
Previous
segments
i
START
END
New segment STRRT tlergad
Prev t ou* hidden segnents
(cl
1
flergc
EN0
STRRT
EN0
overlapped
segments
i -1
- Case 2
i
STRRT
EN0
STRRT
segrlsnt
Id1 kgo Prev I aim hIdden secywnts
- Case
START
New segmsnt
fl*rg.d
seywmts ,
segment
Prev i ous h I dden segmults
over1 apped
i-l
NW segment
merged
EM
segment 611 Merge
overlapped
EM
segments
i-l
- Cose 3
i
START NW
EN0
segment START
[eJ Met-go ovwlopped
(2)
The intersection of the projections of these two lines onto the x-y plane then involves two simultaneous equations which determine the two parameters a, a‘. The point is that given a and a’, eqns (1,2) then provide the associated z coordinates at this intersection which are required (see Fig. 1) to determine which line is above the other at their intersection point in projection on the x-y plane.
i-l
NW
-a’)RH.
segments
ENQ
- Case
q
Fig. 5. Sorting and merging of hidden segments.
712
Technical Note PROGRAM
DESCRIFTION
The
objective of the algorithm presented here is to decide if a line in the model is totally or partially hidden by comparing each line with the planar polygons in the surface of the model. The procedure is summarized in Figs 3 and 4. A listing of the FORTRAN program for the algorithm is given in the Appendix. The program assumes that: (1) All nodal coordinates have been transform into the plotter’s coordinate system; (2) the nodal numbers and coordinates are stored in a consistent manner (i.e. the coordinates of node I are stored in x(Z), y(Z), z(Z)); (3) planar polygons have been partitioned into triangles; (4) duplicate lines and duplicate triangles, if they exist, are eliminated a priori. The subroutine HIDLNE (see Appendix), the main routine of the hidden line removal algorithm, is outlined in Figs 3 and 4. The input parameters of this subroutine include:
NLINE-number of unique lines in the model NPOINT-number of nodal points in the model NPOLY-number of triangular facets in the model NODEl(NLINE), NODE2(NLINE)--arrays storing the two end points of each line segment X(NPOINT), Y(NPOINT), Z(NPOINT)--arrays storing the plot coordinates of node points LPOLY(3, NPOLY)-array containing the node points of the triangles DPOLY(S, NPOLY)-array containing, respectively, the x-min, x-max, y-mm, y-max and z-max quantities of each triangle. Each hidden portion of the line is saved in parametric form and stored in arrays, START (start point of hidden segment) and END (end point of hidden segment). The hidden segments are sorted in increasing order with duplicate and overlapped segments eliminated using a linked list array CLINK. That is, only unique hidden segments are stored and sorted in the same order from the start point of the plotted line to the end point of the plotted line. This sorting and merging of hidden segments are performed by subroutine SRTMRG. The subroutine SRTMRG follows the logic indicated in Fig. 5. As each new hidden segment (stored in parametric form by the variables START and END) is identified, the segment is compared with previous hidden segments stored in the ordered list. If the new segment is found to be overlapping with a hidden segment previously stored, the two hidden segments are merged into one. The new merged segment is compared with other segments stored in the ordered list. This recursive ‘merging’ procedure continues until the new (merged) segment does not overlap with any previously stored hidden segment. The new (merged) segment is then inserted into the list. Subroutine ISECT computes the intersection of a line and an edge of a triangle. Subroutines INIT and RESET are used to initialize and reset the linked list CLINK. Finally, subroutine DRAW is used to draw the visible part of the line from its start point to its end point, skipping the ‘ordered’ hidden segments. An example of hidden line removal for a simple 3-D reticulated dome structure is shown in Fig. 6.
(a)
tb)
(cl
Cd)
Fig. 6. Reticulated dome structure. (a) Plan. (b) Elevation. (c) Oblique elevation. (d) Oblique elevation with hidden
CONCLUDING
REMARKS
Janssen presented a simple algorithm for hidden line removal 111.The aiaorithm can be simplified by using the paramet&‘represen?ation of a line (rather thanareal Goordinates) to determine the geometrical relationship between a triangular facet and a line segment. In addition to mo~f~ng Janssen’s algorithm, a simple sorting routine to eliminate overlapped segments is included in the Appendix. This sorting routine arranges the hidden segments in increasing order to facilitate their plotting. The FORTRAN program listed in the Appendix is well documented and can easily be added to existing plotting programs. REFERENCES
1. T. L. Janssen, A simple efficient hidden line algorithm. Comput. Struct. 17, X53-571 (1983). 2. E. M. Patterson, Topology. Interscience, NY (1959).
713
Technical Note
APPENDIX: ANNOTATED PROGRAM LISTING
~,***.*+_.*.**.+********.*****************-****-****************
c/* c/+
l
SUBROUTINE PURPOSE :
: HIOLNE TO REWOVE THE HIOOEN
C/* C/’ C,*+******+,***-******.****.****************-****-**-***********
C/* C/’
C/*
C/e
C/’
C/e
C/e C/’
C/*
FROM THE WOEL
l l
SUBROUTINE HIOLNE (NOOEI. NOOE2. NLINE. x. Y. 2. NPOINT. LPOl,y. OPOLY. NPOLY. CSTART. CENO, CLIM<) INTEGER NLINE. NPOINT. NPOLY INTEGER NOOEl(NLINE). NDOE2(NLINE). LPOLY(3,NpOLv) REAL ~(NPOINT). V(NPOINT). Z(NPOINT), OPOLY(S.NPOLV) DIMENSION TLINE(2). ZHf(l), TSIOE(Z), ISIOE(2) REAL CSTART(NPOLV). CEND(NPOLV) INTEGER CLINK(NPOLY). CHEAO. ELIST. AVAIL LOGICAL NMRKI .NMRK2 Inltlallze the list structure - ‘CLINK” CALL INIT (CLINK, CHEAO. AVAIL, NPOLY) Set tolerance parameter EPS = 0.001 Loop for each llna In tM model DO 400 LINE - I. NLINE Nl = NOOEl(LINE) N2 * NOOEP(LINE) Xl - X(Nl) Yl - Y(N1) 21 x2 : x’;;:; Y2 - Y(N2) 22 - Z(N2) DEL2 - 22 - 21 Check If line 1s perpendicular to the vleu plane IF( (ABS(XP-Xl).LT.EPS) * .AN). (ABS(V2-Vl).LT.EPS) ) GOT0 400 Flnd MAX/MIN values of the coordinates XMIN - AMINl( X(N1). X(N2) ) XMAX - AMAXl( X(Nl), X(N2) ) YMIN - AMINl( Y(Nl). Y(N2) ) YMAX - AMAXl( V(N1). Y(N2) ) 2MIN - AMINl( Z(N1). Z(N2) ) 2MAX - AMAXl( I(Nl). Z(N2) ) Loop through each polygon In the model ELIST - 0 CHEAO - 0 DO 300 KPOLV - 1. NPOLY Chock If polygon Is perpendicular to vl~u plane IF( ((OPOLV(2,KPOLY)-OPOLY(l,KPOLV)) .LT. EPS) l .AND.((DPOLY(4,KPOLY)-DPOLY(3,KPOLY)) .LT. EPS) l ) GOT0 300 Chock lf line 1s ln front of polygon IF(2MIN.GE.DPOLY(5.KPOLY)) GOfO 300 Chock lf llnm III abovo. klow. loft or right of polygon IF( (x*Ax.LE.~PoLv(I.KP~Lv~~ l .OR.(XMIN.GE.OPOLY(2.KPOLV)) l .OR.(YMAX.LE.OPOLY~3.KPOLV)) l .OR.(YMIN.GE.OPOLY(4,KPOLV)) ) GOT0 300 Check If ths line la an edge of polygon NRRKI - .FALSE. NMRK2 - .FALSE. DO 10 K n 1.3 IF (LP~Lv(K.KPoLv).Eo.N~J NRRK~ - .TRUE. *
C/*
l
LINES
714
Technical Note
IF (LPOLV(K.KPOLV).EG.N2) NMRKZ = .TRUE. CONTINUE IF(NLIRKl.AND.mK2) GOT0 300 Flnd lnter5octlon point5 between the llne and C/* any two 5ldes of th5 triangle NSIDE = 0 DO 200 IS1 = I,3 + 1 IS2 = MDD(ISl.3) IENDI = LPOLV(ISl,KPDLV) IEND = LPDLV(ISP.KPDLV) CALL ISECT(X1.Vl.X2.V2.X(IEND1).V(IEND1). X(IEND2).V(IEH)2).TTLINE.TTSIDE.lb2OD) When 0.0 <= TTSIDE <= 1.0. Inter5ectlon point oxl5t5; otherwl50. no Inter5ectlon polnt found. IF( (TTSIDE.LT.o.o).DR.(TTSIDE.GT.l.0) ) GDTD 200 point exists on slde IS1 Now. Inter5ectlon NSIDE = NSIDE l 1 ISIDE(NSIDE) = IS1 TLINE(NSIDE) - TTLINE TSIDE(NSIDE) = TTSIDE IF(NSIDE.EO.1) GDTO 200 Check if 5econd polnt 15 too close to previous C/’ point (or essentially the 55110 point) IF(AGS(TLINE(l)-TLINE(2)).LT.EPS) NSIDE = 1 IF(NSIDE.E0.2) GDTD 210 CDNTIME 200 DOPSI Inter5ection polnts not found or IGnoredI c/* GDTO 300 CONTINUE 210 Identlfy hidden 5egment Now, inter5ection potnt5 found, c/* Compute the dl5tance of line above the facet c/* at the two Inter5ectlon point5 c/+ DO 220 I = l.NSIDE IENDI = LPOLV(ISIDE(I).KPDLV) NXTSID = m)O(ISIDE(I).3) + 1 - LPDLV(NXTSID.KPDLV) IEND = Z(N1) + TLINE(1) l DEL2 tHT(1) * + TSIDE(1) * (Z(IEND2) - Z(IEND1))) CoNTI~~Z( IENDl) 220 Sort the two lnter5ection polnt5 In lncreaslng order c/* IF(TLINE(l).LT.TLINE(2)) GDTD 230 TEMP = TLINE(2) = TLINE(1) TLINE(2) TLINE( 1) = TEMP TEMP = ZHT(2) MT(P) = ZHT(1) ZHT(1) = TEMP CONTINUE 230 check If llne ha5 portion In polygon c/* IF( (TLINE(P).LT.EPS) * .OR.(TLINE(l).GT.(l.-EPS)) ) GOTD 300 PROJECT DEL2 TO ENDS OF LINE c/* SLOPE - (ZHT(2)-ZHT(l))/(TLINE(2)-TLINE(1)) - SLOPE * TLINE(1) 21 - ZHT(1) 22 - 21 + SLOPE hldd5ned portion of line c/* Now, Identify c/+ START polnt: c/* IO
c/+
c/* w+ c/*
c/+
c/*
* *
715
Technical Note
IF(TLINE(l).LE.O.O) GOT0 241 START = TLINE(1) ZSTART = tHT(l) GDfO 250 START = 0.0 241 ZSTART - 21 END point: c/* IF(TLINE(P).GT.l.O) GDTO 251 250 EM) = TLINE(2) ZEND = ZHT(2) GOT0 260 EN0 = 1.0 251 ZENCJ = 22 CONTINUE 260 Check If end points are above or below plane c/* A. Both polnts above plane, do nothlrw c/+ IF(ZSTART.GE.O.O.AND.ZEND.GE.O.0) GDTO 300 8. START point above but END polnt below plane c/+ reset START polnt C/’ IF(ZSTART.GE.O.O.AND.ZEH).LT.O.O~ + START - START + ZSTART * (END - START) l / (~START - mm) C. START point bellow but EKJ polnt above plane C/’ reset END point C/* IF(ZSTART.LT.O.O.AND.ZEM).GE.O.O) * END = START + ZSTART * (END - START) * / (ZSTART -2ENc11 Put hlddmed portion Into sorted llat C/’ CALL SRTWRG (CLINK. CSfART. CEH). START. END. l CHEAO. AVAIL. ELIST) CONTINUE 300 hlddened segments c/*** * Ready to DRAW the line skipping VI. X2. Y2. CSTARf. CENO. CLINK. CALL DRAW (Xl. l CHEAO, EPS) RESET CLINK vector C/’ CALL RESET (CLINK, CHEAO. AVAIL. ELIST. NPOLY) 400 CONTINUE RETURN END C/*****+‘********+*************************************~*******~ ;;:
SUERDUfINE PURPOSE :
l
**
* *
:
ISECT GIVEN TWO LINES.
FIND C/* C/e C/***L**~*+*+******+****************~***************************
INTERSECTING
SUEROU~~N~2~~~CT~XlA.V1A.XlC.Y1C.X2A.Y2A.X2C,Y2C. l
c/+ All - -(XlC-XII) Al2 = X2C-X21 A21 - -(VlC-VIA) A22 = V2C-V2A * A22 - A21 l Al2 DET = All IF(~G:I~ET).LT.l.E-10) RETURN 1 - X2A t: - V1A - Y2A Tl = (A22 l El - Al2 * 82)/DEf T2 = (-A21 * Bl * All * B2)/OET RETURN END
PARAMETERS
l
*
716
Technical Note
c/***+*********************************************************
c/* c/*
SUBROUTINE : SRTMRG PURPOSE : TO SORT HIDDEN
PART AND ELIMINATE C/S C/* C/***+******+*+*+**********~*********************************~*
* DUPLICATES
* * l
SUBROUTINE SRTMRG(LIW.CSTART.CEND.START.END. HEAO.AVAIL.ELIST) DIMENSION LIM<(l).CSfART(l),CENO(l) INTEGER HEAO.AVAIL. ELIST Initialize parameters NXTNOO = 0 LAST = 0 NODE = HEAD Search for the proper location from sorted llst to place the hlddan segment GOTD ID0 10 IF(NODE.EV.0) IF(CEND(NODE).GT.START) GOT0 20 LAST = NODE NODE = LINK(NODE) GDTD 10 Now, the locatlon 1s found, check for overlaps of current hidden segnsnt and previous segments GOT0 50 20 IF(CSTARf(NODE).GT.END) Merge (sfART.EW) and (CSTART(NODE),CEM(NODE)) C/* START = AMINl(CSTART(NODE).START) END = AMAXl(CENO(fWDE).END) Delete NOOE from llst and make that locatton C/* available for future use c/* NXTNOD = LINK(NODE) IF(LAST.EV.0) HEAD = NXTNDD IF(LAST.NE.0) LINK(LAST) = NXTNOD LINK(NODE) = AVAIL AVAIL * WDE If not end of list. check next NODE In llst C/* IF(NXTNOD.EV.0) GOT0 100 NODE = NXTNOD GOT0 20 Hiddened segment found In betwoon two exlSting SeWnts C/+ 50 NXTNM) = NODE Ready to put the hlddened part Into llst c/+ 100 NODE = AVAIL AVAIL = LIM<(AVAIL) CSTART(NODE) = START CEND(NODE )‘ = END IF(LAST.EV.0) HEAD = NODE IF(LAST.NE.0) LINK(LAST) = NODE LINK(NDDE) = NXTNOD IF(NXTNOD.EV.0) ELIST = NODE RETURN END
L
c/+
c/* c/+
c/*
c/*
ii*
c/* C/*
SUBROUTINE : INIT PURPOSE : INITIALIZE
LINK
LIST
ci* C,****r***r*+******+***********************************.******* SUBROUTINE INIT INTEGER LINK(N). HEAD - 0
(LINK. HEAD,
HEAD. AVAIL
AVAIL,
l
N)
Technical Note
AVAIL - 1 Nl = N - 1 DO IO I = I. Nl IO LINK(I) = x+1 LIM<(N) - 0 RETURN END C/*****~*+*+*****+*******************=************************* . *
C/ + SUBROUTINE : RESET C/’ PURPOSE : RESET LINK LIST C/* C/L C/*+*++******************************************************** SUBROUTINE RESET (LINK. HEAD, INTEGER LINK(N). HEAD, AVAIL, IF(HEAD.Eg.0) RETURN LINK(ELIST) = AVAIL AVAIL - HEAD HEAD - 0 RETURN END c/**+**********+***********************************************
l l
AVAIL, ELIST
ELIST.
N)
C/+ SUBROUTINE : DRAY C/* PURPOSE : f0 DRAW THE LINE IN BROKEN SEGMENTS C/* C/* C/**********+***********=************************~**********=** SUBROUTINE DRAU(Xl.Yl.X2.V2.CSfART.CENO.CLINK.CHEAD.EPS) REAL Xl.Vl,XP.YP.EPS REAL CSTART(1). CEND(1) INTEGER CLINK(l). CHEAD Check If hlddened segments exist C/+ IF(CHEAD.NE.0) GOT0 1 DRAY from (Xl.Vl) to (X2.V2) -----C-----No hlddwmd parts, CALL DRAYLN (Xl.Yi.X2,Y2) RETURN Start drwalng visible segments C/’ 1 MIDE = CHEAD XDIFF - x2 - Xl VDIFF = Y2 - Y1 TSTART = 0 XSTART - Xl YSTART = Yl 3 IF(NODE.EG.0) GOT0 IO TEND = CSrART(NODE) Chock If segment 18 too short to plot C/’ IF(ABS(TEND-TSTART).LE.EPS) GOT0 5 XEND - Xl + TEND l XDIFF YEN0 - Yl + TEND * YDIFF C______ DRAW from (xSTART.YSIART) to (XEND.VEM)) -------CALL cmuLr~ (xSTAR~.YST~~RT.~END.VEND) fSfART - CEND(NODE) 5 XSfART = Xl + TS7ARf l XOIFF YSTART - Yl + TSTARf * VDIFF NODE - CLINK(NODE) GOT0 3 Laat segment to plot C/+ IO IF(rSrARr.GE.(i.O-EPS)) RETURN c-----DRAW from (XSTART.VSTARI) to (x2.Y2) -----CALL DRAWLN (XSrART.VSrART.X2.V2)
* * l
*