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.
Pages: 1 2 
3d Sphere / Delaunay (Read 3946 times)
3d Sphere / Delaunay
Jan 7th, 2010, 1:47am
 
Hello everyone,
I am trying to build a 3d sphere using points and Delaunay triangulation.
For this I used the triangle hack library :
just type delaunay triangle in processing search box or delaunay hack,
you ll find the page (cant post active links)

First Ive built a 3d sphere using points, within two loops.


import processing.opengl.*;


 float Xpos=0;
 float Ypos=0;
 float Zpos=0;

 float z= 0;
 float StepsV =50;
 float StepsH=StepsV;


void setup(){
 size(600, 600,OPENGL);

 background(0);
 lights();
 stroke(255);
 smooth();
}



void draw(){

 translate(width/2, height/2,0);
 background(0);
 
 //////////////////////////////////////////////////////////////////
 camera(mouseX, mouseY, 220.0, // eyeX, eyeY, eyeZ
 0.0, 0.0, 0.0, // centerX, centerY, centerZ
 mouseX, 1.0, 0.0); // upX, upY, up
 /////////////////////////////////////////////////////////////////
 stroke(255);
 strokeWeight(0.5);

 pushMatrix();

 for (int i = 1; i<=StepsV;i++){

   rotateY(radians(360/StepsV));
   for (float a = 90; a < 270; a+=360/StepsV) {

     pushMatrix();
     float lineLength= width/3;
     float Xpos1=Xpos + ( cos(radians(a)) * lineLength );
     float Ypos1=Ypos + ( sin(radians(a)) * lineLength );
     float Zpos1=Zpos + ( sin(radians(z-HALF_PI) * lineLength) );

     stroke(0,255,0);


     point(Xpos1,Ypos1,Zpos1);

     //ellipse(Xpos1,Ypos1,2,5,2,5);
     popMatrix();
   }
 }

 popMatrix();
 //endShape();
}

It works well, I built it this way with the two loops because it will be needed later on.

Then I tried to combine this code with the triangle hack to have triangles between the points using the delaunay method.


import processing.opengl.*;
import com.processinghacks.triangulate.*;

Vector triangles = new Vector();
Vector points = new Vector();
float Xpos=0;
float Ypos=0;
float Zpos=0;

float z= 0;
float StepsH=100;
float StepsV =100;

void setup(){
 size(600, 600,OPENGL);

 background(0);
 lights();
 stroke(255);
 smooth();
 createPoints();
}

void createPoints (){


 for (int i = 1; i<=StepsV;i++){

   rotateY(radians(360/StepsV));
   for (float a = 90; a < 270; a+=(360/StepsV)*2) {


     float lineLength= width/3;
     float Xpos1=Xpos + ( cos(radians(a)) * lineLength );
     float Ypos1=Ypos + ( sin(radians(a)) * lineLength );
     float Zpos1=Zpos + ( sin(radians(z-HALF_PI) * lineLength) );

     points.addElement(new Point3f(Xpos1,Ypos1,0));
     println("addElement");  

   }
 }

}  

void draw(){

 translate(width/2, height/2,0);
 background(0);
 ///*
 //////////////////////////////////////////////////////////////////
 camera(mouseX, mouseY, 220.0, // eyeX, eyeY, eyeZ
 0.0, 0.0, 0.0, // centerX, centerY, centerZ
 mouseX, 1.0, 0.0); // upX, upY, up
 /////////////////////////////////////////////////////////////////
 //*/

 stroke(255);
 strokeWeight(0.5);

 /*  float Xpos=0;
  float Ypos=0;
  float Zpos=0;
 
  float z= 0;
  float StepsH=100;
  float StepsV =100;
  */
 pushMatrix();

 for (int i = 1; i<=StepsV;i++){
   rotateY(radians(360/StepsV));
//    for (float a = 90; a < 270; a+=(360/StepsV)*2) {

   Point3f p = (Point3f)points.elementAt(i);
   stroke(0,255,0);
   ellipse(p.x,p.y,2.5,2.5);
//  }

 }
 /*  for (int i = 1; i<=StepsV;i++){
 
  rotateY(radians(360/StepsV));
  for (float a = 90; a < 270; a+=(360/StepsV)*2) {
 
 
  float lineLength= width/3;
  float Xpos1=Xpos + ( cos(radians(a)) * lineLength );
  float Ypos1=Ypos + ( sin(radians(a)) * lineLength );
  float Zpos1=Zpos + ( sin(radians(z-HALF_PI) * lineLength) );
 
  points.addElement(new Point3f(Xpos1,Ypos1,0));
  println("addElement");  
 
  stroke(0,255,0);
  ellipse(Xpos1,Ypos1,2.5,2.5);
 
  }
  }
  */
 popMatrix();
 //endShape();
}

But it just gives me to sinusoidal circles.
I ve tried other methods, puting more push and pop matrix but it gets just worst ! Seems I cant figure the problem,

Can anyone help?
THanks by advance

Arash Wink
Re: 3d Sphere / Delaunay
Reply #1 - Jan 7th, 2010, 5:37am
 

the build in sphere can also be used as 3D

noStroke();
lights();
translate(58, 48, 0);
sphere(28);

Re: 3d Sphere / Delaunay
Reply #2 - Jan 7th, 2010, 6:16am
 
yes i know, but i need to build my own in order to be able to change every point distance from the center, is it also possible with the built in sphere or not?
Re: 3d Sphere / Delaunay
Reply #3 - Jan 7th, 2010, 8:32am
 

Hello,

I am very sorry to have stated the obvious. I should have known that you know about sphere. My Apologies.

I assume what you want is not possible with the sphere.

Greetings,

Chrisir

Re: 3d Sphere / Delaunay
Reply #4 - Jan 7th, 2010, 8:39am
 
P.S.
did you take a look at

http://processing.org/discourse/yabb2/num_1256759256.html

there is a good posting by quark (second last) for a umbrella. [edited]
Re: 3d Sphere / Delaunay
Reply #5 - Jan 7th, 2010, 11:30am
 
Just spotted this thread.
I have reproduced the code below from the umbrella post, changed it slightly to make a sphere not an umbrella. Also you can now change the x/y/z radii independently.

NOTE this example uses the PeasyCam library to rotate around the sphere, get the library from here http://processing.org/reference/libraries/#3d

Alternatively you could use my Shapes3D library available here http://www.lagers.org.uk/s3d4p/index.html

Smiley

Code:
import peasy.*;

PeasyCam pCamera;

float x, y, z, u, v;

float phi;
int phiSteps = 20;
float phiFactor = PI / phiSteps;

float theta;
int thetaSteps = 20;
float thetaFactor = TWO_PI / thetaSteps;

float radX = 30, radY = 50, radZ = 100;

PVector[] sphereVertexPoints;

PImage skin;


void setup() {
 size(320, 240, P3D);

 pCamera = new PeasyCam(this, 300);
 pCamera.lookAt(0,0,0,150);

 skin = loadImage("globe.png");
 textureMode(NORMALIZED);
 noStroke();
}


void draw() {
 background(50);
 //noFill(); stroke(255);
 fill(255);

 // stage lighting
 directionalLight(255, 255, 255, -100, 100, -100);
 ambientLight(128, 128, 128);

 phi = 0.0;
 for(int p = 0; p < phiSteps; p++) {
   beginShape(QUAD_STRIP);
   texture(skin);
   theta = 0.0;
   for(int t = 0; t < thetaSteps + 1; t++) {
     x = radX * sin(phi) * cos(theta);
     z = radY * sin(phi) * sin(theta);
     y = -radZ * cos(phi);

     u = (float)t / thetaSteps;
     v = (float)p / phiSteps;


     normal(x, y, z);
     vertex(x, y, z, u, v);

     x = radX * sin(phi + phiFactor) * cos(theta);
     z = radY * sin(phi + phiFactor) * sin(theta);
     y = - radZ * cos(phi + phiFactor);

     u = (float)t / thetaSteps;
     v = (float)(p + 1) / phiSteps;

     normal(x, y, z);
     vertex(x, y, z, u, v);
   
     theta += thetaFactor;
   }
   phi += phiFactor;
   endShape(CLOSE);
 }
}


Re: 3d Sphere / Delaunay
Reply #6 - Jan 7th, 2010, 1:57pm
 
tried to load the codes but monitor tells me
"you must first load texture() before calling u and v coordinates with vertex()"

i guess its the jpg file used for the texture but changed the name for a file on my desktop but still not working.
What am I doing wrong?

Btw thanks for the help ! i hope this will be usefull
Re: 3d Sphere / Delaunay
Reply #7 - Jan 7th, 2010, 2:40pm
 

the jpg file must be in the same folder as your pde-file.
also it's case-sensitive.

Did you try menu Sketch | Add File...

Greetings!

Re: 3d Sphere / Delaunay
Reply #8 - Jan 7th, 2010, 2:57pm
 

@Quark:

I just used umbrella for an arrow with shaft and head -
maybe something for your shapes


import peasy.*;
PeasyCam pCamera;

void setup() {
 size(800, 600, P3D);
 background(0);
 stroke(112);
 fill(102);
 pCamera = new PeasyCam(this, 300);
 pCamera.lookAt(0,0,0,10);
 pCamera.setDistance(830);
}

void draw(){
 background(0);
 Test_On_Arrow();
} // void

void Test_On_Arrow () {
 Liner ( 310,200,10, 310,200,110 );
} // function

void Liner(int z1, int x1, int y1, int z2, int x2, int y2 ){

 // prepare Arrow

 final int diff=24;

 PVector MyPVector1 = new PVector(0, 0, 0); // from
 PVector MyPVector2 = new PVector(0, 0, 0); // to

 PVector ArrowVectorPosition= new PVector(0, 0, 0);
 PVector ArrowVectorRotate= new PVector(0, 0, 0);

 MyPVector1.set ( x1,y1,z1 );    
 MyPVector2.set ( x2,y2,z2 );

 MyPVector1.y-=4;
 MyPVector2.y-=4;

 MyPVector2.y=MyPVector2.y-diff;
 ArrowVectorPosition.set(MyPVector2.x,MyPVector2.y+2,MyPVector2.z);
 ArrowVectorRotate.set (90,90,90);

 // paint arrow
 Arrow ( MyPVector1, MyPVector2,
 ArrowVectorPosition, ArrowVectorRotate,
 2.0, 7.0, 110 );

} // void

// ------------------------------------------------------------
// Arrow

void Arrow ( PVector MyPVector1,PVector MyPVector2,
PVector ArrowVectorPosition, PVector ArrowVectorRotate,
float WeightShaft, float RadiusArrowHead,
color Color ){

 // here a lot could be simplified and optimized...
 // in Arrow, MyBox and Arrowhead

 // draws an arrow

 //  Parameters:
 //  MyPVector1 = from / Arrow shaft
 //  MyPVector2 = To / Arrow shaft
 //  ArrowVectorPosition = Arrow head Position
 //  ArrowVectorRotate = Arrow head Rotation
 //  WeightShaft = Weight for the Arrow shaft
 //  RadiusArrow = radius for the Arrow head

 stroke( Color );
 fill( Color );

 // Shaft
 ArrowShaft( MyPVector1.x, MyPVector1.y, MyPVector1.z,
 MyPVector2.x, MyPVector2.y, MyPVector2.z,
 WeightShaft, Color );

 // Arrow head
 pushMatrix();
 translate(ArrowVectorPosition.x, ArrowVectorPosition.y, ArrowVectorPosition.z);
 rotateX (radians(ArrowVectorRotate.x));
 rotateY (radians(ArrowVectorRotate.y));
 rotateZ (radians(ArrowVectorRotate.z));
 Arrowhead (RadiusArrowHead );
 popMatrix();

} // void Arrow

void ArrowShaft(float x1, float y1, float z1, float x2, float y2, float z2, float weight, color strokeColour){

 // arrow shaft

 // was called drawLine; programmed by James Carruthers from the processing-forum
 // see http://processing.org/discourse/yabb2/num_1262458611.html#9

 PVector p1 = new PVector(x1, y1, z1);
 PVector p2 = new PVector(x2, y2, z2);
 PVector v1 = new PVector(x2-x1, y2-y1, z2-z1);
 float rho = sqrt(pow(v1.x,2)+pow(v1.y,2)+pow(v1.z,2));
 float phi = acos(v1.z/rho);
 float the = atan2(v1.y,v1.x);
 v1.mult(0.5);

 pushMatrix();
 translate(x1,y1,z1);
 translate(v1.x, v1.y, v1.z);
 rotateZ(the);
 rotateY(phi);
 noStroke();
 fill(strokeColour);
 box(weight,weight,p1.dist(p2)*1.2);
 popMatrix();

} // ArrowShaft

void Arrowhead(float radius){

 // Arrowhead
 // was umbrella from the processing-forum

 // float radius = 7.0; // 50.0;

 float rho = radius;
 float x, y, z, u, v;

 float phi;
 int phiSteps = 1; //  20;
 float phiFactor = HALF_PI / phiSteps;

 float theta;
 int thetaSteps = 4 ; // 20;
 float thetaFactor = TWO_PI / thetaSteps;

 // PVector[] sphereVertexPoints;

 // for closing the bottom of the arrow head
 // (towards the shaft)
 PVector[] BottomOfTheHead = new PVector [8];

 // PImage skin;

 // noFill(); stroke(255);
 // fill(200, 130, 0);

 // stage lighting
 // directionalLight(255, 255, 255, -100, 100, -100);
 // ambientLight(120, 120, 120);

 phi = 0.0;
 for(int p = 0; p < phiSteps; p++) {
   beginShape(QUAD_STRIP);
   // texture(skin);
   theta = 0.0;
   for(int t = 0; t < thetaSteps + 1; t++) {

     x = rho * sin(phi) * cos(theta);
     z = rho * sin(phi) * sin(theta);
     y = -rho * cos(phi);

     // u = (float)t / thetaSteps;
     // v = (float)p / phiSteps;

     normal(x, y, z);
     vertex(x, y, z ); // , u, v);

     x = rho * sin(phi + phiFactor) * cos(theta);
     z = rho * sin(phi + phiFactor) * sin(theta);
     y = -rho * cos(phi + phiFactor);
     // println (x);

     // u = (float)t / thetaSteps;
     // v = (float)(p + 1) / phiSteps;

     normal(x, y, z);
     vertex(x, y, z);  // , u, v);

     BottomOfTheHead [t] = new PVector (x, y, z);

     theta += thetaFactor;
   }
   phi += phiFactor;
   endShape(CLOSE);
 }

 // close the bottom of the arrow head
 // (towards the shaft)
 beginShape();
 for(int b = 0; b < 4; b++) {
   vertex (BottomOfTheHead[b].x,BottomOfTheHead[b].y,BottomOfTheHead[b].z);
 } // for b
 endShape(CLOSE);

} // Arrowhead
Re: 3d Sphere / Delaunay
Reply #9 - Jan 7th, 2010, 3:14pm
 

@ arash nassiri:

it's a png-file; so either save a jpg as png (eg with irfan-view) or rename it in the code to jpg (if possible)

Code worked for me with a png-file without changes in the code.

Greetings!

Chrisir

Re: 3d Sphere / Delaunay
Reply #10 - Jan 7th, 2010, 3:22pm
 
thanks, will try all of this during the weekend,
ill let ya know if it worked !
cheers

arash
Re: 3d Sphere / Delaunay
Reply #11 - Jan 8th, 2010, 9:05am
 
The code will work with jpg and png files provided the file is inside the sketch directory as pointed out by Chrisir.
Re: 3d Sphere / Delaunay
Reply #12 - Jan 11th, 2010, 4:13am
 
is there anyway to draw the sphere with a grid view?
actually i think i will need to stick with the first code with the delaunay triangulation.
Actually i am doing all this because i am working on a 360° 3d scan with arduino, the hardware is pretty much in its final stage, once the data scanned a would like to remap them in a virtual space with processing, so each point would be placed in the scanned distance thus rendering the 3d space. So the delaunay triangulation would be the best solution to have single polygons with 3points no matter their radius.

Could anyone help with the first code?(see first post)
thanks all

arash
Re: 3d Sphere / Delaunay
Reply #13 - Jan 16th, 2010, 5:44am
 
Hello,

I am afraid I can't.

I just wanted to point you to
http://local.wasp.uwa.edu.au/~pbourke/geometry/prolatespheroid/
and to
http://processing.org/discourse/yabb2/num_1260731680.html
reply #14 which could be similar.

Besides:
I figure your robot takes a look around 360°, in a circle.
But does it really look up and down
Because if it looks only around in a horizontal plane, this is more like 2D: a flat circle of different distances.

You might also take a look
at
the post of the 22.04.2008, 14:31
http://www.roboternetz.de/phpBB2/viewtopic.php?t=39103

it's in German, but there's source-code for a sorround-scan.


Greetings, Chris
Re: 3d Sphere / Delaunay
Reply #14 - Jan 16th, 2010, 10:12am
 


P.S.

or you go for:
http://processing.org/discourse/yabb2/num_1260903198.html

Pages: 1 2