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