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

105
LINES

< > BotCompany Repo | #1035709 // VectorSSI

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

Libraryless. Click here for Pure Java version (15428L/89K).

// VectorSSI - SSI with straight line segments

persistable sclass VectorSSI extends AbstractSSI {
  replace X with short.
  replace Y with short.
  replace toX with toShort_enforce.
  replace toY with toShort_enforce.
  replace newXArray with newShortArrayOrNull.
  
  // core values (mandatory): first column and row occupied
  settable X x1;
  settable Y y1;
  
  // Curves of x start and x end, relative to (x1, y1)
  // Both have same length
  settable InterpolatedDoubleArray leftCurve;
  settable InterpolatedDoubleArray rightCurve;
  
  *(SSI ssi) {
    ssi.copyAbstractSSI(this);
    Rect r = ssi.bounds();
    x1(toX(r.x1()));
    y1(toY(r.y1()));
    int h = r.h();
    int[] left = new int[h], right = new int[h];
    for y to h: {
      left[y] = ssi.getLeft(y1+y)-x1;
      right[y] = ssi.getRight(y1+y)-x1;
    }
    leftCurve(CompressToInterpolatedDoubleArray(left)!);
    rightCurve(CompressToInterpolatedDoubleArray(right)!);
  }
  
  public int height aka h() {
    ret leftCurve.length();
  }
  
  int y2() {
    ret y1+h();
  }
  
  SSI toSSI aka get() {
    int h = h();
    var ssi = new SSI(y1, y2());
    copyAbstractSSI(ssi);
    int[] left = leftCurve.rounded();
    int[] right = rightCurve.rounded();
    for y to h:
      ssi.set(y1+y, x1+left[y], x1+right[y]);
    ret ssi;
  }
  
  public void drawOn(Graphics2D g) {
    toSSI().drawOn(g);
  }
  
  // How many points define the shape of this vector SSI
  // (left + right border)
  int nPoints() {
    ret leftCurve.nPillars() + rightCurve.nPillars();
  }
  
  public long sizeInInts() {
    // "v", color, x1, y1, height(), leftCurve, rightCurve
    ret 5+leftCurve.nInts/*_withoutFirstAndLastIndex*/()+rightCurve.nInts/*_withoutFirstAndLastIndex*/();
  }
  
  // TODO
  /*VectorSSI reduceToInts(int ints) {
    ints -= 5; // constant part
    int nPillars = ints/2; // max pillars we can afford (left+right)
  }*/
  
  // optimizable
  public simplyCached Rect bounds() {
    ret toSSI().bounds();
  }
  
  public simplyCached int numberOfPixels() {
    ret toSSI().numberOfPixels();
  }
  
  public void readWrite(StringHead head) {
    head.exchangeLine(l0 toLine, l1 fromLine);
  }
  
  S toLine() {
    L elements = ll("v", colorToString(), x1, y1, height());
    addAll(elements, asList(iroundAll(leftCurve.indicesAndValues/*_withoutFirstAndLastIndex*/())));
    addAll(elements, asList(iroundAll(rightCurve.indicesAndValues/*_withoutFirstAndLastIndex*/())));
    ret spaceCombine(elements);
  }
  
  void fromLine(S line) {
    Iterator<S> it = splitAtSpaceIterator(line);
    assertEquals("v", nextFromIterator(it));
    x1 = toX(parseInt(nextFromIterator(it)));
    y1 = toY(parseInt(nextFromIterator(it)));
    // TODO
    /*int h = parseInt(nextFromIterator(it));
    init(y1, y1+h);
    for i over data:
      data[i] = toX(parseInt(nextFromIterator(it)));*/
  }
}

Author comment

Began life as a copy of #1035561

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): elmgxqgtpvxh, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035709
Snippet name: VectorSSI
Eternal ID of this version: #1035709/34
Text MD5: 4ff44a92e94995758951e00fbace2aa4
Transpilation MD5: d6a12154ab80c86366ff759fa7c5ccc5
Author: stefan
Category: javax / imaging
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-07-25 14:41:17
Source code size: 2964 bytes / 105 lines
Pitched / IR pitched: No / No
Views / Downloads: 155 / 335
Version history: 33 change(s)
Referenced in: [show references]