Skip to content

Commit

Permalink
Merge pull request #939 from inteqam/vertex-count-bug-fix
Browse files Browse the repository at this point in the history
Fixes #896 : Fixed getVertexCount() method issue of giving output 0 for GROUP shapes
  • Loading branch information
SableRaf authored Feb 21, 2025
2 parents dcaa877 + d430099 commit 64dfa56
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
18 changes: 14 additions & 4 deletions core/src/processing/core/PShape.java
Original file line number Diff line number Diff line change
Expand Up @@ -2335,18 +2335,28 @@ protected void setPath(int vcount, float[][] verts, int ccount, int[] codes) {
}

/**
* The <b>getVertexCount()</b> method returns the number of vertices that
* make up a <b>PShape</b>. In the above example, the value 4 is returned by the
* The <b>getVertexCount()</b> 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 <b>PShape</b>. By default, it does not count child vertices for GROUP shapes. To include child vertices, pass <b>true</b> as a boolean parameter. In the above example, the value 4 is returned by the
* <b>getVertexCount()</b> method because 4 vertices are defined in
* <b>setup()</b>.
*
* @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)
*/
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 == GROUP || family == PRIMITIVE) {
if(family == GROUP || family == PRIMITIVE){
PGraphics.showWarning(NO_VERTICES_ERROR);
}
return vertexCount;
Expand Down
31 changes: 23 additions & 8 deletions core/src/processing/opengl/PShapeOpenGL.java
Original file line number Diff line number Diff line change
Expand Up @@ -1632,30 +1632,45 @@ 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() {
if (family == GROUP) return 0; // Group shapes don't have vertices
else {
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(true); // 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) {
// the input geometry of primitive and path shapes is built during
// tessellation
updateTessellation();
}
return inGeo.vertexCount;
count += inGeo.vertexCount;
}
}
return count;
}


Expand Down

0 comments on commit 64dfa56

Please sign in to comment.