# Extrude function

**URL:** <https://discourse.processing.org/t/extrude-function/25290>\
**Category:** Coding Questions\
**Created:** [November 8, 2020, 10:58pm UTC](https://discourse.processing.org/t/extrude-function/25290 "2020-11-08T22:58:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Vako](https://avatars.discourse-cdn.com/v4/letter/v/a9adbd/32.png) [@Vako](https://discourse.processing.org/u/Vako)\
**Post date:** [November 8, 2020, 10:58pm UTC](https://discourse.processing.org/t/extrude-function/25290/1 "2020-11-08T22:58:22Z")

</div>

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?

```auto
/*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;
  }

```

---

<div class="post-metadata">

**Author:** ![Vako](https://avatars.discourse-cdn.com/v4/letter/v/a9adbd/32.png) [@Vako](https://discourse.processing.org/u/Vako)\
**Post date:** [November 9, 2020, 5:22am UTC](https://discourse.processing.org/t/extrude-function/25290/2 "2020-11-09T05:22:19Z")

</div>

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

```auto
//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;
  }
}

```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 9, 2020, 8:51am UTC](https://discourse.processing.org/t/extrude-function/25290/3 "2020-11-09T08:51:12Z")

</div>

could you please post an entire running example?

I don’t understand what it is doing.

Also, thank you very much

Chrisir

---

<div class="post-metadata">

**Author:** ![Vako](https://avatars.discourse-cdn.com/v4/letter/v/a9adbd/32.png) [@Vako](https://discourse.processing.org/u/Vako)\
**Post date:** [November 9, 2020, 3:11pm UTC](https://discourse.processing.org/t/extrude-function/25290/4 "2020-11-09T15:11:16Z")

</div>

Here it is: [https://github.com/Vako9898/Four-Bar-linkage-3D](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

 ![imagen](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/a/a838b12f4ce8b741f04c1bb846191e50002f3147.png)

---

<div class="post-metadata">

**Author:** ![solub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/solub/32/333_2.png) [@solub](https://discourse.processing.org/u/solub)\
**Post date:** [November 9, 2020, 4:05pm UTC](https://discourse.processing.org/t/extrude-function/25290/5 "2020-11-09T16:05:38Z")

</div>

Hi @Vako,

Have you tried with one of the modifiers from the [HE\_Mesh](https://github.com/wblut/HE_Mesh) library ?

Possibly, [`HEM_Extrude()`](https://github.com/wblut/HE_Mesh/blob/cad811989a3d712cdcc1a90bdef292fccd0e28ce/src/hemesh_modifiers/wblut/hemesh/HEM_Extrude.java) or [`HEM_Shell()`](https://github.com/wblut/HE_Mesh/blob/cad811989a3d712cdcc1a90bdef292fccd0e28ce/src/hemesh_modifiers/wblut/hemesh/HEM_Shell.java) 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/…

---

<div class="post-metadata">

**Author:** ![Vako](https://avatars.discourse-cdn.com/v4/letter/v/a9adbd/32.png) [@Vako](https://discourse.processing.org/u/Vako)\
**Post date:** [November 9, 2020, 7:41pm UTC](https://discourse.processing.org/t/extrude-function/25290/6 "2020-11-09T19:41:00Z")

</div>

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