| 1 |
/************************************************************************* |
| 2 |
* Copyright (c) 2011 AT&T Intellectual Property |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* https://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: Details at https://graphviz.org |
| 9 |
*************************************************************************/ |
| 10 |
|
| 11 |
#include "config.h" |
| 12 |
|
| 13 |
#include <assert.h> |
| 14 |
#include <stdbool.h> |
| 15 |
#include <stdio.h> |
| 16 |
#include <math.h> |
| 17 |
#include <stdlib.h> |
| 18 |
#include <pathplan/pathutil.h> |
| 19 |
#include <pathplan/tri.h> |
| 20 |
#include <util/alloc.h> |
| 21 |
|
| 22 |
static int triangulate(Ppoint_t **pointp, size_t pointn, |
| 23 |
void (*fn)(void *, const Ppoint_t *), void *vc); |
| 24 |
|
| 25 |
int ccw(Ppoint_t p1, Ppoint_t p2, Ppoint_t p3) { |
| 26 |
double d = (p1.y - p2.y) * (p3.x - p2.x) - (p3.y - p2.y) * (p1.x - p2.x); |
| 27 |
return d > 0 ? ISCW : (d < 0 ? ISCCW : ISON); |
| 28 |
} |
| 29 |
|
| 30 |
static Ppoint_t point_indexer(void *base, size_t index) { |
| 31 |
Ppoint_t **b = base; |
| 32 |
return *b[index]; |
| 33 |
} |
| 34 |
|
| 35 |
/* Ptriangulate: |
| 36 |
* Return 0 on success; non-zero on error. |
| 37 |
*/ |
| 38 |
int Ptriangulate(Ppoly_t *polygon, void (*fn)(void *, const Ppoint_t *), |
| 39 |
void *vc) { |
| 40 |
Ppoint_t **pointp; |
| 41 |
|
| 42 |
const size_t pointn = polygon->pn; |
| 43 |
|
| 44 |
pointp = gv_calloc(pointn, sizeof(Ppoint_t*)); |
| 45 |
|
| 46 |
for (size_t i = 0; i < pointn; i++) |
| 47 |
pointp[i] = &(polygon->ps[i]); |
| 48 |
|
| 49 |
assert(pointn >= 3); |
| 50 |
if (triangulate(pointp, pointn, fn, vc) != 0) { |
| 51 |
free(pointp); |
| 52 |
return 1; |
| 53 |
} |
| 54 |
|
| 55 |
free(pointp); |
| 56 |
return 0; |
| 57 |
} |
| 58 |
|
| 59 |
/* triangulate: |
| 60 |
* Triangulates the given polygon. |
| 61 |
* Returns non-zero if no diagonal exists. |
| 62 |
*/ |
| 63 |
static int triangulate(Ppoint_t **pointp, size_t pointn, |
| 64 |
void (*fn)(void *, const Ppoint_t *), void *vc) { |
| 65 |
assert(pointn >= 3); |
| 66 |
Ppoint_t A[3]; |
| 67 |
if (pointn > 3) { |
| 68 |
for (size_t i = 0; i < pointn; i++) { |
| 69 |
const size_t ip1 = (i + 1) % pointn; |
| 70 |
const size_t ip2 = (i + 2) % pointn; |
| 71 |
if (isdiagonal(i, ip2, pointp, pointn, point_indexer)) { |
| 72 |
A[0] = *pointp[i]; |
| 73 |
A[1] = *pointp[ip1]; |
| 74 |
A[2] = *pointp[ip2]; |
| 75 |
fn(vc, A); |
| 76 |
size_t j = 0; |
| 77 |
for (i = 0; i < pointn; i++) |
| 78 |
if (i != ip1) |
| 79 |
pointp[j++] = pointp[i]; |
| 80 |
return triangulate(pointp, pointn - 1, fn, vc); |
| 81 |
} |
| 82 |
} |
| 83 |
return -1; |
| 84 |
} else { |
| 85 |
A[0] = *pointp[0]; |
| 86 |
A[1] = *pointp[1]; |
| 87 |
A[2] = *pointp[2]; |
| 88 |
fn(vc, A); |
| 89 |
} |
| 90 |
return 0; |
| 91 |
} |
| 92 |
|
| 93 |
/// is pb between pa and pc? |
| 94 |
static bool between(Ppoint_t pa, Ppoint_t pb, Ppoint_t pc) { |
| 95 |
const Ppoint_t pba = {.x = pb.x - pa.x, .y = pb.y - pa.y}; |
| 96 |
const Ppoint_t pca = {.x = pc.x - pa.x, .y = pc.y - pa.y}; |
| 97 |
if (ccw(pa, pb, pc) != ISON) |
| 98 |
return false; |
| 99 |
return pca.x * pba.x + pca.y * pba.y >= 0 && |
| 100 |
pca.x * pca.x + pca.y * pca.y <= pba.x * pba.x + pba.y * pba.y; |
| 101 |
} |
| 102 |
|
| 103 |
/// line to line intersection |
| 104 |
static bool intersects(Ppoint_t pa, Ppoint_t pb, Ppoint_t pc, Ppoint_t pd) { |
| 105 |
int ccw1, ccw2, ccw3, ccw4; |
| 106 |
|
| 107 |
if (ccw(pa, pb, pc) == ISON || ccw(pa, pb, pd) == ISON || |
| 108 |
ccw(pc, pd, pa) == ISON || ccw(pc, pd, pb) == ISON) { |
| 109 |
if (between(pa, pb, pc) || between(pa, pb, pd) || between(pc, pd, pa) || |
| 110 |
between(pc, pd, pb)) |
| 111 |
return true; |
| 112 |
} else { |
| 113 |
ccw1 = ccw(pa, pb, pc) == ISCCW ? 1 : 0; |
| 114 |
ccw2 = ccw(pa, pb, pd) == ISCCW ? 1 : 0; |
| 115 |
ccw3 = ccw(pc, pd, pa) == ISCCW ? 1 : 0; |
| 116 |
ccw4 = ccw(pc, pd, pb) == ISCCW ? 1 : 0; |
| 117 |
return (ccw1 ^ ccw2) && (ccw3 ^ ccw4); |
| 118 |
} |
| 119 |
return false; |
| 120 |
} |
| 121 |
|
| 122 |
bool isdiagonal(size_t i, size_t ip2, void *pointp, size_t pointn, |
| 123 |
indexer_t indexer) { |
| 124 |
int res; |
| 125 |
|
| 126 |
/* neighborhood test */ |
| 127 |
const size_t ip1 = (i + 1) % pointn; |
| 128 |
const size_t im1 = (i + pointn - 1) % pointn; |
| 129 |
/* If P[i] is a convex vertex [ i+1 left of (i-1,i) ]. */ |
| 130 |
if (ccw(indexer(pointp, im1), indexer(pointp, i), indexer(pointp, ip1)) == ISCCW) |
| 131 |
res = ccw(indexer(pointp, i), indexer(pointp, ip2), indexer(pointp, im1)) == ISCCW && |
| 132 |
ccw(indexer(pointp, ip2), indexer(pointp, i), indexer(pointp, ip1)) == ISCCW; |
| 133 |
/* Assume (i - 1, i, i + 1) not collinear. */ |
| 134 |
else |
| 135 |
res = ccw(indexer(pointp, i), indexer(pointp, ip2), indexer(pointp, ip1)) == ISCW; |
| 136 |
if (!res) { |
| 137 |
return false; |
| 138 |
} |
| 139 |
|
| 140 |
/* check against all other edges */ |
| 141 |
for (size_t j = 0; j < pointn; j++) { |
| 142 |
const size_t jp1 = (j + 1) % pointn; |
| 143 |
if (!(j == i || jp1 == i || j == ip2 || jp1 == ip2)) |
| 144 |
if (intersects |
| 145 |
(indexer(pointp, i), indexer(pointp, ip2), indexer(pointp, j), indexer(pointp, jp1))) { |
| 146 |
return false; |
| 147 |
} |
| 148 |
} |
| 149 |
return true; |
| 150 |
} |