Skip to content

Commit 64dfa56

Browse files
authored
Merge pull request #939 from inteqam/vertex-count-bug-fix
Fixes #896 : Fixed getVertexCount() method issue of giving output 0 for GROUP shapes
2 parents dcaa877 + d430099 commit 64dfa56

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

core/src/processing/core/PShape.java

+14-4
Original file line numberDiff line numberDiff line change
@@ -2335,18 +2335,28 @@ protected void setPath(int vcount, float[][] verts, int ccount, int[] codes) {
23352335
}
23362336

23372337
/**
2338-
* The <b>getVertexCount()</b> method returns the number of vertices that
2339-
* make up a <b>PShape</b>. In the above example, the value 4 is returned by the
2338+
* 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
2339+
* 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
23402340
* <b>getVertexCount()</b> method because 4 vertices are defined in
23412341
* <b>setup()</b>.
23422342
*
23432343
* @webref pshape:method
2344-
* @webBrief Returns the total number of vertices as an int
2344+
* @webBrief Returns the total number of vertices as an int with an option to count children Vertex for GROUP Shapes
23452345
* @see PShape#getVertex(int)
23462346
* @see PShape#setVertex(int, float, float)
23472347
*/
2348+
public int getVertexCount(boolean includeChildren) {
2349+
if(!includeChildren && family == GROUP){
2350+
PGraphics.showWarning(NO_VERTICES_ERROR);
2351+
}
2352+
else if (family == PRIMITIVE) {
2353+
PGraphics.showWarning(NO_VERTICES_ERROR);
2354+
}
2355+
return vertexCount;
2356+
}
2357+
23482358
public int getVertexCount() {
2349-
if (family == GROUP || family == PRIMITIVE) {
2359+
if(family == GROUP || family == PRIMITIVE){
23502360
PGraphics.showWarning(NO_VERTICES_ERROR);
23512361
}
23522362
return vertexCount;

core/src/processing/opengl/PShapeOpenGL.java

+23-8
Original file line numberDiff line numberDiff line change
@@ -1632,30 +1632,45 @@ protected void curveVertexImpl(float x, float y, float z) {
16321632

16331633
// Setters/getters of individual vertices
16341634

1635-
1635+
//for taking the default value as false , so user don't have to explicitly enter false
1636+
// if user don't want to include children vertex count
16361637
@Override
16371638
public int getVertexCount() {
1638-
if (family == GROUP) return 0; // Group shapes don't have vertices
1639-
else {
1639+
return getVertexCount(false); // Calls the main method with default false
1640+
}
1641+
@Override
1642+
public int getVertexCount(boolean includeChildren) {
1643+
int count = 0;
1644+
// If the shape is a group, recursively count the vertices of its children
1645+
if (family == GROUP) {
1646+
if(!includeChildren){
1647+
return 0;
1648+
}
1649+
// Iterate through all the child shapes and count their vertices
1650+
for (int i = 0; i < getChildCount(); i++) {
1651+
count += getChild(i).getVertexCount(true); // Recursive call to get the vertex count of child shapes
1652+
}
1653+
} else {
16401654
if (root.tessUpdate) {
16411655
if (root.tessKind == TRIANGLES) {
1642-
return lastPolyVertex - firstPolyVertex + 1;
1656+
count += lastPolyVertex - firstPolyVertex + 1;
16431657
} else if (root.tessKind == LINES) {
1644-
return lastLineVertex - firstLineVertex + 1;
1658+
count += lastLineVertex - firstLineVertex + 1;
16451659
} else if (root.tessKind == POINTS) {
1646-
return lastPointVertex - firstPointVertex + 1;
1660+
count += lastPointVertex - firstPointVertex + 1;
16471661
} else {
1648-
return 0;
1662+
count += 0; // Handle other cases
16491663
}
16501664
} else {
16511665
if (family == PRIMITIVE || family == PATH) {
16521666
// the input geometry of primitive and path shapes is built during
16531667
// tessellation
16541668
updateTessellation();
16551669
}
1656-
return inGeo.vertexCount;
1670+
count += inGeo.vertexCount;
16571671
}
16581672
}
1673+
return count;
16591674
}
16601675

16611676

0 commit comments

Comments
 (0)