1c2c66affSColin Finck /*
2c2c66affSColin Finck ** License Applicability. Except to the extent portions of this file are
3c2c66affSColin Finck ** made subject to an alternative license as permitted in the SGI Free
4c2c66affSColin Finck ** Software License B, Version 1.1 (the "License"), the contents of this
5c2c66affSColin Finck ** file are subject only to the provisions of the License. You may not use
6c2c66affSColin Finck ** this file except in compliance with the License. You may obtain a copy
7c2c66affSColin Finck ** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
8c2c66affSColin Finck ** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
9c2c66affSColin Finck **
10c2c66affSColin Finck ** http://oss.sgi.com/projects/FreeB
11c2c66affSColin Finck **
12c2c66affSColin Finck ** Note that, as provided in the License, the Software is distributed on an
13c2c66affSColin Finck ** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
14c2c66affSColin Finck ** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
15c2c66affSColin Finck ** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
16c2c66affSColin Finck ** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
17c2c66affSColin Finck **
18c2c66affSColin Finck ** Original Code. The Original Code is: OpenGL Sample Implementation,
19c2c66affSColin Finck ** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
20c2c66affSColin Finck ** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
21c2c66affSColin Finck ** Copyright in any portions created by third parties is as indicated
22c2c66affSColin Finck ** elsewhere herein. All Rights Reserved.
23c2c66affSColin Finck **
24c2c66affSColin Finck ** Additional Notice Provisions: The application programming interfaces
25c2c66affSColin Finck ** established by SGI in conjunction with the Original Code are The
26c2c66affSColin Finck ** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
27c2c66affSColin Finck ** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
28c2c66affSColin Finck ** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
29c2c66affSColin Finck ** Window System(R) (Version 1.3), released October 19, 1998. This software
30c2c66affSColin Finck ** was created using the OpenGL(R) version 1.2.1 Sample Implementation
31c2c66affSColin Finck ** published by SGI, but has not been independently verified as being
32c2c66affSColin Finck ** compliant with the OpenGL(R) version 1.2.1 Specification.
33c2c66affSColin Finck */
34c2c66affSColin Finck 
35c2c66affSColin Finck /*
36c2c66affSColin Finck  * trimline.c++
37c2c66affSColin Finck  *
38c2c66affSColin Finck  */
39c2c66affSColin Finck 
40c2c66affSColin Finck //#include "glimports.h"
41c2c66affSColin Finck //#include "myassert.h"
42c2c66affSColin Finck //#include "mystdio.h"
43c2c66affSColin Finck #include "trimline.h"
44c2c66affSColin Finck #include "backend.h"
45c2c66affSColin Finck 
Trimline()46c2c66affSColin Finck Trimline::Trimline()
47c2c66affSColin Finck {
48c2c66affSColin Finck     size = 0; pts = 0; numverts = 0;
49c2c66affSColin Finck     tinterp = &t; binterp = &b;
50c2c66affSColin Finck }
51c2c66affSColin Finck 
~Trimline()52c2c66affSColin Finck Trimline::~Trimline()
53c2c66affSColin Finck {
54c2c66affSColin Finck     if( pts ) delete[] pts;
55c2c66affSColin Finck }
56c2c66affSColin Finck 
57c2c66affSColin Finck void
init(TrimVertex * v)58c2c66affSColin Finck Trimline::init( TrimVertex *v )
59c2c66affSColin Finck {
60c2c66affSColin Finck     reset();
61c2c66affSColin Finck     grow(1);
62c2c66affSColin Finck     append(v);
63c2c66affSColin Finck }
64c2c66affSColin Finck 
65c2c66affSColin Finck inline void
grow(long npts)66c2c66affSColin Finck Trimline::grow( long npts )
67c2c66affSColin Finck {
68c2c66affSColin Finck     if( size < npts ) {
69c2c66affSColin Finck 	size = 2 * npts;
70c2c66affSColin Finck 	if( pts ) delete[] pts;
71c2c66affSColin Finck         pts = new TrimVertex_p[size];
72c2c66affSColin Finck     }
73c2c66affSColin Finck }
74c2c66affSColin Finck 
75c2c66affSColin Finck inline void
append(TrimVertex * v)76c2c66affSColin Finck Trimline::append( TrimVertex *v )
77c2c66affSColin Finck {
78c2c66affSColin Finck     assert( numverts != size );
79c2c66affSColin Finck     pts[numverts++] = v;
80c2c66affSColin Finck }
81c2c66affSColin Finck 
82c2c66affSColin Finck void
init(long npts,Arc_ptr jarc,long last)83c2c66affSColin Finck Trimline::init( long npts, Arc_ptr jarc, long last )
84c2c66affSColin Finck {
85c2c66affSColin Finck     jarcl.init( jarc, 0, last );
86c2c66affSColin Finck     grow( npts + 2 );
87c2c66affSColin Finck }
88c2c66affSColin Finck 
89c2c66affSColin Finck inline void
swap()90c2c66affSColin Finck Trimline::swap()
91c2c66affSColin Finck {
92c2c66affSColin Finck     TrimVertex *tmp=tinterp;
93c2c66affSColin Finck     tinterp=binterp;
94c2c66affSColin Finck     binterp=tmp;
95c2c66affSColin Finck }
96c2c66affSColin Finck 
97c2c66affSColin Finck void
getNextPt()98c2c66affSColin Finck Trimline::getNextPt()
99c2c66affSColin Finck {
100c2c66affSColin Finck     *binterp = *jarcl.getnextpt();
101c2c66affSColin Finck }
102c2c66affSColin Finck 
103c2c66affSColin Finck void
getPrevPt()104c2c66affSColin Finck Trimline::getPrevPt()
105c2c66affSColin Finck {
106c2c66affSColin Finck     *binterp = *jarcl.getprevpt();
107c2c66affSColin Finck }
108c2c66affSColin Finck 
109c2c66affSColin Finck /*----------------------------------------------------------------------
110c2c66affSColin Finck  * getNextPts - make arrays of pointers to trim points on left and right
111c2c66affSColin Finck  *		hulls of trim strip.
112c2c66affSColin Finck  *----------------------------------------------------------------------
113c2c66affSColin Finck  */
114c2c66affSColin Finck void
getNextPts(REAL vval,Backend & backend)115c2c66affSColin Finck Trimline::getNextPts( REAL vval, Backend& backend )
116c2c66affSColin Finck {
117c2c66affSColin Finck     reset(); swap(); append( tinterp );
118c2c66affSColin Finck     assert( tinterp->param[1] >= vval );
119c2c66affSColin Finck 
120*695946a5SMasanori Ogino     TrimVertex *p;
121c2c66affSColin Finck     for( p=jarcl.getnextpt() ; p->param[1] >= vval; p=jarcl.getnextpt() ) {
122c2c66affSColin Finck 	append( p );
123c2c66affSColin Finck     }
124c2c66affSColin Finck 
125c2c66affSColin Finck     /* compute and copy pointer to final point on left hull */
126c2c66affSColin Finck     if( interpvert( last(), p, binterp, vval ) ) {
127c2c66affSColin Finck 	binterp->nuid = p->nuid;
128c2c66affSColin Finck 	backend.triangle( p, binterp, last() );
129c2c66affSColin Finck         append( binterp );
130c2c66affSColin Finck     }
131c2c66affSColin Finck     jarcl.reverse();
132c2c66affSColin Finck     (void) jarcl.getprevpt(); 	/* reset jarcl to proper position */
133c2c66affSColin Finck     jarcl.reverse();
134c2c66affSColin Finck }
135c2c66affSColin Finck 
136c2c66affSColin Finck void
getPrevPts(REAL vval,Backend & backend)137c2c66affSColin Finck Trimline::getPrevPts( REAL vval, Backend& backend )
138c2c66affSColin Finck {
139c2c66affSColin Finck     reset(); swap(); append( tinterp );
140c2c66affSColin Finck     assert( tinterp->param[1] >= vval );
141c2c66affSColin Finck 
142*695946a5SMasanori Ogino     TrimVertex *q;
143c2c66affSColin Finck     for( q=jarcl.getprevpt(); q->param[1] >= vval; q=jarcl.getprevpt() ) {
144c2c66affSColin Finck 	append( q );
145c2c66affSColin Finck     }
146c2c66affSColin Finck 
147c2c66affSColin Finck     /* compute and copy pointer to final point on right hull */
148c2c66affSColin Finck     if( interpvert( q, last(), binterp, vval ) ) {
149c2c66affSColin Finck 	binterp->nuid = q->nuid;
150c2c66affSColin Finck 	backend.triangle( last(), binterp, q );
151c2c66affSColin Finck         append( binterp );
152c2c66affSColin Finck     }
153c2c66affSColin Finck     jarcl.reverse();
154c2c66affSColin Finck     (void) jarcl.getnextpt();  /* reset jarcl to proper position */
155c2c66affSColin Finck     jarcl.reverse();
156c2c66affSColin Finck }
157c2c66affSColin Finck 
158c2c66affSColin Finck void
getNextPts(Arc_ptr botarc)159c2c66affSColin Finck Trimline::getNextPts( Arc_ptr botarc )
160c2c66affSColin Finck {
161c2c66affSColin Finck     reset(); swap(); append( tinterp );
162c2c66affSColin Finck 
163c2c66affSColin Finck #ifndef NDEBUG
164c2c66affSColin Finck     PwlArc *lastpwl = botarc->prev->pwlArc;
165c2c66affSColin Finck     TrimVertex *lastpt1 = &lastpwl->pts[lastpwl->npts-1];
166c2c66affSColin Finck #endif
167c2c66affSColin Finck     TrimVertex *lastpt2 = botarc->pwlArc->pts;
168*695946a5SMasanori Ogino     TrimVertex *p = jarcl.getnextpt();
169c2c66affSColin Finck     for( append( p ); p != lastpt2; append( p ) ) {
170c2c66affSColin Finck 	assert( p != lastpt1 );
171c2c66affSColin Finck 	p = jarcl.getnextpt();
172c2c66affSColin Finck     }
173c2c66affSColin Finck }
174c2c66affSColin Finck 
175c2c66affSColin Finck void
getPrevPts(Arc_ptr botarc)176c2c66affSColin Finck Trimline::getPrevPts( Arc_ptr botarc )
177c2c66affSColin Finck {
178c2c66affSColin Finck     reset();  swap(); append( tinterp );
179c2c66affSColin Finck 
180c2c66affSColin Finck     PwlArc *lastpwl = botarc->prev->pwlArc;
181c2c66affSColin Finck     TrimVertex *lastpt1 = &lastpwl->pts[lastpwl->npts-1];
182c2c66affSColin Finck #ifndef NDEBUG
183c2c66affSColin Finck     TrimVertex *lastpt2 = botarc->pwlArc->pts;
184c2c66affSColin Finck #endif
185c2c66affSColin Finck 
186*695946a5SMasanori Ogino     TrimVertex *q =  jarcl.getprevpt();
187c2c66affSColin Finck     for( append( q ); q != lastpt1; append( q ) ) {
188c2c66affSColin Finck 	assert( q != lastpt2 );
189c2c66affSColin Finck 	q = jarcl.getprevpt();
190c2c66affSColin Finck     }
191c2c66affSColin Finck }
192c2c66affSColin Finck 
193c2c66affSColin Finck 
194c2c66affSColin Finck long
interpvert(TrimVertex * a,TrimVertex * b,TrimVertex * c,REAL vval)195c2c66affSColin Finck Trimline::interpvert( TrimVertex *a, TrimVertex *b, TrimVertex *c, REAL vval )
196c2c66affSColin Finck {
197c2c66affSColin Finck     REAL denom = a->param[1] - b->param[1];
198c2c66affSColin Finck 
199c2c66affSColin Finck     if(denom != 0) {
200c2c66affSColin Finck 	if( vval == a->param[1] ) {
201c2c66affSColin Finck 	    c->param[0] = a->param[0];
202c2c66affSColin Finck 	    c->param[1] = a->param[1];
203c2c66affSColin Finck 	    c->nuid = a->nuid;
204c2c66affSColin Finck 	    return 0;
205c2c66affSColin Finck 	} else if( vval == b->param[1] ) {
206c2c66affSColin Finck 	    c->param[0] = b->param[0];
207c2c66affSColin Finck 	    c->param[1] = b->param[1];
208c2c66affSColin Finck 	    c->nuid = b->nuid;
209c2c66affSColin Finck 	    return 0;
210c2c66affSColin Finck 	} else {
211c2c66affSColin Finck 	    REAL r = (a->param[1] - vval)/denom;
212c2c66affSColin Finck 	    c->param[0] =  a->param[0] - r * (a->param[0] - b->param[0]);
213c2c66affSColin Finck 	    c->param[1] = vval;
214c2c66affSColin Finck 	    return 1;
215c2c66affSColin Finck 	}
216c2c66affSColin Finck     } else {
217c2c66affSColin Finck         c->param[0] = a->param[0];
218c2c66affSColin Finck         c->param[1] = a->param[1];
219c2c66affSColin Finck 	c->nuid = a->nuid;
220c2c66affSColin Finck 	return 0;
221c2c66affSColin Finck     }
222c2c66affSColin Finck }
223c2c66affSColin Finck 
224