here:
I made something for you.
As I described above the steps.
- ArrayList listOfFrames holds the steps of your scans.
- Each frame / scan holds several points. Here you have to implement your scan.
- in state 0 you can loop through the points (space bar)
- Press return now
- in state 1 the points are connected to the 3D form using a circle form (aka cylinder). This is the core idea. That’s how you can go from 2D (points) to 3D points deforming/making a cylinder.
- Look at it with peasyCam
Chrisir
import peasy.*;
final int show2D=0;
final int show3D=1;
int state=show2D;
PeasyCam camera;
ArrayList<Frame> listOfFrames = new ArrayList();
ArrayList<OneQuad> listOneQuad = new ArrayList();
// Tool classes
ToolsPVector toolsPVector = new ToolsPVector();
// float a3=0;
int i_key=0;
// ---------------------------------------------------------------
void setup() {
size(1300, 990, P3D);
camera = new PeasyCam(this, width/2, height/2, 0, 900);
createArrayList();
make3DShape();
} //func setup()
void draw() {
switch(state) {
case show2D:
background(0); // black
camera();
Frame cf=listOfFrames.get(i_key); // for (Frame cf : listOfFrames) {
cf.display(300);
fill(255);
text("Space Bar to show next frame; ENTER to go to 3D mode; showing frame "
+ i_key
+" of "
+listOfFrames.size()
+".",
19, 19);
break;
case show3D:
background(111); // gray
avoidClipping();
ambientLight(128, 128, 128) ;
directionalLight(128, 128, 128,
0, 0, 1);
directionalLight(128, 128, 128,
0, 0, -1);
lightFalloff(1, 0, 0);
lightSpecular(0, 0, 0);
// --------
CheckeredFloor();
for ( OneQuad current : listOneQuad ) {
current.display();
}
break;
default :
println("Error in draw()");
exit();
break;
}//switch
//
} // func draw()
// ---------------------------------------------------------------------------------------------------------------------
// Inputs
void keyPressed() {
if (key==' ') {
i_key++;
if (i_key>=listOfFrames.size()) {
//i_key=listOfFrames.size()-1;
i_key=0;
}
} else if (key==RETURN || key==ENTER) {
state = show3D;
i_key=0;
}
//
}//func
// ---------------------------------------------------------------------------------------------------------------------
// creating Data
void createArrayList() {
// create frames
ArrayList<Frame> listOfFramesHelper = new ArrayList();
Frame newF = new Frame();
newF.listOfCells.add(new Cell(width/2, 100));
newF.listOfCells.add(new Cell(width/2-210, 200));
newF.listOfCells.add(new Cell(width/2-300, 300));
newF.listOfCells.add(new Cell(width/2-20, 400));
listOfFramesHelper.add(newF);
newF = new Frame();
newF.listOfCells.add(new Cell(width/2, 100));
newF.listOfCells.add(new Cell(width/2-110, 200));
newF.listOfCells.add(new Cell(width/2-400, 300));
newF.listOfCells.add(new Cell(width/2-40, 400));
listOfFramesHelper.add(newF);
newF = new Frame();
newF.listOfCells.add(new Cell(width/2, 100));
newF.listOfCells.add(new Cell(width/2-130, 200));
newF.listOfCells.add(new Cell(width/2-400, 300));
newF.listOfCells.add(new Cell(width/2-330, 400));
listOfFramesHelper.add(newF);
// ------------------------------------------------
// interpolate ---
for (int i=0; i<listOfFramesHelper.size()-1; i++) {
Frame f1=listOfFramesHelper.get(i);
Frame f2=listOfFramesHelper.get(i+1);
for (int i2=0; i2<10; i2++) { // 10 is the resolution
newF = new Frame();
for (int i3=0; i3<f1.listOfCells.size(); i3++) {
Cell c1=f1.listOfCells.get(i3);
Cell c2=f2.listOfCells.get(i3);
float x1 = lerp( c1.x, c2.x, i2 / 10.0 );
float y1 = lerp( c1.y, c2.y, i2 / 10.0 );
newF.listOfCells.add(new Cell(x1, y1));
}//for
listOfFrames.add(newF);
}//for
}//for
// interpolate between last and first ---
Frame f1=listOfFramesHelper.get(listOfFramesHelper.size()-1);
Frame f2=listOfFramesHelper.get(0);
for (int i2=0; i2<10; i2++) { // 10 is the resolution
newF = new Frame();
for (int i3=0; i3<f1.listOfCells.size(); i3++) {
Cell c1=f1.listOfCells.get(i3);
Cell c2=f2.listOfCells.get(i3);
float x1 = lerp( c1.x, c2.x, i2 / 10.0 );
float y1 = lerp( c1.y, c2.y, i2 / 10.0 );
newF.listOfCells.add(new Cell(x1, y1));
}//for
listOfFrames.add(newF);
}//for
println(listOfFrames.size());
}//func
// -- -- -- -- -- --
void make3DShape() {
// The Core Function
for (int i=0; i<listOfFrames.size()-1; i++) {
float angle = map (i, 0, listOfFrames.size(), 0, TWO_PI);
float angle2 = map (i+1, 0, listOfFrames.size(), 0, TWO_PI);
Frame f1=listOfFrames.get(i);
Frame f2=listOfFrames.get(i+1);
for (int i3=0; i3<f1.listOfCells.size()-1; i3++) {
Cell c1=f1.listOfCells.get(i3); // upper left corner
Cell c2=f2.listOfCells.get(i3); // upper right
Cell c3=f1.listOfCells.get(i3+1); // lower left
Cell c4=f2.listOfCells.get(i3+1); // lower right
//---
float x1 = cos(angle) * (width/2-c1.x); // same corners as above
float y1 = c1.y;
float z1 = sin(angle) * (width/2-c1.x) +0;
float x2 = cos(angle2) * (width/2-c2.x);
float y2 = c2.y;
float z2 = sin(angle2) * (width/2-c2.x) +0;
float x3 = cos(angle) * (width/2-c3.x);
float y3 = c3.y;
float z3 = sin(angle) * (width/2-c3.x) +0;
float x4 = cos(angle2) * (width/2-c4.x);
float y4 = c4.y;
float z4 = sin(angle2) * (width/2-c4.x) +0;
OneQuad newOneQuad = new OneQuad (
new PVector ( x1, y1, z1 ),
new PVector ( x2, y2, z2 ),
new PVector ( x4, y4, z4 ), // changed order here
new PVector ( x3, y3, z3 )
);
listOneQuad.add(newOneQuad);
}//for
//
}//for
//---------------------------------------
// close shape
float angle = map (listOfFrames.size()-1, 0, listOfFrames.size(), 0, TWO_PI);
float angle2 = map (0, 0, listOfFrames.size(), 0, TWO_PI);
Frame f1=listOfFrames.get(listOfFrames.size()-1);
Frame f2=listOfFrames.get(0);
for (int i3=0; i3<f1.listOfCells.size()-1; i3++) {
Cell c1=f1.listOfCells.get(i3); // upper left corner
Cell c2=f2.listOfCells.get(i3); // upper right
Cell c3=f1.listOfCells.get(i3+1); // lower left
Cell c4=f2.listOfCells.get(i3+1); // lower right
//---
float x1 = cos(angle) * (width/2-c1.x); // same corners as above
float y1 = c1.y;
float z1 = sin(angle) * (width/2-c1.x) +0;
float x2 = cos(angle2) * (width/2-c2.x);
float y2 = c2.y;
float z2 = sin(angle2) * (width/2-c2.x) +0;
float x3 = cos(angle) * (width/2-c3.x);
float y3 = c3.y;
float z3 = sin(angle) * (width/2-c3.x) +0;
float x4 = cos(angle2) * (width/2-c4.x);
float y4 = c4.y;
float z4 = sin(angle2) * (width/2-c4.x) +0;
OneQuad newOneQuad = new OneQuad (
new PVector ( x1, y1, z1 ),
new PVector ( x2, y2, z2 ),
new PVector ( x4, y4, z4 ), // changed order here
new PVector ( x3, y3, z3 )
);
listOneQuad.add(newOneQuad);
}//for
// -------------------------
println( listOneQuad.size());
}//func
//-----------------------------------------------------------------------------
// floor
void CheckeredFloor() {
//floor
noStroke();
for (int i = -40; i < 40; i++) {
for (int j = -40; j < 40; j++) {
// get color
// % is modulo, meaning rest of division
if (i%2 == 0) {
if (j%2 == 0) {
fill (255, 0, 0);
} else
{
fill ( 103 );
}
} else {
if (j%2 == 0) {
fill ( 103 );
} else
{
fill (255, 0, 0);
}
} // if
pushMatrix();
translate ( 80*i, height/2+210+30+3.5, 80*j );
box ( 80, 7, 80); // one cell / tile
popMatrix();
} // for
} // for
} // function
//-----------------------------------------------------------------------------------------------
// Tools
void avoidClipping() {
// avoid clipping (at camera):
// https : //
// forum.processing.org/two/discussion/4128/quick-q-how-close-is-too-close-why-when-do-3d-objects-disappear
perspective(PI/3.0, (float) width/height, 1, 1000000);
}//func
// ==============================================================================================
class Frame {
// 2D
// one frame holds a list of points from the image scan
ArrayList<Cell> listOfCells = new ArrayList();
void display(float xpos) {
stroke(255);
for (Cell c1 : listOfCells) {
fill(i_key*49+60, 0, 0);
ellipse(c1.x+xpos, c1.y,
6, 6);
}
}
}//class
// ==============================================================================================
class OneQuad {
// 3D surface (rect)
PVector pv0, pv1, pv2, pv3;
// constr
OneQuad (PVector pv0_, PVector pv1_, PVector pv2_, PVector pv3_) {
pv0=pv0_.copy();
pv1=pv1_.copy();
pv2=pv2_.copy();
pv3=pv3_.copy();
}// constr
void display() {
fill(255, 0, 0);
beginShape(QUAD); // //class
fill(255, 0, 0);
vertex(pv0.x, pv0.y, pv0.z);
vertex(pv1.x, pv1.y, pv1.z);
vertex(pv2.x, pv2.y, pv2.z);
vertex(pv3.x, pv3.y, pv3.z);
endShape(CLOSE); // CLOSE
}
}//class
// ==============================================================================================
class Cell {
float x, y; // pos
int sclx=1, scly=1; // size
// int i, j; // pos in grid (col/row)
boolean state=false; // ON / OFF
// Two colors
color colorForOff= color(random(255), random(255), random(255)); // color(0); //
color colorForON=color((255), 0, 0);
String strImgName = "";
PImage imgCell = null;
//constr
Cell(float x_, float y_) {
x = x_;
y = y_;
} //constr
void display() {
fill( colorForOff );
stroke(111);
rect(x, y,
sclx, scly);
}//func
String toString() {
return str(x)
+","+
str(y);
}
//
}//class
//===================================================================================
class ToolsPVector {
// not a class for an object like a car but a class that collects Tools - FOR 3D -----------!!!!
void linePV ( PVector pv1, PVector pv2) {
line(pv1.x, pv1.y, pv1.z,
pv2.x, pv2.y, pv2.z);
}//func
void pointPV ( PVector pv) {
point(pv.x, pv.y, pv.z);
}//func
void spherePV ( PVector pv) {
pushMatrix();
translate(pv.x, pv.y, pv.z);
noStroke();
fill(255, 0, 0);
sphere(17);
popMatrix();
}//func
void spherePVCol ( PVector pv, color c1) {
pushMatrix();
translate(pv.x, pv.y, pv.z);
noStroke();
fill(c1);
sphere(17);
popMatrix();
}//func
void vertexPV(color col_, PVector pv_) {
fill(col_);
vertex(pv_.x, pv_.y, pv_.z);
}
//
}//class
//