Transpiled version (15644L) is out of date.
1 | sclass Test_CompressToInterpolatedDoubleArray { |
2 | settable int maxPillars = 3; |
3 | settable int minX =-10, maxX = 10; |
4 | settable int maxRunlength = 10; |
5 | settable int nTests = 1_000; |
6 | |
7 | // result |
8 | new AverageAndMinMax pixelErrors; |
9 | |
10 | int pixelErrors() { ret iround(pixelErrors.sum()); } |
11 | |
12 | run { |
13 | simpleTests(); |
14 | repeat nTests { multiSlopeTest(); } |
15 | printResults(); |
16 | } |
17 | |
18 | void simpleTests { |
19 | for (double slope = -1; slope <= 1; slope += 1/8.0) |
20 | singleSlopeTest(slope); |
21 | printResults(); |
22 | } |
23 | |
24 | void printResults { |
25 | print(); |
26 | printVars(+pixelErrors); |
27 | if (pixelErrors.average() == 0) |
28 | print("PERFECT!"); |
29 | else if (pixelErrors.average() <= 1) |
30 | print("A bit less than optimal, but only by 1 pixel"); |
31 | else |
32 | print("This is not good."); |
33 | } |
34 | |
35 | void singleSlopeTest(double slope) { |
36 | int n = 16; |
37 | var data = intArrayFromFunction(n, i -> iround(slope*i)); |
38 | testCompression(data, 2); |
39 | } |
40 | |
41 | void testCompression(int[] data, int maxPillarsAllowed) { |
42 | print(); |
43 | printStruct(+data); |
44 | var compressed = CompressToInterpolatedDoubleArray(data)!; |
45 | printStruct(+compressed); |
46 | var reconstructed = compressed!; |
47 | var reconstructed2 = iroundDoubleArray(reconstructed); |
48 | printStruct(+reconstructed); |
49 | |
50 | // Test reconstructed array |
51 | assertEquals(l(data), l(reconstructed2)); |
52 | for i over data: { |
53 | int diff = absDiff(data[i], reconstructed2[i]); |
54 | pixelErrors.add(diff); |
55 | if (diff != 0) { |
56 | print("pixel error " + diff + " at " + i + ": " + data[i] + "/" + reconstructed2[i]); |
57 | } |
58 | } |
59 | |
60 | // Test that we have achieved sufficient compression |
61 | assertLessThanOrEqualTo("pillars", maxPillarsAllowed, compressed.nPillars()); |
62 | } |
63 | |
64 | int randomX() { ret random_incl(minX, maxX); } |
65 | |
66 | void multiSlopeTest() { |
67 | int nPillars = random_incl(2, maxPillars); |
68 | new IntBuffer values; |
69 | new IntBuffer pillars; |
70 | |
71 | // first pillar |
72 | pillars.add(0); |
73 | values.add(randomX()); |
74 | |
75 | // remaining pillars |
76 | for (int iPillar = 1; iPillar < nPillars; iPillar++) { |
77 | int value = randomX(); |
78 | int runlength = random_incl(1, maxRunlength); |
79 | if (runlength > 1) { |
80 | int lastValue = last(values); |
81 | double slope = (value-lastValue)/(double) runlength; |
82 | double x = lastValue; |
83 | for (int i = 1; i < runlength; i++) |
84 | values.add(iround(x += slope)); |
85 | } |
86 | pillars.add(l(values)); |
87 | values.add(value); |
88 | } |
89 | print(+pillars); |
90 | int[] data = values.toArrayNonNull(); |
91 | testCompression(data, nPillars); |
92 | } |
93 | } |
Began life as a copy of #1035702
download show line numbers debug dex old transpilations
Travelled to 3 computer(s): elmgxqgtpvxh, mqqgnosmbjvj, wnsclhtenguj
No comments. add comment
Snippet ID: | #1035703 |
Snippet name: | Test_CompressToInterpolatedDoubleArray |
Eternal ID of this version: | #1035703/23 |
Text MD5: | 5e49a45aeb5e8bdc88e5400c93378613 |
Author: | stefan |
Category: | javax / geometric compression |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-07-11 20:10:34 |
Source code size: | 2670 bytes / 93 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 157 / 300 |
Version history: | 22 change(s) |
Referenced in: | [show references] |