We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProcessing DevelopmentCore,  Processing Development Environment (PDE) › Access to internal fields in core classes
Page Index Toggle Pages: 1
Access to internal fields in core classes (Read 4939 times)
Access to internal fields in core classes
Jul 5th, 2009, 11:49pm
 
When working with Processing as a Java framework it is sometimes frustrating that the core classes are so good at hiding their internal fields. I realize that this is good programming practice and that these fields might just be confusing to beginners who aren't familiar with the details of OOP programming.

An example: I am currently writing an application where I need to extract shape details from a SVG file. PShape reads the file just fine, but I can't get access to the vertices and shape codes I need to recreate the paths in a different form.

Since I'm working in Eclipse I can easily just go into processing.core.PShape and make all the fields public. But it doesn't seem logical to me that I have to do such a kludgy hack, when there are plenty of legitimate reasons why one might want access to the internal fields of PShape.

Any thoughts?
Re: Access to internal fields in core classes
Reply #1 - Jul 6th, 2009, 1:04am
 
If I am not mistaken (haven't tried it yet), some libraries like Geomerative provide much more control on SVG data.
Re: Access to internal fields in core classes
Reply #2 - Jul 6th, 2009, 1:47am
 
Hi,

most fileds in PShape are protected. That means, they are accessible to classes that extend PShape. So if you would restructure your programm in a way that your own Shape class extends PShapeSVG, you could probably access the fields you need without hacking the lib.

hey looking at the source, I found this comment by Fry:

"For the time being, this class and its shape() and loadShape() friends in
PApplet exist as placeholders for more exciting things to come. If you'd
like to work with this class, make a subclass (see how PShapeSVG works)
and you can play with its internal methods all you like."

here an example:

Code:

import processing.core.PApplet;
import processing.core.PShapeSVG;

public class ShapeTest extends PApplet {
   MyShape myShape;
   
   public void setup() {
       myShape = new MyShape(this, "test.svg");
       // now you can access protected fileds
       println(myShape.getVertexCount());
   }
   
   public void draw() {
       background(0);
       shape(myShape, 0, 0);
   }
}

// MyShape helper class extends PShapeSVG
class MyShape extends PShapeSVG {
   
   MyShape(PApplet parent, String filename) {
       super(parent, filename);
   }
   
   // add your own PUBLIC getter ans setter methods
   // to access protected fields
   public int getVertexCount() {        
       return vertexCount; // vertexCount is protected in PShapeSVG
   }

}


Re: Access to internal fields in core classes
Reply #3 - Jul 6th, 2009, 9:38am
 
Alvaro: The problem with protected fields in PShape is that PShape uses a tree-structure with children, mirroring the DOM of the SVG file. So your proposal won't work since the child objects I'm interested in are themselves PShape instances with protected fields. To get around this issue I've extended PShapeSVG with a replacement class that copies out the data, but it seems a little excessive when all I want is access to the vertex data.

I realize that it's the recommended practice to protect internal data fields. But it does make it hard to tinker with more obscure features hidden inside the core classes, particularly if one is working in the Processing IDE instead of Eclipse.

Having said that, I realize that PShape is a work-in-progress and I look forward to seeing what it will become in due time..
Re: Access to internal fields in core classes
Reply #4 - Jul 6th, 2009, 12:45pm
 
Hi,

I would like to know if you have tried using Geomerative, since this is the kind of use cases I have made it for.
If so and you are unsatisfied, could you please tell me the reason? I will try to fix it.

This is how you would access vertices:


RG.init(this);
RShape shp = RG.loadShape("myDrawing.svg");
RPoint[] vertices = shp.getPoints();


And you would even have different ways where the vertices on the shape will be evaluated: separated uniformly, adaptatively or at a uniform advancement (using RG.setPolygonizer() and others).

Hope it can help
ricard

EDIT: fix the name of the method to change the place where vertices are evaluated.
Re: Access to internal fields in core classes
Reply #5 - Jul 7th, 2009, 4:55am
 
Hi Ricard,

I haven't actually tried Geomerative, I've been trying not to rely too much on external libraries since I already use quite a few. Every so often a Processing update will break the libraries, and I don't always have the time to wait for them to update.

In any case I have solved the current problem, I started this thread more as a general suggestion regarding the Processing core.

However, I'm now running into the issue of beginShape() and cut-out polygons, which works in JAVA2D but not OPENGL. Does Geomerative solve that?
Re: Access to internal fields in core classes
Reply #6 - Jul 7th, 2009, 1:47pm
 
Yep, I understand you.   I don't like either to be too dependent on libraries in case they break with Processing updates.  I can tell you however, that with the new version of Geomerative at http://www.ricardmarxer.com/geomerative I have been very active in maintaining it up to date with Processing and any bugs have been usually fixed in a matter of days.  I hope I can keep up like that.

About the issue of shapes with holes, this was one of my main goals of Geomerative since in typography shapes this is very common.  So yes, Geomerative supports rendering correctly shapes with holes.

It uses the breakShape() undocumented command of Processing in the backends that support it (JAVA2D and PDF) and it uses an internal software based tesselator for backends that don't support breakShape() (OPENGL, P3D, etc...).

So for the user it is transparent, you can create shapes using a very similar syntax as in Processing:
Code:

RG.beginShape();
RG.vertex();
RG.bezierVertex();

// and even to have several paths to form a shape (e.g. holes)
RG.breakShape();
// or
RG.breakShape(CLOSE);

RG.endShape();
// or
RShape shp = RG.getShape()

You just have to keep in mind that holes must be counter clockwise.

When you load SVG or TTF the holes in the shapes are respected.
There are quite a few Tutorial sketches with Geomerative that shows all this.  Let me know if any of it is not clear or if there are any bugs so that I can fix them.

ricard
Re: Access to internal fields in core classes
Reply #7 - Jul 7th, 2009, 2:30pm
 
That sounds very useful, looking at the Javadoc it looks like you've done most of what I need. Thanks for the headsup!

ll have a quick try at integrating it into my project and see how hard it will be to switch. Unfortunately I'm working on a time-sensitive project and only have this week, so I'm trying to avoid wasting time on code experimentation.
Re: Access to internal fields in core classes
Reply #8 - Jul 7th, 2009, 4:06pm
 
Ricard, I've posted a followup thread about Geomerative here to not confuse this thread further:

http://processing.org/discourse/yabb2/num_1247007952.html#0

Thanks again for suggesting your library!
Re: Access to internal fields in core classes
Reply #9 - Aug 13th, 2009, 5:51am
 
To answer the initial question, when an API is finished, then more of the fields are made public, or accessor methods are added. PShape wasn't at that point when 1.0 was released, and its internal structure will change as I finish up proper support for working with vertex data. This takes a while because it has to work across the different renderers (e.g. in OpenGL you want a PShape to cache itself as a VBO or something along those lines).

The comment in the code is correct, however, you can subclass to get access to all of the methods you'd like. I do this on a regular basis for other projects.

And in the meantime, I definitely recommend Ricard's library if you're actually working with SVG data. PShape just doesn't do that currently.
Page Index Toggle Pages: 1