Debian Codegraph @

src/com/github/weisj/jsvg/geometry/util/PathLengthCalculator.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2022-2026 Jannis Weis
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
7 * associated documentation files (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
9 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in all copies or
13 * substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16 * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 *
21 */
22 package com.github.weisj.jsvg.geometry.util;
23
24
25 import java.awt.geom.PathIterator;
26
27
28 public final class PathLengthCalculator {
29
30 private double x = 0;
31 private double y = 0;
32 private double xStart = x;
33 private double yStart = y;
34
35 public double segmentLength(int segmentType, double[] coords) {
36 double segmentLength = 0;
37 switch (segmentType) {
38 case PathIterator.SEG_MOVETO:
39 x = coords[0];
40 y = coords[1];
41 xStart = x;
42 yStart = y;
43 break;
44 case PathIterator.SEG_LINETO:
45 segmentLength = lineLength(x, y, coords[0], coords[1]);
46 x = coords[0];
47 y = coords[1];
48 break;
49 case PathIterator.SEG_QUADTO:
50 segmentLength = quadraticParametricLength(x, y, coords[0], coords[1], coords[2], coords[3]);
51 x = coords[2];
52 y = coords[3];
53 break;
54 case PathIterator.SEG_CUBICTO:
55 segmentLength =
56 cubicParametricLength(x, y, coords[0], coords[1], coords[2], coords[3], coords[4], coords[5]);
57 x = coords[4];
58 y = coords[5];
59 break;
60 case PathIterator.SEG_CLOSE:
61 segmentLength = lineLength(x, y, xStart, yStart);
62 x = xStart;
63 y = yStart;
64 break;
65 default:
66 throw new IllegalStateException();
67 }
68 return segmentLength;
69 }
70
71
72 private double lineLength(double x1, double y1, double x2, double y2) {
73 return GeometryUtil.lineLength(x1, y1, x2, y2);
74 }
75
76 /*
77 * Takes the start-, control- and end-point coordinates.
78 *
79 * Integral calculation by Dave Eberly, slightly modified for the edge case with collinear control
80 * point.
81 *
82 * See: http://www.gamedev.net/topic/551455-length-of-a-generalized-quadratic-bezier-curve-in-3d/
83 */
84 private double quadraticParametricLength(double ax, double ay, double bx, double by, double cx, double cy) {
85 // A == C
86 if (ax == cx && ay == cy) {
87 // A == B
88 if (ax == bx && ay == by) return 0;
89 return lineLength(ax, ay, bx, by);
90 }
91 // A == B || C == B
92 if ((ax == bx && ay == by)
93 || (cx == bx && cy == by)) {
94 return lineLength(ax, ay, cx, cy);
95 }
96 double ax0 = bx - ax;
97 double ay0 = by - ay;
98
99 double ax1 = ax - 2 * bx + cx;
100 double ay1 = ay - 2 * by + cy;
101
102 if (ax1 != 0 || ay1 != 0) {
103 double c = 4f * dot2D(ax1, ay1, ax1, ay1);
104 double b = 8f * dot2D(ax0, ay0, ax1, ay1);
105 double a = 4f * dot2D(ax0, ay0, ax0, ay0);
106 double q = 4f * a * c - b * b;
107 double twoCpB = 2.0 * c + b;
108 double sumCBA = c + b + a;
109 double l0 = (0.25 / c) * (twoCpB * Math.sqrt(sumCBA) - b * Math.sqrt(a));
110 if (q == 0.0) return l0;
111 double l1 = (q / (8.0 * Math.pow(c, 1.5)))
112 * (Math.log(2.0 * Math.sqrt(c * sumCBA) + twoCpB) - Math.log(2.0 * Math.sqrt(c * a) + b));
113 return l0 + l1;
114 } else {
115 return 2f * lineLength(0, 0, ax0, ay0);
116 }
117 }
118
119 private double dot2D(double x1, double y1, double x2, double y2) {
120 return x1 * x2 + y1 * y2;
121 }
122
123
124 /*
125 * Arc-length calculation for a cubic Bézier curve using 12-point Gauss-Legendre quadrature.
126 *
127 * P'(t) = 3[(B-A)(1-t)² + 2(C-B)(1-t)t + (D-C)t²]
128 *
129 * Nodes and weights taken from standard Gauss-Legendre tables (interval [-1,1], mapped to [0,1]).
130 *
131 * Note: Exact calculation as for the quadratic case isn't possible due to the integral not being
132 * solvable with elementary functions.
133 */
134 private double cubicParametricLength(double ax, double ay, double bx, double by, double cx, double cy,
135 double dx, double dy) {
136 // 12-point Gauss-Legendre nodes on [-1,1]
137 final double[] nodes = {
138 -0.9815606342467192, -0.9041172563704749, -0.7699026741943047,
139 -0.5873179542866175, -0.3678314989981802, -0.1252334085114689,
140 0.1252334085114689, 0.3678314989981802, 0.5873179542866175,
141 0.7699026741943047, 0.9041172563704749, 0.9815606342467192
142 };
143 // Corresponding weights
144 final double[] weights = {
145 0.0471753363865118, 0.1069393259953184, 0.1600783285433462,
146 0.2031674267230659, 0.2334925365383548, 0.2491470458134028,
147 0.2491470458134028, 0.2334925365383548, 0.2031674267230659,
148 0.1600783285433462, 0.1069393259953184, 0.0471753363865118
149 };
150
151 // Derivative coefficients: P'(t) = 3[A1*(1-t)^2 + 2*A2*(1-t)*t + A3*t^2]
152 // where A1 = B-A, A2 = C-B, A3 = D-C
153 double a1x = bx - ax;
154 double a1y = by - ay;
155 double a2x = cx - bx;
156 double a2y = cy - by;
157 double a3x = dx - cx;
158 double a3y = dy - cy;
159
160 double sum = 0;
161 for (int i = 0; i < nodes.length; i++) {
162 // Map node from [-1,1] to [0,1]: t = (node + 1) / 2
163 double t = (nodes[i] + 1.0) * 0.5;
164 double mt = 1.0 - t;
165 // Derivative / 3
166 double dtx = a1x * mt * mt + 2.0 * a2x * mt * t + a3x * t * t;
167 double dty = a1y * mt * mt + 2.0 * a2y * mt * t + a3y * t * t;
168 sum += weights[i] * Math.sqrt(dtx * dtx + dty * dty);
169 }
170 // Factor of 3 from derivative, factor of 0.5 from interval mapping [-1,1] -> [0,1]
171 return 3.0 * 0.5 * sum;
172 }
173 }