Loading...
Logo
Processing Forum
Hi guys,

was hoping someone might be able to help me out with a toxiclibs related problem. I have a TriangleMesh that I'm building around points but it seems to be doing something a bit strange....when I instruct the mesh to build it doesn't do it immediately as with most examples I have seen. Instead it grows slowly and slows the sketch down considerable. I also have problems with it running out of memory after a while.

Is this something to do with how I have it set up? I would really appreciate someone having a look for me. I have posted part of the sketch containing the mesh below in case anyone can spot any glaring errors.

There is a video of an early experiment on my vimeo page which shows what I mean when I describe the mesh as 'growing' (vimeo.com/33283368).

Thanks,

Phill


Copy code
  1. Mesh threadMesh;
  2. ToxiclibsSupport gfx;

  3. void setup() {

  4.  gfx = new ToxiclibsSupport(this);
  5.   threadMesh = new Mesh(15, 100);

  6. }

  7. void draw() {

  8. threadMesh.drawMesh(meshPartList);

  9. }


  10. class Mesh extends TriangleMesh {

  11.   /*------------------------------------------------------------------
  12.    *** CLASS VARIABLES ***
  13.    ------------------------------------------------------------------*/

  14.   float density = 1;
  15.   int meshDens = 300;

  16.   ArrayList meshPartList;

  17.   Vec3D SCALE = new Vec3D(bBox.x, bBox.y, bBox.z).scale(2);

  18.   float isoThreshold;
  19.   float brushRadius = 20;
  20.   int brushSize;

  21.   VolumetricSpace volume;
  22.   VolumetricBrush brush;
  23.   IsoSurface surface;

  24.   /*------------------------------------------------------------------
  25.    *** CLASS CONSTRUCTOR ***
  26.    ------------------------------------------------------------------*/

  27.   Mesh(int brushSize_, float isoThreshold_) {


  28.     brushSize = brushSize_;
  29.     isoThreshold = isoThreshold_;
  30.     volume = new VolumetricSpaceArray(SCALE, meshDens, meshDens, meshDens);
  31.     brush = new RoundBrush(volume, brushRadius);
  32.     surface = new ArrayIsoSurface(volume);
  33.   }

  34.   /*------------------------------------------------------------------
  35.    *** CLASS FUNCTIONS ***
  36.    ------------------------------------------------------------------*/


  37.   void drawMesh(ArrayList meshPartList_) {

  38.     meshPartList = meshPartList_;




  39.     if (drawMesh) {
  40.       for (int i = 0; i < meshPartList.size(); i++) {
  41.         brush.setSize(brushSize);
  42.         Particle newPart = (Particle) meshPartList.get(i);
  43.         brush.drawAtAbsolutePos(new Vec3D(newPart.pos.x, newPart.pos.y, newPart.pos.z), density);
  44.       }


  45.       volume.closeSides();
  46.       surface.reset();
  47.       surface.computeSurfaceMesh(this, isoThreshold);
  48.     }

  49.     if (isWireframe) {
  50.       stroke(255);
  51.       strokeWeight(1);
  52.       noFill();
  53.     } 
  54.     else {
  55.       lightSpecular(230, 230, 230);
  56.       directionalLight(255, 255, 255, 1, 1, -1);
  57.       shininess(1.0);
  58.       noStroke();
  59.       fill(200);
  60.     }

  61.     gfx.mesh(this);
  62.   }
  63. }

Replies(2)

Line 65 to 76 are not really drawing the mesh (in a Processing draw way), but rather creating the mesh. If the points do not change, it would be more efficient to create the mesh once instead of 60 times per second. In other words seperate the creation from the drawing of the mesh. For example by putting that code in a createMesh() function that you call when you want to build the mesh. Doing the creation once (only when needed) and then drawing continuously, will improve the speed of your sketch significantly. If necessary you could put a if (mesh == null) check in your code or just make sure the mesh is never drawn before it's created.

On a side note you could use java generics with your arraylists. While you have only posted a partial, non-runnable example, I believe something like this would work in your code:
Copy code
  1. // something like this...
  2. ArrayList meshPartList = new ArrayList();
  3. for (int i = 0; i < meshPartList.size(); i++) {
  4.   Particle newPart = (Particle) meshPartList.get(i);
  5.   brush.drawAtAbsolutePos(new Vec3D(newPart.pos.x, newPart.pos.y, newPart.pos.z), density);
  6. }

  7. // could be rewritten like this...
  8. ArrayList <Particle> meshPartList = new ArrayList <Particle> ();
  9. for (Particle p : meshPartList) {
  10.   brush.drawAtAbsolutePos(p.pos, density);
  11. }
Hi Amnon,

thanks a lot for your help. It was exactly as you said, I just had to set up a create mesh function and only run it once.

Is the java generics you have posted just a more alternative and more efficient way of writing arraylists? I'd seen some of this before but never got round to implementing it in my script....

thanks again,

Phill