aboutsummaryrefslogtreecommitdiffstats
path: root/src/glu/mini/tesselat.c
blob: 47d230073fdb26433d310b3dac318a4e228b4b57 (plain)
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

/*
 * Mesa 3-D graphics library
 * Version:  3.3
 * Copyright (C) 1995-2000  Brian Paul
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */


/*
 * This file is part of the polygon tesselation code contributed by
 * Bogdan Sikorski
 */


#ifdef PC_HEADER
#include "all.h"
#else
#include <stdlib.h>
#include <math.h>
#include "tess.h"
#endif



static GLboolean edge_flag;

static void emit_triangle(GLUtriangulatorObj *, tess_vertex *,
			  tess_vertex *, tess_vertex *);

static void emit_triangle_with_edge_flag(GLUtriangulatorObj *,
					 tess_vertex *, GLboolean,
					 tess_vertex *, GLboolean,
					 tess_vertex *, GLboolean);

static GLdouble
twice_the_triangle_area(tess_vertex * va, tess_vertex * vb, tess_vertex * vc)
{
   return (vb->x - va->x) * (vc->y - va->y) - (vb->y - va->y) * (vc->x -
								 va->x);
}

static GLboolean
left(GLdouble A, GLdouble B, GLdouble C, GLdouble x, GLdouble y)
{
   if (A * x + B * y + C > -EPSILON)
      return GL_TRUE;
   else
      return GL_FALSE;
}

static GLboolean
right(GLdouble A, GLdouble B, GLdouble C, GLdouble x, GLdouble y)
{
   if (A * x + B * y + C < EPSILON)
      return GL_TRUE;
   else
      return GL_FALSE;
}

static GLint
convex_ccw(tess_vertex * va,
	   tess_vertex * vb, tess_vertex * vc, GLUtriangulatorObj * tobj)
{
   GLdouble d;

   d = twice_the_triangle_area(va, vb, vc);

   if (d > EPSILON) {
      return 1;
   }
   else if (d < -EPSILON) {
      return 0;
   }
   else {
      return -1;
   }
}

static GLint
convex_cw(tess_vertex * va,
	  tess_vertex * vb, tess_vertex * vc, GLUtriangulatorObj * tobj)
{
   GLdouble d;

   d = twice_the_triangle_area(va, vb, vc);

   if (d < -EPSILON) {
      return 1;
   }
   else if (d > EPSILON) {
      return 0;
   }
   else {
      return -1;
   }
}

static GLboolean
diagonal_ccw(tess_vertex * va,
	     tess_vertex * vb,
	     GLUtriangulatorObj * tobj, tess_contour * contour)
{
   tess_vertex *vc = va->next, *vertex, *shadow_vertex;
   struct
   {
      GLdouble A, B, C;
   }
   ac, cb, ba;
   GLdouble x, y;

   GLint res = convex_ccw(va, vc, vb, tobj);
   if (res == 0)
      return GL_FALSE;
   if (res == -1)
      return GL_TRUE;

   ba.A = vb->y - va->y;
   ba.B = va->x - vb->x;
   ba.C = -ba.A * va->x - ba.B * va->y;
   ac.A = va->y - vc->y;
   ac.B = vc->x - va->x;
   ac.C = -ac.A * vc->x - ac.B * vc->y;
   cb.A = vc->y - vb->y;
   cb.B = vb->x - vc->x;
   cb.C = -cb.A * vb->x - cb.B * vb->y;
   for (vertex = vb->next; vertex != va; vertex = vertex->next) {
      shadow_vertex = vertex->shadow_vertex;
      if (shadow_vertex != NULL &&
	  (shadow_vertex == va || shadow_vertex == vb || shadow_vertex == vc))
	 continue;
      x = vertex->x;
      y = vertex->y;
      if (left(ba.A, ba.B, ba.C, x, y) &&
	  left(ac.A, ac.B, ac.C, x, y) && left(cb.A, cb.B, cb.C, x, y))
	 return GL_FALSE;
   }
   return GL_TRUE;
}

static GLboolean
diagonal_cw(tess_vertex * va,
	    tess_vertex * vb,
	    GLUtriangulatorObj * tobj, tess_contour * contour)
{
   tess_vertex *vc = va->next, *vertex, *shadow_vertex;
   struct
   {
      GLdouble A, B, C;
   }
   ac, cb, ba;
   GLdouble x, y;

   GLint res = convex_cw(va, vc, vb, tobj);
   if (res == 0)
      return GL_FALSE;
   if (res == -1)
      return GL_TRUE;

   ba.A = vb->y - va->y;
   ba.B = va->x - vb->x;
   ba.C = -ba.A * va->x - ba.B * va->y;
   ac.A = va->y - vc->y;
   ac.B = vc->x - va->x;
   ac.C = -ac.A * vc->x - ac.B * vc->y;
   cb.A = vc->y - vb->y;
   cb.B = vb->x - vc->x;
   cb.C = -cb.A * vb->x - cb.B * vb->y;
   for (vertex = vb->next; vertex != va; vertex = vertex->next) {
      shadow_vertex = vertex->shadow_vertex;
      if (shadow_vertex != NULL &&
	  (shadow_vertex == va || shadow_vertex == vb || shadow_vertex == vc))
	 continue;
      x = vertex->x;
      y = vertex->y;
      if (right(ba.A, ba.B, ba.C, x, y) &&
	  right(ac.A, ac.B, ac.C, x, y) && right(cb.A, cb.B, cb.C, x, y))
	 return GL_FALSE;
   }
   return GL_TRUE;
}

static void
clip_ear(GLUtriangulatorObj * tobj, tess_vertex * v, tess_contour * contour)
{
   emit_triangle(tobj, v->previous, v, v->next);
   /* the first in the list */
   if (contour->vertices == v) {
      contour->vertices = v->next;
      contour->last_vertex->next = v->next;
      v->next->previous = contour->last_vertex;
   }
   else
      /* the last ? */
   if (contour->last_vertex == v) {
      contour->vertices->previous = v->previous;
      v->previous->next = v->next;
      contour->last_vertex = v->previous;
   }
   else {
      v->next->previous = v->previous;
      v->previous->next = v->next;
   }
   free(v);
   --(contour->vertex_cnt);
}

static void
clip_ear_with_edge_flag(GLUtriangulatorObj * tobj,
			tess_vertex * v, tess_contour * contour)
{
   emit_triangle_with_edge_flag(tobj, v->previous, v->previous->edge_flag,
				v, v->edge_flag, v->next, GL_FALSE);
   v->previous->edge_flag = GL_FALSE;
   /* the first in the list */
   if (contour->vertices == v) {
      contour->vertices = v->next;
      contour->last_vertex->next = v->next;
      v->next->previous = contour->last_vertex;
   }
   else
      /* the last ? */
   if (contour->last_vertex == v) {
      contour->vertices->previous = v->previous;
      v->previous->next = v->next;
      contour->last_vertex = v->previous;
   }
   else {
      v->next->previous = v->previous;
      v->previous->next = v->next;
   }
   free(v);
   --(contour->vertex_cnt);
}

static void
triangulate_ccw(GLUtriangulatorObj * tobj, tess_contour * contour)
{
   tess_vertex *vertex;
   GLuint vertex_cnt = contour->vertex_cnt;

   while (vertex_cnt > 3) {
      vertex = contour->vertices;
      while (diagonal_ccw(vertex, vertex->next->next, tobj, contour) ==
	     GL_FALSE && tobj->error == GLU_NO_ERROR)
	 vertex = vertex->next;
      if (tobj->error != GLU_NO_ERROR)
	 return;
      clip_ear(tobj, vertex->next, contour);
      --vertex_cnt;
   }
}

static void
triangulate_cw(GLUtriangulatorObj * tobj, tess_contour * contour)
{
   tess_vertex *vertex;
   GLuint vertex_cnt = contour->vertex_cnt;

   while (vertex_cnt > 3) {
      vertex = contour->vertices;
      while (diagonal_cw(vertex, vertex->next->next, tobj, contour) ==
	     GL_FALSE && tobj->error == GLU_NO_ERROR)
	 vertex = vertex->next;
      if (tobj->error != GLU_NO_ERROR)
	 return;
      clip_ear(tobj, vertex->next, contour);
      --vertex_cnt;
   }
}

static void
triangulate_ccw_with_edge_flag(GLUtriangulatorObj * tobj,
			       tess_contour * contour)
{
   tess_vertex *vertex;
   GLuint vertex_cnt = contour->vertex_cnt;

   while (vertex_cnt > 3) {
      vertex = contour->vertices;
      while (diagonal_ccw(vertex, vertex->next->next, tobj, contour) ==
	     GL_FALSE && tobj->error == GLU_NO_ERROR)
	 vertex = vertex->next;
      if (tobj->error != GLU_NO_ERROR)
	 return;
      clip_ear_with_edge_flag(tobj, vertex->next, contour);
      --vertex_cnt;
   }
}

static void
triangulate_cw_with_edge_flag(GLUtriangulatorObj * tobj,
			      tess_contour * contour)
{
   tess_vertex *vertex;
   GLuint vertex_cnt = contour->vertex_cnt;

   while (vertex_cnt > 3) {
      vertex = contour->vertices;
      while (diagonal_cw(vertex, vertex->next->next, tobj, contour) ==
	     GL_FALSE && tobj->error == GLU_NO_ERROR)
	 vertex = vertex->next;
      if (tobj->error != GLU_NO_ERROR)
	 return;
      clip_ear_with_edge_flag(tobj, vertex->next, contour);
      --vertex_cnt;
   }
}

void
tess_tesselate(GLUtriangulatorObj * tobj)
{
   tess_contour *contour;

   for (contour = tobj->contours; contour != NULL; contour = contour->next) {
      if (contour->orientation == GLU_CCW) {
	 triangulate_ccw(tobj, contour);
      }
      else {
	 triangulate_cw(tobj, contour);
      }
      if (tobj->error != GLU_NO_ERROR)
	 return;

      /* emit the last triangle */
      emit_triangle(tobj, contour->vertices, contour->vertices->next,
		    contour->vertices->next->next);
   }
}

void
tess_tesselate_with_edge_flag(GLUtriangulatorObj * tobj)
{
   tess_contour *contour;

   edge_flag = GL_TRUE;
   /* first callback with edgeFlag set to GL_TRUE */
   (tobj->callbacks.edgeFlag) (GL_TRUE);

   for (contour = tobj->contours; contour != NULL; contour = contour->next) {
      if (contour->orientation == GLU_CCW)
	 triangulate_ccw_with_edge_flag(tobj, contour);
      else
	 triangulate_cw_with_edge_flag(tobj, contour);
      if (tobj->error != GLU_NO_ERROR)
	 return;
      /* emit the last triangle */
      emit_triangle_with_edge_flag(tobj, contour->vertices,
				   contour->vertices->edge_flag,
				   contour->vertices->next,
				   contour->vertices->next->edge_flag,
				   contour->vertices->next->next,
				   contour->vertices->next->next->edge_flag);
   }
}

static void
emit_triangle(GLUtriangulatorObj * tobj,
	      tess_vertex * v1, tess_vertex * v2, tess_vertex * v3)
{
   (tobj->callbacks.begin) (GL_TRIANGLES);
   (tobj->callbacks.vertex) (v1->data);
   (tobj->callbacks.vertex) (v2->data);
   (tobj->callbacks.vertex) (v3->data);
   (tobj->callbacks.end) ();
}

static void
emit_triangle_with_edge_flag(GLUtriangulatorObj * tobj,
			     tess_vertex * v1,
			     GLboolean edge_flag1,
			     tess_vertex * v2,
			     GLboolean edge_flag2,
			     tess_vertex * v3, GLboolean edge_flag3)
{
   (tobj->callbacks.begin) (GL_TRIANGLES);
   if (edge_flag1 != edge_flag) {
      edge_flag = (edge_flag == GL_TRUE ? GL_FALSE : GL_TRUE);
      (tobj->callbacks.edgeFlag) (edge_flag);
   }
   (tobj->callbacks.vertex) (v1->data);
   if (edge_flag2 != edge_flag) {
      edge_flag = (edge_flag == GL_TRUE ? GL_FALSE : GL_TRUE);
      (tobj->callbacks.edgeFlag) (edge_flag);
   }
   (tobj->callbacks.vertex) (v2->data);
   if (edge_flag3 != edge_flag) {
      edge_flag = (edge_flag == GL_TRUE ? GL_FALSE : GL_TRUE);
      (tobj->callbacks.edgeFlag) (edge_flag);
   }
   (tobj->callbacks.vertex) (v3->data);
   (tobj->callbacks.end) ();
}