| 1 |
/// @file |
| 2 |
/// @ingroup common_render |
| 3 |
/************************************************************************* |
| 4 |
* Copyright (c) 2012 AT&T Intellectual Property |
| 5 |
* All rights reserved. This program and the accompanying materials |
| 6 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 7 |
* which accompanies this distribution, and is available at |
| 8 |
* https://www.eclipse.org/legal/epl-v10.html |
| 9 |
* |
| 10 |
* Contributors: Details at https://graphviz.org |
| 11 |
*************************************************************************/ |
| 12 |
|
| 13 |
/* This code is derived from the Java implementation by Luc Maisonobe */ |
| 14 |
/* Copyright (c) 2003-2004, Luc Maisonobe |
| 15 |
* All rights reserved. |
| 16 |
* |
| 17 |
* Redistribution and use in source and binary forms, with |
| 18 |
* or without modification, are permitted provided that |
| 19 |
* the following conditions are met: |
| 20 |
* |
| 21 |
* Redistributions of source code must retain the |
| 22 |
* above copyright notice, this list of conditions and |
| 23 |
* the following disclaimer. |
| 24 |
* Redistributions in binary form must reproduce the |
| 25 |
* above copyright notice, this list of conditions and |
| 26 |
* the following disclaimer in the documentation |
| 27 |
* and/or other materials provided with the |
| 28 |
* distribution. |
| 29 |
* Neither the names of spaceroots.org, spaceroots.com |
| 30 |
* nor the names of their contributors may be used to |
| 31 |
* endorse or promote products derived from this |
| 32 |
* software without specific prior written permission. |
| 33 |
* |
| 34 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 35 |
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED |
| 36 |
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 37 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
| 38 |
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 39 |
* THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY |
| 40 |
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 41 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 42 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF |
| 43 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 44 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 45 |
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 46 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
| 47 |
* USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 48 |
* POSSIBILITY OF SUCH DAMAGE. |
| 49 |
*/ |
| 50 |
|
| 51 |
#include "config.h" |
| 52 |
|
| 53 |
#include <assert.h> |
| 54 |
#include <common/render.h> |
| 55 |
#include <limits.h> |
| 56 |
#include <math.h> |
| 57 |
#include <pathplan/pathplan.h> |
| 58 |
#include <stdbool.h> |
| 59 |
#include <util/alloc.h> |
| 60 |
#include <util/list.h> |
| 61 |
|
| 62 |
#define TWOPI (2 * M_PI) |
| 63 |
|
| 64 |
typedef struct { |
| 65 |
double cx, cy; /* center */ |
| 66 |
double a, b; /* semi-major and -minor axes */ |
| 67 |
|
| 68 |
/* Start and end angles of the arc. */ |
| 69 |
double eta1, eta2; |
| 70 |
} ellipse_t; |
| 71 |
|
| 72 |
static void initEllipse(ellipse_t *ep, double cx, double cy, double a, double b, |
| 73 |
double lambda1, double lambda2) { |
| 74 |
ep->cx = cx; |
| 75 |
ep->cy = cy; |
| 76 |
ep->a = a; |
| 77 |
ep->b = b; |
| 78 |
|
| 79 |
ep->eta1 = atan2(sin(lambda1) / b, cos(lambda1) / a); |
| 80 |
ep->eta2 = atan2(sin(lambda2) / b, cos(lambda2) / a); |
| 81 |
|
| 82 |
// make sure we have eta1 <= eta2 <= eta1 + 2*PI |
| 83 |
ep->eta2 -= TWOPI * floor((ep->eta2 - ep->eta1) / TWOPI); |
| 84 |
|
| 85 |
// the preceding correction fails if we have exactly eta2 - eta1 = 2*PI |
| 86 |
// it reduces the interval to zero length |
| 87 |
if (lambda2 - lambda1 > M_PI && ep->eta2 - ep->eta1 < M_PI) { |
| 88 |
ep->eta2 += TWOPI; |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
typedef double erray_t[2][4][4]; |
| 93 |
|
| 94 |
// coefficients for error estimation |
| 95 |
// while using cubic Bézier curves for approximation |
| 96 |
// 0 < b/a < 1/4 |
| 97 |
static const erray_t coeffs3Low = { |
| 98 |
{{3.85268, -21.229, -0.330434, 0.0127842}, |
| 99 |
{-1.61486, 0.706564, 0.225945, 0.263682}, |
| 100 |
{-0.910164, 0.388383, 0.00551445, 0.00671814}, |
| 101 |
{-0.630184, 0.192402, 0.0098871, 0.0102527}}, |
| 102 |
{{-0.162211, 9.94329, 0.13723, 0.0124084}, |
| 103 |
{-0.253135, 0.00187735, 0.0230286, 0.01264}, |
| 104 |
{-0.0695069, -0.0437594, 0.0120636, 0.0163087}, |
| 105 |
{-0.0328856, -0.00926032, -0.00173573, 0.00527385}}}; |
| 106 |
|
| 107 |
// coefficients for error estimation |
| 108 |
// while using cubic Bézier curves for approximation |
| 109 |
// 1/4 <= b/a <= 1 |
| 110 |
static const erray_t coeffs3High = { |
| 111 |
{{0.0899116, -19.2349, -4.11711, 0.183362}, |
| 112 |
{0.138148, -1.45804, 1.32044, 1.38474}, |
| 113 |
{0.230903, -0.450262, 0.219963, 0.414038}, |
| 114 |
{0.0590565, -0.101062, 0.0430592, 0.0204699}}, |
| 115 |
{{0.0164649, 9.89394, 0.0919496, 0.00760802}, |
| 116 |
{0.0191603, -0.0322058, 0.0134667, -0.0825018}, |
| 117 |
{0.0156192, -0.017535, 0.00326508, -0.228157}, |
| 118 |
{-0.0236752, 0.0405821, -0.0173086, 0.176187}}}; |
| 119 |
|
| 120 |
// safety factor to convert the "best" error approximation |
| 121 |
// into a "max bound" error |
| 122 |
static const double safety3[] = {0.001, 4.98, 0.207, 0.0067}; |
| 123 |
|
| 124 |
/* Compute the value of a rational function. |
| 125 |
* This method handles rational functions where the numerator is |
| 126 |
* quadratic and the denominator is linear |
| 127 |
*/ |
| 128 |
static double RationalFunction(double x, const double *c) { |
| 129 |
return (x * (x * c[0] + c[1]) + c[2]) / (x + c[3]); |
| 130 |
} |
| 131 |
|
| 132 |
/* Estimate the approximation error for a sub-arc of the instance. |
| 133 |
* tA and tB give the start and end angle of the subarc |
| 134 |
* Returns upper bound of the approximation error between the Bézier |
| 135 |
* curve and the real ellipse |
| 136 |
*/ |
| 137 |
static double estimateError(ellipse_t *ep, double etaA, double etaB) { |
| 138 |
double c0, c1, eta = 0.5 * (etaA + etaB); |
| 139 |
|
| 140 |
double x = ep->b / ep->a; |
| 141 |
double dEta = etaB - etaA; |
| 142 |
double cos2 = cos(2 * eta); |
| 143 |
double cos4 = cos(4 * eta); |
| 144 |
double cos6 = cos(6 * eta); |
| 145 |
|
| 146 |
// select the right coefficient's set according to b/a |
| 147 |
const double(*coeffs)[4][4] = x < 0.25 ? coeffs3Low : coeffs3High; |
| 148 |
|
| 149 |
c0 = RationalFunction(x, coeffs[0][0]) + |
| 150 |
cos2 * RationalFunction(x, coeffs[0][1]) + |
| 151 |
cos4 * RationalFunction(x, coeffs[0][2]) + |
| 152 |
cos6 * RationalFunction(x, coeffs[0][3]); |
| 153 |
|
| 154 |
c1 = RationalFunction(x, coeffs[1][0]) + |
| 155 |
cos2 * RationalFunction(x, coeffs[1][1]) + |
| 156 |
cos4 * RationalFunction(x, coeffs[1][2]) + |
| 157 |
cos6 * RationalFunction(x, coeffs[1][3]); |
| 158 |
|
| 159 |
return RationalFunction(x, safety3) * ep->a * exp(c0 + c1 * dEta); |
| 160 |
} |
| 161 |
|
| 162 |
typedef LIST(pointf) bezier_path_t; |
| 163 |
|
| 164 |
/* append points to a Bézier path |
| 165 |
* Assume initial call to moveTo to initialize, followed by |
| 166 |
* calls to curveTo and lineTo, and finished with endPath. |
| 167 |
*/ |
| 168 |
|
| 169 |
static void moveTo(bezier_path_t *polypath, double x, double y) { |
| 170 |
LIST_APPEND(polypath, ((pointf){.x = x, .y = y})); |
| 171 |
} |
| 172 |
|
| 173 |
static void curveTo(bezier_path_t *polypath, double x1, double y1, double x2, |
| 174 |
double y2, double x3, double y3) { |
| 175 |
LIST_APPEND(polypath, ((pointf){.x = x1, .y = y1})); |
| 176 |
LIST_APPEND(polypath, ((pointf){.x = x2, .y = y2})); |
| 177 |
LIST_APPEND(polypath, ((pointf){.x = x3, .y = y3})); |
| 178 |
} |
| 179 |
|
| 180 |
static void lineTo(bezier_path_t *polypath, double x, double y) { |
| 181 |
const pointf curp = LIST_GET(polypath, LIST_SIZE(polypath) - 1); |
| 182 |
curveTo(polypath, curp.x, curp.y, x, y, x, y); |
| 183 |
} |
| 184 |
|
| 185 |
static void endPath(bezier_path_t *polypath) { |
| 186 |
const pointf p0 = LIST_GET(polypath, 0); |
| 187 |
lineTo(polypath, p0.x, p0.y); |
| 188 |
} |
| 189 |
|
| 190 |
/* genEllipticPath: |
| 191 |
* Approximate an elliptical arc via Béziers of degree 3 |
| 192 |
* The path begins and ends with line segments to the center of the ellipse. |
| 193 |
* Returned path must be freed by the caller. |
| 194 |
*/ |
| 195 |
static Ppolyline_t *genEllipticPath(ellipse_t *ep) { |
| 196 |
Ppolyline_t *polypath = gv_alloc(sizeof(Ppolyline_t)); |
| 197 |
|
| 198 |
static const double THRESHOLD = 0.00001; // quality of approximation |
| 199 |
|
| 200 |
// find the number of Bézier curves needed |
| 201 |
bool found = false; |
| 202 |
int i, n = 1; |
| 203 |
while (!found && n < 1024) { |
| 204 |
double diffEta = (ep->eta2 - ep->eta1) / n; |
| 205 |
if (diffEta <= 0.5 * M_PI) { |
| 206 |
double etaOne = ep->eta1; |
| 207 |
found = true; |
| 208 |
for (i = 0; found && i < n; ++i) { |
| 209 |
double etaA = etaOne; |
| 210 |
etaOne += diffEta; |
| 211 |
found = estimateError(ep, etaA, etaOne) <= THRESHOLD; |
| 212 |
} |
| 213 |
} |
| 214 |
n = n << 1; |
| 215 |
} |
| 216 |
|
| 217 |
const double dEta = (ep->eta2 - ep->eta1) / n; |
| 218 |
double etaB = ep->eta1; |
| 219 |
|
| 220 |
double cosEtaB = cos(etaB); |
| 221 |
double sinEtaB = sin(etaB); |
| 222 |
double aCosEtaB = ep->a * cosEtaB; |
| 223 |
double bSinEtaB = ep->b * sinEtaB; |
| 224 |
double aSinEtaB = ep->a * sinEtaB; |
| 225 |
double bCosEtaB = ep->b * cosEtaB; |
| 226 |
double xB = ep->cx + aCosEtaB; |
| 227 |
double yB = ep->cy + bSinEtaB; |
| 228 |
double xBDot = -aSinEtaB; |
| 229 |
double yBDot = bCosEtaB; |
| 230 |
|
| 231 |
bezier_path_t bezier_path = {0}; |
| 232 |
moveTo(&bezier_path, ep->cx, ep->cy); |
| 233 |
lineTo(&bezier_path, xB, yB); |
| 234 |
|
| 235 |
const double t = tan(0.5 * dEta); |
| 236 |
const double alpha = sin(dEta) * (sqrt(4 + 3 * t * t) - 1) / 3; |
| 237 |
|
| 238 |
for (i = 0; i < n; ++i) { |
| 239 |
|
| 240 |
double xA = xB; |
| 241 |
double yA = yB; |
| 242 |
double xADot = xBDot; |
| 243 |
double yADot = yBDot; |
| 244 |
|
| 245 |
etaB += dEta; |
| 246 |
cosEtaB = cos(etaB); |
| 247 |
sinEtaB = sin(etaB); |
| 248 |
aCosEtaB = ep->a * cosEtaB; |
| 249 |
bSinEtaB = ep->b * sinEtaB; |
| 250 |
aSinEtaB = ep->a * sinEtaB; |
| 251 |
bCosEtaB = ep->b * cosEtaB; |
| 252 |
xB = ep->cx + aCosEtaB; |
| 253 |
yB = ep->cy + bSinEtaB; |
| 254 |
xBDot = -aSinEtaB; |
| 255 |
yBDot = bCosEtaB; |
| 256 |
|
| 257 |
curveTo(&bezier_path, xA + alpha * xADot, yA + alpha * yADot, |
| 258 |
xB - alpha * xBDot, yB - alpha * yBDot, xB, yB); |
| 259 |
} |
| 260 |
|
| 261 |
endPath(&bezier_path); |
| 262 |
|
| 263 |
LIST_DETACH(&bezier_path, &polypath->ps, &polypath->pn); |
| 264 |
|
| 265 |
return polypath; |
| 266 |
} |
| 267 |
|
| 268 |
/* ellipticWedge: |
| 269 |
* Return a cubic Bézier for an elliptical wedge, with center ctr, x and y |
| 270 |
* semi-axes xsemi and ysemi, start angle angle0 and end angle angle1. |
| 271 |
* This includes beginning and ending line segments to the ellipse center. |
| 272 |
* Calling function must free storage of returned path. |
| 273 |
*/ |
| 274 |
Ppolyline_t *ellipticWedge(pointf ctr, double xsemi, double ysemi, |
| 275 |
double angle0, double angle1) { |
| 276 |
ellipse_t ell; |
| 277 |
|
| 278 |
initEllipse(&ell, ctr.x, ctr.y, xsemi, ysemi, angle0, angle1); |
| 279 |
return genEllipticPath(&ell); |
| 280 |
} |