migrate substitution keywords to SVN
[reactos.git] / reactos / lib / glu32 / libnurbs / nurbtess / gridWrap.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 ** $Date$ $Revision: 1.1 $
35 */
36 /*
37 ** $Header: /cygdrive/c/RCVS/CVS/ReactOS/reactos/lib/glu32/libnurbs/nurbtess/gridWrap.cc,v 1.1 2004/02/02 16:39:13 navaraf Exp $
38 */
39
40 #include "gluos.h"
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <GL/gl.h>
44 #include "zlassert.h"
45 #include "gridWrap.h"
46
47
48 /*******************grid structure****************************/
49 void gridWrap::print()
50 {
51 printf("n_ulines = %i\n", n_ulines);
52 printf("n_vlines = %i\n", n_vlines);
53 printf("u_min=%f, umax=%f, vmin=%f, vmax=%f\n", u_min, u_max, v_min, v_max);
54 }
55
56 gridWrap::gridWrap(Int nUlines, Real* uvals,
57 Int nVlines, Real* vvals)
58 {
59 assert(nUlines>=2);
60 assert(nVlines>=2);
61
62 is_uniform = 0;
63 n_ulines = nUlines;
64 n_vlines = nVlines;
65 u_min = uvals[0];
66 u_max = uvals[nUlines-1];
67 v_min = vvals[0];
68 v_max = vvals[nVlines-1];
69 u_values = (Real*) malloc(sizeof(Real) * n_ulines);
70 assert(u_values);
71 v_values = (Real*) malloc(sizeof(Real) * n_vlines);
72 assert(v_values);
73
74 Int i;
75 for(i=0; i<n_ulines; i++)
76 u_values[i] = uvals[i];
77 for(i=0; i<n_vlines; i++)
78 v_values[i] = vvals[i];
79 }
80
81 gridWrap::gridWrap(Int nUlines, Int nVlines,
82 Real uMin, Real uMax,
83 Real vMin, Real vMax
84 )
85 {
86 is_uniform = 1;
87 n_ulines = nUlines;
88 n_vlines = nVlines;
89 u_min = uMin;
90 u_max = uMax;
91 v_min = vMin;
92 v_max = vMax;
93 u_values = (Real*) malloc(sizeof(Real) * n_ulines);
94 assert(u_values);
95 v_values = (Real*) malloc(sizeof(Real) * n_vlines);
96 assert(v_values);
97
98 Int i;
99 assert(nUlines>=2);
100 assert(nVlines>=2);
101 Real du = (uMax-uMin)/(nUlines-1);
102 Real dv = (vMax-vMin)/(nVlines-1);
103
104 float tempu=uMin;
105 u_values[0] = tempu;
106 for(i=1; i<nUlines; i++)
107 {
108 tempu += du;
109 u_values[i] = tempu;
110 }
111 u_values[nUlines-1] = uMax;
112
113 float tempv=vMin;
114 v_values[0] = tempv;
115 for(i=1; i<nVlines; i++)
116 {
117 tempv += dv;
118 v_values[i] = tempv;
119 }
120 v_values[nVlines-1] = vMax;
121 }
122
123 gridWrap::~gridWrap()
124 {
125 free(u_values);
126 free(v_values);
127 }
128
129 void gridWrap::draw()
130 {
131 int i,j;
132 glBegin(GL_POINTS);
133 for(i=0; i<n_ulines; i++)
134 for(j=0; j<n_vlines; j++)
135 glVertex2f(get_u_value(i), get_v_value(j));
136 glEnd();
137 }
138
139 void gridWrap::outputFanWithPoint(Int v, Int uleft, Int uright, Real vert[2], primStream* pStream)
140 {
141 Int i;
142 if(uleft >= uright)
143 return; //no triangles to output.
144
145 pStream->begin();
146 pStream->insert(vert);
147
148 assert(vert[1] != v_values[v]); //don't output degenerate triangles
149
150 if(vert[1] > v_values[v]) //vertex is above this grid line: notice the orientation
151 {
152 for(i=uleft; i<=uright; i++)
153 pStream->insert(u_values[i], v_values[v]);
154 }
155 else //vertex is below the grid line
156 {
157 for(i=uright; i>= uleft; i--)
158 pStream->insert(u_values[i], v_values[v]);
159 }
160
161 pStream->end(PRIMITIVE_STREAM_FAN);
162 }
163
164
165
166 /*each chain stores a number of consecutive
167 *V-lines within a grid.
168 *There is one grid vertex on each V-line.
169 * The total number of V-lines is:
170 * nVlines.
171 * with respect to the grid, the index of the first V-line is
172 * firstVlineIndex.
173 * So with respect to the grid, the index of the ith V-line is
174 * firstVlineIndex-i.
175 * the grid-index of the uline at the ith vline (recall that each vline has one grid point)
176 * is ulineIndices[i]. The u_value is cached in ulineValues[i], that is,
177 * ulineValues[i] = grid->get_u_value(ulineIndices[i])
178 */
179 gridBoundaryChain::gridBoundaryChain(
180 gridWrap* gr,
181 Int first_vline_index,
182 Int n_vlines,
183 Int* uline_indices,
184 Int* inner_indices
185 )
186 : grid(gr), firstVlineIndex(first_vline_index), nVlines(n_vlines)
187 {
188 ulineIndices = (Int*) malloc(sizeof(Int) * n_vlines);
189 assert(ulineIndices);
190
191 innerIndices = (Int*) malloc(sizeof(Int) * n_vlines);
192 assert(innerIndices);
193
194 vertices = (Real2*) malloc(sizeof(Real2) * n_vlines);
195 assert(vertices);
196
197
198
199 Int i;
200 for(i=0; i<n_vlines; i++){
201 ulineIndices[i] = uline_indices[i];
202 innerIndices[i] = inner_indices[i];
203 }
204
205 for(i=0; i<n_vlines; i++){
206 vertices[i][0] = gr->get_u_value(ulineIndices[i]);
207 vertices[i][1] = gr->get_v_value(first_vline_index-i);
208 }
209 }
210
211 void gridBoundaryChain::draw()
212 {
213 Int i;
214 glBegin(GL_LINE_STRIP);
215 for(i=0; i<nVlines; i++)
216 {
217 glVertex2fv(vertices[i]);
218 }
219 glEnd();
220 }
221
222 void gridBoundaryChain::drawInner()
223 {
224 Int i;
225 for(i=1; i<nVlines; i++)
226 {
227 glBegin(GL_LINE_STRIP);
228 glVertex2f(grid->get_u_value(innerIndices[i]), get_v_value(i-1) );
229 glVertex2f(grid->get_u_value(innerIndices[i]), get_v_value(i) );
230 glEnd();
231 }
232 }
233
234 Int gridBoundaryChain::lookfor(Real v, Int i1, Int i2)
235 {
236 Int mid;
237 while(i1 < i2-1)
238 {
239 mid = (i1+i2)/2;
240 if(v > vertices[mid][1])
241 {
242 i2 = mid;
243 }
244 else
245 i1 = mid;
246 }
247 return i1;
248 }
249
250 /*output the fan of the right end between grid line i-1 and grid line i*/
251 void gridBoundaryChain::rightEndFan(Int i, primStream* pStream)
252 {
253 Int j;
254 if(getUlineIndex(i) > getUlineIndex(i-1))
255 {
256 pStream->begin();
257 pStream->insert(get_vertex(i-1));
258 for(j=getUlineIndex(i-1); j<= getUlineIndex(i); j++)
259 pStream->insert(grid->get_u_value(j), get_v_value(i));
260 pStream->end(PRIMITIVE_STREAM_FAN);
261 }
262 else if(getUlineIndex(i) < getUlineIndex(i-1))
263 {
264 pStream->begin();
265 pStream->insert(get_vertex(i));
266 for(j=getUlineIndex(i-1); j>= getUlineIndex(i); j--)
267 pStream->insert(grid->get_u_value(j), get_v_value(i-1));
268 pStream->end(PRIMITIVE_STREAM_FAN);
269 }
270 //otherside, the two are equal, so there is no fan to output
271 }
272
273
274 /*output the fan of the left end between grid line i-1 and grid line i*/
275 void gridBoundaryChain::leftEndFan(Int i, primStream* pStream)
276 {
277 Int j;
278 if(getUlineIndex(i) < getUlineIndex(i-1))
279 {
280 pStream->begin();
281 pStream->insert(get_vertex(i-1));
282 for(j=getUlineIndex(i); j<= getUlineIndex(i-1); j++)
283 pStream->insert(grid->get_u_value(j), get_v_value(i));
284 pStream->end(PRIMITIVE_STREAM_FAN);
285 }
286 else if(getUlineIndex(i) > getUlineIndex(i-1))
287 {
288 pStream->begin();
289 pStream->insert(get_vertex(i));
290 for(j=getUlineIndex(i); j>= getUlineIndex(i-1); j--)
291 pStream->insert(grid->get_u_value(j), get_v_value(i-1));
292 pStream->end(PRIMITIVE_STREAM_FAN);
293 }
294 /*otherwisem, the two are equal, so there is no fan to outout*/
295 }