Debian Codegraph @

fontforge/utanvec.c

raw symbols SCIP
1 /* Copyright (C) 2019 by Skef Iterum */
2 /*
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5
6 * Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12
13 * The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 // ends of degenerate splines to find
40 // a slope.
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 /* Orders unit tangent vectors on an absolute -PI+e to PI
58 * equivalent basis
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 /* True if rotating from ut1 to ut3 in the specified direction
75 * the angle passes through ut2.
76 *
77 * Note: Returns true if ut1 is near ut2 but false if ut2 is
78 * near ut3
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 // magnitude of cross product
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 // If one control point is colocated with its on-curve point the
120 // slope will be undefined at that end, so walk back a bit for
121 // consistency
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 // For missing slopes take a very small step away and try again.
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 /* Return the lowest t value greater than min_t such that the
137 * tangent at the point is parallel to ut, or -1 if the tangent
138 * never has that slope.
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 // Nothing to "solve" for; lines should be handled by different means
150 // because you can't "advance" along them based on slope changes
151 if ( SplineIsLinear(spl) )
152 return -1;
153
154 // Only check t==0 if min_t is negative
155 if ( min_t<0 && UTNear(ut, SplineUTanVecAt(spl, 0.0)) )
156 return 0.0;
157
158 // This is probably over-optimized
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 // After rotating by theta the desired angle will be at a y extrema
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 // Check t==1
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 // Check t==1
200 if ( (min_t+UTRETRY) < 1
201 && BPWithin(ut, SplineUTanVecAt(spl, 1.0), UTSOFTMARGIN) )
202 return 1.0;
203
204 // Nothing found
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 // printf("i:%d, i.x:%lf, i.y:%lf\n", i, ut[i].x, ut[i].y);
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