| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright (C) 2000 Silicon Graphics, Inc. All Rights Reserved. |
| 4 |
* |
| 5 |
* This library is free software; you can redistribute it and/or |
| 6 |
* modify it under the terms of the GNU Lesser General Public |
| 7 |
* License as published by the Free Software Foundation; either |
| 8 |
* version 2.1 of the License, or (at your option) any later version. |
| 9 |
* |
| 10 |
* This library is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 |
* Lesser General Public License for more details. |
| 14 |
* |
| 15 |
* Further, this software is distributed without any warranty that it is |
| 16 |
* free of the rightful claim of any third person regarding infringement |
| 17 |
* or the like. Any license provided herein, whether implied or |
| 18 |
* otherwise, applies only to this software file. Patent licenses, if |
| 19 |
* any, provided herein do not apply to combinations of this program with |
| 20 |
* other software, or any other product whatsoever. |
| 21 |
* |
| 22 |
* You should have received a copy of the GNU Lesser General Public |
| 23 |
* License along with this library; if not, write to the Free Software |
| 24 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 25 |
* |
| 26 |
* Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 27 |
* Mountain View, CA 94043, or: |
| 28 |
* |
| 29 |
* http://www.sgi.com |
| 30 |
* |
| 31 |
* For further information regarding this notice, see: |
| 32 |
* |
| 33 |
* http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 34 |
* |
| 35 |
*/ |
| 36 |
|
| 37 |
/* |
| 38 |
* varray.c++ - $Revision: 1.1.1.1 $ |
| 39 |
* Derrick Burns - 1991 |
| 40 |
*/ |
| 41 |
|
| 42 |
#include "glimports.h" |
| 43 |
#include "myassert.h" |
| 44 |
#include "mystdio.h" |
| 45 |
#include "varray.h" |
| 46 |
#include "arc.h" |
| 47 |
#include <cmath> // fabs() |
| 48 |
|
| 49 |
#define TINY 0.0001 |
| 50 |
inline long sgn( REAL x ) |
| 51 |
{ |
| 52 |
return (x < -TINY) ? -1 : ((x > TINY) ? 1 : 0 ); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
Varray::Varray( void ) |
| 57 |
{ |
| 58 |
varray = 0; |
| 59 |
size = 0; |
| 60 |
} |
| 61 |
|
| 62 |
Varray::~Varray( void ) |
| 63 |
{ |
| 64 |
if( varray ) delete[] varray; |
| 65 |
} |
| 66 |
|
| 67 |
inline void |
| 68 |
Varray::update( Arc_ptr arc, long dir[2], REAL val ) |
| 69 |
{ |
| 70 |
register long ds = sgn(arc->tail()[0] - arc->prev->tail()[0]); |
| 71 |
register long dt = sgn(arc->tail()[1] - arc->prev->tail()[1]); |
| 72 |
|
| 73 |
if( dir[0] != ds || dir[1] != dt ) { |
| 74 |
dir[0] = ds; |
| 75 |
dir[1] = dt; |
| 76 |
append( val ); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
void |
| 81 |
Varray::grow( long guess ) |
| 82 |
{ |
| 83 |
if( size < guess ) { |
| 84 |
size = guess * 2; |
| 85 |
if( varray ) delete[] varray; |
| 86 |
varray = new REAL[size]; |
| 87 |
assert( varray ); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
long |
| 92 |
Varray::init( REAL delta, Arc_ptr toparc, Arc_ptr botarc ) |
| 93 |
{ |
| 94 |
Arc_ptr left = toparc->next; |
| 95 |
Arc_ptr right = toparc; |
| 96 |
long ldir[2], rdir[2]; |
| 97 |
|
| 98 |
ldir[0] = sgn( left->tail()[0] - left->prev->tail()[0] ); |
| 99 |
ldir[1] = sgn( left->tail()[1] - left->prev->tail()[1] ); |
| 100 |
rdir[0] = sgn( right->tail()[0] - right->prev->tail()[0] ); |
| 101 |
rdir[1] = sgn( right->tail()[1] - right->prev->tail()[1] ); |
| 102 |
|
| 103 |
vval[0] = toparc->tail()[1]; |
| 104 |
numquads = 0; |
| 105 |
|
| 106 |
while( 1 ) { |
| 107 |
switch( sgn( left->tail()[1] - right->prev->tail()[1] ) ) { |
| 108 |
case 1: |
| 109 |
left = left->next; |
| 110 |
update( left, ldir, left->prev->tail()[1] ); |
| 111 |
break; |
| 112 |
case -1: |
| 113 |
right = right->prev; |
| 114 |
update( right, rdir, right->tail()[1] ); |
| 115 |
break; |
| 116 |
case 0: |
| 117 |
if( fabs(left->tail()[1] - botarc->tail()[1]) < TINY) goto end; |
| 118 |
if( fabs(left->tail()[0]-right->prev->tail()[0]) < TINY && |
| 119 |
fabs(left->tail()[1]-right->prev->tail()[1]) < TINY) goto end; |
| 120 |
left = left->next; |
| 121 |
break; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
end: |
| 126 |
append( botarc->tail()[1] ); |
| 127 |
|
| 128 |
grow( ((long) ((vval[0] - vval[numquads])/delta)) + numquads + 2 ); |
| 129 |
|
| 130 |
long i, index = 0; |
| 131 |
for( i=0; i<numquads; i++ ) { |
| 132 |
voffset[i] = index; |
| 133 |
varray[index++] = vval[i]; |
| 134 |
REAL dist = vval[i] - vval[i+1]; |
| 135 |
if( dist > delta ) { |
| 136 |
long steps = ((long) (dist/delta)) +1; |
| 137 |
float deltav = - dist / (REAL) steps; |
| 138 |
for( long j=1; j<steps; j++ ) |
| 139 |
varray[index++] = vval[i] + j * deltav; |
| 140 |
} |
| 141 |
} |
| 142 |
voffset[i] = index; |
| 143 |
varray[index] = vval[i]; |
| 144 |
return index; |
| 145 |
} |
| 146 |
|