FreeWRL / FreeX3D 4.3.0
arc.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 * arc.c++
37 *
38 */
39
40#include <stdio.h>
41#include "glimports.h"
42#include "mystdio.h"
43#include "myassert.h"
44#include "arc.h"
45#include "bin.h"
46#include "bezierarc.h"
47#include "pwlarc.h"
48#include "simplemath.h"
49
50/* local preprocessor definitions */
51#define ZERO 0.00001/*0.000001*/
52
53const int Arc::bezier_tag = (1<<13);
54const int Arc::arc_tag = (1<<3);
55const int Arc::tail_tag = (1<<6);
56
57/*--------------------------------------------------------------------------
58 * makeSide - attach a pwl arc to an arc and mark it as a border arc
59 *--------------------------------------------------------------------------
60 */
61
62void
63Arc::makeSide( PwlArc *pwl, arc_side side )
64{
65 assert( pwl != 0);
66 assert( pwlArc == 0 );
67 assert( pwl->npts > 0 );
68 assert( pwl->pts != 0);
69 pwlArc = pwl;
70 clearbezier();
71 setside( side );
72}
73
74
75/*--------------------------------------------------------------------------
76 * numpts - count number of points on arc loop
77 *--------------------------------------------------------------------------
78 */
79
80int
81Arc::numpts( void )
82{
83 Arc_ptr jarc = this;
84 int npts = 0;
85 do {
86 npts += jarc->pwlArc->npts;
87 jarc = jarc->next;
88 } while( jarc != this );
89 return npts;
90}
91
92/*--------------------------------------------------------------------------
93 * markverts - mark each point with id of arc
94 *--------------------------------------------------------------------------
95 */
96
97void
98Arc::markverts( void )
99{
100 Arc_ptr jarc = this;
101
102 do {
103 TrimVertex *p = jarc->pwlArc->pts;
104 for( int i=0; i<jarc->pwlArc->npts; i++ )
105 p[i].nuid = jarc->nuid;
106 jarc = jarc->next;
107 } while( jarc != this );
108}
109
110/*--------------------------------------------------------------------------
111 * getextrema - find axis extrema on arc loop
112 *--------------------------------------------------------------------------
113 */
114
115void
116Arc::getextrema( Arc_ptr extrema[4] )
117{
118 REAL leftpt, botpt, rightpt, toppt;
119
120 extrema[0] = extrema[1] = extrema[2] = extrema[3] = this;
121
122 leftpt = rightpt = this->tail()[0];
123 botpt = toppt = this->tail()[1];
124
125 for( Arc_ptr jarc = this->next; jarc != this; jarc = jarc->next ) {
126 if ( jarc->tail()[0] < leftpt ||
127 (jarc->tail()[0] <= leftpt && jarc->rhead()[0]<=leftpt)) {
128 leftpt = jarc->pwlArc->pts->param[0];
129 extrema[1] = jarc;
130 }
131 if ( jarc->tail()[0] > rightpt ||
132 (jarc->tail()[0] >= rightpt && jarc->rhead()[0] >= rightpt)) {
133 rightpt = jarc->pwlArc->pts->param[0];
134 extrema[3] = jarc;
135 }
136 if ( jarc->tail()[1] < botpt ||
137 (jarc->tail()[1] <= botpt && jarc->rhead()[1] <= botpt )) {
138 botpt = jarc->pwlArc->pts->param[1];
139 extrema[2] = jarc;
140 }
141 if ( jarc->tail()[1] > toppt ||
142 (jarc->tail()[1] >= toppt && jarc->rhead()[1] >= toppt)) {
143 toppt = jarc->pwlArc->pts->param[1];
144 extrema[0] = jarc;
145 }
146 }
147}
148
149
150/*-------------------------------------------------------------------------
151 * show - print to the stdout the vertices of a pwl arc
152 *-------------------------------------------------------------------------
153 */
154
155void
156Arc::show()
157{
158#ifndef NDEBUG
159 _glu_dprintf( "\tPWLARC NP: %d FL: 1\n", pwlArc->npts );
160 for( int i = 0; i < pwlArc->npts; i++ ) {
161 _glu_dprintf( "\t\tVERTEX %f %f\n", pwlArc->pts[i].param[0],
162 pwlArc->pts[i].param[1] );
163 }
164#endif
165}
166
167/*-------------------------------------------------------------------------
168 * print - print out the vertices of all pwl arcs on a loop
169 *-------------------------------------------------------------------------
170 */
171
172void
173Arc::print( void )
174{
175 Arc_ptr jarc = this;
176
177#ifndef NDEBUG
178 _glu_dprintf( "BGNTRIM\n" );
179#endif
180 do {
181 jarc->show( );
182 jarc = jarc->next;
183 } while (jarc != this);
184#ifndef NDEBUG
185 _glu_dprintf("ENDTRIM\n" );
186#endif
187}
188
189/*-------------------------------------------------------------------------
190 * isDisconnected - check if tail of arc and head of prev meet
191 *-------------------------------------------------------------------------
192 */
193
194int
195Arc::isDisconnected( void )
196{
197 if( pwlArc == 0 ) return 0;
198 if( prev->pwlArc == 0 ) return 0;
199
200 REAL *p0 = tail();
201 REAL *p1 = prev->rhead();
202
203 if( ((p0[0] - p1[0]) > ZERO) || ((p1[0] - p0[0]) > ZERO) ||
204 ((p0[1] - p1[1]) > ZERO) || ((p1[1] - p0[1]) > ZERO) ) {
205#ifndef NDEBUG
206 _glu_dprintf( "x coord = %f %f %f\n", p0[0], p1[0], p0[0] - p1[0] );
207 _glu_dprintf( "y coord = %f %f %f\n", p0[1], p1[1], p0[1] - p1[1] );
208#endif
209 return 1;
210 } else {
211 /* average two points together */
212 p0[0] = p1[0] = (p1[0] + p0[0]) * 0.5;
213 p0[1] = p1[1] = (p1[1] + p0[1]) * 0.5;
214 return 0;
215 }
216}
217
218/*-------------------------------------------------------------------------
219 * neq_vert - assert that two 2D vertices are not equal
220 *-------------------------------------------------------------------------
221 */
222
223inline static int
224neq_vert( REAL *v1, REAL *v2 )
225{
226 return ((v1[0] != v2[0]) || (v1[1] != v2[1] )) ? 1 : 0;
227}
228
229/*-------------------------------------------------------------------------
230 * check - verify consistency of a loop, including
231 * 1) if pwl, no two consecutive vertices are identical
232 * 2) the circular link pointers are valid
233 * 3) the geometric info at the head and tail are consistent
234 *-------------------------------------------------------------------------
235 */
236
237int
238Arc::check( void )
239{
240 if( this == 0 ) return 1;
241 Arc_ptr jarc = this;
242 do {
243 assert( (jarc->pwlArc != 0) || (jarc->bezierArc != 0) );
244
245 if (jarc->prev == 0 || jarc->next == 0) {
246#ifndef NDEBUG
247 _glu_dprintf( "checkjarc:null next/prev pointer\n");
248 jarc->print( );
249#endif
250 return 0;
251 }
252
253 if (jarc->next->prev != jarc) {
254#ifndef NDEBUG
255 _glu_dprintf( "checkjarc: pointer linkage screwed up\n");
256 jarc->print( );
257#endif
258 return 0;
259 }
260
261 if( jarc->pwlArc ) {
262#ifndef NDEBUG
263 assert( jarc->pwlArc->npts >= 1 );
264 assert( jarc->pwlArc->npts < 100000 );
265/*
266 for( int i=0; i < jarc->pwlArc->npts-1; i++ )
267 assert( neq_vert( jarc->pwlArc->pts[i].param,
268 jarc->pwlArc->pts[i+1].param) );
269*/
270#endif
271 if( jarc->prev->pwlArc ) {
272 if( jarc->tail()[1] != jarc->prev->rhead()[1] ) {
273#ifndef NDEBUG
274 _glu_dprintf( "checkjarc: geometric linkage screwed up 1\n");
275 jarc->prev->show();
276 jarc->show();
277#endif
278 return 0;
279 }
280 if( jarc->tail()[0] != jarc->prev->rhead()[0] ) {
281
282#ifndef NDEBUG
283 _glu_dprintf( "checkjarc: geometric linkage screwed up 2\n");
284 jarc->prev->show();
285 jarc->show();
286#endif
287 return 0;
288 }
289 }
290 if( jarc->next->pwlArc ) {
291 if( jarc->next->tail()[0] != jarc->rhead()[0] ) {
292#ifndef NDEBUG
293 _glu_dprintf( "checkjarc: geometric linkage screwed up 3\n");
294 jarc->show();
295 jarc->next->show();
296#endif
297 return 0;
298 }
299 if( jarc->next->tail()[1] != jarc->rhead()[1] ) {
300#ifndef NDEBUG
301 _glu_dprintf( "checkjarc: geometric linkage screwed up 4\n");
302 jarc->show();
303 jarc->next->show();
304#endif
305 return 0;
306 }
307 }
308 if( jarc->isbezier() ) {
309 assert( jarc->pwlArc->npts == 2 );
310 assert( (jarc->pwlArc->pts[0].param[0] == \
311 jarc->pwlArc->pts[1].param[0]) ||\
312 (jarc->pwlArc->pts[0].param[1] == \
313 jarc->pwlArc->pts[1].param[1]) );
314 }
315 }
316 jarc = jarc->next;
317 } while (jarc != this);
318 return 1;
319}
320
321
322#define TOL 0.00001
323
324inline long tooclose( REAL x, REAL y )
325{
326 return (glu_abs(x-y) < TOL) ? 1 : 0;
327}
328
329
330/*--------------------------------------------------------------------------
331 * append - append a jordan arc to a circularly linked list
332 *--------------------------------------------------------------------------
333 */
334
335Arc_ptr
336Arc::append( Arc_ptr jarc )
337{
338 if( jarc != 0 ) {
339 next = jarc->next;
340 prev = jarc;
341 next->prev = prev->next = this;
342 } else {
343 next = prev = this;
344 }
345 return this;
346}
347