Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
tess.c
Go to the documentation of this file.
1 /*
2  * SGI FREE SOFTWARE LICENSE B (Version 2.0, Sept. 18, 2008)
3  * Copyright (C) 1991-2000 Silicon Graphics, Inc. All Rights Reserved.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice including the dates of first publication and
13  * either this permission notice or a reference to
14  * http://oss.sgi.com/projects/FreeB/
15  * shall be included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20  * SILICON GRAPHICS, INC. BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
22  * OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  *
25  * Except as contained in this notice, the name of Silicon Graphics, Inc.
26  * shall not be used in advertising or otherwise to promote the sale, use or
27  * other dealings in this Software without prior written authorization from
28  * Silicon Graphics, Inc.
29  */
30 /*
31 ** Author: Eric Veach, July 1994.
32 **
33 */
34 
35 #include "gluos.h"
36 #include <stddef.h>
37 #include <assert.h>
38 #include <setjmp.h>
39 #include "memalloc.h"
40 #include "tess.h"
41 #include "mesh.h"
42 #include "normal.h"
43 #include "sweep.h"
44 #include "tessmono.h"
45 #include "render.h"
46 
47 #define GLU_TESS_DEFAULT_TOLERANCE 0.0
48 #define GLU_TESS_MESH 100112 /* void (*)(GLUmesh *mesh) */
49 
50 #ifndef TRUE
51 #define TRUE 1
52 #endif
53 #ifndef FALSE
54 #define FALSE 0
55 #endif
56 
57 #if defined(__GNUC__) || defined(__clang__)
58 #pragma GCC diagnostic push
59 /* A padding warning is just plain useless */
60 #pragma GCC diagnostic ignored "-Wunused-parameter"
61 #endif
62 
63 /*ARGSUSED*/ static void GLAPIENTRY noBegin( GLenum type ) {}
64 /*ARGSUSED*/ static void GLAPIENTRY noEdgeFlag( GLboolean boundaryEdge ) {}
65 /*ARGSUSED*/ static void GLAPIENTRY noVertex( void *data ) {}
66 /*ARGSUSED*/ static void GLAPIENTRY noEnd( void ) {}
67 /*ARGSUSED*/ static void GLAPIENTRY noError( GLenum errnum ) {}
68 /*ARGSUSED*/ static void GLAPIENTRY noCombine( GLdouble coords[3], void *data[4],
69  GLfloat weight[4], void **dataOut ) {}
70 /*ARGSUSED*/ static void GLAPIENTRY noMesh( GLUmesh *mesh ) {}
71 
72 
73 /*ARGSUSED*/ void GLAPIENTRY __gl_noBeginData( GLenum type,
74  void *polygonData ) {}
75 /*ARGSUSED*/ void GLAPIENTRY __gl_noEdgeFlagData( GLboolean boundaryEdge,
76  void *polygonData ) {}
77 /*ARGSUSED*/ void GLAPIENTRY __gl_noVertexData( void *data,
78  void *polygonData ) {}
79 /*ARGSUSED*/ void GLAPIENTRY __gl_noEndData( void *polygonData ) {}
80 /*ARGSUSED*/ void GLAPIENTRY __gl_noErrorData( GLenum errnum,
81  void *polygonData ) {}
82 /*ARGSUSED*/ void GLAPIENTRY __gl_noCombineData( GLdouble coords[3],
83  void *data[4],
84  GLfloat weight[4],
85  void **outData,
86  void *polygonData ) {}
87 
88 #if defined(__GNUC__) || defined(__clang__)
89 #pragma GCC diagnostic pop
90 #endif
91 
92 /* Half-edges are allocated in pairs (see mesh.c) */
93 //typedef struct { GLUhalfEdge e, eSym; } EdgePair;
94 
95 #undef MAX
96 #define MAX(a,b) ((a) > (b) ? (a) : (b))
97 #define MAX_FAST_ALLOC (MAX(sizeof(EdgePair), \
98  MAX(sizeof(GLUvertex),sizeof(GLUface))))
99 
100 
101 GLUtesselator * GLAPIENTRY
102 gluNewTess( void )
103 {
104  GLUtesselator *tess;
105 
106  /* Only initialize fields which can be changed by the api. Other fields
107  * are initialized where they are used.
108  */
109 
110  if (memInit( MAX_FAST_ALLOC ) == 0) {
111  return 0; /* out of memory */
112  }
113  tess = (GLUtesselator *)memAlloc( sizeof( GLUtesselator ));
114  if (tess == NULL) {
115  return 0; /* out of memory */
116  }
117 
118  tess->state = T_DORMANT;
119 
120  tess->normal[0] = 0;
121  tess->normal[1] = 0;
122  tess->normal[2] = 0;
123 
124  tess->relTolerance = GLU_TESS_DEFAULT_TOLERANCE;
125  tess->windingRule = GLU_TESS_WINDING_ODD;
126  tess->flagBoundary = FALSE;
127  tess->boundaryOnly = FALSE;
128 
129  tess->callBegin = &noBegin;
130  tess->callEdgeFlag = &noEdgeFlag;
131  tess->callVertex = &noVertex;
132  tess->callEnd = &noEnd;
133 
134  tess->callError = &noError;
135  tess->callCombine = &noCombine;
136  tess->callMesh = &noMesh;
137 
138  tess->callBeginData= &__gl_noBeginData;
139  tess->callEdgeFlagData= &__gl_noEdgeFlagData;
140  tess->callVertexData= &__gl_noVertexData;
141  tess->callEndData= &__gl_noEndData;
142  tess->callErrorData= &__gl_noErrorData;
143  tess->callCombineData= &__gl_noCombineData;
144 
145  tess->polygonData= NULL;
146 
147  return tess;
148 }
149 
150 static void MakeDormant( GLUtesselator *tess )
151 {
152  /* Return the tessellator to its original dormant state. */
153 
154  if( tess->mesh != NULL ) {
155  __gl_meshDeleteMesh( tess->mesh );
156  }
157  tess->state = T_DORMANT;
158  tess->lastEdge = NULL;
159  tess->mesh = NULL;
160 }
161 
162 #define RequireState( tess, s ) if( tess->state != s ) GotoState(tess,s)
163 
164 static void GotoState( GLUtesselator *tess, enum TessState newState )
165 {
166  while( tess->state != newState ) {
167  /* We change the current state one level at a time, to get to
168  * the desired state.
169  */
170  if( tess->state < newState ) {
171  switch( tess->state ) {
172  case T_DORMANT:
173  CALL_ERROR_OR_ERROR_DATA( GLU_TESS_MISSING_BEGIN_POLYGON );
174  gluTessBeginPolygon( tess, NULL );
175  break;
176  case T_IN_POLYGON:
177  CALL_ERROR_OR_ERROR_DATA( GLU_TESS_MISSING_BEGIN_CONTOUR );
178  gluTessBeginContour( tess );
179  break;
180  default:
181  ;
182  }
183  } else {
184  switch( tess->state ) {
185  case T_IN_CONTOUR:
186  CALL_ERROR_OR_ERROR_DATA( GLU_TESS_MISSING_END_CONTOUR );
187  gluTessEndContour( tess );
188  break;
189  case T_IN_POLYGON:
190  CALL_ERROR_OR_ERROR_DATA( GLU_TESS_MISSING_END_POLYGON );
191  /* gluTessEndPolygon( tess ) is too much work! */
192  MakeDormant( tess );
193  break;
194  default:
195  ;
196  }
197  }
198  }
199 }
200 
201 
202 void GLAPIENTRY
203 gluDeleteTess( GLUtesselator *tess )
204 {
205  RequireState( tess, T_DORMANT );
206  memFree( tess );
207 }
208 
209 
210 void GLAPIENTRY
211 gluTessProperty( GLUtesselator *tess, GLenum which, GLdouble value )
212 {
213  GLenum windingRule;
214 
215  switch( which ) {
216  case GLU_TESS_TOLERANCE:
217  if( value < 0.0 || value > 1.0 ) break;
218  tess->relTolerance = value;
219  return;
220 
221  case GLU_TESS_WINDING_RULE:
222  windingRule = (GLenum) value;
223  if( windingRule != value ) break; /* not an integer */
224 
225  switch( windingRule ) {
226  case GLU_TESS_WINDING_ODD:
227  case GLU_TESS_WINDING_NONZERO:
228  case GLU_TESS_WINDING_POSITIVE:
229  case GLU_TESS_WINDING_NEGATIVE:
230  case GLU_TESS_WINDING_ABS_GEQ_TWO:
231  tess->windingRule = windingRule;
232  return;
233  default:
234  break;
235  }
236 
237  case GLU_TESS_BOUNDARY_ONLY:
238  tess->boundaryOnly = (value != 0);
239  return;
240 
241  default:
242  CALL_ERROR_OR_ERROR_DATA( GLU_INVALID_ENUM );
243  return;
244  }
245  CALL_ERROR_OR_ERROR_DATA( GLU_INVALID_VALUE );
246 }
247 
248 /* Returns tessellator property */
249 void GLAPIENTRY
250 gluGetTessProperty( GLUtesselator *tess, GLenum which, GLdouble *value )
251 {
252  switch (which) {
253  case GLU_TESS_TOLERANCE:
254  /* tolerance should be in range [0..1] */
255  assert(0.0 <= tess->relTolerance && tess->relTolerance <= 1.0);
256  *value= tess->relTolerance;
257  break;
258  case GLU_TESS_WINDING_RULE:
259  assert(tess->windingRule == GLU_TESS_WINDING_ODD ||
260  tess->windingRule == GLU_TESS_WINDING_NONZERO ||
261  tess->windingRule == GLU_TESS_WINDING_POSITIVE ||
262  tess->windingRule == GLU_TESS_WINDING_NEGATIVE ||
263  tess->windingRule == GLU_TESS_WINDING_ABS_GEQ_TWO);
264  *value= tess->windingRule;
265  break;
266  case GLU_TESS_BOUNDARY_ONLY:
267  assert(tess->boundaryOnly == TRUE || tess->boundaryOnly == FALSE);
268  *value= tess->boundaryOnly;
269  break;
270  default:
271  *value= 0.0;
272  CALL_ERROR_OR_ERROR_DATA( GLU_INVALID_ENUM );
273  break;
274  }
275 } /* gluGetTessProperty() */
276 
277 void GLAPIENTRY
278 gluTessNormal( GLUtesselator *tess, GLdouble x, GLdouble y, GLdouble z )
279 {
280  tess->normal[0] = x;
281  tess->normal[1] = y;
282  tess->normal[2] = z;
283 }
284 
285 void GLAPIENTRY
286 gluTessCallback( GLUtesselator *tess, GLenum which, _GLUfuncptr fn)
287 {
288  switch( which ) {
289  case GLU_TESS_BEGIN:
290  tess->callBegin = (fn == NULL) ? &noBegin : (void (GLAPIENTRY *)(GLenum)) fn;
291  return;
292  case GLU_TESS_BEGIN_DATA:
293  tess->callBeginData = (fn == NULL) ?
294  &__gl_noBeginData : (void (GLAPIENTRY *)(GLenum, void *)) fn;
295  return;
296  case GLU_TESS_EDGE_FLAG:
297  tess->callEdgeFlag = (fn == NULL) ? &noEdgeFlag :
298  (void (GLAPIENTRY *)(GLboolean)) fn;
299  /* If the client wants boundary edges to be flagged,
300  * we render everything as separate triangles (no strips or fans).
301  */
302  tess->flagBoundary = (fn != NULL);
303  return;
304  case GLU_TESS_EDGE_FLAG_DATA:
305  tess->callEdgeFlagData= (fn == NULL) ?
306  &__gl_noEdgeFlagData : (void (GLAPIENTRY *)(GLboolean, void *)) fn;
307  /* If the client wants boundary edges to be flagged,
308  * we render everything as separate triangles (no strips or fans).
309  */
310  tess->flagBoundary = (fn != NULL);
311  return;
312  case GLU_TESS_VERTEX:
313  tess->callVertex = (fn == NULL) ? &noVertex :
314  (void (GLAPIENTRY *)(void *)) fn;
315  return;
316  case GLU_TESS_VERTEX_DATA:
317  tess->callVertexData = (fn == NULL) ?
318  &__gl_noVertexData : (void (GLAPIENTRY *)(void *, void *)) fn;
319  return;
320  case GLU_TESS_END:
321  tess->callEnd = (fn == NULL) ? &noEnd : (void (GLAPIENTRY *)(void)) fn;
322  return;
323  case GLU_TESS_END_DATA:
324  tess->callEndData = (fn == NULL) ? &__gl_noEndData :
325  (void (GLAPIENTRY *)(void *)) fn;
326  return;
327  case GLU_TESS_ERROR:
328  tess->callError = (fn == NULL) ? &noError : (void (GLAPIENTRY *)(GLenum)) fn;
329  return;
330  case GLU_TESS_ERROR_DATA:
331  tess->callErrorData = (fn == NULL) ?
332  &__gl_noErrorData : (void (GLAPIENTRY *)(GLenum, void *)) fn;
333  return;
334  case GLU_TESS_COMBINE:
335  tess->callCombine = (fn == NULL) ? &noCombine :
336  (void (GLAPIENTRY *)(GLdouble [3],void *[4], GLfloat [4], void ** )) fn;
337  return;
338  case GLU_TESS_COMBINE_DATA:
339  tess->callCombineData = (fn == NULL) ? &__gl_noCombineData :
340  (void (GLAPIENTRY *)(GLdouble [3],
341  void *[4],
342  GLfloat [4],
343  void **,
344  void *)) fn;
345  return;
346  case GLU_TESS_MESH:
347  tess->callMesh = (fn == NULL) ? &noMesh : (void (GLAPIENTRY *)(GLUmesh *)) fn;
348  return;
349  default:
350  CALL_ERROR_OR_ERROR_DATA( GLU_INVALID_ENUM );
351  return;
352  }
353 }
354 
355 static int AddVertex( GLUtesselator *tess, GLdouble coords[3], void *data )
356 {
357  GLUhalfEdge *e;
358 
359  e = tess->lastEdge;
360  if( e == NULL ) {
361  /* Make a self-loop (one vertex, one edge). */
362 
363  e = __gl_meshMakeEdge( tess->mesh );
364  if (e == NULL) return 0;
365  if ( !__gl_meshSplice( e, e->Sym ) ) return 0;
366  } else {
367  /* Create a new vertex and edge which immediately follow e
368  * in the ordering around the left face.
369  */
370  if (__gl_meshSplitEdge( e ) == NULL) return 0;
371  e = e->Lnext;
372  }
373 
374  /* The new vertex is now e->Org. */
375  e->Org->data = data;
376  e->Org->coords[0] = coords[0];
377  e->Org->coords[1] = coords[1];
378  e->Org->coords[2] = coords[2];
379 
380  /* The winding of an edge says how the winding number changes as we
381  * cross from the edge''s right face to its left face. We add the
382  * vertices in such an order that a CCW contour will add +1 to
383  * the winding number of the region inside the contour.
384  */
385  e->winding = 1;
386  e->Sym->winding = -1;
387 
388  tess->lastEdge = e;
389 
390  return 1;
391 }
392 
393 
394 static void CacheVertex( GLUtesselator *tess, GLdouble coords[3], void *data )
395 {
396  CachedVertex *v = &tess->cache[tess->cacheCount];
397 
398  v->data = data;
399  v->coords[0] = coords[0];
400  v->coords[1] = coords[1];
401  v->coords[2] = coords[2];
402  ++tess->cacheCount;
403 }
404 
405 
406 static int EmptyCache( GLUtesselator *tess )
407 {
408  CachedVertex *v = tess->cache;
409  CachedVertex *vLast;
410 
411  tess->mesh = __gl_meshNewMesh();
412  if (tess->mesh == NULL) return 0;
413 
414  for( vLast = v + tess->cacheCount; v < vLast; ++v ) {
415  if ( !AddVertex( tess, v->coords, v->data ) ) return 0;
416  }
417  tess->cacheCount = 0;
418  tess->emptyCache = FALSE;
419 
420  return 1;
421 }
422 
423 
424 void GLAPIENTRY
425 gluTessVertex( GLUtesselator *tess, GLdouble coords[3], void *data )
426 {
427  int i, tooLarge = FALSE;
428  GLdouble x, clamped[3];
429 
430  RequireState( tess, T_IN_CONTOUR );
431 
432  if( tess->emptyCache ) {
433  if ( !EmptyCache( tess ) ) {
434  CALL_ERROR_OR_ERROR_DATA( GLU_OUT_OF_MEMORY );
435  return;
436  }
437  tess->lastEdge = NULL;
438  }
439  for( i = 0; i < 3; ++i ) {
440  x = coords[i];
441  if( x < - GLU_TESS_MAX_COORD ) {
442  x = - GLU_TESS_MAX_COORD;
443  tooLarge = TRUE;
444  }
445  if( x > GLU_TESS_MAX_COORD ) {
446  x = GLU_TESS_MAX_COORD;
447  tooLarge = TRUE;
448  }
449  clamped[i] = x;
450  }
451  if( tooLarge ) {
452  CALL_ERROR_OR_ERROR_DATA( GLU_TESS_COORD_TOO_LARGE );
453  }
454 
455  if( tess->mesh == NULL ) {
456  if( tess->cacheCount < TESS_MAX_CACHE ) {
457  CacheVertex( tess, clamped, data );
458  return;
459  }
460  if ( !EmptyCache( tess ) ) {
461  CALL_ERROR_OR_ERROR_DATA( GLU_OUT_OF_MEMORY );
462  return;
463  }
464  }
465  if ( !AddVertex( tess, clamped, data ) ) {
466  CALL_ERROR_OR_ERROR_DATA( GLU_OUT_OF_MEMORY );
467  }
468 }
469 
470 
471 void GLAPIENTRY
472 gluTessBeginPolygon( GLUtesselator *tess, void *data )
473 {
474  RequireState( tess, T_DORMANT );
475 
476  tess->state = T_IN_POLYGON;
477  tess->cacheCount = 0;
478  tess->emptyCache = FALSE;
479  tess->mesh = NULL;
480 
481  tess->polygonData= data;
482 }
483 
484 
485 void GLAPIENTRY
486 gluTessBeginContour( GLUtesselator *tess )
487 {
488  RequireState( tess, T_IN_POLYGON );
489 
490  tess->state = T_IN_CONTOUR;
491  tess->lastEdge = NULL;
492  if( tess->cacheCount > 0 ) {
493  /* Just set a flag so we don't get confused by empty contours
494  * -- these can be generated accidentally with the obsolete
495  * NextContour() interface.
496  */
497  tess->emptyCache = TRUE;
498  }
499 }
500 
501 
502 void GLAPIENTRY
503 gluTessEndContour( GLUtesselator *tess )
504 {
505  RequireState( tess, T_IN_CONTOUR );
506  tess->state = T_IN_POLYGON;
507 }
508 
509 void GLAPIENTRY
510 gluTessEndPolygon( GLUtesselator *tess )
511 {
512  GLUmesh *mesh;
513 
514  if (setjmp(tess->env) != 0) {
515  /* come back here if out of memory */
516  CALL_ERROR_OR_ERROR_DATA( GLU_OUT_OF_MEMORY );
517  return;
518  }
519 
520  RequireState( tess, T_IN_POLYGON );
521  tess->state = T_DORMANT;
522 
523  if( tess->mesh == NULL ) {
524  if( ! tess->flagBoundary && tess->callMesh == &noMesh ) {
525 
526  /* Try some special code to make the easy cases go quickly
527  * (eg. convex polygons). This code does NOT handle multiple contours,
528  * intersections, edge flags, and of course it does not generate
529  * an explicit mesh either.
530  */
531  if( __gl_renderCache( tess )) {
532  tess->polygonData= NULL;
533  return;
534  }
535  }
536  if ( !EmptyCache( tess ) ) longjmp(tess->env,1); /* could've used a label*/
537  }
538 
539  /* Determine the polygon normal and project vertices onto the plane
540  * of the polygon.
541  */
542  __gl_projectPolygon( tess );
543 
544  /* __gl_computeInterior( tess ) computes the planar arrangement specified
545  * by the given contours, and further subdivides this arrangement
546  * into regions. Each region is marked "inside" if it belongs
547  * to the polygon, according to the rule given by tess->windingRule.
548  * Each interior region is guaranteed be monotone.
549  */
550  if ( !__gl_computeInterior( tess ) ) {
551  longjmp(tess->env,1); /* could've used a label */
552  }
553 
554  mesh = tess->mesh;
555  if( ! tess->fatalError ) {
556  int rc = 1;
557 
558  /* If the user wants only the boundary contours, we throw away all edges
559  * except those which separate the interior from the exterior.
560  * Otherwise we tessellate all the regions marked "inside".
561  */
562  if( tess->boundaryOnly ) {
563  rc = __gl_meshSetWindingNumber( mesh, 1, TRUE );
564  } else {
565  rc = __gl_meshTessellateInterior( mesh );
566  }
567  if (rc == 0) longjmp(tess->env,1); /* could've used a label */
568 
569  __gl_meshCheckMesh( mesh );
570 
571  if( tess->callBegin != &noBegin || tess->callEnd != &noEnd
572  || tess->callVertex != &noVertex || tess->callEdgeFlag != &noEdgeFlag
573  || tess->callBeginData != &__gl_noBeginData
574  || tess->callEndData != &__gl_noEndData
575  || tess->callVertexData != &__gl_noVertexData
576  || tess->callEdgeFlagData != &__gl_noEdgeFlagData )
577  {
578  if( tess->boundaryOnly ) {
579  __gl_renderBoundary( tess, mesh ); /* output boundary contours */
580  } else {
581  __gl_renderMesh( tess, mesh ); /* output strips and fans */
582  }
583  }
584  if( tess->callMesh != &noMesh ) {
585 
586  /* Throw away the exterior faces, so that all faces are interior.
587  * This way the user doesn't have to check the "inside" flag,
588  * and we don't need to even reveal its existence. It also leaves
589  * the freedom for an implementation to not generate the exterior
590  * faces in the first place.
591  */
592  __gl_meshDiscardExterior( mesh );
593  (*tess->callMesh)( mesh ); /* user wants the mesh itself */
594  tess->mesh = NULL;
595  tess->polygonData= NULL;
596  return;
597  }
598  }
599  __gl_meshDeleteMesh( mesh );
600  tess->polygonData= NULL;
601  tess->mesh = NULL;
602 }
603 
604 
605 /*XXXblythe unused function*/
606 #if 0
607 void GLAPIENTRY
608 gluDeleteMesh( GLUmesh *mesh )
609 {
610  __gl_meshDeleteMesh( mesh );
611 }
612 #endif
613 
614 
615 
616 /*******************************************************/
617 
618 /* Obsolete calls -- for backward compatibility */
619 
620 void GLAPIENTRY
621 gluBeginPolygon( GLUtesselator *tess )
622 {
623  gluTessBeginPolygon( tess, NULL );
624  gluTessBeginContour( tess );
625 }
626 
627 
628 /*ARGSUSED*/
629 void GLAPIENTRY
630 gluNextContour( GLUtesselator *tess, GLenum type)
631 {
632  gluTessEndContour( tess );
633  gluTessBeginContour( tess );
634  (void)type;
635 }
636 
637 
638 void GLAPIENTRY
639 gluEndPolygon( GLUtesselator *tess )
640 {
641  gluTessEndContour( tess );
642  gluTessEndPolygon( tess );
643 }