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
|
/* $Id: triang.c,v 1.3 2004/12/11 19:26:11 ellson Exp $ $Revision: 1.3 $ */
/* vim:set shiftwidth=4 ts=8: */
/**********************************************************
* This software is part of the graphviz package *
* http://www.graphviz.org/ *
* *
* Copyright (c) 1994-2004 AT&T Corp. *
* and is licensed under the *
* Common Public License, Version 1.0 *
* by AT&T Corp. *
* *
* Information and Software Systems Research *
* AT&T Research, Florham Park NJ *
**********************************************************/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <pathutil.h>
#include <tri.h>
#ifdef DMALLOC
#include "dmalloc.h"
#endif
typedef struct lvertex_2_t {
double x, y;
} lvertex_2_t;
typedef struct dpd_triangle {
Ppoint_t *v[3];
} ltriangle_t;
#define ISCCW 1
#define ISCW 2
#define ISON 3
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
static int dpd_ccw(Ppoint_t *, Ppoint_t *, Ppoint_t *);
static int dpd_isdiagonal(int, int, Ppoint_t **, int);
static int dpd_intersects(Ppoint_t *, Ppoint_t *, Ppoint_t *, Ppoint_t *);
static int dpd_between(Ppoint_t *, Ppoint_t *, Ppoint_t *);
static void triangulate(Ppoint_t ** pointp, int pointn,
void (*fn) (void *, Ppoint_t *), void *vc);
static int dpd_ccw(Ppoint_t * p1, Ppoint_t * p2, Ppoint_t * p3)
{
double d =
((p1->y - p2->y) * (p3->x - p2->x)) -
((p3->y - p2->y) * (p1->x - p2->x));
return (d > 0) ? ISCW : ((d < 0) ? ISCCW : ISON);
}
void Ptriangulate(Ppoly_t * polygon, void (*fn) (void *, Ppoint_t *),
void *vc)
{
int i;
int pointn;
Ppoint_t **pointp;
pointn = polygon->pn;
pointp = (Ppoint_t **) malloc(pointn * sizeof(Ppoint_t *));
for (i = 0; i < pointn; i++)
pointp[i] = &(polygon->ps[i]);
triangulate(pointp, pointn, fn, vc);
free(pointp);
}
static void
triangulate(Ppoint_t ** pointp, int pointn,
void (*fn) (void *, Ppoint_t *), void *vc)
{
int i, ip1, ip2, j;
Ppoint_t A[3];
if (pointn > 3) {
for (i = 0; i < pointn; i++) {
ip1 = (i + 1) % pointn;
ip2 = (i + 2) % pointn;
if (dpd_isdiagonal(i, ip2, pointp, pointn)) {
A[0] = *pointp[i];
A[1] = *pointp[ip1];
A[2] = *pointp[ip2];
fn(vc, A);
j = 0;
for (i = 0; i < pointn; i++)
if (i != ip1)
pointp[j++] = pointp[i];
triangulate(pointp, pointn - 1, fn, vc);
return;
}
}
abort();
} else {
A[0] = *pointp[0];
A[1] = *pointp[1];
A[2] = *pointp[2];
fn(vc, A);
}
}
/* check if (i, i + 2) is a diagonal */
static int dpd_isdiagonal(int i, int ip2, Ppoint_t ** pointp, int pointn)
{
int ip1, im1, j, jp1, res;
/* neighborhood test */
ip1 = (i + 1) % pointn;
im1 = (i + pointn - 1) % pointn;
/* If P[i] is a convex vertex [ i+1 left of (i-1,i) ]. */
if (dpd_ccw(pointp[im1], pointp[i], pointp[ip1]) == ISCCW)
res = (dpd_ccw(pointp[i], pointp[ip2], pointp[im1]) == ISCCW) &&
(dpd_ccw(pointp[ip2], pointp[i], pointp[ip1]) == ISCCW);
/* Assume (i - 1, i, i + 1) not collinear. */
else
res = ((dpd_ccw(pointp[i], pointp[ip2], pointp[ip1]) == ISCW)
);
/*
&&
(dpd_ccw (pointp[ip2], pointp[i], pointp[im1]) != ISCW));
*/
if (!res) {
return FALSE;
}
/* check against all other edges */
for (j = 0; j < pointn; j++) {
jp1 = (j + 1) % pointn;
if (!((j == i) || (jp1 == i) || (j == ip2) || (jp1 == ip2)))
if (dpd_intersects
(pointp[i], pointp[ip2], pointp[j], pointp[jp1])) {
return FALSE;
}
}
return TRUE;
}
/* line to line intersection */
static int dpd_intersects(Ppoint_t * pa, Ppoint_t * pb, Ppoint_t * pc,
Ppoint_t * pd)
{
int ccw1, ccw2, ccw3, ccw4;
if (dpd_ccw(pa, pb, pc) == ISON || dpd_ccw(pa, pb, pd) == ISON ||
dpd_ccw(pc, pd, pa) == ISON || dpd_ccw(pc, pd, pb) == ISON) {
if (dpd_between(pa, pb, pc) || dpd_between(pa, pb, pd) ||
dpd_between(pc, pd, pa) || dpd_between(pc, pd, pb))
return TRUE;
} else {
ccw1 = (dpd_ccw(pa, pb, pc) == ISCCW) ? 1 : 0;
ccw2 = (dpd_ccw(pa, pb, pd) == ISCCW) ? 1 : 0;
ccw3 = (dpd_ccw(pc, pd, pa) == ISCCW) ? 1 : 0;
ccw4 = (dpd_ccw(pc, pd, pb) == ISCCW) ? 1 : 0;
return (ccw1 ^ ccw2) && (ccw3 ^ ccw4);
}
return FALSE;
}
static int dpd_between(Ppoint_t * pa, Ppoint_t * pb, Ppoint_t * pc)
{
Ppoint_t pba, pca;
pba.x = pb->x - pa->x, pba.y = pb->y - pa->y;
pca.x = pc->x - pa->x, pca.y = pc->y - pa->y;
if (dpd_ccw(pa, pb, pc) != ISON)
return FALSE;
return (pca.x * pba.x + pca.y * pba.y >= 0) &&
(pca.x * pca.x + pca.y * pca.y <= pba.x * pba.x + pba.y * pba.y);
}
|