1 /* 2 * Copyright (c) 2007, 2017, 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 com.sun.marlin; 27 28 final class Curve { 29 30 float ax, ay, bx, by, cx, cy, dx, dy; 31 float dax, day, dbx, dby; 32 33 Curve() { 34 } 35 36 void set(float[] points, int type) { 37 switch(type) { 38 case 8: 39 set(points[0], points[1], 40 points[2], points[3], 41 points[4], points[5], 42 points[6], points[7]); 43 return; 44 case 6: 45 set(points[0], points[1], 46 points[2], points[3], 47 points[4], points[5]); 48 return; 49 default: 50 throw new InternalError("Curves can only be cubic or quadratic"); 51 } 52 } 53 54 void set(float x1, float y1, 55 float x2, float y2, 56 float x3, float y3, 57 float x4, float y4) 58 { 59 final float dx32 = 3.0f * (x3 - x2); 60 final float dy32 = 3.0f * (y3 - y2); 61 final float dx21 = 3.0f * (x2 - x1); 62 final float dy21 = 3.0f * (y2 - y1); 63 ax = (x4 - x1) - dx32; 64 ay = (y4 - y1) - dy32; 65 bx = (dx32 - dx21); 66 by = (dy32 - dy21); 67 cx = dx21; 68 cy = dy21; 69 dx = x1; 70 dy = y1; 71 dax = 3.0f * ax; day = 3.0f * ay; 72 dbx = 2.0f * bx; dby = 2.0f * by; 73 } 74 75 void set(float x1, float y1, 76 float x2, float y2, 77 float x3, float y3) 78 { 79 final float dx21 = (x2 - x1); 80 final float dy21 = (y2 - y1); 81 ax = 0.0f; ay = 0.0f; 82 bx = (x3 - x2) - dx21; 83 by = (y3 - y2) - dy21; 84 cx = 2.0f * dx21; 85 cy = 2.0f * dy21; 86 dx = x1; 87 dy = y1; 88 dax = 0.0f; day = 0.0f; 89 dbx = 2.0f * bx; dby = 2.0f * by; 90 } 91 92 float xat(float t) { 93 return t * (t * (t * ax + bx) + cx) + dx; 94 } 95 float yat(float t) { 96 return t * (t * (t * ay + by) + cy) + dy; 97 } 98 99 float dxat(float t) { 100 return t * (t * dax + dbx) + cx; 101 } 102 103 float dyat(float t) { 104 return t * (t * day + dby) + cy; 105 } 106 107 int dxRoots(float[] roots, int off) { 108 return Helpers.quadraticRoots(dax, dbx, cx, roots, off); 109 } 110 111 int dyRoots(float[] roots, int off) { 112 return Helpers.quadraticRoots(day, dby, cy, roots, off); 113 } 114 115 int infPoints(float[] pts, int off) { 116 // inflection point at t if -f'(t)x*f''(t)y + f'(t)y*f''(t)x == 0 117 // Fortunately, this turns out to be quadratic, so there are at 118 // most 2 inflection points. 119 final float a = dax * dby - dbx * day; 120 final float b = 2.0f * (cy * dax - day * cx); 121 final float c = cy * dbx - cx * dby; 122 123 return Helpers.quadraticRoots(a, b, c, pts, off); 124 } 125 126 // finds points where the first and second derivative are 127 // perpendicular. This happens when g(t) = f'(t)*f''(t) == 0 (where 128 // * is a dot product). Unfortunately, we have to solve a cubic. 129 private int perpendiculardfddf(float[] pts, int off) { 130 assert pts.length >= off + 4; 131 132 // these are the coefficients of some multiple of g(t) (not g(t), 133 // because the roots of a polynomial are not changed after multiplication 134 // by a constant, and this way we save a few multiplications). 135 final float a = 2.0f * (dax*dax + day*day); 136 final float b = 3.0f * (dax*dbx + day*dby); 137 final float c = 2.0f * (dax*cx + day*cy) + dbx*dbx + dby*dby; 138 final float d = dbx*cx + dby*cy; 139 return Helpers.cubicRootsInAB(a, b, c, d, pts, off, 0.0f, 1.0f); 140 } 141 142 // Tries to find the roots of the function ROC(t)-w in [0, 1). It uses 143 // a variant of the false position algorithm to find the roots. False 144 // position requires that 2 initial values x0,x1 be given, and that the 145 // function must have opposite signs at those values. To find such 146 // values, we need the local extrema of the ROC function, for which we 147 // need the roots of its derivative; however, it's harder to find the 148 // roots of the derivative in this case than it is to find the roots 149 // of the original function. So, we find all points where this curve's 150 // first and second derivative are perpendicular, and we pretend these 151 // are our local extrema. There are at most 3 of these, so we will check 152 // at most 4 sub-intervals of (0,1). ROC has asymptotes at inflection 153 // points, so roc-w can have at least 6 roots. This shouldn't be a 154 // problem for what we're trying to do (draw a nice looking curve). 155 int rootsOfROCMinusW(float[] roots, int off, final float w, final float err) { 156 // no OOB exception, because by now off<=6, and roots.length >= 10 157 assert off <= 6 && roots.length >= 10; 158 int ret = off; 159 int numPerpdfddf = perpendiculardfddf(roots, off); 160 float t0 = 0.0f, ft0 = ROCsq(t0) - w*w; 161 roots[off + numPerpdfddf] = 1.0f; // always check interval end points 162 numPerpdfddf++; 163 for (int i = off; i < off + numPerpdfddf; i++) { 164 float t1 = roots[i], ft1 = ROCsq(t1) - w*w; 165 if (ft0 == 0.0f) { 166 roots[ret++] = t0; 167 } else if (ft1 * ft0 < 0.0f) { // have opposite signs 168 // (ROC(t)^2 == w^2) == (ROC(t) == w) is true because 169 // ROC(t) >= 0 for all t. 170 roots[ret++] = falsePositionROCsqMinusX(t0, t1, w*w, err); 171 } 172 t0 = t1; 173 ft0 = ft1; 174 } 175 176 return ret - off; 177 } 178 179 private static float eliminateInf(float x) { 180 return (x == Float.POSITIVE_INFINITY ? Float.MAX_VALUE : 181 (x == Float.NEGATIVE_INFINITY ? Float.MIN_VALUE : x)); 182 } 183 184 // A slight modification of the false position algorithm on wikipedia. 185 // This only works for the ROCsq-x functions. It might be nice to have 186 // the function as an argument, but that would be awkward in java6. 187 // TODO: It is something to consider for java8 (or whenever lambda 188 // expressions make it into the language), depending on how closures 189 // and turn out. Same goes for the newton's method 190 // algorithm in Helpers.java 191 private float falsePositionROCsqMinusX(float x0, float x1, 192 final float x, final float err) 193 { 194 final int iterLimit = 100; 195 int side = 0; 196 float t = x1, ft = eliminateInf(ROCsq(t) - x); 197 float s = x0, fs = eliminateInf(ROCsq(s) - x); 198 float r = s, fr; 199 for (int i = 0; i < iterLimit && Math.abs(t - s) > err * Math.abs(t + s); i++) { 200 r = (fs * t - ft * s) / (fs - ft); 201 fr = ROCsq(r) - x; 202 if (sameSign(fr, ft)) { 203 ft = fr; t = r; 204 if (side < 0) { 205 fs /= (1 << (-side)); 206 side--; 207 } else { 208 side = -1; 209 } 210 } else if (fr * fs > 0) { 211 fs = fr; s = r; 212 if (side > 0) { 213 ft /= (1 << side); 214 side++; 215 } else { 216 side = 1; 217 } 218 } else { 219 break; 220 } 221 } 222 return r; 223 } 224 225 private static boolean sameSign(float x, float y) { 226 // another way is to test if x*y > 0. This is bad for small x, y. 227 return (x < 0.0f && y < 0.0f) || (x > 0.0f && y > 0.0f); 228 } 229 230 // returns the radius of curvature squared at t of this curve 231 // see http://en.wikipedia.org/wiki/Radius_of_curvature_(applications) 232 private float ROCsq(final float t) { 233 // dx=xat(t) and dy=yat(t). These calls have been inlined for efficiency 234 final float dx = t * (t * dax + dbx) + cx; 235 final float dy = t * (t * day + dby) + cy; 236 final float ddx = 2.0f * dax * t + dbx; 237 final float ddy = 2.0f * day * t + dby; 238 final float dx2dy2 = dx*dx + dy*dy; 239 final float ddx2ddy2 = ddx*ddx + ddy*ddy; 240 final float ddxdxddydy = ddx*dx + ddy*dy; 241 return dx2dy2*((dx2dy2*dx2dy2) / (dx2dy2 * ddx2ddy2 - ddxdxddydy*ddxdxddydy)); 242 } 243 } | 1 /* 2 * Copyright (c) 2007, 2018, 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 com.sun.marlin; 27 28 final class Curve { 29 30 float ax, ay, bx, by, cx, cy, dx, dy; 31 float dax, day, dbx, dby; 32 33 Curve() { 34 } 35 36 void set(final float[] points, final int type) { 37 // if instead of switch (perf + most probable cases first) 38 if (type == 8) { 39 set(points[0], points[1], 40 points[2], points[3], 41 points[4], points[5], 42 points[6], points[7]); 43 } else if (type == 4) { 44 set(points[0], points[1], 45 points[2], points[3]); 46 } else { 47 set(points[0], points[1], 48 points[2], points[3], 49 points[4], points[5]); 50 } 51 } 52 53 void set(final float x1, final float y1, 54 final float x2, final float y2, 55 final float x3, final float y3, 56 final float x4, final float y4) 57 { 58 final float dx32 = 3.0f * (x3 - x2); 59 final float dy32 = 3.0f * (y3 - y2); 60 final float dx21 = 3.0f * (x2 - x1); 61 final float dy21 = 3.0f * (y2 - y1); 62 ax = (x4 - x1) - dx32; // A = P3 - P0 - 3 (P2 - P1) = (P3 - P0) + 3 (P1 - P2) 63 ay = (y4 - y1) - dy32; 64 bx = (dx32 - dx21); // B = 3 (P2 - P1) - 3(P1 - P0) = 3 (P2 + P0) - 6 P1 65 by = (dy32 - dy21); 66 cx = dx21; // C = 3 (P1 - P0) 67 cy = dy21; 68 dx = x1; // D = P0 69 dy = y1; 70 dax = 3.0f * ax; 71 day = 3.0f * ay; 72 dbx = 2.0f * bx; 73 dby = 2.0f * by; 74 } 75 76 void set(final float x1, final float y1, 77 final float x2, final float y2, 78 final float x3, final float y3) 79 { 80 final float dx21 = (x2 - x1); 81 final float dy21 = (y2 - y1); 82 ax = 0.0f; // A = 0 83 ay = 0.0f; 84 bx = (x3 - x2) - dx21; // B = P3 - P0 - 2 P2 85 by = (y3 - y2) - dy21; 86 cx = 2.0f * dx21; // C = 2 (P2 - P1) 87 cy = 2.0f * dy21; 88 dx = x1; // D = P1 89 dy = y1; 90 dax = 0.0f; 91 day = 0.0f; 92 dbx = 2.0f * bx; 93 dby = 2.0f * by; 94 } 95 96 void set(final float x1, final float y1, 97 final float x2, final float y2) 98 { 99 final float dx21 = (x2 - x1); 100 final float dy21 = (y2 - y1); 101 ax = 0.0f; // A = 0 102 ay = 0.0f; 103 bx = 0.0f; // B = 0 104 by = 0.0f; 105 cx = dx21; // C = (P2 - P1) 106 cy = dy21; 107 dx = x1; // D = P1 108 dy = y1; 109 dax = 0.0f; 110 day = 0.0f; 111 dbx = 0.0f; 112 dby = 0.0f; 113 } 114 115 int dxRoots(final float[] roots, final int off) { 116 return Helpers.quadraticRoots(dax, dbx, cx, roots, off); 117 } 118 119 int dyRoots(final float[] roots, final int off) { 120 return Helpers.quadraticRoots(day, dby, cy, roots, off); 121 } 122 123 int infPoints(final float[] pts, final int off) { 124 // inflection point at t if -f'(t)x*f''(t)y + f'(t)y*f''(t)x == 0 125 // Fortunately, this turns out to be quadratic, so there are at 126 // most 2 inflection points. 127 final float a = dax * dby - dbx * day; 128 final float b = 2.0f * (cy * dax - day * cx); 129 final float c = cy * dbx - cx * dby; 130 131 return Helpers.quadraticRoots(a, b, c, pts, off); 132 } 133 134 int xPoints(final float[] ts, final int off, final float x) 135 { 136 return Helpers.cubicRootsInAB(ax, bx, cx, dx - x, ts, off, 0.0f, 1.0f); 137 } 138 139 int yPoints(final float[] ts, final int off, final float y) 140 { 141 return Helpers.cubicRootsInAB(ay, by, cy, dy - y, ts, off, 0.0f, 1.0f); 142 } 143 144 // finds points where the first and second derivative are 145 // perpendicular. This happens when g(t) = f'(t)*f''(t) == 0 (where 146 // * is a dot product). Unfortunately, we have to solve a cubic. 147 private int perpendiculardfddf(final float[] pts, final int off) { 148 assert pts.length >= off + 4; 149 150 // these are the coefficients of some multiple of g(t) (not g(t), 151 // because the roots of a polynomial are not changed after multiplication 152 // by a constant, and this way we save a few multiplications). 153 final float a = 2.0f * (dax * dax + day * day); 154 final float b = 3.0f * (dax * dbx + day * dby); 155 final float c = 2.0f * (dax * cx + day * cy) + dbx * dbx + dby * dby; 156 final float d = dbx * cx + dby * cy; 157 158 return Helpers.cubicRootsInAB(a, b, c, d, pts, off, 0.0f, 1.0f); 159 } 160 161 // Tries to find the roots of the function ROC(t)-w in [0, 1). It uses 162 // a variant of the false position algorithm to find the roots. False 163 // position requires that 2 initial values x0,x1 be given, and that the 164 // function must have opposite signs at those values. To find such 165 // values, we need the local extrema of the ROC function, for which we 166 // need the roots of its derivative; however, it's harder to find the 167 // roots of the derivative in this case than it is to find the roots 168 // of the original function. So, we find all points where this curve's 169 // first and second derivative are perpendicular, and we pretend these 170 // are our local extrema. There are at most 3 of these, so we will check 171 // at most 4 sub-intervals of (0,1). ROC has asymptotes at inflection 172 // points, so roc-w can have at least 6 roots. This shouldn't be a 173 // problem for what we're trying to do (draw a nice looking curve). 174 int rootsOfROCMinusW(final float[] roots, final int off, final float w2, final float err) { 175 // no OOB exception, because by now off<=6, and roots.length >= 10 176 assert off <= 6 && roots.length >= 10; 177 178 int ret = off; 179 final int end = off + perpendiculardfddf(roots, off); 180 roots[end] = 1.0f; // always check interval end points 181 182 float t0 = 0.0f, ft0 = ROCsq(t0) - w2; 183 184 for (int i = off; i <= end; i++) { 185 float t1 = roots[i], ft1 = ROCsq(t1) - w2; 186 if (ft0 == 0.0f) { 187 roots[ret++] = t0; 188 } else if (ft1 * ft0 < 0.0f) { // have opposite signs 189 // (ROC(t)^2 == w^2) == (ROC(t) == w) is true because 190 // ROC(t) >= 0 for all t. 191 roots[ret++] = falsePositionROCsqMinusX(t0, t1, w2, err); 192 } 193 t0 = t1; 194 ft0 = ft1; 195 } 196 197 return ret - off; 198 } 199 200 private static float eliminateInf(final float x) { 201 return (x == Float.POSITIVE_INFINITY ? Float.MAX_VALUE : 202 (x == Float.NEGATIVE_INFINITY ? Float.MIN_VALUE : x)); 203 } 204 205 // A slight modification of the false position algorithm on wikipedia. 206 // This only works for the ROCsq-x functions. It might be nice to have 207 // the function as an argument, but that would be awkward in java6. 208 // TODO: It is something to consider for java8 (or whenever lambda 209 // expressions make it into the language), depending on how closures 210 // and turn out. Same goes for the newton's method 211 // algorithm in Helpers.java 212 private float falsePositionROCsqMinusX(final float t0, final float t1, 213 final float w2, final float err) 214 { 215 final int iterLimit = 100; 216 int side = 0; 217 float t = t1, ft = eliminateInf(ROCsq(t) - w2); 218 float s = t0, fs = eliminateInf(ROCsq(s) - w2); 219 float r = s, fr; 220 221 for (int i = 0; i < iterLimit && Math.abs(t - s) > err * Math.abs(t + s); i++) { 222 r = (fs * t - ft * s) / (fs - ft); 223 fr = ROCsq(r) - w2; 224 if (sameSign(fr, ft)) { 225 ft = fr; t = r; 226 if (side < 0) { 227 fs /= (1 << (-side)); 228 side--; 229 } else { 230 side = -1; 231 } 232 } else if (fr * fs > 0.0f) { 233 fs = fr; s = r; 234 if (side > 0) { 235 ft /= (1 << side); 236 side++; 237 } else { 238 side = 1; 239 } 240 } else { 241 break; 242 } 243 } 244 return r; 245 } 246 247 private static boolean sameSign(final float x, final float y) { 248 // another way is to test if x*y > 0. This is bad for small x, y. 249 return (x < 0.0f && y < 0.0f) || (x > 0.0f && y > 0.0f); 250 } 251 252 // returns the radius of curvature squared at t of this curve 253 // see http://en.wikipedia.org/wiki/Radius_of_curvature_(applications) 254 private float ROCsq(final float t) { 255 final float dx = t * (t * dax + dbx) + cx; 256 final float dy = t * (t * day + dby) + cy; 257 final float ddx = 2.0f * dax * t + dbx; 258 final float ddy = 2.0f * day * t + dby; 259 final float dx2dy2 = dx * dx + dy * dy; 260 final float ddx2ddy2 = ddx * ddx + ddy * ddy; 261 final float ddxdxddydy = ddx * dx + ddy * dy; 262 return dx2dy2 * ((dx2dy2 * dx2dy2) / (dx2dy2 * ddx2ddy2 - ddxdxddydy * ddxdxddydy)); 263 } 264 } |