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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
|
/*
** 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.
*/
/*
* intersect.c++
*
* $Date: 2001/03/17 00:25:41 $ $Revision: 1.1 $
* $Header: /home/krh/git/sync/mesa-cvs-repo/Mesa/src/glu/sgi/libnurbs/internals/intersect.cc,v 1.1 2001/03/17 00:25:41 brianp Exp $
*/
#include "glimports.h"
#include "myassert.h"
#include "mystdio.h"
#include "subdivider.h"
#include "arc.h"
#include "bin.h"
#include "backend.h"
#include "trimvertpool.h"
/*#define NOTDEF*/
enum i_result { INTERSECT_VERTEX, INTERSECT_EDGE };
/* local functions */
static int arc_classify( Arc_ptr, int, REAL );
static enum i_result pwlarc_intersect( PwlArc *, int, REAL, int, int[3] );
void
Subdivider::partition( Bin & bin, Bin & left, Bin & intersections,
Bin & right, Bin & unknown, int param, REAL value )
{
Bin headonleft, headonright, tailonleft, tailonright;
for( Arc_ptr jarc = bin.removearc(); jarc; jarc = bin.removearc() ) {
REAL tdiff = jarc->tail()[param] - value;
REAL hdiff = jarc->head()[param] - value;
if( tdiff > 0.0 ) {
if( hdiff > 0.0 ) {
right.addarc( jarc );
} else if( hdiff == 0.0 ) {
tailonright.addarc( jarc );
} else {
Arc_ptr jtemp;
switch( arc_split(jarc, param, value, 0) ) {
case 2:
tailonright.addarc( jarc );
headonleft.addarc( jarc->next );
break;
case 31:
assert( jarc->head()[param] > value );
right.addarc( jarc );
tailonright.addarc( jtemp = jarc->next );
headonleft.addarc( jtemp->next );
break;
case 32:
assert( jarc->head()[param] <= value );
tailonright .addarc( jarc );
headonleft.addarc( jtemp = jarc->next );
left.addarc( jtemp->next );
break;
case 4:
right.addarc( jarc );
tailonright.addarc( jtemp = jarc->next );
headonleft.addarc( jtemp = jtemp->next );
left.addarc( jtemp->next );
}
}
} else if( tdiff == 0.0 ) {
if( hdiff > 0.0 ) {
headonright.addarc( jarc );
} else if( hdiff == 0.0 ) {
unknown.addarc( jarc );
} else {
headonleft.addarc( jarc );
}
} else {
if( hdiff > 0.0 ) {
Arc_ptr jtemp;
switch( arc_split(jarc, param, value, 1) ) {
case 2:
tailonleft.addarc( jarc );
headonright.addarc( jarc->next );
break;
case 31:
assert( jarc->head()[param] < value );
left.addarc( jarc );
tailonleft.addarc( jtemp = jarc->next );
headonright.addarc( jtemp->next );
break;
case 32:
assert( jarc->head()[param] >= value );
tailonleft.addarc( jarc );
headonright.addarc( jtemp = jarc->next );
right.addarc( jtemp->next );
break;
case 4:
left.addarc( jarc );
tailonleft.addarc( jtemp = jarc->next );
headonright.addarc( jtemp = jtemp->next );
right.addarc( jtemp->next );
}
} else if( hdiff == 0.0 ) {
tailonleft.addarc( jarc );
} else {
left.addarc( jarc );
}
}
}
if( param == 0 ) {
classify_headonleft_s( headonleft, intersections, left, value );
classify_tailonleft_s( tailonleft, intersections, left, value );
classify_headonright_s( headonright, intersections, right, value );
classify_tailonright_s( tailonright, intersections, right, value );
} else {
classify_headonleft_t( headonleft, intersections, left, value );
classify_tailonleft_t( tailonleft, intersections, left, value );
classify_headonright_t( headonright, intersections, right, value );
classify_tailonright_t( tailonright, intersections, right, value );
}
}
inline static void
vert_interp( TrimVertex *n, TrimVertex *l, TrimVertex *r, int p, REAL val )
{
assert( val > l->param[p]);
assert( val < r->param[p]);
n->nuid = l->nuid;
n->param[p] = val;
if( l->param[1-p] != r->param[1-p] ) {
REAL ratio = (val - l->param[p]) / (r->param[p] - l->param[p]);
n->param[1-p] = l->param[1-p] +
ratio * (r->param[1-p] - l->param[1-p]);
} else {
n->param[1-p] = l->param[1-p];
}
}
int
Subdivider::arc_split( Arc_ptr jarc, int param, REAL value, int dir )
{
int maxvertex = jarc->pwlArc->npts;
Arc_ptr jarc1, jarc2, jarc3;
TrimVertex* v = jarc->pwlArc->pts;
int loc[3];
switch( pwlarc_intersect( jarc->pwlArc, param, value, dir, loc ) ) {
// When the parameter value lands on a vertex, life is sweet
case INTERSECT_VERTEX: {
jarc1 = new(arcpool) Arc( jarc, new( pwlarcpool) PwlArc( maxvertex-loc[1], &v[loc[1]] ) );
jarc->pwlArc->npts = loc[1] + 1;
jarc1->next = jarc->next;
jarc1->next->prev = jarc1;
jarc->next = jarc1;
jarc1->prev = jarc;
assert(jarc->check() != 0);
return 2;
}
// When the parameter value intersects an edge, we have to
// interpolate a new vertex. There are special cases
// if the new vertex is adjacent to one or both of the
// endpoints of the arc.
case INTERSECT_EDGE: {
int i, j;
if( dir == 0 ) {
i = loc[0];
j = loc[2];
} else {
i = loc[2];
j = loc[0];
}
#ifndef NOTDEF
// The split is between vertices at index j and i, in that
// order (j < i)
// JEB: This code is my idea of how to do the split without
// increasing the number of links. I'm doing this so that
// the is_rect routine can recognize rectangles created by
// subdivision. In exchange for simplifying the curve list,
// however, it costs in allocated space and vertex copies.
TrimVertex *newjunk = trimvertexpool.get(maxvertex -i+1 /*-j*/);
int k;
for(k=0; k<maxvertex-i; k++)
{
newjunk[k+1] = v[i+k];
newjunk[k+1].nuid = jarc->nuid;
}
TrimVertex *vcopy = trimvertexpool.get(maxvertex);
for(k=0; k<maxvertex; k++)
{
vcopy[k].param[0] = v[k].param[0];
vcopy[k].param[1] = v[k].param[1];
}
jarc->pwlArc->pts=vcopy;
v[i].nuid = jarc->nuid;
v[j].nuid = jarc->nuid;
vert_interp( &newjunk[0], &v[loc[0]], &v[loc[2]], param, value );
if( showingDegenerate() )
backend.triangle( &v[i], &newjunk[0], &v[j] );
vcopy[j+1].param[0]=newjunk[0].param[0];
vcopy[j+1].param[1]=newjunk[0].param[1];
jarc1 = new(arcpool) Arc( jarc,
new(pwlarcpool) PwlArc(maxvertex-i+1 , newjunk ) );
jarc->pwlArc->npts = j+2;
jarc1->next = jarc->next;
jarc1->next->prev = jarc1;
jarc->next = jarc1;
jarc1->prev = jarc;
assert(jarc->check() != 0);
return 2;
#endif //not NOTDEF
// JEB: This is the original version:
#ifdef NOTDEF
TrimVertex *newjunk = trimvertexpool.get(3);
v[i].nuid = jarc->nuid;
v[j].nuid = jarc->nuid;
newjunk[0] = v[j];
newjunk[2] = v[i];
vert_interp( &newjunk[1], &v[loc[0]], &v[loc[2]], param, value );
if( showingDegenerate() )
backend.triangle( &newjunk[2], &newjunk[1], &newjunk[0] );
// New vertex adjacent to both endpoints
if (maxvertex == 2) {
jarc1 = new(arcpool) Arc( jarc, new(pwlarcpool) PwlArc( 2, newjunk+1 ) );
jarc->pwlArc->npts = 2;
jarc->pwlArc->pts = newjunk;
jarc1->next = jarc->next;
jarc1->next->prev = jarc1;
jarc->next = jarc1;
jarc1->prev = jarc;
assert(jarc->check() != 0);
return 2;
// New vertex adjacent to ending point of arc
} else if (maxvertex - j == 2) {
jarc1 = new(arcpool) Arc( jarc, new(pwlarcpool) PwlArc( 2, newjunk ) );
jarc2 = new(arcpool) Arc( jarc, new(pwlarcpool) PwlArc( 2, newjunk+1 ) );
jarc->pwlArc->npts = maxvertex-1;
jarc2->next = jarc->next;
jarc2->next->prev = jarc2;
jarc->next = jarc1;
jarc1->prev = jarc;
jarc1->next = jarc2;
jarc2->prev = jarc1;
assert(jarc->check() != 0);
return 31;
// New vertex adjacent to starting point of arc
} else if (i == 1) {
jarc1 = new(arcpool) Arc( jarc, new(pwlarcpool) PwlArc( 2, newjunk+1 ) );
jarc2 = new(arcpool) Arc( jarc,
new(pwlarcpool) PwlArc( maxvertex-1, &jarc->pwlArc->pts[1] ) );
jarc->pwlArc->npts = 2;
jarc->pwlArc->pts = newjunk;
jarc2->next = jarc->next;
jarc2->next->prev = jarc2;
jarc->next = jarc1;
jarc1->prev = jarc;
jarc1->next = jarc2;
jarc2->prev = jarc1;
assert(jarc->check() != 0);
return 32;
// It's somewhere in the middle
} else {
jarc1 = new(arcpool) Arc( jarc, new(pwlarcpool) PwlArc( 2, newjunk ) );
jarc2 = new(arcpool) Arc( jarc, new(pwlarcpool) PwlArc( 2, newjunk+1 ) );
jarc3 = new(arcpool) Arc( jarc, new(pwlarcpool) PwlArc( maxvertex-i, v+i ) );
jarc->pwlArc->npts = j + 1;
jarc3->next = jarc->next;
jarc3->next->prev = jarc3;
jarc->next = jarc1;
jarc1->prev = jarc;
jarc1->next = jarc2;
jarc2->prev = jarc1;
jarc2->next = jarc3;
jarc3->prev = jarc2;
assert(jarc->check() != 0);
return 4;
}
#endif // NOTDEF
}
default:
return -1; //picked -1 since it's not used
}
}
/*----------------------------------------------------------------------------
* pwlarc_intersect - find intersection of pwlArc and isoparametric line
*----------------------------------------------------------------------------
*/
static enum i_result
pwlarc_intersect(
PwlArc *pwlArc,
int param,
REAL value,
int dir,
int loc[3] )
{
assert( pwlArc->npts > 0 );
if( dir ) {
TrimVertex *v = pwlArc->pts;
int imin = 0;
int imax = pwlArc->npts - 1;
assert( value > v[imin].param[param] );
assert( value < v[imax].param[param] );
while( (imax - imin) > 1 ) {
int imid = (imax + imin)/2;
if( v[imid].param[param] > value )
imax = imid;
else if( v[imid].param[param] < value )
imin = imid;
else {
loc[1] = imid;
return INTERSECT_VERTEX;
}
}
loc[0] = imin;
loc[2] = imax;
return INTERSECT_EDGE;
} else {
TrimVertex *v = pwlArc->pts;
int imax = 0;
int imin = pwlArc->npts - 1;
assert( value > v[imin].param[param] );
assert( value < v[imax].param[param] );
while( (imin - imax) > 1 ) {
int imid = (imax + imin)/2;
if( v[imid].param[param] > value )
imax = imid;
else if( v[imid].param[param] < value )
imin = imid;
else {
loc[1] = imid;
return INTERSECT_VERTEX;
}
}
loc[0] = imin;
loc[2] = imax;
return INTERSECT_EDGE;
}
}
/*----------------------------------------------------------------------------
* arc_classify - determine which side of a line a jarc lies
*----------------------------------------------------------------------------
*/
static int
arc_classify( Arc_ptr jarc, int param, REAL value )
{
REAL tdiff, hdiff;
if( param == 0 ) {
tdiff = jarc->tail()[0] - value;
hdiff = jarc->head()[0] - value;
} else {
tdiff = jarc->tail()[1] - value;
hdiff = jarc->head()[1] - value;
}
if( tdiff > 0.0 ) {
if( hdiff > 0.0 ) {
return 0x11;
} else if( hdiff == 0.0 ) {
return 0x12;
} else {
return 0x10;
}
} else if( tdiff == 0.0 ) {
if( hdiff > 0.0 ) {
return 0x21;
} else if( hdiff == 0.0 ) {
return 0x22;
} else {
return 0x20;
}
} else {
if( hdiff > 0.0 ) {
return 0x01;
} else if( hdiff == 0.0 ) {
return 0x02;
} else {
return 0;
}
}
}
void
Subdivider::classify_tailonleft_s( Bin& bin, Bin& in, Bin& out, REAL val )
{
/* tail at left, head on line */
Arc_ptr j;
while( (j = bin.removearc()) != NULL ) {
assert( arc_classify( j, 0, val ) == 0x02 );
j->clearitail();
REAL diff = j->next->head()[0] - val;
if( diff > 0.0 ) {
in.addarc( j );
} else if( diff < 0.0 ) {
if( ccwTurn_sl( j, j->next ) )
out.addarc( j );
else
in.addarc( j );
} else {
if( j->next->tail()[1] > j->next->head()[1] )
in.addarc(j);
else
out.addarc(j);
}
}
}
void
Subdivider::classify_tailonleft_t( Bin& bin, Bin& in, Bin& out, REAL val )
{
/* tail at left, head on line */
Arc_ptr j;
while( (j = bin.removearc()) != NULL ) {
assert( arc_classify( j, 1, val ) == 0x02 );
j->clearitail();
REAL diff = j->next->head()[1] - val;
if( diff > 0.0 ) {
in.addarc( j );
} else if( diff < 0.0 ) {
if( ccwTurn_tl( j, j->next ) )
out.addarc( j );
else
in.addarc( j );
} else {
if (j->next->tail()[0] > j->next->head()[0] )
out.addarc( j );
else
in.addarc( j );
}
}
}
void
Subdivider::classify_headonleft_s( Bin& bin, Bin& in, Bin& out, REAL val )
{
/* tail on line, head at left */
Arc_ptr j;
while( (j = bin.removearc()) != NULL ) {
assert( arc_classify( j, 0, val ) == 0x20 );
j->setitail();
REAL diff = j->prev->tail()[0] - val;
if( diff > 0.0 ) {
out.addarc( j );
} else if( diff < 0.0 ) {
if( ccwTurn_sl( j->prev, j ) )
out.addarc( j );
else
in.addarc( j );
} else {
if( j->prev->tail()[1] > j->prev->head()[1] )
in.addarc( j );
else
out.addarc( j );
}
}
}
void
Subdivider::classify_headonleft_t( Bin& bin, Bin& in, Bin& out, REAL val )
{
/* tail on line, head at left */
Arc_ptr j;
while( (j = bin.removearc()) != NULL ) {
assert( arc_classify( j, 1, val ) == 0x20 );
j->setitail();
REAL diff = j->prev->tail()[1] - val;
if( diff > 0.0 ) {
out.addarc( j );
} else if( diff < 0.0 ) {
if( ccwTurn_tl( j->prev, j ) )
out.addarc( j );
else
in.addarc( j );
} else {
if( j->prev->tail()[0] > j->prev->head()[0] )
out.addarc( j );
else
in.addarc( j );
}
}
}
void
Subdivider::classify_tailonright_s( Bin& bin, Bin& in, Bin& out, REAL val )
{
/* tail at right, head on line */
Arc_ptr j;
while( (j = bin.removearc()) != NULL ) {
assert( arc_classify( j, 0, val ) == 0x12);
j->clearitail();
REAL diff = j->next->head()[0] - val;
if( diff > 0.0 ) {
if( ccwTurn_sr( j, j->next ) )
out.addarc( j );
else
in.addarc( j );
} else if( diff < 0.0 ) {
in.addarc( j );
} else {
if( j->next->tail()[1] > j->next->head()[1] )
out.addarc( j );
else
in.addarc( j );
}
}
}
void
Subdivider::classify_tailonright_t( Bin& bin, Bin& in, Bin& out, REAL val )
{
/* tail at right, head on line */
Arc_ptr j;
while( (j = bin.removearc()) != NULL ) {
assert( arc_classify( j, 1, val ) == 0x12);
j->clearitail();
REAL diff = j->next->head()[1] - val;
if( diff > 0.0 ) {
if( ccwTurn_tr( j, j->next ) )
out.addarc( j );
else
in.addarc( j );
} else if( diff < 0.0 ) {
in.addarc( j );
} else {
if( j->next->tail()[0] > j->next->head()[0] )
in.addarc( j );
else
out.addarc( j );
}
}
}
void
Subdivider::classify_headonright_s( Bin& bin, Bin& in, Bin& out, REAL val )
{
/* tail on line, head at right */
Arc_ptr j;
while( (j = bin.removearc()) != NULL ) {
assert( arc_classify( j, 0, val ) == 0x21 );
j->setitail();
REAL diff = j->prev->tail()[0] - val;
if( diff > 0.0 ) {
if( ccwTurn_sr( j->prev, j ) )
out.addarc( j );
else
in.addarc( j );
} else if( diff < 0.0 ) {
out.addarc( j );
} else {
if( j->prev->tail()[1] > j->prev->head()[1] )
out.addarc( j );
else
in.addarc( j );
}
}
}
void
Subdivider::classify_headonright_t( Bin& bin, Bin& in, Bin& out, REAL val )
{
/* tail on line, head at right */
Arc_ptr j;
while( (j = bin.removearc()) != NULL ) {
assert( arc_classify( j, 1, val ) == 0x21 );
j->setitail();
REAL diff = j->prev->tail()[1] - val;
if( diff > 0.0 ) {
if( ccwTurn_tr( j->prev, j ) )
out.addarc( j );
else
in.addarc( j );
} else if( diff < 0.0 ) {
out.addarc( j );
} else {
if( j->prev->tail()[0] > j->prev->head()[0] )
in.addarc( j );
else
out.addarc( j );
}
}
}
|