summaryrefslogtreecommitdiff
path: root/src/glu/sgi/libnurbs/nurbtess/sampleComp.cc
blob: b58de10af701d21da199a6e096ed8aebdf6480d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
** License Applicability. Except to the extent portions of this file are
** made subject to an alternative license as permitted in the SGI Free
** Software License B, Version 1.1 (the "License"), the contents of this
** file are subject only to the provisions of the License. You may not use
** this file except in compliance with the License. You may obtain a copy
** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
** 
** http://oss.sgi.com/projects/FreeB
** 
** Note that, as provided in the License, the Software is distributed on an
** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
** 
** Original Code. The Original Code is: OpenGL Sample Implementation,
** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
** Copyright in any portions created by third parties is as indicated
** elsewhere herein. All Rights Reserved.
** 
** Additional Notice Provisions: The application programming interfaces
** established by SGI in conjunction with the Original Code are The
** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
** Window System(R) (Version 1.3), released October 19, 1998. This software
** was created using the OpenGL(R) version 1.2.1 Sample Implementation
** published by SGI, but has not been independently verified as being
** compliant with the OpenGL(R) version 1.2.1 Specification.
**
** $Date: 2005/10/28 13:09:23 $ $Revision: 1.2 $
*/
/*
** $Header: /home/krh/git/sync/mesa-cvs-repo/Mesa/src/glu/sgi/libnurbs/nurbtess/sampleComp.cc,v 1.2 2005/10/28 13:09:23 brianp Exp $
*/

#include <stdlib.h>
#include <stdio.h>
#include "glimports.h"
#include "sampleComp.h"
#include "sampleCompTop.h"
#include "sampleCompBot.h"
#include "sampleCompRight.h"



#define max(a,b) ((a>b)? a:b)
#define min(a,b) ((a>b)? b:a)

void sampleConnectedComp(Real* topVertex, Real* botVertex,
		    vertexArray* leftChain, 
		    Int leftStartIndex, Int leftEndIndex,
		    vertexArray* rightChain,
		    Int rightStartIndex, Int rightEndIndex,
		    gridBoundaryChain* leftGridChain,
		    gridBoundaryChain* rightGridChain,
		    Int gridIndex1, Int gridIndex2,
		    Int up_leftCornerWhere,
		    Int up_leftCornerIndex,
		    Int up_rightCornerWhere,
		    Int up_rightCornerIndex,
		    Int down_leftCornerWhere,
		    Int down_leftCornerIndex,
		    Int down_rightCornerWhere,
		    Int down_rightCornerIndex,
		    primStream* pStream,
		    rectBlockArray* rbArray
		    )
{

 sampleCompLeft(topVertex, botVertex,
		leftChain,
		leftStartIndex,	leftEndIndex,
		rightChain,
		rightStartIndex, rightEndIndex,
		leftGridChain,
		gridIndex1,
		gridIndex2,
		up_leftCornerWhere,
		up_leftCornerIndex,
		down_leftCornerWhere,
		down_leftCornerIndex,
		pStream);


 sampleCompRight(topVertex, botVertex,
		 leftChain,
		 leftStartIndex, leftEndIndex,
		 rightChain,
		 rightStartIndex,
		 rightEndIndex,
		 rightGridChain,
		 gridIndex1, gridIndex2,
		 up_rightCornerWhere,
		 up_rightCornerIndex,
		 down_rightCornerWhere,
		 down_rightCornerIndex,
		 pStream);
		 

 sampleCompTop(topVertex,
		     leftChain,
		     leftStartIndex,
		     rightChain,
		     rightStartIndex,
		     leftGridChain,
		     rightGridChain,
		     gridIndex1,
		     up_leftCornerWhere,
		     up_leftCornerIndex,
		     up_rightCornerWhere,
		     up_rightCornerIndex,
		     pStream);

 sampleCompBot(botVertex,
		     leftChain,
		     leftEndIndex,
		     rightChain,
		     rightEndIndex,
		     leftGridChain,
		     rightGridChain,
		     gridIndex2,
		     down_leftCornerWhere,
		     down_leftCornerIndex,
		     down_rightCornerWhere,
		     down_rightCornerIndex,
		     pStream);


 //the center

 rbArray->insert(new rectBlock(leftGridChain, rightGridChain, gridIndex1, gridIndex2));

	
}

/*notice that we need rightChain because the 
 *corners could be on the rightChain.
 *here comp means component.
 */
void sampleCompLeft(Real* topVertex, Real* botVertex,
		    vertexArray* leftChain,
		    Int leftStartIndex, Int leftEndIndex,
		    vertexArray* rightChain,
		    Int rightStartIndex, Int rightEndIndex,
		    gridBoundaryChain* leftGridChain,
		    Int gridIndex1, Int gridIndex2,
		    Int up_leftCornerWhere,
		    Int up_leftCornerIndex,
		    Int down_leftCornerWhere,
		    Int down_leftCornerIndex,
		    primStream* pStream)
{
  /*find out whether there is a trim vertex which is
   *inbetween the top and bot grid lines or not.
   */
  Int midIndex1;
  Int midIndex2;
  Int gridMidIndex1 = 0, gridMidIndex2 = 0;
  //midIndex1: array[i] <= v, array[i-1] > v
  //midIndex2: array[i] >= v, array[i+1] < v
  // v(gridMidIndex1) >= v(midindex1) > v(gridMidIndex1+1)
  // v(gridMidIndex2-1) >= v(midIndex2) > v(gridMidIndex2) ??
  midIndex1 = leftChain->findIndexBelowGen(
					   leftGridChain->get_v_value(gridIndex1),
					   leftStartIndex,
					   leftEndIndex);

  midIndex2 = -1; /*initilization*/
  if(midIndex1<= leftEndIndex && gridIndex1<gridIndex2)
    if(leftChain->getVertex(midIndex1)[1] >= leftGridChain->get_v_value(gridIndex2))
      {
	midIndex2 = leftChain->findIndexAboveGen(
						 leftGridChain->get_v_value(gridIndex2),
						 midIndex1, //midIndex1 <= midIndex2.
						 leftEndIndex);
	gridMidIndex1  = leftGridChain->lookfor(leftChain->getVertex(midIndex1)[1],
						gridIndex1, gridIndex2);
	gridMidIndex2 = 1+leftGridChain->lookfor(leftChain->getVertex(midIndex2)[1],
					       gridMidIndex1, gridIndex2);	
      }


  /*to interprete the corner information*/
  Real* cornerTop;
  Real* cornerBot;
  Int cornerLeftStart;
  Int cornerLeftEnd;
  Int cornerRightUpEnd;
  Int cornerRightDownStart;
  if(up_leftCornerWhere == 0) /*left corner is on left chain*/
    {
      cornerTop = leftChain->getVertex(up_leftCornerIndex);
      cornerLeftStart = up_leftCornerIndex+1;
      cornerRightUpEnd = -1; /*no right*/
    }
  else if(up_leftCornerWhere == 1) /*left corner is on top*/
    {
      cornerTop = topVertex;
      cornerLeftStart = leftStartIndex;
      cornerRightUpEnd = -1; /*no right*/
    }
  else /*left corner is on right chain*/
    {
      cornerTop = topVertex;
      cornerLeftStart = leftStartIndex;
      cornerRightUpEnd = up_leftCornerIndex;
    }
  
  if(down_leftCornerWhere == 0) /*left corner is on left chain*/
    {
      cornerBot = leftChain->getVertex(down_leftCornerIndex);
      cornerLeftEnd = down_leftCornerIndex-1;
      cornerRightDownStart = rightEndIndex+1; /*no right*/
    }
  else if(down_leftCornerWhere == 1) /*left corner is on bot*/
    {
      cornerBot = botVertex;
      cornerLeftEnd = leftEndIndex;
      cornerRightDownStart = rightEndIndex+1; /*no right*/
    }
  else /*left corner is on the right chian*/
    {
      cornerBot = botVertex;
      cornerLeftEnd = leftEndIndex;
      cornerRightDownStart = down_leftCornerIndex;
    }
      



  /*sample*/
  if(midIndex2 >= 0) /*there is a trim point inbewteen grid lines*/
    {

      sampleLeftSingleTrimEdgeRegionGen(cornerTop, leftChain->getVertex(midIndex1),
					leftChain,
					cornerLeftStart,
					midIndex1-1,
					leftGridChain,
					gridIndex1,
					gridMidIndex1,
					rightChain,
					rightStartIndex,
					cornerRightUpEnd,
					0, //no right down section
					-1,
					pStream);

      sampleLeftSingleTrimEdgeRegionGen(leftChain->getVertex(midIndex2),
					cornerBot,
					leftChain,
					midIndex2+1,
					cornerLeftEnd,
					leftGridChain,
					gridMidIndex2,
					gridIndex2,
					rightChain,
					0, //no right up section
					-1,
					cornerRightDownStart,
					rightEndIndex,
					pStream);


      sampleLeftStripRecF(leftChain,
			  midIndex1,
			  midIndex2,
			  leftGridChain,
			  gridMidIndex1,
			  gridMidIndex2,
			  pStream);
    }
  else
    {
      sampleLeftSingleTrimEdgeRegionGen(cornerTop, cornerBot,
					leftChain,
					cornerLeftStart,
					cornerLeftEnd,
					leftGridChain,
					gridIndex1,
					gridIndex2,
					rightChain,
					rightStartIndex,
					cornerRightUpEnd,
					cornerRightDownStart,
					rightEndIndex,
					pStream);
    }					 
}
		    
void sampleLeftSingleTrimEdgeRegionGen(Real topVert[2], Real botVert[2],
				       vertexArray* leftChain,
				       Int leftStart,
				       Int leftEnd,
				       gridBoundaryChain* gridChain,
				       Int gridBeginIndex,
				       Int gridEndIndex,
				       vertexArray* rightChain,
				       Int rightUpBegin,
				       Int rightUpEnd,
				       Int rightDownBegin,
				       Int rightDownEnd,
				       primStream* pStream)
{
  Int i,j,k;

  /*creat an array to store all the up and down secments of the right chain,
   *and the left end grid points
   *
   *although vertex array is a dynamic array, but to gain efficiency,
   *it is better to initiliza the exact array size
   */
  vertexArray vArray(gridEndIndex-gridBeginIndex+1 +
		     max(0,rightUpEnd - rightUpBegin+1)+
		     max(0,rightDownEnd - rightDownBegin+1));
  
  /*append the vertices on the up section of thr right chain*/
  for(i=rightUpBegin; i<= rightUpEnd; i++)
    vArray.appendVertex(rightChain->getVertex(i));
  
  /*append the vertices of the left extremal grid points,
   *and at the same time, perform triangulation for the stair cases
   */
  vArray.appendVertex(gridChain->get_vertex(gridBeginIndex));

  for(k=1, i=gridBeginIndex+1; i<=gridEndIndex; i++, k++)
    {
      vArray.appendVertex(gridChain->get_vertex(i));

      /*output the fan of the grid points of the (i)th and (i-1)th grid line.
       */
      if(gridChain->getUlineIndex(i) < gridChain->getUlineIndex(i-1))
	{
	  pStream->begin();	  
	  pStream->insert(gridChain->get_vertex(i-1));
	  for(j=gridChain->getUlineIndex(i); j<= gridChain->getUlineIndex(i-1); j++)
	    pStream->insert(gridChain->getGrid()->get_u_value(j), gridChain->get_v_value(i));
	  pStream->end(PRIMITIVE_STREAM_FAN);
	}
      else if(gridChain->getUlineIndex(i) > gridChain->getUlineIndex(i-1))
	{
	  pStream->begin();
	  pStream->insert(gridChain->get_vertex(i));
	  for(j=gridChain->getUlineIndex(i); j>= gridChain->getUlineIndex(i-1); j--)
	    pStream->insert(gridChain->getGrid()->get_u_value(j), gridChain->get_v_value(i-1));
	  pStream->end(PRIMITIVE_STREAM_FAN);
	}
      /*otherwisem, the two are equal, so there is no fan to outout*/	  
    }

  /*then append all the vertices on the down section of the right chain*/
  for(i=rightDownBegin; i<= rightDownEnd; i++)
    vArray.appendVertex(rightChain->getVertex(i));  

  monoTriangulationRecGen(topVert, botVert,
			  leftChain, leftStart, leftEnd,
			  &vArray, 0, vArray.getNumElements()-1,
			  pStream);
		     
}