Extrude function

Hello, I’ve been coding for a few years in processing and I’ve been developing some simple simulations, however I wanted to take my work and do it in 3D, my pieces are quite simple (just a rectangle with 2 semi circles in each end) at first I tried to use the extrude library but it was way too slow and had a lot of restrictions (the extruction size could only be an integer) so I tried to make my own function to extrude, however it ended way slower, any tips on any library that allows me to extrude or a way to make this code better?

/*Function that returns a PShape[], its arguments are the original face, where the extruded
version will be store and the size of the extrusion 
*/
PShape[] extrusion(PShape baseExtrusion, PShape[] parte, float extrudeT) {
//copies the face to the extruded version
    parte=new PShape[0];
    parte=(PShape[])append(parte, baseExtrusion);

//creates an offset copy of the face
    PShape temp;
    temp=createShape();
    parte=(PShape[])append(parte, temp);

    parte[1].beginShape();
    for (int j=0; j<parte[0].getVertexCount(); j++) {
      PVector v = parte[0].getVertex(j).copy();
      v.z+=extrudeT;
      parte[1].vertex(v.x, v.y, v.z);
    }
    parte[1].endShape(CLOSE);

//creates n faces that conects the base with the offset one
//I tried to make this one just two lines however they wouldn't show for some reason
    for (int i=0; i<parte[0].getVertexCount()-1; i++) {
      temp=createShape();
      parte=(PShape[])append(parte, temp);

      PVector v = parte[0].getVertex(i);
      PVector vNext;
      vNext=parte[0].getVertex(i+1);

      parte[i+2].beginShape();
      parte[i+2].vertex(v.x, v.y, v.z);
      parte[i+2].vertex(vNext.x, vNext.y, vNext.z);
      parte[i+2].vertex(vNext.x, vNext.y, vNext.z+extrudeT);
      parte[i+2].vertex(v.x, v.y, v.z+extrudeT);
      parte[i+2].endShape(CLOSE);
    }

    return parte;
  }
1 Like

Might as well post this, I found a way to do an extrusion which doesn’t consume a lot of resources, however beware since because of the way is constructed the fill() function doesn’t work properly

//function for extrusion, returns a single PShape which will be the whole solid
//takes as arguments the figure to extrude and the thickness
  PShape extrusion(PShape baseExtrusion,  float extrudeT) {
    //initialize the result and generates a vector that will have the base vertices
    PShape parte=new PShape();
    PVector[] v=new PVector[0];

    //fill the vector with the vertices
    for (int i=0; i<baseExtrusion.getVertexCount(); i++) {
      v=(PVector[])append(v,baseExtrusion.getVertex(i));
    }
    //starts constructing the whole solid
    parte=createShape();
    parte.beginShape();
    for (int i=0; i<baseExtrusion.getVertexCount(); i++) {
     /*
creates the lateral rectangles of the extrusion in a single motion by looping around itself*/

      if (i+1<baseExtrusion.getVertexCount()) {
        parte.vertex(v[i+1].x, v[i+1].y, v[i+1].z);
        parte.vertex(v[i+1].x, v[i+1].y, v[i+1].z+extrudeT);
        parte.vertex(v[i].x, v[i].y, v[i].z+extrudeT);
        parte.vertex(v[i].x, v[i].y, v[i].z);
        parte.vertex(v[i+1].x, v[i+1].y, v[i+1].z);
      } else {
//when is at the last rectangle it just conects to the start
        parte.vertex(v[0].x, v[0].y, v[0].z);
        parte.vertex(v[0].x, v[0].y, v[0].z+extrudeT);
        parte.vertex(v[i].x, v[i].y, v[i].z+extrudeT);
      }
    }
    parte.endShape();
    return parte;
  }
}

could you please post an entire running example?

I don’t understand what it is doing.

Also, thank you very much

Chrisir

Here it is: https://github.com/Vako9898/Four-Bar-linkage-3D
as I said before is a extrude function, takes a polygon (either regular or irregular) and gives it thickness

1 Like

Hi @Vako,

Have you tried with one of the modifiers from the HE_Mesh library ?

Possibly, HEM_Extrude() or HEM_Shell() combined with setThickness()

If interested, have a look at the bundled examples named Ref_HEM_Shell and Ref_HEM_Extrude inside Contributed_Libraries/HE_Mesh/hemesh/modify/…

2 Likes

how dang, just checked the examples you said, they run smoothly, I guess I’ll have to do some reading, thanks