From e4dbc6fd9c64355e4ac95a6866652847fce9196d Mon Sep 17 00:00:00 2001 From: codeanticode Date: Wed, 11 Apr 2012 22:27:21 +0000 Subject: [PATCH] Removed center() method, added getCenter(), updated examples --- core/src/processing/core/PShape.java | 5 + .../examples/Advanced/Particles/Particle.pde | 3 +- .../DynamicParticlesRetained.pde | 3 +- .../processing/opengl/PGraphicsOpenGL.java | 132 +---------------- .../src/processing/opengl/PShape3D.java | 140 +++++++----------- 5 files changed, 67 insertions(+), 216 deletions(-) diff --git a/core/src/processing/core/PShape.java b/core/src/processing/core/PShape.java index d83166d6d..a712f3e69 100644 --- a/core/src/processing/core/PShape.java +++ b/core/src/processing/core/PShape.java @@ -393,6 +393,11 @@ public class PShape implements PConstants { } + public PVector getCenter() { + return new PVector(); + } + + /** * Return true if this shape is 3D. Defaults to false. */ diff --git a/java/libraries/opengl/examples/Advanced/Particles/Particle.pde b/java/libraries/opengl/examples/Advanced/Particles/Particle.pde index e3ed12573..05b2974dc 100644 --- a/java/libraries/opengl/examples/Advanced/Particles/Particle.pde +++ b/java/libraries/opengl/examples/Advanced/Particles/Particle.pde @@ -36,7 +36,8 @@ class Particle { velocity = new PVector(cos(a), sin(a)); velocity.mult(speed); lifespan = 255; - part.center(x,y); + PVector center = part.getCenter(); + part.translate(x - center.x, y - center.y); } boolean isDead() { diff --git a/java/libraries/opengl/examples/Performance/DynamicParticlesRetained/DynamicParticlesRetained.pde b/java/libraries/opengl/examples/Performance/DynamicParticlesRetained/DynamicParticlesRetained.pde index 42f8f659e..0b7f81f2e 100644 --- a/java/libraries/opengl/examples/Performance/DynamicParticlesRetained/DynamicParticlesRetained.pde +++ b/java/libraries/opengl/examples/Performance/DynamicParticlesRetained/DynamicParticlesRetained.pde @@ -62,7 +62,8 @@ void draw () { if (lifetimes[n] == 0) { // Re-spawn dead particle - part.center(mouseX, mouseY); + PVector center = part.getCenter(); + part.translate(mouseX - center.x, mouseY - center.y); float angle = random(0, TWO_PI); float s = random(0.5 * speed, 0.5 * speed); velocities[n].x = s * cos(angle); diff --git a/java/libraries/opengl/src/processing/opengl/PGraphicsOpenGL.java b/java/libraries/opengl/src/processing/opengl/PGraphicsOpenGL.java index dc070a5a4..2dd570381 100644 --- a/java/libraries/opengl/src/processing/opengl/PGraphicsOpenGL.java +++ b/java/libraries/opengl/src/processing/opengl/PGraphicsOpenGL.java @@ -1938,7 +1938,7 @@ public class PGraphicsOpenGL extends PGraphics { } else if (type == TRIANGLE_STRIP) { shape = new PShape3D(parent, PShape.GEOMETRY); shape.setKind(TRIANGLE_STRIP); - } else if (type == QUADS || type == QUADS) { + } else if (type == QUAD || type == QUADS) { shape = new PShape3D(parent, PShape.GEOMETRY); shape.setKind(QUADS); } else if (type == QUAD_STRIP) { @@ -7862,135 +7862,7 @@ public class PGraphicsOpenGL extends PGraphics { return newSize; } - public void center(float cx, float cy) { - int index; - - // Computing current center - float cx0 = 0; - float cy0 = 0; - for (int i = 0; i < fillVertexCount; i++) { - index = 3 * i; - cx0 += fillVertices[index++]; - cy0 += fillVertices[index ]; - } - for (int i = 0; i < lineVertexCount; i++) { - index = 3 * i; - cx0 += lineVertices[index++]; - cy0 += lineVertices[index ]; - } - for (int i = 0; i < pointVertexCount; i++) { - index = 3 * i; - cx0 += pointVertices[index++]; - cy0 += pointVertices[index ]; - } - int nt = fillVertexCount + lineVertexCount + pointVertexCount; - if (0 < nt) { - cx0 /= nt; - cy0 /= nt; - } - - float tx = cx - cx0; - float ty = cy - cy0; - - if (0 < fillVertexCount) { - for (int i = 0; i < fillVertexCount; i++) { - index = 3 * i; - fillVertices[index++] += tx; - fillVertices[index ] += ty; - } - } - - if (0 < lineVertexCount) { - for (int i = 0; i < lineVertexCount; i++) { - index = 3 * i; - lineVertices[index++] += tx; - lineVertices[index ] += ty; - - index = 4 * i; - lineDirWidths[index++] += tx; - lineDirWidths[index ] += ty; - } - } - - if (0 < pointVertexCount) { - for (int i = 0; i < pointVertexCount; i++) { - index = 3 * i; - pointVertices[index++] += tx; - pointVertices[index ] += ty; - } - } - } - - public void center(float cx, float cy, float cz) { - int index; - - // Computing current center - float cx0 = 0; - float cy0 = 0; - float cz0 = 0; - for (int i = 0; i < fillVertexCount; i++) { - index = 3 * i; - cx0 += fillVertices[index++]; - cy0 += fillVertices[index++]; - cz0 += fillVertices[index ]; - } - for (int i = 0; i < lineVertexCount; i++) { - index = 3 * i; - cx0 += lineVertices[index++]; - cy0 += lineVertices[index++]; - cz0 += lineVertices[index ]; - } - for (int i = 0; i < pointVertexCount; i++) { - index = 3 * i; - cx0 += pointVertices[index++]; - cy0 += pointVertices[index++]; - cz0 += pointVertices[index ]; - } - int nt = fillVertexCount + lineVertexCount + pointVertexCount; - if (0 < nt) { - cx0 /= nt; - cy0 /= nt; - cz0 /= nt; - } - - float tx = cx - cx0; - float ty = cy - cy0; - float tz = cz - cz0; - - if (0 < fillVertexCount) { - for (int i = 0; i < fillVertexCount; i++) { - index = 3 * i; - fillVertices[index++] += tx; - fillVertices[index++] += ty; - fillVertices[index ] += tz; - } - } - - if (0 < lineVertexCount) { - for (int i = 0; i < lineVertexCount; i++) { - index = 3 * i; - lineVertices[index++] += tx; - lineVertices[index++] += ty; - lineVertices[index ] += tz; - - index = 4 * i; - lineDirWidths[index++] += tx; - lineDirWidths[index++] += ty; - lineDirWidths[index ] += tz; - } - } - - if (0 < pointVertexCount) { - for (int i = 0; i < pointVertexCount; i++) { - index = 3 * i; - pointVertices[index++] += tx; - pointVertices[index++] += ty; - pointVertices[index ] += tz; - } - } - } - - public int getCenter(PVector v) { + public int getVertexSum(PVector v) { int index; for (int i = 0; i < fillVertexCount; i++) { index = 3 * i; diff --git a/java/libraries/opengl/src/processing/opengl/PShape3D.java b/java/libraries/opengl/src/processing/opengl/PShape3D.java index e99f60bd0..63937c41d 100644 --- a/java/libraries/opengl/src/processing/opengl/PShape3D.java +++ b/java/libraries/opengl/src/processing/opengl/PShape3D.java @@ -467,7 +467,62 @@ public class PShape3D extends PShape { pg.finalizeVertexBufferObject(glPointIndexBufferID); } } - + + + /////////////////////////////////////////////////////////// + + // + + // Query methods + + + public float getWidth() { + + return width; + } + + + public float getHeight() { + + return height; + } + + + public float getDepth() { + return depth; + } + + + public PVector getCenter() { + PVector center = new PVector(); + int count = 0; + if (shapeEnded) { + updateTesselation(); + + count = getVertexSum(center, count); + if (0 < count) { + center.x /= count; + center.y /= count; + center.z /= count; + } + } + return center; + } + + + protected int getVertexSum(PVector sum, int count) { + if (family == GROUP) { + for (int i = 0; i < childCount; i++) { + PShape3D child = (PShape3D) children[i]; + count += child.getVertexSum(sum, count); + } + } else { + count += tess.getVertexSum(sum); + } + return count; + } + + /////////////////////////////////////////////////////////// // @@ -1355,89 +1410,6 @@ public class PShape3D extends PShape { // Geometric transformations - //------------------------------------------------------------------------------ - - // TODO: Remove center, add function to get center from point... - public void center(float cx, float cy) { - if (shapeEnded) { - updateTesselation(); - - if (family == GROUP) { - PVector center = new PVector(); - - int count = updateCenter(center, 0); - center.x /= count; - center.y /= count; - - float tx = cx - center.x; - float ty = cy - center.y; - - setChildHasMatrix(true); - applyMatrix = true; - super.translate(tx, ty); - } else { - PVector vec = new PVector(); - int count = tess.getCenter(vec); - vec.x /= count; - vec.y /= count; - - float tx = cx - vec.x; - float ty = cy - vec.y; - - translate(tx, ty); - } - } - } - - - public void center(float cx, float cy, float cz) { - if (shapeEnded) { - updateTesselation(); - - if (family == GROUP) { - PVector center0 = new PVector(); - - int count = updateCenter(center0, 0); - center0.x /= count; - center0.y /= count; - center0.z /= count; - - float tx = cx - center0.x; - float ty = cy - center0.y; - float tz = cz - center0.z; - - setChildHasMatrix(true); - applyMatrix = true; - super.translate(tx, ty, tz); - } else { - PVector vec = new PVector(); - int count = tess.getCenter(vec); - vec.x /= count; - vec.y /= count; - vec.z /= count; - - float tx = cx - vec.x; - float ty = cy - vec.y; - float tz = cz - vec.z; - - translate(tx, ty, tz); - } - } - } - - - protected int updateCenter(PVector vec, int count) { - if (family == GROUP) { - count = updateCenter(vec, count); - } else { - count += tess.getCenter(vec); - } - return count; - } - //------------------------------------------------------------------------------ - - - public void translate(float tx, float ty) { transform(TRANSLATE, tx, ty); }