Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/java2d/pisces/Curve.java
38918 views
1
/*
2
* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package sun.java2d.pisces;
27
28
import java.util.Iterator;
29
30
final class Curve {
31
32
float ax, ay, bx, by, cx, cy, dx, dy;
33
float dax, day, dbx, dby;
34
35
Curve() {
36
}
37
38
void set(float[] points, int type) {
39
switch(type) {
40
case 8:
41
set(points[0], points[1],
42
points[2], points[3],
43
points[4], points[5],
44
points[6], points[7]);
45
break;
46
case 6:
47
set(points[0], points[1],
48
points[2], points[3],
49
points[4], points[5]);
50
break;
51
default:
52
throw new InternalError("Curves can only be cubic or quadratic");
53
}
54
}
55
56
void set(float x1, float y1,
57
float x2, float y2,
58
float x3, float y3,
59
float x4, float y4)
60
{
61
ax = 3 * (x2 - x3) + x4 - x1;
62
ay = 3 * (y2 - y3) + y4 - y1;
63
bx = 3 * (x1 - 2 * x2 + x3);
64
by = 3 * (y1 - 2 * y2 + y3);
65
cx = 3 * (x2 - x1);
66
cy = 3 * (y2 - y1);
67
dx = x1;
68
dy = y1;
69
dax = 3 * ax; day = 3 * ay;
70
dbx = 2 * bx; dby = 2 * by;
71
}
72
73
void set(float x1, float y1,
74
float x2, float y2,
75
float x3, float y3)
76
{
77
ax = ay = 0f;
78
79
bx = x1 - 2 * x2 + x3;
80
by = y1 - 2 * y2 + y3;
81
cx = 2 * (x2 - x1);
82
cy = 2 * (y2 - y1);
83
dx = x1;
84
dy = y1;
85
dax = 0; day = 0;
86
dbx = 2 * bx; dby = 2 * by;
87
}
88
89
float xat(float t) {
90
return t * (t * (t * ax + bx) + cx) + dx;
91
}
92
float yat(float t) {
93
return t * (t * (t * ay + by) + cy) + dy;
94
}
95
96
float dxat(float t) {
97
return t * (t * dax + dbx) + cx;
98
}
99
100
float dyat(float t) {
101
return t * (t * day + dby) + cy;
102
}
103
104
int dxRoots(float[] roots, int off) {
105
return Helpers.quadraticRoots(dax, dbx, cx, roots, off);
106
}
107
108
int dyRoots(float[] roots, int off) {
109
return Helpers.quadraticRoots(day, dby, cy, roots, off);
110
}
111
112
int infPoints(float[] pts, int off) {
113
// inflection point at t if -f'(t)x*f''(t)y + f'(t)y*f''(t)x == 0
114
// Fortunately, this turns out to be quadratic, so there are at
115
// most 2 inflection points.
116
final float a = dax * dby - dbx * day;
117
final float b = 2 * (cy * dax - day * cx);
118
final float c = cy * dbx - cx * dby;
119
120
return Helpers.quadraticRoots(a, b, c, pts, off);
121
}
122
123
// finds points where the first and second derivative are
124
// perpendicular. This happens when g(t) = f'(t)*f''(t) == 0 (where
125
// * is a dot product). Unfortunately, we have to solve a cubic.
126
private int perpendiculardfddf(float[] pts, int off) {
127
assert pts.length >= off + 4;
128
129
// these are the coefficients of some multiple of g(t) (not g(t),
130
// because the roots of a polynomial are not changed after multiplication
131
// by a constant, and this way we save a few multiplications).
132
final float a = 2*(dax*dax + day*day);
133
final float b = 3*(dax*dbx + day*dby);
134
final float c = 2*(dax*cx + day*cy) + dbx*dbx + dby*dby;
135
final float d = dbx*cx + dby*cy;
136
return Helpers.cubicRootsInAB(a, b, c, d, pts, off, 0f, 1f);
137
}
138
139
// Tries to find the roots of the function ROC(t)-w in [0, 1). It uses
140
// a variant of the false position algorithm to find the roots. False
141
// position requires that 2 initial values x0,x1 be given, and that the
142
// function must have opposite signs at those values. To find such
143
// values, we need the local extrema of the ROC function, for which we
144
// need the roots of its derivative; however, it's harder to find the
145
// roots of the derivative in this case than it is to find the roots
146
// of the original function. So, we find all points where this curve's
147
// first and second derivative are perpendicular, and we pretend these
148
// are our local extrema. There are at most 3 of these, so we will check
149
// at most 4 sub-intervals of (0,1). ROC has asymptotes at inflection
150
// points, so roc-w can have at least 6 roots. This shouldn't be a
151
// problem for what we're trying to do (draw a nice looking curve).
152
int rootsOfROCMinusW(float[] roots, int off, final float w, final float err) {
153
// no OOB exception, because by now off<=6, and roots.length >= 10
154
assert off <= 6 && roots.length >= 10;
155
int ret = off;
156
int numPerpdfddf = perpendiculardfddf(roots, off);
157
float t0 = 0, ft0 = ROCsq(t0) - w*w;
158
roots[off + numPerpdfddf] = 1f; // always check interval end points
159
numPerpdfddf++;
160
for (int i = off; i < off + numPerpdfddf; i++) {
161
float t1 = roots[i], ft1 = ROCsq(t1) - w*w;
162
if (ft0 == 0f) {
163
roots[ret++] = t0;
164
} else if (ft1 * ft0 < 0f) { // have opposite signs
165
// (ROC(t)^2 == w^2) == (ROC(t) == w) is true because
166
// ROC(t) >= 0 for all t.
167
roots[ret++] = falsePositionROCsqMinusX(t0, t1, w*w, err);
168
}
169
t0 = t1;
170
ft0 = ft1;
171
}
172
173
return ret - off;
174
}
175
176
private static float eliminateInf(float x) {
177
return (x == Float.POSITIVE_INFINITY ? Float.MAX_VALUE :
178
(x == Float.NEGATIVE_INFINITY ? Float.MIN_VALUE : x));
179
}
180
181
// A slight modification of the false position algorithm on wikipedia.
182
// This only works for the ROCsq-x functions. It might be nice to have
183
// the function as an argument, but that would be awkward in java6.
184
// TODO: It is something to consider for java8 (or whenever lambda
185
// expressions make it into the language), depending on how closures
186
// and turn out. Same goes for the newton's method
187
// algorithm in Helpers.java
188
private float falsePositionROCsqMinusX(float x0, float x1,
189
final float x, final float err)
190
{
191
final int iterLimit = 100;
192
int side = 0;
193
float t = x1, ft = eliminateInf(ROCsq(t) - x);
194
float s = x0, fs = eliminateInf(ROCsq(s) - x);
195
float r = s, fr;
196
for (int i = 0; i < iterLimit && Math.abs(t - s) > err * Math.abs(t + s); i++) {
197
r = (fs * t - ft * s) / (fs - ft);
198
fr = ROCsq(r) - x;
199
if (sameSign(fr, ft)) {
200
ft = fr; t = r;
201
if (side < 0) {
202
fs /= (1 << (-side));
203
side--;
204
} else {
205
side = -1;
206
}
207
} else if (fr * fs > 0) {
208
fs = fr; s = r;
209
if (side > 0) {
210
ft /= (1 << side);
211
side++;
212
} else {
213
side = 1;
214
}
215
} else {
216
break;
217
}
218
}
219
return r;
220
}
221
222
private static boolean sameSign(double x, double y) {
223
// another way is to test if x*y > 0. This is bad for small x, y.
224
return (x < 0 && y < 0) || (x > 0 && y > 0);
225
}
226
227
// returns the radius of curvature squared at t of this curve
228
// see http://en.wikipedia.org/wiki/Radius_of_curvature_(applications)
229
private float ROCsq(final float t) {
230
// dx=xat(t) and dy=yat(t). These calls have been inlined for efficiency
231
final float dx = t * (t * dax + dbx) + cx;
232
final float dy = t * (t * day + dby) + cy;
233
final float ddx = 2 * dax * t + dbx;
234
final float ddy = 2 * day * t + dby;
235
final float dx2dy2 = dx*dx + dy*dy;
236
final float ddx2ddy2 = ddx*ddx + ddy*ddy;
237
final float ddxdxddydy = ddx*dx + ddy*dy;
238
return dx2dy2*((dx2dy2*dx2dy2) / (dx2dy2 * ddx2ddy2 - ddxdxddydy*ddxdxddydy));
239
}
240
241
// curve to be broken should be in pts
242
// this will change the contents of pts but not Ts
243
// TODO: There's no reason for Ts to be an array. All we need is a sequence
244
// of t values at which to subdivide. An array statisfies this condition,
245
// but is unnecessarily restrictive. Ts should be an Iterator<Float> instead.
246
// Doing this will also make dashing easier, since we could easily make
247
// LengthIterator an Iterator<Float> and feed it to this function to simplify
248
// the loop in Dasher.somethingTo.
249
static Iterator<Integer> breakPtsAtTs(final float[] pts, final int type,
250
final float[] Ts, final int numTs)
251
{
252
assert pts.length >= 2*type && numTs <= Ts.length;
253
return new Iterator<Integer>() {
254
// these prevent object creation and destruction during autoboxing.
255
// Because of this, the compiler should be able to completely
256
// eliminate the boxing costs.
257
final Integer i0 = 0;
258
final Integer itype = type;
259
int nextCurveIdx = 0;
260
Integer curCurveOff = i0;
261
float prevT = 0;
262
263
@Override public boolean hasNext() {
264
return nextCurveIdx < numTs + 1;
265
}
266
267
@Override public Integer next() {
268
Integer ret;
269
if (nextCurveIdx < numTs) {
270
float curT = Ts[nextCurveIdx];
271
float splitT = (curT - prevT) / (1 - prevT);
272
Helpers.subdivideAt(splitT,
273
pts, curCurveOff,
274
pts, 0,
275
pts, type, type);
276
prevT = curT;
277
ret = i0;
278
curCurveOff = itype;
279
} else {
280
ret = curCurveOff;
281
}
282
nextCurveIdx++;
283
return ret;
284
}
285
286
@Override public void remove() {}
287
};
288
}
289
}
290
291
292