| 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 |
#include <fontforge-config.h> |
| 29 |
|
| 30 |
#include "utanvec.h" |
| 31 |
#include "splineutil.h" |
| 32 |
#include "splineutil2.h" |
| 33 |
|
| 34 |
#include <math.h> |
| 35 |
#include <assert.h> |
| 36 |
|
| 37 |
#define UTSOFTMARGIN (1e-5) // Fallback margin for some cases |
| 38 |
#define UTRETRY (1e-9) // Amount of "t" to walk back from the |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
BasePoint MakeUTanVec(bigreal x, bigreal y) { |
| 43 |
BasePoint ret = { 0.0, 0.0 }; |
| 44 |
bigreal len = x*x + y*y; |
| 45 |
if ( len!=0 ) { |
| 46 |
len = sqrt(len); |
| 47 |
ret.x = x/len; |
| 48 |
ret.y = y/len; |
| 49 |
} |
| 50 |
return ret; |
| 51 |
} |
| 52 |
|
| 53 |
BasePoint NormVec(BasePoint v) { |
| 54 |
return MakeUTanVec(v.x, v.y); |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
int UTanVecGreater(BasePoint uta, BasePoint utb) { |
| 61 |
assert( RealNear(BPLenSq(uta),1) |
| 62 |
&& RealNear(BPLenSq(utb),1) ); |
| 63 |
|
| 64 |
if (uta.y >= 0) { |
| 65 |
if (utb.y < 0) |
| 66 |
return true; |
| 67 |
return uta.x < utb.x && !UTNear(uta, utb); |
| 68 |
} |
| 69 |
if (utb.y >= 0) |
| 70 |
return false; |
| 71 |
return uta.x > utb.x && !UTNear(uta, utb); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
int UTanVecsSequent(BasePoint ut1, BasePoint ut2, BasePoint ut3, |
| 81 |
int ccw) { |
| 82 |
BasePoint tmp; |
| 83 |
|
| 84 |
if ( UTNear(ut1, ut2) ) |
| 85 |
return true; |
| 86 |
|
| 87 |
if ( UTNear(ut2, ut3) || UTNear(ut1, ut3) ) |
| 88 |
return false; |
| 89 |
|
| 90 |
if (ccw) { |
| 91 |
tmp = ut1; |
| 92 |
ut1 = ut3; |
| 93 |
ut3 = tmp; |
| 94 |
} |
| 95 |
|
| 96 |
if ( UTanVecGreater(ut1, ut3) ) { |
| 97 |
return UTanVecGreater(ut1, ut2) && UTanVecGreater(ut2, ut3); |
| 98 |
} else { |
| 99 |
return (UTanVecGreater(ut1, ut2) && UTanVecGreater(ut3, ut2)) |
| 100 |
|| (UTanVecGreater(ut2, ut1) && UTanVecGreater(ut2, ut3)); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
int JointBendsCW(BasePoint ut_ref, BasePoint ut_vec) { |
| 105 |
|
| 106 |
bigreal r = ut_ref.x*ut_vec.y - ut_ref.y*ut_vec.x; |
| 107 |
if ( RealWithin(r, 0.0, UTMARGIN) ) |
| 108 |
return false; |
| 109 |
return r > 0; |
| 110 |
} |
| 111 |
|
| 112 |
BasePoint SplineUTanVecAt(Spline *s, bigreal t) { |
| 113 |
BasePoint raw; |
| 114 |
|
| 115 |
if ( SplineIsLinearish(s) ) { |
| 116 |
raw.x = s->to->me.x - s->from->me.x; |
| 117 |
raw.y = s->to->me.y - s->from->me.y; |
| 118 |
} else { |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
if ( RealWithin(t, 0, UTRETRY) |
| 123 |
&& BPWithin(s->from->me, s->from->nextcp, 1e-13) ) |
| 124 |
t = UTRETRY; |
| 125 |
else if ( RealWithin(t, 1, UTRETRY) |
| 126 |
&& BPWithin(s->to->me, s->to->prevcp, 1e-13) ) |
| 127 |
t = 1-UTRETRY; |
| 128 |
raw = SPLINEPTANVAL(s, t); |
| 129 |
|
| 130 |
if ( raw.x==0 && raw.y==0 ) |
| 131 |
raw = SPLINEPTANVAL(s, (t+UTRETRY <= 1.0) ? t+UTRETRY : t-UTRETRY); |
| 132 |
} |
| 133 |
return MakeUTanVec(raw.x, raw.y); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
#define ROTY(p, ut) (-(ut).y*(p).x + (ut).x*(p).y) |
| 141 |
#define ZCHECK(v) (RealWithin((v),0,1e-10) ? 0 : (v)) |
| 142 |
|
| 143 |
bigreal SplineSolveForUTanVec(Spline *spl, BasePoint ut, bigreal min_t, |
| 144 |
bool picky) { |
| 145 |
bigreal tmp, yto; |
| 146 |
extended te1, te2; |
| 147 |
Spline1D ys1d; |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
if ( SplineIsLinear(spl) ) |
| 152 |
return -1; |
| 153 |
|
| 154 |
|
| 155 |
if ( min_t<0 && UTNear(ut, SplineUTanVecAt(spl, 0.0)) ) |
| 156 |
return 0.0; |
| 157 |
|
| 158 |
|
| 159 |
ys1d.d = ROTY(spl->from->me, ut); |
| 160 |
yto = ROTY(spl->to->me, ut); |
| 161 |
if ( spl->order2 ) { |
| 162 |
ys1d.c = ZCHECK(2*(ROTY(spl->from->nextcp, ut)-ys1d.d)); |
| 163 |
ys1d.b = ZCHECK(yto-ys1d.d-ys1d.c); |
| 164 |
ys1d.a = 0; |
| 165 |
} else { |
| 166 |
tmp = ROTY(spl->from->nextcp, ut); |
| 167 |
ys1d.c = ZCHECK(3*(tmp-ys1d.d)); |
| 168 |
ys1d.b = ZCHECK(3*(ROTY(spl->to->prevcp, ut)-tmp)-ys1d.c); |
| 169 |
ys1d.a = ZCHECK(yto-ys1d.d-ys1d.c-ys1d.b); |
| 170 |
} |
| 171 |
if ( ys1d.a!=0 && ( Within16RoundingErrors(ys1d.a+ys1d.d,ys1d.d) |
| 172 |
|| Within16RoundingErrors(ys1d.a+yto,yto))) |
| 173 |
ys1d.a = 0; |
| 174 |
|
| 175 |
|
| 176 |
SplineFindExtrema(&ys1d, &te1, &te2); |
| 177 |
|
| 178 |
if ( te1 != -1 && te1 > (min_t+1e-9) |
| 179 |
&& UTNear(ut, SplineUTanVecAt(spl, te1)) ) |
| 180 |
return te1; |
| 181 |
if ( te2 != -1 && te2 > (min_t+1e-9) |
| 182 |
&& UTNear(ut, SplineUTanVecAt(spl, te2)) ) |
| 183 |
return te2; |
| 184 |
|
| 185 |
|
| 186 |
if ( (min_t+UTRETRY) < 1 && UTNear(ut, SplineUTanVecAt(spl, 1.0)) ) |
| 187 |
return 1.0; |
| 188 |
|
| 189 |
if ( picky ) |
| 190 |
return -1.0; |
| 191 |
|
| 192 |
if ( te1 != -1 && te1 > (min_t+1e-9) |
| 193 |
&& BPWithin(ut, SplineUTanVecAt(spl, te1), UTSOFTMARGIN) ) |
| 194 |
return te1; |
| 195 |
if ( te2 != -1 && te2 > (min_t+1e-9) |
| 196 |
&& BPWithin(ut, SplineUTanVecAt(spl, te2), UTSOFTMARGIN) ) |
| 197 |
return te2; |
| 198 |
|
| 199 |
|
| 200 |
if ( (min_t+UTRETRY) < 1 |
| 201 |
&& BPWithin(ut, SplineUTanVecAt(spl, 1.0), UTSOFTMARGIN) ) |
| 202 |
return 1.0; |
| 203 |
|
| 204 |
|
| 205 |
return -1.0; |
| 206 |
} |
| 207 |
|
| 208 |
#if 0 |
| 209 |
void UTanVecTests() { |
| 210 |
int i, j, k, a, b; |
| 211 |
bigreal r, x, y, z; |
| 212 |
BasePoint ut[361], utmax = { -1, 0 }, utmin = UTMIN; |
| 213 |
|
| 214 |
for (i = 0; i<=360; i++) { |
| 215 |
r = ((180-i) * FF_PI / 180); |
| 216 |
ut[i] = MakeUTanVec(cos(r), sin(r)); |
| 217 |
|
| 218 |
} |
| 219 |
|
| 220 |
for (i=0; i<360; ++i) { |
| 221 |
for (j = 0; j<360; ++j) { |
| 222 |
if ( i==j ) |
| 223 |
continue; |
| 224 |
if ( i<j ) |
| 225 |
assert( UTanVecGreater(ut[i], ut[j]) |
| 226 |
&& !UTanVecGreater(ut[j], ut[i])); |
| 227 |
else |
| 228 |
assert( UTanVecGreater(ut[j], ut[i]) |
| 229 |
&& !UTanVecGreater(ut[i], ut[j])); |
| 230 |
assert(!UTNear(ut[i], ut[j])); |
| 231 |
x = atan2(ut[i].x, ut[i].y); |
| 232 |
y = atan2(ut[j].x, ut[j].y); |
| 233 |
z = x-y; |
| 234 |
z = atan2(sin(z), cos(z)); |
| 235 |
if ( RealNear(z, FF_PI) || RealNear(z, -FF_PI) ) |
| 236 |
continue; |
| 237 |
assert( (z>0) == JointBendsCW(ut[i], ut[j]) ); |
| 238 |
} |
| 239 |
assert( i==0 || UTanVecGreater(utmax, ut[i]) ); |
| 240 |
assert( UTanVecGreater(ut[i], utmin) ); |
| 241 |
} |
| 242 |
|
| 243 |
for (i=0; i<360; ++i) |
| 244 |
for (j = 0; j<360; ++j) |
| 245 |
for (k = 0; k<360; ++k) { |
| 246 |
a = UTanVecsSequent(ut[i], ut[j], ut[k], false); |
| 247 |
b = UTanVecsSequent(ut[i], ut[j], ut[k], true); |
| 248 |
if ( i==j ) |
| 249 |
assert(a && b); |
| 250 |
else if ( j==k || i==k ) |
| 251 |
assert(!a && !b); |
| 252 |
else { |
| 253 |
x = atan2(ut[i].x, ut[i].y); |
| 254 |
y = atan2(ut[j].x, ut[j].y); |
| 255 |
z = atan2(ut[k].x, ut[k].y); |
| 256 |
y = fmod(4*FF_PI+y-x,2*FF_PI); |
| 257 |
z = fmod(4*FF_PI+z-x,2*FF_PI); |
| 258 |
if ( y<z ) |
| 259 |
assert( a && !b ); |
| 260 |
else |
| 261 |
assert( !a && b ); |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
#endif |