selecting vertices and moving them

edited March 2016 in Android Mode

Hello forumers,

I am looking for tips and hints on "the best way" to approach something. I want to either import, or create, geometry (initially a cylinder), isolate half of it, and move the vertices around, then export it again as an .obj or .stl. I realise there are libraries that will do this but I need this to work on Android and the libraries (as far as I know) don't. I made these images in 3DMax to explain what I mean.

cylinder cylinder-2

I have adapted this method for creating a cylinder from an example in the book: Processing 2: Creative Coding Hotshot...

float[][] vertx;
float[][] verty;

void setup() {
  size(800, 600, P3D);   

  vertx = new float[36][36];//36 triangle strips, 36 vertices
  verty = new float[36][36];
}

void draw() {
  hint( ENABLE_DEPTH_TEST );
  pushMatrix();
  background(125);
  fill(255);
  strokeWeight(0.5);

  translate( width/2, height/2, 200);
  rotateX(radians(-45));
  scale( 1 );

  translate(0, -50, 0);

  initPoints();

  beginShape(TRIANGLE_STRIP );
  for ( int h = 1; h < 36; h++) {
    for ( int a = 0; a<37; a++ ) {
      int aa = a % 36;
     // normal( vertx[h][aa], 0, verty[h][aa]);
      vertex( vertx[h][aa], h*5.0, verty[h][aa] );
      //normal( vertx[h-1][aa], 0, verty[h-1][aa]);
      vertex( vertx[h-1][aa], (h-1)*5.0, verty[h-1][aa] );
    }
  }
  endShape();

  beginShape(TRIANGLE_FAN); //bottom
  int h = 35;
  vertex( 0, h*5, 0 );
  for ( int a = 0; a<37; a++ ) {
    int aa = a % 36;    
    vertex( vertx[h][aa], h*5, verty[h][aa] );
  }
  endShape();

  popMatrix();
  hint(DISABLE_DEPTH_TEST);
}

float getR( float a, float h ) {
  float r = 50;
  return r;
}

void initPoints() {
  for ( int h = 0; h < 36; h++) {
    for ( int a = 0; a<36; a++) {
      float r = getR( a*10.0, h*5.0 ); //a = 10 (360/36)
      vertx[h][a] = cos( radians( a*10.0 )) * r;
      verty[h][a] = sin( radians( a*10.0 )) * r;
    }
  }
}

...and I am assuming it is possible to isolate/grab certain vertices from the array?

Any other approaches, or any advice on how to develop this? Is the import > transform method even possible?

Answers

  • Answer ✓

    In you method initPoints() you create the cylinder, where a defines the angle.
    So if you modify the vertices where i.e. a is lower than 18, this should be one half of the cylinder. Here is an example that increaes the radius of the vertices using noise():

    void initPoints() {
      for ( int h = 0; h < 36; h++) {
        for ( int a = 0; a<36; a++) {
    
          float offset = 0;
          if (a > 17) {
            offset = noise((frameCount+h)/10.0, (frameCount+a)/10.0)*20;
          }
    
          float r = getR( a*10.0, h*5.0 ); //a = 10 (360/36)
          vertx[h][a] = cos( radians( a*10.0 )) * (r+offset);
          verty[h][a] = sin( radians( a*10.0 )) * (r+offset);
        }
      }
    }
    
  • @benja - That is a good approach as I wish to do this dynamically, though oddly, when I run the new code (below) I get three Errors:

    errors

    This is strange because the indicated values must be used somewhere to create the cylinder. I assume offset is never called because float a is never used. Either float a or int a, are superfluous here?

    float[][] vertx;
    float[][] verty;
    
    void setup() {
      size(800, 600, P3D);   
    
      vertx = new float[36][36];//36 triangle strips, 36 vertices
      verty = new float[36][36];
    }
    
    void draw() {
      hint( ENABLE_DEPTH_TEST );
      pushMatrix();
      background(125);
      fill(255);
      strokeWeight(0.5);
    
      translate( width/2, height/2, 200);
      rotateX(radians(-45));
      scale( 1 );
    
      translate(0, -50, 0);
    
      initPoints();
    
      beginShape(TRIANGLE_STRIP );
      for ( int h = 1; h < 36; h++) {
        for ( int a = 0; a<37; a++ ) {
          int aa = a % 36;
          // normal( vertx[h][aa], 0, verty[h][aa]);
          vertex( vertx[h][aa], h*5.0, verty[h][aa] );
          //normal( vertx[h-1][aa], 0, verty[h-1][aa]);
          vertex( vertx[h-1][aa], (h-1)*5.0, verty[h-1][aa] );
        }
      }
      endShape();
    
      beginShape(TRIANGLE_FAN); //bottom
      int h = 35;
      vertex( 0, h*5, 0 );
      for ( int a = 0; a<37; a++ ) {
        int aa = a % 36;    
        vertex( vertx[h][aa], h*5, verty[h][aa] );
      }
      endShape();
    
      popMatrix();
      hint(DISABLE_DEPTH_TEST);
    }
    
    float getR( float a, float h ) {
      float r = 50;
      return r;
    }
    
    
    void initPoints() {
      for ( int h = 0; h < 36; h++) {
        for ( int a = 0; a<36; a++) {
    
          float offset = 0;
          if (a > 17) {
            offset = noise((frameCount+h)/10.0, (frameCount+a)/10.0)*20;
          }
    
          float r = getR( a*10.0, h*5.0 ); //a = 10 (360/36)
          vertx[h][a] = cos( radians( a*10.0 )) * r;
          verty[h][a] = sin( radians( a*10.0 )) * r;
        }
      }
    }
    
  • @Chrisir - that is very impressive, and something I will have a look at in more depth. PeasyCam does not work on Android, but there will be ways around this I am sure. Thank you.

  • float[][] vertx;
    float[][] verty;
    
    void setup() {
      size(800, 600, P3D);  
      //frameRate(30);
    
      vertx = new float[36][36];
      verty = new float[36][36];
    }
    
    void draw() {
      hint( ENABLE_DEPTH_TEST );
      pushMatrix();
      background(125);
      fill(255);
      strokeWeight(0.5);
    
      translate( width/2, height/2, 200);
      rotateX(radians(-45));
      scale( 1.5 );
    
      translate(0, -50, 0);
    
      initPoints();
    
      beginShape(TRIANGLE_STRIP );
      for ( int h = 1; h < 36; h++) {
        for ( int a = 0; a<37; a++ ) {
          int aa = a % 36;
          //normal( vertx[h][aa], 0, verty[h][aa]);
          vertex( vertx[h][aa], h*5.0, verty[h][aa] );
         // normal( vertx[h-1][aa], 0, verty[h-1][aa]);
          vertex( vertx[h-1][aa], (h-1)*5.0, verty[h-1][aa] );
        }
      }
      endShape();
    
      beginShape(TRIANGLE_FAN);
      int h = 35;
      vertex( 0, h*5, 0 );
      for ( int a = 0; a<37; a++ ) {
        int aa = a % 36;    
        vertex( vertx[h][aa], h*5, verty[h][aa] );
      }
      endShape();
    
      popMatrix();
      hint(DISABLE_DEPTH_TEST);
    }
    
    float getR( float ab, float hb ) {
      float r = 0 * ab * hb + 50;
      return r;
    }
    
    void initPoints() {
      for ( int h = 0; h < 36; h++) {
        for ( int a = 0; a<36; a++) {
    
          float offset = noise((frameCount+h)/10.0, (frameCount+a)/10)*50;
          if(a < 17);
    
          float r = getR( a, h );
          vertx[h][a] = cos( radians( a*10.0 )) * offset;
          verty[h][a] = sin( radians( a*10.0 )) * r;
    
        }
      }
    }
    

    I trimmed some of the superfluous code, and this does create noise, but across the entire cylinder. I guess the following is in the wrong place:

     float offset = noise((frameCount+h)/10.0, (frameCount+a)/10)*50;
              if(a < 17);
    

    But thanks to @benja, this is progress...

  • yes, swap the 2 lines...

  • edited January 2016 Answer ✓

    do it like this please

    float offset = 1.0; // 1 means no effect for multiplication 
    
    // or angle < 17 modify the offset 
    if (a < 17)
        offset = noise((frameCount+h)/10.0, (frameCount+a)/10)*50;
    

    by the way: if(a < 17); would fail since the sign ; ends the line and the command if....

    Processing would read this as "if true do all between ) and ; " which is... nothing. Oups. Thus no ; in this place!

    remark

    maybe in this line

    vertx[h][a] = cos( radians( a*10.0 )) * offset;
    

    you want to say + offset - I am not sure:

    vertx[h][a] = cos( radians( a*10.0 )) + offset;
    

    then this

    float offset = 1.0; // 1 means no effect for multiplication 
    

    would be

    float offset = 0.0; // 0 means no effect for addition
    

    (not tested....)

    Best, Chrisir

  • This just about works...

    void initPoints() {
      for ( int h = 0; h < 36; h++) {
        for ( int a = 0; a < 36; a++) {
    
          float offset = 0.0;
          if(a < 19)
          offset = noise((frameCount+h)/10.0, (frameCount+a)/10)*50;
    
          float r = getR( a, h );
          vertx[h][a] = cos( radians( a*10.0 )) * r + offset;
          verty[h][a] = sin( radians( a*10.0 )) * r;
    
        }
      }
    }
    
  • Answer ✓

    that's the key

                  cos( radians( a*10.0 )) * r + offset;
    

    or

    cos( radians( a*10.0 )) * r * offset;
    

    or

    cos( radians( a*10.0 )) * (r + offset);
    
Sign In or Register to comment.