Debian Codegraph @

src/com/github/weisj/jsvg/animation/interpolation/DefaultInterpolator.java

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /*
2 * MIT License
3 *
4 * Copyright (c) 2024-2025 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.animation.interpolation;
23
24 import static com.github.weisj.jsvg.geometry.util.GeometryUtil.lerp;
25
26 import java.awt.*;
27 import java.awt.geom.AffineTransform;
28
29 import org.jetbrains.annotations.NotNull;
30 import org.jetbrains.annotations.Nullable;
31
32 import com.github.weisj.jsvg.animation.Additive;
33 import com.github.weisj.jsvg.animation.AnimationValuesType;
34 import com.github.weisj.jsvg.attributes.transform.TransformPart;
35 import com.github.weisj.jsvg.attributes.value.TransformValue;
36 import com.github.weisj.jsvg.geometry.util.GeometryUtil;
37 import com.github.weisj.jsvg.paint.SVGPaint;
38 import com.github.weisj.jsvg.paint.SimplePaintSVGPaint;
39 import com.github.weisj.jsvg.paint.impl.AwtSVGPaint;
40 import com.github.weisj.jsvg.paint.impl.RGBColor;
41 import com.github.weisj.jsvg.renderer.MeasureContext;
42
43 public final class DefaultInterpolator
44 implements FloatInterpolator, FloatListInterpolator, PaintInterpolator, TransformInterpolator {
45 private final Additive additive;
46 private final AnimationValuesType valuesType;
47
48 public DefaultInterpolator(AnimationValuesType valuesType, Additive additive) {
49 this.valuesType = valuesType;
50 this.additive = additive;
51 }
52
53 @Override
54 public float interpolate(float initial, float a, float b, float progress) {
55 switch (valuesType) {
56 case FROM_BY: {
57 return a + b * progress;
58 }
59 case BY: {
60 return initial + b * progress;
61 }
62 case TO: {
63 return initial + (b - initial) * progress;
64 }
65 case FROM_TO:
66 case VALUES:
67 default: {
68 float result = a + (b - a) * progress;
69
70 if (additive == Additive.SUM) {
71 result += initial;
72 }
73 return result;
74 }
75 }
76 }
77
78 private static float @NotNull [] ensureCacheCapacity(float @Nullable [] cache, int length) {
79 if (cache == null || cache.length != length) {
80 return new float[length];
81 }
82 return cache;
83 }
84
85 private static float @NotNull [] arrayLerp(float @Nullable [] cache, float @NotNull [] from, float @Nullable [] to,
86 float progress) {
87 float t = progress;
88
89 boolean isEndTime = GeometryUtil.approximatelyEqual(t, 1);
90 if (to == null || (from.length != to.length && !isEndTime)) {
91 // Use discrete animation
92 t = 0;
93 }
94
95 float[] result = cache;
96 if (to == null || GeometryUtil.approximatelyEqual(t, 0)) {
97 result = ensureCacheCapacity(result, from.length);
98 System.arraycopy(from, 0, result, 0, from.length);
99 } else if (isEndTime) {
100 result = ensureCacheCapacity(result, to.length);
101 System.arraycopy(to, 0, result, 0, to.length);
102 } else {
103 result = ensureCacheCapacity(result, from.length);
104 for (int i = 0; i < from.length; i++) {
105 result[i] = lerp(t, from[i], to[i]);
106 }
107 }
108 return result;
109 }
110
111 /**
112 * This method is used to calculate the result of the following operation: result_i = a * x_i + b_i
113 * <ul>
114 * <li> If b_i is shorter than a_i, b_i is repeated.
115 * <li> If b_i is longer than a_i, the extra values are ignored.
116 * </ul>
117 */
118 private static float @NotNull [] saxpy(float @Nullable [] cache, float @NotNull [] b, float @Nullable [] x,
119 float a) {
120 float[] result = ensureCacheCapacity(cache, b.length);
121 System.arraycopy(b, 0, result, 0, b.length);
122
123 if (x == null) return b;
124
125 int n = Math.min(result.length, x.length);
126 for (int i = 0; i < n; i++) {
127 result[i] += a * x[i];
128 }
129 for (int i = n; i < result.length; i++) {
130 result[i] += a * x[i % n];
131 }
132 return result;
133 }
134
135 @Override
136 public float @NotNull [] interpolate(float @NotNull [] initial, float @NotNull [] a, float @Nullable [] b,
137 float progress, float @Nullable [] cache) {
138 switch (valuesType) {
139 case FROM_BY: {
140 return saxpy(cache, a, b, progress);
141 }
142 case BY: {
143 return saxpy(cache, initial, b, progress);
144 }
145 case TO: {
146 return arrayLerp(cache, initial, b, progress);
147 }
148 case FROM_TO:
149 case VALUES:
150 default: {
151 float[] result = arrayLerp(cache, a, b, progress);
152
153 if (additive == Additive.SUM) {
154 result = saxpy(result, result, initial, 1);
155 }
156 return result;
157 }
158 }
159 }
160
161 private static @Nullable RGBColor extractColor(@NotNull SVGPaint p) {
162 if (!(p instanceof SimplePaintSVGPaint)) return null;
163 Paint paint = ((SimplePaintSVGPaint) p).paint();
164 if (paint instanceof Color) return new RGBColor((Color) paint);
165 if (paint instanceof RGBColor) return (RGBColor) paint;
166 return null;
167 }
168
169 @Override
170 public @NotNull SVGPaint interpolate(@NotNull SVGPaint initial, @NotNull SVGPaint a, @NotNull SVGPaint b,
171 float progress) {
172 RGBColor colorA = extractColor(a);
173 RGBColor colorB = extractColor(b);
174
175 if (colorA == null || colorB == null) {
176 return discreteAnimation(initial, a, b, progress);
177 }
178
179 switch (valuesType) {
180 case FROM_BY: {
181 return new AwtSVGPaint(RGBColor.saxpy(progress, colorA, colorB));
182 }
183 case BY: {
184 RGBColor initialColor = extractColor(initial);
185 if (initialColor == null) return initial;
186 return new AwtSVGPaint(RGBColor.saxpy(progress, initialColor, colorB));
187 }
188 case TO: {
189 RGBColor initialColor = extractColor(initial);
190 if (initialColor == null) return initial;
191 return new AwtSVGPaint(RGBColor.interpolate(progress, initialColor, colorB));
192 }
193 case FROM_TO:
194 case VALUES:
195 default: {
196 RGBColor result = RGBColor.interpolate(progress, colorA, colorB);
197
198 if (additive == Additive.SUM) {
199 RGBColor initialColor = extractColor(initial);
200 if (initialColor == null) return initial;
201 result = RGBColor.add(initialColor, result);
202 }
203 return new AwtSVGPaint(result);
204 }
205 }
206 }
207
208 private SVGPaint discreteAnimation(@NotNull SVGPaint initial, @NotNull SVGPaint a, @NotNull SVGPaint b,
209 float progress) {
210 if (additive != Additive.REPLACE) {
211 return initial;
212 }
213 return GeometryUtil.approximatelyEqual(progress, 1) ? b : a;
214 }
215
216 @Override
217 public @NotNull AffineTransform interpolate(@NotNull MeasureContext context, @NotNull TransformValue initial,
218 @NotNull TransformPart a, @NotNull TransformPart b, float progress) {
219 switch (valuesType) {
220 case FROM_BY: {
221 return b.applyToTransform(a.applyToTransform(new AffineTransform(), context), context, progress);
222 }
223 case BY: {
224 return b.applyToTransform(initial.get(context), context, progress);
225 }
226 case TO: {
227 // NOTE: This is undefined by the specification
228 return GeometryUtil.interpolate(initial.get(context), b.toTransform(context), progress);
229 }
230 case FROM_TO:
231 case VALUES:
232 default: {
233 AffineTransform result = TransformPart.interpolate(a, b, context, progress);
234
235 if (additive == Additive.SUM) {
236 result.preConcatenate(initial.get(context));
237 }
238 return result;
239 }
240 }
241 }
242 }