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:
10 ** http://oss.sgi.com/projects/FreeB
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.
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.
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.
38 * $Date: 2005/10/28 13:09:23 $ $Revision: 1.3 $
39 * $Header: /home/krh/git/sync/mesa-cvs-repo/Mesa/src/glu/sgi/libnurbs/internals/intersect.cc,v 1.3 2005/10/28 13:09:23 brianp Exp $
42 #include "glimports.h"
45 #include "subdivider.h"
49 #include "trimvertpool.h"
53 enum i_result
{ INTERSECT_VERTEX
, INTERSECT_EDGE
};
56 #ifndef NDEBUG // for asserts only
57 static int arc_classify( Arc_ptr
, int, REAL
);
59 static enum i_result
pwlarc_intersect( PwlArc
*, int, REAL
, int, int[3] );
63 Subdivider::partition( Bin
& bin
, Bin
& left
, Bin
& intersections
,
64 Bin
& right
, Bin
& unknown
, int param
, REAL value
)
66 Bin headonleft
, headonright
, tailonleft
, tailonright
;
68 for( Arc_ptr jarc
= bin
.removearc(); jarc
; jarc
= bin
.removearc() ) {
70 REAL tdiff
= jarc
->tail()[param
] - value
;
71 REAL hdiff
= jarc
->head()[param
] - value
;
76 } else if( hdiff
== 0.0 ) {
77 tailonright
.addarc( jarc
);
80 switch( arc_split(jarc
, param
, value
, 0) ) {
82 tailonright
.addarc( jarc
);
83 headonleft
.addarc( jarc
->next
);
86 assert( jarc
->head()[param
] > value
);
88 tailonright
.addarc( jtemp
= jarc
->next
);
89 headonleft
.addarc( jtemp
->next
);
92 assert( jarc
->head()[param
] <= value
);
93 tailonright
.addarc( jarc
);
94 headonleft
.addarc( jtemp
= jarc
->next
);
95 left
.addarc( jtemp
->next
);
99 tailonright
.addarc( jtemp
= jarc
->next
);
100 headonleft
.addarc( jtemp
= jtemp
->next
);
101 left
.addarc( jtemp
->next
);
104 } else if( tdiff
== 0.0 ) {
106 headonright
.addarc( jarc
);
107 } else if( hdiff
== 0.0 ) {
108 unknown
.addarc( jarc
);
110 headonleft
.addarc( jarc
);
115 switch( arc_split(jarc
, param
, value
, 1) ) {
117 tailonleft
.addarc( jarc
);
118 headonright
.addarc( jarc
->next
);
121 assert( jarc
->head()[param
] < value
);
123 tailonleft
.addarc( jtemp
= jarc
->next
);
124 headonright
.addarc( jtemp
->next
);
127 assert( jarc
->head()[param
] >= value
);
128 tailonleft
.addarc( jarc
);
129 headonright
.addarc( jtemp
= jarc
->next
);
130 right
.addarc( jtemp
->next
);
134 tailonleft
.addarc( jtemp
= jarc
->next
);
135 headonright
.addarc( jtemp
= jtemp
->next
);
136 right
.addarc( jtemp
->next
);
138 } else if( hdiff
== 0.0 ) {
139 tailonleft
.addarc( jarc
);
146 classify_headonleft_s( headonleft
, intersections
, left
, value
);
147 classify_tailonleft_s( tailonleft
, intersections
, left
, value
);
148 classify_headonright_s( headonright
, intersections
, right
, value
);
149 classify_tailonright_s( tailonright
, intersections
, right
, value
);
151 classify_headonleft_t( headonleft
, intersections
, left
, value
);
152 classify_tailonleft_t( tailonleft
, intersections
, left
, value
);
153 classify_headonright_t( headonright
, intersections
, right
, value
);
154 classify_tailonright_t( tailonright
, intersections
, right
, value
);
159 vert_interp( TrimVertex
*n
, TrimVertex
*l
, TrimVertex
*r
, int p
, REAL val
)
161 assert( val
> l
->param
[p
]);
162 assert( val
< r
->param
[p
]);
167 if( l
->param
[1-p
] != r
->param
[1-p
] ) {
168 REAL ratio
= (val
- l
->param
[p
]) / (r
->param
[p
] - l
->param
[p
]);
169 n
->param
[1-p
] = l
->param
[1-p
] +
170 ratio
* (r
->param
[1-p
] - l
->param
[1-p
]);
172 n
->param
[1-p
] = l
->param
[1-p
];
177 Subdivider::arc_split( Arc_ptr jarc
, int param
, REAL value
, int dir
)
179 int maxvertex
= jarc
->pwlArc
->npts
;
181 TrimVertex
* v
= jarc
->pwlArc
->pts
;
184 switch( pwlarc_intersect( jarc
->pwlArc
, param
, value
, dir
, loc
) ) {
186 // When the parameter value lands on a vertex, life is sweet
187 case INTERSECT_VERTEX
: {
188 jarc1
= new(arcpool
) Arc( jarc
, new( pwlarcpool
) PwlArc( maxvertex
-loc
[1], &v
[loc
[1]] ) );
189 jarc
->pwlArc
->npts
= loc
[1] + 1;
190 jarc1
->next
= jarc
->next
;
191 jarc1
->next
->prev
= jarc1
;
194 assert(jarc
->check() != 0);
198 // When the parameter value intersects an edge, we have to
199 // interpolate a new vertex. There are special cases
200 // if the new vertex is adjacent to one or both of the
201 // endpoints of the arc.
202 case INTERSECT_EDGE
: {
213 // The split is between vertices at index j and i, in that
216 // JEB: This code is my idea of how to do the split without
217 // increasing the number of links. I'm doing this so that
218 // the is_rect routine can recognize rectangles created by
219 // subdivision. In exchange for simplifying the curve list,
220 // however, it costs in allocated space and vertex copies.
222 TrimVertex
*newjunk
= trimvertexpool
.get(maxvertex
-i
+1 /*-j*/);
224 for(k
=0; k
<maxvertex
-i
; k
++)
226 newjunk
[k
+1] = v
[i
+k
];
227 newjunk
[k
+1].nuid
= jarc
->nuid
;
230 TrimVertex
*vcopy
= trimvertexpool
.get(maxvertex
);
231 for(k
=0; k
<maxvertex
; k
++)
233 vcopy
[k
].param
[0] = v
[k
].param
[0];
234 vcopy
[k
].param
[1] = v
[k
].param
[1];
236 jarc
->pwlArc
->pts
=vcopy
;
238 v
[i
].nuid
= jarc
->nuid
;
239 v
[j
].nuid
= jarc
->nuid
;
240 vert_interp( &newjunk
[0], &v
[loc
[0]], &v
[loc
[2]], param
, value
);
242 if( showingDegenerate() )
243 backend
.triangle( &v
[i
], &newjunk
[0], &v
[j
] );
245 vcopy
[j
+1].param
[0]=newjunk
[0].param
[0];
246 vcopy
[j
+1].param
[1]=newjunk
[0].param
[1];
249 jarc1
= new(arcpool
) Arc( jarc
,
250 new(pwlarcpool
) PwlArc(maxvertex
-i
+1 , newjunk
) );
252 jarc
->pwlArc
->npts
= j
+2;
253 jarc1
->next
= jarc
->next
;
254 jarc1
->next
->prev
= jarc1
;
257 assert(jarc
->check() != 0);
261 // JEB: This is the original version:
263 Arc_ptr jarc2
, jarc3
;
265 TrimVertex
*newjunk
= trimvertexpool
.get(3);
266 v
[i
].nuid
= jarc
->nuid
;
267 v
[j
].nuid
= jarc
->nuid
;
270 vert_interp( &newjunk
[1], &v
[loc
[0]], &v
[loc
[2]], param
, value
);
272 if( showingDegenerate() )
273 backend
.triangle( &newjunk
[2], &newjunk
[1], &newjunk
[0] );
275 // New vertex adjacent to both endpoints
276 if (maxvertex
== 2) {
277 jarc1
= new(arcpool
) Arc( jarc
, new(pwlarcpool
) PwlArc( 2, newjunk
+1 ) );
278 jarc
->pwlArc
->npts
= 2;
279 jarc
->pwlArc
->pts
= newjunk
;
280 jarc1
->next
= jarc
->next
;
281 jarc1
->next
->prev
= jarc1
;
284 assert(jarc
->check() != 0);
288 // New vertex adjacent to ending point of arc
289 } else if (maxvertex
- j
== 2) {
290 jarc1
= new(arcpool
) Arc( jarc
, new(pwlarcpool
) PwlArc( 2, newjunk
) );
291 jarc2
= new(arcpool
) Arc( jarc
, new(pwlarcpool
) PwlArc( 2, newjunk
+1 ) );
292 jarc
->pwlArc
->npts
= maxvertex
-1;
293 jarc2
->next
= jarc
->next
;
294 jarc2
->next
->prev
= jarc2
;
299 assert(jarc
->check() != 0);
302 // New vertex adjacent to starting point of arc
304 jarc1
= new(arcpool
) Arc( jarc
, new(pwlarcpool
) PwlArc( 2, newjunk
+1 ) );
305 jarc2
= new(arcpool
) Arc( jarc
,
306 new(pwlarcpool
) PwlArc( maxvertex
-1, &jarc
->pwlArc
->pts
[1] ) );
307 jarc
->pwlArc
->npts
= 2;
308 jarc
->pwlArc
->pts
= newjunk
;
309 jarc2
->next
= jarc
->next
;
310 jarc2
->next
->prev
= jarc2
;
315 assert(jarc
->check() != 0);
318 // It's somewhere in the middle
320 jarc1
= new(arcpool
) Arc( jarc
, new(pwlarcpool
) PwlArc( 2, newjunk
) );
321 jarc2
= new(arcpool
) Arc( jarc
, new(pwlarcpool
) PwlArc( 2, newjunk
+1 ) );
322 jarc3
= new(arcpool
) Arc( jarc
, new(pwlarcpool
) PwlArc( maxvertex
-i
, v
+i
) );
323 jarc
->pwlArc
->npts
= j
+ 1;
324 jarc3
->next
= jarc
->next
;
325 jarc3
->next
->prev
= jarc3
;
332 assert(jarc
->check() != 0);
338 return -1; //picked -1 since it's not used
342 /*----------------------------------------------------------------------------
343 * pwlarc_intersect - find intersection of pwlArc and isoparametric line
344 *----------------------------------------------------------------------------
355 assert( pwlArc
->npts
> 0 );
358 TrimVertex
*v
= pwlArc
->pts
;
360 int imax
= pwlArc
->npts
- 1;
361 assert( value
> v
[imin
].param
[param
] );
362 assert( value
< v
[imax
].param
[param
] );
363 while( (imax
- imin
) > 1 ) {
364 int imid
= (imax
+ imin
)/2;
365 if( v
[imid
].param
[param
] > value
)
367 else if( v
[imid
].param
[param
] < value
)
371 return INTERSECT_VERTEX
;
376 return INTERSECT_EDGE
;
378 TrimVertex
*v
= pwlArc
->pts
;
380 int imin
= pwlArc
->npts
- 1;
381 assert( value
> v
[imin
].param
[param
] );
382 assert( value
< v
[imax
].param
[param
] );
383 while( (imin
- imax
) > 1 ) {
384 int imid
= (imax
+ imin
)/2;
385 if( v
[imid
].param
[param
] > value
)
387 else if( v
[imid
].param
[param
] < value
)
391 return INTERSECT_VERTEX
;
396 return INTERSECT_EDGE
;
400 /*----------------------------------------------------------------------------
401 * arc_classify - determine which side of a line a jarc lies
402 *----------------------------------------------------------------------------
405 #ifndef NDEBUG // for asserts only
407 arc_classify( Arc_ptr jarc
, int param
, REAL value
)
411 tdiff
= jarc
->tail()[0] - value
;
412 hdiff
= jarc
->head()[0] - value
;
414 tdiff
= jarc
->tail()[1] - value
;
415 hdiff
= jarc
->head()[1] - value
;
421 } else if( hdiff
== 0.0 ) {
426 } else if( tdiff
== 0.0 ) {
429 } else if( hdiff
== 0.0 ) {
437 } else if( hdiff
== 0.0 ) {
447 Subdivider::classify_tailonleft_s( Bin
& bin
, Bin
& in
, Bin
& out
, REAL val
)
449 /* tail at left, head on line */
452 while( (j
= bin
.removearc()) != NULL
) {
453 assert( arc_classify( j
, 0, val
) == 0x02 );
456 REAL diff
= j
->next
->head()[0] - val
;
459 } else if( diff
< 0.0 ) {
460 if( ccwTurn_sl( j
, j
->next
) )
465 if( j
->next
->tail()[1] > j
->next
->head()[1] )
474 Subdivider::classify_tailonleft_t( Bin
& bin
, Bin
& in
, Bin
& out
, REAL val
)
476 /* tail at left, head on line */
479 while( (j
= bin
.removearc()) != NULL
) {
480 assert( arc_classify( j
, 1, val
) == 0x02 );
483 REAL diff
= j
->next
->head()[1] - val
;
486 } else if( diff
< 0.0 ) {
487 if( ccwTurn_tl( j
, j
->next
) )
492 if (j
->next
->tail()[0] > j
->next
->head()[0] )
501 Subdivider::classify_headonleft_s( Bin
& bin
, Bin
& in
, Bin
& out
, REAL val
)
503 /* tail on line, head at left */
506 while( (j
= bin
.removearc()) != NULL
) {
507 assert( arc_classify( j
, 0, val
) == 0x20 );
511 REAL diff
= j
->prev
->tail()[0] - val
;
514 } else if( diff
< 0.0 ) {
515 if( ccwTurn_sl( j
->prev
, j
) )
520 if( j
->prev
->tail()[1] > j
->prev
->head()[1] )
529 Subdivider::classify_headonleft_t( Bin
& bin
, Bin
& in
, Bin
& out
, REAL val
)
531 /* tail on line, head at left */
534 while( (j
= bin
.removearc()) != NULL
) {
535 assert( arc_classify( j
, 1, val
) == 0x20 );
538 REAL diff
= j
->prev
->tail()[1] - val
;
541 } else if( diff
< 0.0 ) {
542 if( ccwTurn_tl( j
->prev
, j
) )
547 if( j
->prev
->tail()[0] > j
->prev
->head()[0] )
557 Subdivider::classify_tailonright_s( Bin
& bin
, Bin
& in
, Bin
& out
, REAL val
)
559 /* tail at right, head on line */
562 while( (j
= bin
.removearc()) != NULL
) {
563 assert( arc_classify( j
, 0, val
) == 0x12);
567 REAL diff
= j
->next
->head()[0] - val
;
569 if( ccwTurn_sr( j
, j
->next
) )
573 } else if( diff
< 0.0 ) {
576 if( j
->next
->tail()[1] > j
->next
->head()[1] )
585 Subdivider::classify_tailonright_t( Bin
& bin
, Bin
& in
, Bin
& out
, REAL val
)
587 /* tail at right, head on line */
590 while( (j
= bin
.removearc()) != NULL
) {
591 assert( arc_classify( j
, 1, val
) == 0x12);
595 REAL diff
= j
->next
->head()[1] - val
;
597 if( ccwTurn_tr( j
, j
->next
) )
601 } else if( diff
< 0.0 ) {
604 if( j
->next
->tail()[0] > j
->next
->head()[0] )
613 Subdivider::classify_headonright_s( Bin
& bin
, Bin
& in
, Bin
& out
, REAL val
)
615 /* tail on line, head at right */
618 while( (j
= bin
.removearc()) != NULL
) {
619 assert( arc_classify( j
, 0, val
) == 0x21 );
623 REAL diff
= j
->prev
->tail()[0] - val
;
625 if( ccwTurn_sr( j
->prev
, j
) )
629 } else if( diff
< 0.0 ) {
632 if( j
->prev
->tail()[1] > j
->prev
->head()[1] )
641 Subdivider::classify_headonright_t( Bin
& bin
, Bin
& in
, Bin
& out
, REAL val
)
643 /* tail on line, head at right */
646 while( (j
= bin
.removearc()) != NULL
) {
647 assert( arc_classify( j
, 1, val
) == 0x21 );
651 REAL diff
= j
->prev
->tail()[1] - val
;
653 if( ccwTurn_tr( j
->prev
, j
) )
657 } else if( diff
< 0.0 ) {
660 if( j
->prev
->tail()[0] > j
->prev
->head()[0] )