Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

93
LINES

< > BotCompany Repo | #1035703 // Test_CompressToInterpolatedDoubleArray

JavaX fragment (include) [tags: use-pretranspiled]

Transpiled version (15644L) is out of date.

sclass Test_CompressToInterpolatedDoubleArray {
  settable int maxPillars = 3;
  settable int minX =-10, maxX = 10;
  settable int maxRunlength = 10;
  settable int nTests = 1_000;
  
  // result
  new AverageAndMinMax pixelErrors;
  
  int pixelErrors() { ret iround(pixelErrors.sum()); }
  
  run {
    simpleTests();
    repeat nTests { multiSlopeTest(); }
    printResults();
  }
  
  void simpleTests {
    for (double slope = -1; slope <= 1; slope += 1/8.0)
      singleSlopeTest(slope);
    printResults();
  }
  
  void printResults {
    print();
    printVars(+pixelErrors);
    if (pixelErrors.average() == 0)
      print("PERFECT!");
    else if (pixelErrors.average() <= 1)
      print("A bit less than optimal, but only by 1 pixel");
    else
      print("This is not good.");
  }
  
  void singleSlopeTest(double slope) {
    int n = 16;
    var data = intArrayFromFunction(n, i -> iround(slope*i));
    testCompression(data, 2);
  }
  
  void testCompression(int[] data, int maxPillarsAllowed) {
    print();
    printStruct(+data);
    var compressed = CompressToInterpolatedDoubleArray(data)!;
    printStruct(+compressed);
    var reconstructed = compressed!;
    var reconstructed2 = iroundDoubleArray(reconstructed);
    printStruct(+reconstructed);
    
    // Test reconstructed array
    assertEquals(l(data), l(reconstructed2));
    for i over data: {
      int diff = absDiff(data[i], reconstructed2[i]);
      pixelErrors.add(diff);
      if (diff != 0) {
        print("pixel error " + diff + " at " + i + ": " + data[i] + "/" + reconstructed2[i]);
      }
    }
    
    // Test that we have achieved sufficient compression
    assertLessThanOrEqualTo("pillars", maxPillarsAllowed, compressed.nPillars());
  }
  
  int randomX() { ret random_incl(minX, maxX); }
  
  void multiSlopeTest() {
    int nPillars = random_incl(2, maxPillars);
    new IntBuffer values;
    new IntBuffer pillars;
    
    // first pillar
    pillars.add(0);
    values.add(randomX());
    
    // remaining pillars
    for (int iPillar = 1; iPillar < nPillars; iPillar++) {
      int value = randomX();
      int runlength = random_incl(1, maxRunlength);
      if (runlength > 1) {
        int lastValue = last(values);
        double slope = (value-lastValue)/(double) runlength;
        double x = lastValue;
        for (int i = 1; i < runlength; i++)
          values.add(iround(x += slope));
      }
      pillars.add(l(values));
      values.add(value);
    }
    print(+pillars);
    int[] data = values.toArrayNonNull();
    testCompression(data, nPillars);
  }
}

Author comment

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: 155 / 296
Version history: 22 change(s)
Referenced in: [show references]