processing4 icon indicating copy to clipboard operation
processing4 copied to clipboard

Expose a getter for PShape::vertices

Open HoloTheDrunk opened this issue 2 years ago • 1 comments

Description

The exposed PShape API could use some QoL changes.

It would be nice to have a method allowing easier access and iteration over PShape::vertices.

Expected Behavior

Returning a Stream of PVector mapped from the PShape::vertices array using similar logic as PShape::getVertex would probably be a nice clean way of doing it.

Current Behavior

Currently the only way to access the PShape::vertices array is through PShape::getVertex and similar methods.

HoloTheDrunk avatar Feb 18 '23 19:02 HoloTheDrunk

This works well for me:

for (int i = 0; i < shape.getVertexCount(); i++) {
	PVector v = shape.getVertex(i);
}

It should be easy to write a little helper function to manage a developer-defined data structure if you want to keep track of vertices in a custom way. Here's a complete example of what you're looking for.

Though now looking at PShape.getChildren(), I could see there being a PShape.getVertices() too :)

import java.util.*;
import java.util.stream.*;

PShape shape;
  
public void setup() {
  size(640, 360, P3D);
  shape = createShape(BOX, 100, 100, 100);
  printVerts();
}

public Stream<PVector> vertexStreamForShape(PShape s) {
  int numVerts = s.getVertexCount();
  Stream.Builder<PVector> builder = Stream.builder();
  for (int i = 0; i < numVerts; i++) {
    PVector v = s.getVertex(i);
    builder.add(v);
  }
  return builder.build();
}

public void printVerts() {
  Stream<PVector> vertices = vertexStreamForShape(shape);
  Iterator<PVector> it = vertices.iterator();
  while (it.hasNext()) {
    println(it.next() + " ");
  }
}

public void draw() {
  background(0);
  lights();
  
  translate(width/2, height/2, 0);
  rotateY(frameCount * 0.01);
  shape(shape);
}

cacheflowe avatar Feb 23 '23 16:02 cacheflowe