| 1 |
/* $Id: tesselat.c,v 1.2 2000/11/02 00:28:54 mholst Exp $ */ |
| 2 |
|
| 3 |
/* |
| 4 |
* Mesa 3-D graphics library |
| 5 |
* Version: 2.2 |
| 6 |
* Copyright (C) 1995-1997 Brian Paul |
| 7 |
* |
| 8 |
* This library is free software; you can redistribute it and/or |
| 9 |
* modify it under the terms of the GNU Library General Public |
| 10 |
* License as published by the Free Software Foundation; either |
| 11 |
* version 2 of the License, or (at your option) any later version. |
| 12 |
* |
| 13 |
* This library is distributed in the hope that it will be useful, |
| 14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 16 |
* Library General Public License for more details. |
| 17 |
* |
| 18 |
* You should have received a copy of the GNU Library General Public |
| 19 |
* License along with this library; if not, write to the Free |
| 20 |
* Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
/* |
| 24 |
* This file is part of the polygon tesselation code contributed by |
| 25 |
* Bogdan Sikorski |
| 26 |
*/ |
| 27 |
|
| 28 |
|
| 29 |
#include <stdlib.h> |
| 30 |
#include <math.h> |
| 31 |
#include "tess.h" |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
static GLboolean edge_flag; |
| 36 |
|
| 37 |
static void emit_triangle(GLUtriangulatorObj *, tess_vertex *, |
| 38 |
tess_vertex *,tess_vertex *); |
| 39 |
|
| 40 |
static void emit_triangle_with_edge_flag(GLUtriangulatorObj *, |
| 41 |
tess_vertex *,GLboolean,tess_vertex *,GLboolean, |
| 42 |
tess_vertex *,GLboolean); |
| 43 |
|
| 44 |
static GLdouble twice_the_triangle_area( |
| 45 |
tess_vertex *va, |
| 46 |
tess_vertex *vb, |
| 47 |
tess_vertex *vc) |
| 48 |
{ |
| 49 |
return (vb->x - va->x)*(vc->y - va->y) - (vb->y - va->y)*(vc->x - va->x); |
| 50 |
} |
| 51 |
|
| 52 |
static GLboolean left( |
| 53 |
GLdouble A, |
| 54 |
GLdouble B, |
| 55 |
GLdouble C, |
| 56 |
GLdouble x, |
| 57 |
GLdouble y) |
| 58 |
{ |
| 59 |
if(A*x+B*y+C > -EPSILON) |
| 60 |
return GL_TRUE; |
| 61 |
else |
| 62 |
return GL_FALSE; |
| 63 |
} |
| 64 |
|
| 65 |
static GLboolean right( |
| 66 |
GLdouble A, |
| 67 |
GLdouble B, |
| 68 |
GLdouble C, |
| 69 |
GLdouble x, |
| 70 |
GLdouble y) |
| 71 |
{ |
| 72 |
if(A*x+B*y+C < EPSILON) |
| 73 |
return GL_TRUE; |
| 74 |
else |
| 75 |
return GL_FALSE; |
| 76 |
} |
| 77 |
|
| 78 |
static GLint convex_ccw( |
| 79 |
tess_vertex *va, |
| 80 |
tess_vertex *vb, |
| 81 |
tess_vertex *vc, |
| 82 |
GLUtriangulatorObj *tobj) |
| 83 |
{ |
| 84 |
GLdouble d; |
| 85 |
|
| 86 |
d = twice_the_triangle_area(va,vb,vc); |
| 87 |
|
| 88 |
if (d > EPSILON ) { |
| 89 |
return 1; |
| 90 |
} else if (d < -EPSILON ) { |
| 91 |
return 0; |
| 92 |
} else { |
| 93 |
return -1; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
static GLint convex_cw( |
| 98 |
tess_vertex *va, |
| 99 |
tess_vertex *vb, |
| 100 |
tess_vertex *vc, |
| 101 |
GLUtriangulatorObj *tobj) |
| 102 |
{ |
| 103 |
GLdouble d; |
| 104 |
|
| 105 |
d = twice_the_triangle_area(va,vb,vc); |
| 106 |
|
| 107 |
if (d < -EPSILON ) { |
| 108 |
return 1; |
| 109 |
} else if (d > EPSILON ) { |
| 110 |
return 0; |
| 111 |
} else { |
| 112 |
return -1; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
static GLboolean diagonal_ccw( |
| 117 |
tess_vertex *va, |
| 118 |
tess_vertex *vb, |
| 119 |
GLUtriangulatorObj *tobj, |
| 120 |
tess_contour *contour) |
| 121 |
{ |
| 122 |
tess_vertex *vc=va->next , *vertex , *shadow_vertex; |
| 123 |
struct |
| 124 |
{ |
| 125 |
GLdouble A,B,C; |
| 126 |
} ac,cb,ba; |
| 127 |
GLdouble x,y; |
| 128 |
|
| 129 |
GLint res = convex_ccw(va,vc,vb,tobj); |
| 130 |
if (res == 0) return GL_FALSE; |
| 131 |
if (res == -1) return GL_TRUE; |
| 132 |
|
| 133 |
ba.A=vb->y - va->y; |
| 134 |
ba.B=va->x - vb->x; |
| 135 |
ba.C= -ba.A*va->x - ba.B*va->y; |
| 136 |
ac.A=va->y - vc->y; |
| 137 |
ac.B=vc->x - va->x; |
| 138 |
ac.C= -ac.A*vc->x - ac.B*vc->y; |
| 139 |
cb.A=vc->y - vb->y; |
| 140 |
cb.B=vb->x - vc->x; |
| 141 |
cb.C= -cb.A*vb->x - cb.B*vb->y; |
| 142 |
for(vertex=vb->next;vertex!=va;vertex=vertex->next) |
| 143 |
{ |
| 144 |
shadow_vertex=vertex->shadow_vertex; |
| 145 |
if(shadow_vertex!=NULL && |
| 146 |
(shadow_vertex==va || shadow_vertex==vb || shadow_vertex==vc)) |
| 147 |
continue; |
| 148 |
x=vertex->x; |
| 149 |
y=vertex->y; |
| 150 |
if(left(ba.A,ba.B,ba.C,x,y) && |
| 151 |
left(ac.A,ac.B,ac.C,x,y) && |
| 152 |
left(cb.A,cb.B,cb.C,x,y)) |
| 153 |
return GL_FALSE; |
| 154 |
} |
| 155 |
return GL_TRUE; |
| 156 |
} |
| 157 |
|
| 158 |
static GLboolean diagonal_cw( |
| 159 |
tess_vertex *va, |
| 160 |
tess_vertex *vb, |
| 161 |
GLUtriangulatorObj *tobj, |
| 162 |
tess_contour *contour) |
| 163 |
{ |
| 164 |
tess_vertex *vc=va->next , *vertex , *shadow_vertex; |
| 165 |
struct |
| 166 |
{ |
| 167 |
GLdouble A,B,C; |
| 168 |
} ac,cb,ba; |
| 169 |
GLdouble x,y; |
| 170 |
|
| 171 |
GLint res = convex_cw(va,vc,vb,tobj); |
| 172 |
if (res == 0) return GL_FALSE; |
| 173 |
if (res == -1) return GL_TRUE; |
| 174 |
|
| 175 |
ba.A=vb->y - va->y; |
| 176 |
ba.B=va->x - vb->x; |
| 177 |
ba.C= -ba.A*va->x - ba.B*va->y; |
| 178 |
ac.A=va->y - vc->y; |
| 179 |
ac.B=vc->x - va->x; |
| 180 |
ac.C= -ac.A*vc->x - ac.B*vc->y; |
| 181 |
cb.A=vc->y - vb->y; |
| 182 |
cb.B=vb->x - vc->x; |
| 183 |
cb.C= -cb.A*vb->x - cb.B*vb->y; |
| 184 |
for(vertex=vb->next;vertex!=va;vertex=vertex->next) |
| 185 |
{ |
| 186 |
shadow_vertex=vertex->shadow_vertex; |
| 187 |
if(shadow_vertex!=NULL && |
| 188 |
(shadow_vertex==va || shadow_vertex==vb || shadow_vertex==vc)) |
| 189 |
continue; |
| 190 |
x=vertex->x; |
| 191 |
y=vertex->y; |
| 192 |
if(right(ba.A,ba.B,ba.C,x,y) && |
| 193 |
right(ac.A,ac.B,ac.C,x,y) && |
| 194 |
right(cb.A,cb.B,cb.C,x,y)) |
| 195 |
return GL_FALSE; |
| 196 |
} |
| 197 |
return GL_TRUE; |
| 198 |
} |
| 199 |
|
| 200 |
static void clip_ear( |
| 201 |
GLUtriangulatorObj *tobj, |
| 202 |
tess_vertex *v, |
| 203 |
tess_contour *contour) |
| 204 |
{ |
| 205 |
emit_triangle(tobj,v->previous,v,v->next); |
| 206 |
/* the first in the list */ |
| 207 |
if(contour->vertices==v) |
| 208 |
{ |
| 209 |
contour->vertices=v->next; |
| 210 |
contour->last_vertex->next=v->next; |
| 211 |
v->next->previous=contour->last_vertex; |
| 212 |
} |
| 213 |
else |
| 214 |
/* the last ? */ |
| 215 |
if(contour->last_vertex==v) |
| 216 |
{ |
| 217 |
contour->vertices->previous=v->previous; |
| 218 |
v->previous->next=v->next; |
| 219 |
contour->last_vertex=v->previous; |
| 220 |
} |
| 221 |
else |
| 222 |
{ |
| 223 |
v->next->previous=v->previous; |
| 224 |
v->previous->next=v->next; |
| 225 |
} |
| 226 |
free(v); |
| 227 |
--(contour->vertex_cnt); |
| 228 |
} |
| 229 |
|
| 230 |
static void clip_ear_with_edge_flag( |
| 231 |
GLUtriangulatorObj *tobj, |
| 232 |
tess_vertex *v, |
| 233 |
tess_contour *contour) |
| 234 |
{ |
| 235 |
emit_triangle_with_edge_flag(tobj,v->previous,v->previous->edge_flag, |
| 236 |
v,v->edge_flag,v->next,GL_FALSE); |
| 237 |
v->previous->edge_flag=GL_FALSE; |
| 238 |
/* the first in the list */ |
| 239 |
if(contour->vertices==v) |
| 240 |
{ |
| 241 |
contour->vertices=v->next; |
| 242 |
contour->last_vertex->next=v->next; |
| 243 |
v->next->previous=contour->last_vertex; |
| 244 |
} |
| 245 |
else |
| 246 |
/* the last ? */ |
| 247 |
if(contour->last_vertex==v) |
| 248 |
{ |
| 249 |
contour->vertices->previous=v->previous; |
| 250 |
v->previous->next=v->next; |
| 251 |
contour->last_vertex=v->previous; |
| 252 |
} |
| 253 |
else |
| 254 |
{ |
| 255 |
v->next->previous=v->previous; |
| 256 |
v->previous->next=v->next; |
| 257 |
} |
| 258 |
free(v); |
| 259 |
--(contour->vertex_cnt); |
| 260 |
} |
| 261 |
|
| 262 |
static void triangulate_ccw( |
| 263 |
GLUtriangulatorObj *tobj, |
| 264 |
tess_contour *contour) |
| 265 |
{ |
| 266 |
tess_vertex *vertex; |
| 267 |
GLuint vertex_cnt=contour->vertex_cnt; |
| 268 |
|
| 269 |
while(vertex_cnt > 3) |
| 270 |
{ |
| 271 |
vertex=contour->vertices; |
| 272 |
while(diagonal_ccw(vertex,vertex->next->next,tobj,contour)==GL_FALSE && |
| 273 |
tobj->error==GLU_NO_ERROR) |
| 274 |
vertex=vertex->next; |
| 275 |
if(tobj->error!=GLU_NO_ERROR) |
| 276 |
return; |
| 277 |
clip_ear(tobj,vertex->next,contour); |
| 278 |
--vertex_cnt; |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
static void triangulate_cw( |
| 283 |
GLUtriangulatorObj *tobj, |
| 284 |
tess_contour *contour) |
| 285 |
{ |
| 286 |
tess_vertex *vertex; |
| 287 |
GLuint vertex_cnt=contour->vertex_cnt; |
| 288 |
|
| 289 |
while(vertex_cnt > 3) |
| 290 |
{ |
| 291 |
vertex=contour->vertices; |
| 292 |
while(diagonal_cw(vertex,vertex->next->next,tobj,contour)==GL_FALSE && |
| 293 |
tobj->error==GLU_NO_ERROR) |
| 294 |
vertex=vertex->next; |
| 295 |
if(tobj->error!=GLU_NO_ERROR) |
| 296 |
return; |
| 297 |
clip_ear(tobj,vertex->next,contour); |
| 298 |
--vertex_cnt; |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
static void triangulate_ccw_with_edge_flag( |
| 303 |
GLUtriangulatorObj *tobj, |
| 304 |
tess_contour *contour) |
| 305 |
{ |
| 306 |
tess_vertex *vertex; |
| 307 |
GLuint vertex_cnt=contour->vertex_cnt; |
| 308 |
|
| 309 |
while(vertex_cnt > 3) |
| 310 |
{ |
| 311 |
vertex=contour->vertices; |
| 312 |
while(diagonal_ccw(vertex,vertex->next->next,tobj,contour)==GL_FALSE && |
| 313 |
tobj->error==GLU_NO_ERROR) |
| 314 |
vertex=vertex->next; |
| 315 |
if(tobj->error!=GLU_NO_ERROR) |
| 316 |
return; |
| 317 |
clip_ear_with_edge_flag(tobj,vertex->next,contour); |
| 318 |
--vertex_cnt; |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
static void triangulate_cw_with_edge_flag( |
| 323 |
GLUtriangulatorObj *tobj, |
| 324 |
tess_contour *contour) |
| 325 |
{ |
| 326 |
tess_vertex *vertex; |
| 327 |
GLuint vertex_cnt=contour->vertex_cnt; |
| 328 |
|
| 329 |
while(vertex_cnt > 3) |
| 330 |
{ |
| 331 |
vertex=contour->vertices; |
| 332 |
while(diagonal_cw(vertex,vertex->next->next,tobj,contour)==GL_FALSE && |
| 333 |
tobj->error==GLU_NO_ERROR) |
| 334 |
vertex=vertex->next; |
| 335 |
if(tobj->error!=GLU_NO_ERROR) |
| 336 |
return; |
| 337 |
clip_ear_with_edge_flag(tobj,vertex->next,contour); |
| 338 |
--vertex_cnt; |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
void tess_tesselate(GLUtriangulatorObj *tobj) |
| 343 |
{ |
| 344 |
tess_contour *contour; |
| 345 |
|
| 346 |
for(contour=tobj->contours;contour!=NULL;contour=contour->next) |
| 347 |
{ |
| 348 |
if(contour->orientation==GLU_CCW) { |
| 349 |
triangulate_ccw(tobj,contour); |
| 350 |
} else { |
| 351 |
triangulate_cw(tobj,contour); |
| 352 |
} |
| 353 |
if(tobj->error!=GLU_NO_ERROR) |
| 354 |
return; |
| 355 |
|
| 356 |
/* emit the last triangle */ |
| 357 |
emit_triangle(tobj,contour->vertices,contour->vertices->next, |
| 358 |
contour->vertices->next->next); |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
void tess_tesselate_with_edge_flag(GLUtriangulatorObj *tobj) |
| 363 |
{ |
| 364 |
tess_contour *contour; |
| 365 |
|
| 366 |
edge_flag=GL_TRUE; |
| 367 |
/* first callback with edgeFlag set to GL_TRUE */ |
| 368 |
(tobj->callbacks.edgeFlag)(GL_TRUE); |
| 369 |
|
| 370 |
for(contour=tobj->contours;contour!=NULL;contour=contour->next) |
| 371 |
{ |
| 372 |
if(contour->orientation==GLU_CCW) |
| 373 |
triangulate_ccw_with_edge_flag(tobj,contour); |
| 374 |
else |
| 375 |
triangulate_cw_with_edge_flag(tobj,contour); |
| 376 |
if(tobj->error!=GLU_NO_ERROR) |
| 377 |
return; |
| 378 |
/* emit the last triangle */ |
| 379 |
emit_triangle_with_edge_flag(tobj,contour->vertices, |
| 380 |
contour->vertices->edge_flag,contour->vertices->next, |
| 381 |
contour->vertices->next->edge_flag,contour->vertices->next->next, |
| 382 |
contour->vertices->next->next->edge_flag); |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
static void emit_triangle( |
| 387 |
GLUtriangulatorObj *tobj, |
| 388 |
tess_vertex *v1, |
| 389 |
tess_vertex *v2, |
| 390 |
tess_vertex *v3) |
| 391 |
{ |
| 392 |
(tobj->callbacks.begin)(GL_TRIANGLES); |
| 393 |
(tobj->callbacks.vertex)(v1->data); |
| 394 |
(tobj->callbacks.vertex)(v2->data); |
| 395 |
(tobj->callbacks.vertex)(v3->data); |
| 396 |
(tobj->callbacks.end)(); |
| 397 |
} |
| 398 |
|
| 399 |
static void emit_triangle_with_edge_flag( |
| 400 |
GLUtriangulatorObj *tobj, |
| 401 |
tess_vertex *v1, |
| 402 |
GLboolean edge_flag1, |
| 403 |
tess_vertex *v2, |
| 404 |
GLboolean edge_flag2, |
| 405 |
tess_vertex *v3, |
| 406 |
GLboolean edge_flag3) |
| 407 |
{ |
| 408 |
(tobj->callbacks.begin)(GL_TRIANGLES); |
| 409 |
if(edge_flag1!=edge_flag) |
| 410 |
{ |
| 411 |
edge_flag = (edge_flag==GL_TRUE ? GL_FALSE : GL_TRUE); |
| 412 |
(tobj->callbacks.edgeFlag)(edge_flag); |
| 413 |
} |
| 414 |
(tobj->callbacks.vertex)(v1->data); |
| 415 |
if(edge_flag2!=edge_flag) |
| 416 |
{ |
| 417 |
edge_flag = (edge_flag==GL_TRUE ? GL_FALSE : GL_TRUE); |
| 418 |
(tobj->callbacks.edgeFlag)(edge_flag); |
| 419 |
} |
| 420 |
(tobj->callbacks.vertex)(v2->data); |
| 421 |
if(edge_flag3!=edge_flag) |
| 422 |
{ |
| 423 |
edge_flag = (edge_flag==GL_TRUE ? GL_FALSE : GL_TRUE); |
| 424 |
(tobj->callbacks.edgeFlag)(edge_flag); |
| 425 |
} |
| 426 |
(tobj->callbacks.vertex)(v3->data); |
| 427 |
(tobj->callbacks.end)(); |
| 428 |
} |