From 46b8de48e560b1452eba9249babd622f5af856a5 Mon Sep 17 00:00:00 2001 From: Aditya Chaudhary Date: Sat, 1 Feb 2025 11:41:22 +0530 Subject: [PATCH 1/3] Fixed getVertexCount() method issue of giving output 0 for GROUP shape by Updating it in PShape & PShapeOpenGL class --- core/src/processing/core/PShape.java | 2 +- core/src/processing/opengl/PShapeOpenGL.java | 22 +++++++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/core/src/processing/core/PShape.java b/core/src/processing/core/PShape.java index 0538cb761d..da2af3a900 100644 --- a/core/src/processing/core/PShape.java +++ b/core/src/processing/core/PShape.java @@ -2346,7 +2346,7 @@ protected void setPath(int vcount, float[][] verts, int ccount, int[] codes) { * @see PShape#setVertex(int, float, float) */ public int getVertexCount() { - if (family == GROUP || family == PRIMITIVE) { + if (family == PRIMITIVE) { PGraphics.showWarning(NO_VERTICES_ERROR); } return vertexCount; diff --git a/core/src/processing/opengl/PShapeOpenGL.java b/core/src/processing/opengl/PShapeOpenGL.java index 810cbab708..c611b6afa3 100644 --- a/core/src/processing/opengl/PShapeOpenGL.java +++ b/core/src/processing/opengl/PShapeOpenGL.java @@ -1635,17 +1635,23 @@ protected void curveVertexImpl(float x, float y, float z) { @Override public int getVertexCount() { - if (family == GROUP) return 0; // Group shapes don't have vertices - else { + int count = 0; + // If the shape is a group, recursively count the vertices of its children + if (family == GROUP) { + // Iterate through all the child shapes and count their vertices + for (int i = 0; i < getChildCount(); i++) { + count += getChild(i).getVertexCount(); // Recursive call to get the vertex count of child shapes + } + } else { if (root.tessUpdate) { if (root.tessKind == TRIANGLES) { - return lastPolyVertex - firstPolyVertex + 1; + count += lastPolyVertex - firstPolyVertex + 1; } else if (root.tessKind == LINES) { - return lastLineVertex - firstLineVertex + 1; + count += lastLineVertex - firstLineVertex + 1; } else if (root.tessKind == POINTS) { - return lastPointVertex - firstPointVertex + 1; + count += lastPointVertex - firstPointVertex + 1; } else { - return 0; + count += 0; // Handle other cases } } else { if (family == PRIMITIVE || family == PATH) { @@ -1653,12 +1659,14 @@ public int getVertexCount() { // tessellation updateTessellation(); } - return inGeo.vertexCount; + count += inGeo.vertexCount; } } + return count; } + @Override public PVector getVertex(int index, PVector vec) { if (vec == null) vec = new PVector(); From 93d2f933791876d985566f613e0b0f40250b455c Mon Sep 17 00:00:00 2001 From: Aditya Chaudhary Date: Tue, 4 Feb 2025 01:11:09 +0530 Subject: [PATCH 2/3] Fixed issue : Updated the getVertexCount() to take optional boolean value to count children --- core/src/processing/core/PShape.java | 12 +++++++++++- core/src/processing/opengl/PShapeOpenGL.java | 13 ++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/core/src/processing/core/PShape.java b/core/src/processing/core/PShape.java index da2af3a900..27c66b4728 100644 --- a/core/src/processing/core/PShape.java +++ b/core/src/processing/core/PShape.java @@ -2345,8 +2345,18 @@ protected void setPath(int vcount, float[][] verts, int ccount, int[] codes) { * @see PShape#getVertex(int) * @see PShape#setVertex(int, float, float) */ + public int getVertexCount(boolean includeChildren) { + if(!includeChildren && family == GROUP){ + PGraphics.showWarning(NO_VERTICES_ERROR); + } + else if (family == PRIMITIVE) { + PGraphics.showWarning(NO_VERTICES_ERROR); + } + return vertexCount; + } + public int getVertexCount() { - if (family == PRIMITIVE) { + if(family == GROUP || family == PRIMITIVE){ PGraphics.showWarning(NO_VERTICES_ERROR); } return vertexCount; diff --git a/core/src/processing/opengl/PShapeOpenGL.java b/core/src/processing/opengl/PShapeOpenGL.java index c611b6afa3..f34031f1ac 100644 --- a/core/src/processing/opengl/PShapeOpenGL.java +++ b/core/src/processing/opengl/PShapeOpenGL.java @@ -1632,15 +1632,23 @@ protected void curveVertexImpl(float x, float y, float z) { // Setters/getters of individual vertices - + //for taking the default value as false , so user don't have to explicitly enter false + // if user don't want to include children vertex count @Override public int getVertexCount() { + return getVertexCount(false); // Calls the main method with default false + } + @Override + public int getVertexCount(boolean includeChildren) { int count = 0; // If the shape is a group, recursively count the vertices of its children if (family == GROUP) { + if(!includeChildren){ + return 0; + } // Iterate through all the child shapes and count their vertices for (int i = 0; i < getChildCount(); i++) { - count += getChild(i).getVertexCount(); // Recursive call to get the vertex count of child shapes + count += getChild(i).getVertexCount(true); // Recursive call to get the vertex count of child shapes } } else { if (root.tessUpdate) { @@ -1666,7 +1674,6 @@ public int getVertexCount() { } - @Override public PVector getVertex(int index, PVector vec) { if (vec == null) vec = new PVector(); From d4300995faccf8ef42ab6b5f544b74353539f7df Mon Sep 17 00:00:00 2001 From: Aditya Chaudhary Date: Tue, 4 Feb 2025 20:55:26 +0530 Subject: [PATCH 3/3] JavaDocs Updated for getVertexCount() in PShape class --- core/src/processing/core/PShape.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core/src/processing/core/PShape.java b/core/src/processing/core/PShape.java index 27c66b4728..09c5e07362 100644 --- a/core/src/processing/core/PShape.java +++ b/core/src/processing/core/PShape.java @@ -2335,13 +2335,13 @@ protected void setPath(int vcount, float[][] verts, int ccount, int[] codes) { } /** - * The getVertexCount() method returns the number of vertices that - * make up a PShape. In the above example, the value 4 is returned by the + * The getVertexCount() method returns the number of vertices (with an option to count children by passing true as boolean parameter to method call) that + * make up a PShape. By default, it does not count child vertices for GROUP shapes. To include child vertices, pass true as a boolean parameter. In the above example, the value 4 is returned by the * getVertexCount() method because 4 vertices are defined in * setup(). * * @webref pshape:method - * @webBrief Returns the total number of vertices as an int + * @webBrief Returns the total number of vertices as an int with an option to count children Vertex for GROUP Shapes * @see PShape#getVertex(int) * @see PShape#setVertex(int, float, float) */