1f220fa62Smrg/*
2f220fa62Smrg** License Applicability. Except to the extent portions of this file are
3f220fa62Smrg** made subject to an alternative license as permitted in the SGI Free
4f220fa62Smrg** Software License B, Version 1.1 (the "License"), the contents of this
5f220fa62Smrg** file are subject only to the provisions of the License. You may not use
6f220fa62Smrg** this file except in compliance with the License. You may obtain a copy
7f220fa62Smrg** of the License at Silicon Graphics, Inc., attn: Legal Services, 1600
8f220fa62Smrg** Amphitheatre Parkway, Mountain View, CA 94043-1351, or at:
9f220fa62Smrg**
10f220fa62Smrg** http://oss.sgi.com/projects/FreeB
11f220fa62Smrg**
12f220fa62Smrg** Note that, as provided in the License, the Software is distributed on an
13f220fa62Smrg** "AS IS" basis, with ALL EXPRESS AND IMPLIED WARRANTIES AND CONDITIONS
14f220fa62Smrg** DISCLAIMED, INCLUDING, WITHOUT LIMITATION, ANY IMPLIED WARRANTIES AND
15f220fa62Smrg** CONDITIONS OF MERCHANTABILITY, SATISFACTORY QUALITY, FITNESS FOR A
16f220fa62Smrg** PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
17f220fa62Smrg**
18f220fa62Smrg** Original Code. The Original Code is: OpenGL Sample Implementation,
19f220fa62Smrg** Version 1.2.1, released January 26, 2000, developed by Silicon Graphics,
20f220fa62Smrg** Inc. The Original Code is Copyright (c) 1991-2000 Silicon Graphics, Inc.
21f220fa62Smrg** Copyright in any portions created by third parties is as indicated
22f220fa62Smrg** elsewhere herein. All Rights Reserved.
23f220fa62Smrg**
24f220fa62Smrg** Additional Notice Provisions: The application programming interfaces
25f220fa62Smrg** established by SGI in conjunction with the Original Code are The
26f220fa62Smrg** OpenGL(R) Graphics System: A Specification (Version 1.2.1), released
27f220fa62Smrg** April 1, 1999; The OpenGL(R) Graphics System Utility Library (Version
28f220fa62Smrg** 1.3), released November 4, 1998; and OpenGL(R) Graphics with the X
29f220fa62Smrg** Window System(R) (Version 1.3), released October 19, 1998. This software
30f220fa62Smrg** was created using the OpenGL(R) version 1.2.1 Sample Implementation
31f220fa62Smrg** published by SGI, but has not been independently verified as being
32f220fa62Smrg** compliant with the OpenGL(R) version 1.2.1 Specification.
33f220fa62Smrg*/
34f220fa62Smrg
35f220fa62Smrg/*
36f220fa62Smrg * varray.c++
37f220fa62Smrg *
38f220fa62Smrg */
39f220fa62Smrg
40f220fa62Smrg#include "glimports.h"
41f220fa62Smrg#include "myassert.h"
42f220fa62Smrg#include "mystdio.h"
43f220fa62Smrg#include "varray.h"
44f220fa62Smrg#include "arc.h"
45f220fa62Smrg#include "simplemath.h"         // glu_abs()
46f220fa62Smrg
47f220fa62Smrg#define TINY 0.0001
48f220fa62Smrginline long sgn( REAL x )
49f220fa62Smrg{
50f220fa62Smrg    return (x < -TINY) ? -1 :  ((x > TINY) ? 1 : 0 );
51f220fa62Smrg}
52f220fa62Smrg
53f220fa62Smrg
54f220fa62SmrgVarray::Varray( void )
55f220fa62Smrg{
56f220fa62Smrg    int i;
57f220fa62Smrg
58f220fa62Smrg    varray = 0;
59f220fa62Smrg    size = 0;
60f220fa62Smrg    numquads = 0;
61f220fa62Smrg
62f220fa62Smrg    for (i = 0; i < 1000; i++) {
63f220fa62Smrg        vval[i] = 0;
64f220fa62Smrg        voffset[i] = 0;
65f220fa62Smrg    }
66f220fa62Smrg}
67f220fa62Smrg
68f220fa62SmrgVarray::~Varray( void )
69f220fa62Smrg{
70f220fa62Smrg    if( varray ) delete[] varray;
71f220fa62Smrg}
72f220fa62Smrg
73f220fa62Smrginline void
74f220fa62SmrgVarray::update( Arc_ptr arc, long dir[2], REAL val )
75f220fa62Smrg{
76f220fa62Smrg    register long ds = sgn(arc->tail()[0] - arc->prev->tail()[0]);
77f220fa62Smrg    register long dt = sgn(arc->tail()[1] - arc->prev->tail()[1]);
78f220fa62Smrg
79f220fa62Smrg    if( dir[0] != ds || dir[1] != dt ) {
80f220fa62Smrg	dir[0] = ds;
81f220fa62Smrg	dir[1] = dt;
82f220fa62Smrg	append( val );
83f220fa62Smrg    }
84f220fa62Smrg}
85f220fa62Smrg
86f220fa62Smrgvoid
87f220fa62SmrgVarray::grow( long guess )
88f220fa62Smrg{
89f220fa62Smrg    if( size < guess ) {
90f220fa62Smrg	size = guess * 2;
91f220fa62Smrg	if( varray ) delete[] varray;
92f220fa62Smrg	varray = new REAL[size];
93f220fa62Smrg	assert( varray != 0 );
94f220fa62Smrg    }
95f220fa62Smrg}
96f220fa62Smrg
97f220fa62Smrglong
98f220fa62SmrgVarray::init( REAL delta, Arc_ptr toparc, Arc_ptr botarc )
99f220fa62Smrg{
100f220fa62Smrg    Arc_ptr left = toparc->next;
101f220fa62Smrg    Arc_ptr right = toparc;
102f220fa62Smrg    long ldir[2], rdir[2];
103f220fa62Smrg
104f220fa62Smrg    ldir[0] = sgn( left->tail()[0] - left->prev->tail()[0] );
105f220fa62Smrg    ldir[1] = sgn( left->tail()[1] - left->prev->tail()[1] );
106f220fa62Smrg    rdir[0] = sgn( right->tail()[0] - right->prev->tail()[0] );
107f220fa62Smrg    rdir[1] = sgn( right->tail()[1] - right->prev->tail()[1] );
108f220fa62Smrg
109f220fa62Smrg    vval[0] = toparc->tail()[1];
110f220fa62Smrg    numquads = 0;
111f220fa62Smrg
112f220fa62Smrg    while( 1 ) {
113f220fa62Smrg	switch( sgn( left->tail()[1] - right->prev->tail()[1] ) ) {
114f220fa62Smrg	case 1:
115f220fa62Smrg	    left = left->next;
116f220fa62Smrg	    update( left, ldir, left->prev->tail()[1] );
117f220fa62Smrg	    break;
118f220fa62Smrg	case -1:
119f220fa62Smrg	    right = right->prev;
120f220fa62Smrg	    update( right, rdir, right->tail()[1] );
121f220fa62Smrg	    break;
122f220fa62Smrg	case 0:
123f220fa62Smrg	    if( glu_abs(left->tail()[1] - botarc->tail()[1]) < TINY) goto end;
124f220fa62Smrg            if( glu_abs(left->tail()[0]-right->prev->tail()[0]) < TINY &&
125f220fa62Smrg                glu_abs(left->tail()[1]-right->prev->tail()[1]) < TINY) goto end;
126f220fa62Smrg	    left = left->next;
127f220fa62Smrg	    break;
128f220fa62Smrg 	}
129f220fa62Smrg    }
130f220fa62Smrg
131f220fa62Smrgend:
132f220fa62Smrg    append( botarc->tail()[1] );
133f220fa62Smrg
134f220fa62Smrg    grow( ((long) ((vval[0] - vval[numquads])/delta)) + numquads + 2 );
135f220fa62Smrg
136f220fa62Smrg    long i, index = 0;
137f220fa62Smrg    for( i=0; i<numquads; i++ ) {
138f220fa62Smrg	voffset[i] = index;
139f220fa62Smrg        varray[index++] = vval[i];
140f220fa62Smrg	REAL dist = vval[i] - vval[i+1];
141f220fa62Smrg	if( dist > delta ) {
142f220fa62Smrg	    long steps = ((long) (dist/delta)) +1;
143f220fa62Smrg	    float deltav = - dist / (REAL) steps;
144f220fa62Smrg	    for( long j=1; j<steps; j++ )
145f220fa62Smrg		varray[index++] = vval[i] + j * deltav;
146f220fa62Smrg	}
147f220fa62Smrg    }
148f220fa62Smrg    voffset[i] = index;
149f220fa62Smrg    varray[index] = vval[i];
150f220fa62Smrg    return index;
151f220fa62Smrg}
152f220fa62Smrg
153