Skip to content
Permalink
master
Go to file
 
 
Cannot retrieve contributors at this time
642 lines (560 sloc) 16.1 KB
import controlP5.*;
import processing.opengl.*;
// define float lists to store value of x, y, z
FloatList xValue = new FloatList();
FloatList yValue = new FloatList();
FloatList zValue = new FloatList();
float[] xCoordsValue = new float[100];
float[] yCoordsValue = new float[100];
float[] zCoordsValue = new float[100];
// define Strings to store the input for x, y, z
String xString;
String yString;
String zString;
// define String to store the iostream of x, y, z
String pointSet = "X-Value Y-Value Z-Value";
// define ControlP5 object and other corressponding object
ControlP5 gui;
ControlGroup messageBox;
ControlGroup ajustmentBox;
ControlGroup functionBox;
Button control;
Textarea txt;
Accordion accordion;
// define other variables
int messageBoxResult = -1; // hide or show
// define varibles for adjustmentBox
int aRevert = -1; // revert value for ajustmentBox
float vSidesway = 0.00;
float vShift = 0.00;
float vScaler = 0.00;
float vRotateX = 1.18;
float vRotateY = 0.00;
float vRotateZ = 1.25;
//float vRotateX = 0;
//float vRotateY = phi;
//float zRotateZ = the;
int rColor = 128;
int bColor = 128;
int gColor = 128;
// define variables for functionBox
int functionBoxResult = -1;
float zVariable = 0.0;
float xVariable = 0.0;
float yVariable = 0.0;
Textarea xv;
Textarea yv;
Textarea zv;
void setup() {
size(1000, 681, P3D);
noStroke();
// create GUI
gui = new ControlP5(this);
gui.setFont(createFont("helvetica", 10));
//revertAjustment();
createBox();
control = gui.addButton("controlBox", 1, 20, 20, 100, 20);
control.setLabel("HIDE");
}
void draw() {
hint(ENABLE_DEPTH_TEST);
pushMatrix();
if(messageBox.isVisible()) {
controlBackgroundColor();
} else {
background(0);
fill(255);
}
popMatrix();
hint(DISABLE_DEPTH_TEST);
if (functionBoxResult == -1){
//Drawing the coordinate system and points by the 3D point sets
pushMatrix();
//camera(mouseX, mouseY, (height/2) / tan(PI/5), width/2, height/2, 0, 0, 1, 0);
controlCamera();
moveCoords();
scalerCoords();
smooth();
stroke(255);
rotateCoords();
noFill();
for (int i=0; i<500; i += 20){
stroke(96);
line(i, 0, 0, i, 0, 500);
line(0, i, 0, 0, i, 500);
line(0, 0, i, 0, 500, i);
line(i, 0, 0, i, 500, 0);
line(0, 0, i, 500, 0, i);
line(0, i, 0, 500, i, 0);
}
strokeWeight(1);
//z-coordinate: red
stroke(255, 0, 0);
line(0, 0, -500, 0, 0, 500);
arrow(0, 0, 500);
//y-coordinate: green
stroke(0, 102, 0);
line(0, -500, 0, 0, 500, 0);
arrow(0, 500, 0);
//x-coordinate: blue
stroke(0, 76, 153);
line(-500, 0, 0, 500, 0, 0);
arrow(500, 0, 0);
for (int i=0; i<xValue.size(); i++)
{
pointSets(xValue.get(i), yValue.get(i), zValue.get(i));
}
popMatrix();
}
//pressedEnter();
//getPressedPoints( mouseX-width/2, mouseY-width/2, map (mouseY-width/2, -height/2, height/2, -height/2, height/2),
//mouseX, mouseY, map (mouseY, -height/2, height/2, -height/2, height/2), color (2, 0, 255));
getPressedPoints( width/2, height/2, 0,
mouseX, mouseY, map (mouseY, -height/2, height/2, -height/2, height/2), color (2, 0, 255));
//popMatrix();
}
void createBox() {
// group messageBox
// create a group to store the messageBox elements
messageBox = gui.addGroup("messageBox");
messageBox.setBackgroundHeight(400);
messageBox.setBackgroundColor(color(0,100));
messageBox.setBarHeight(14);
// add a TextLable to the messageBox.
Textlabel l = gui.addTextlabel("messageBoxLabel", "Entering 3D point sets.", 20, 20);
l.moveTo(messageBox);
// add a textfield-controller with name-id input x-value
Textfield x = gui.addTextfield("inputX", 20, 36, 260, 20);
x.moveTo(messageBox);
x.setColorForeground(color(20));
x.setColorBackground(color(20));
x.setColorActive(color(20));
x.setCaptionLabel("Input X-coordinate Value");
// add a textfield-controller with name-id input y-value
Textfield y = gui.addTextfield("inputY", 20, 76, 260, 20);
y.moveTo(messageBox);
y.setColorForeground(color(20));
y.setColorBackground(color(20));
y.setColorActive(color(20));
y.setCaptionLabel("Input Y-coordinate Value");
// add a textfield-controller with name-id input z-value
Textfield z = gui.addTextfield("inputZ", 20, 116, 260, 20);
z.moveTo(messageBox);
z.setColorForeground(color(20));
z.setColorBackground(color(20));
z.setColorActive(color(20));
z.setCaptionLabel("Input Z-coordinate Value");
// add the Confirm button to the messageBox.
Button b1 = gui.addButton("buttonConfirm",0,65,160,80,24);
b1.moveTo(messageBox);
b1.setColorBackground(color(40));
b1.setColorActive(color(20));
b1.setBroadcast(false);
b1.setValue(1);
b1.setBroadcast(true);
b1.setCaptionLabel("Confirm");
// add the Cancel button to the messageBox.
Button b2 = gui.addButton("buttonCancel", 0, 155, 160, 80, 24);
b2.moveTo(messageBox);
b2.setBroadcast(false);
b2.setValue(0);
b2.setBroadcast(true);
b2.setCaptionLabel("Cancel");
b2.setColorBackground(color(40));
b2.setColorActive(color(20));
// add the text area to show the point set of inputting
txt = gui.addTextarea("showPointSets");
txt.moveTo(messageBox);
txt.setPosition(20, 200);
txt.setSize(260, 100);
txt.setFont(createFont("arial",12));
txt.setLineHeight(14);
txt.setColor(color(128));
txt.setColorBackground(color(255, 100));
txt.setColorForeground(color(255, 100));
// group ajustmentBox
// create a group to store the messageBox elements
ajustmentBox = gui.addGroup("ajustmentBox");
ajustmentBox.setBackgroundHeight(300);
ajustmentBox.setBackgroundColor(color(0,100));
ajustmentBox.setBarHeight(14);
// create slider to control the sidesway of coordinate system
Slider s1 = gui.addSlider("sidesway");
s1.moveTo(ajustmentBox);
s1.setPosition(20, 20);
s1.setSize(100, 20);
s1.setRange(-300, 300);
s1.setValue(vSidesway);
// create slider to control the shift of coordinate system
Slider s2 = gui.addSlider("shift");
s2.moveTo(ajustmentBox);
s2.setPosition(20, 50);
s2.setSize(100, 20);
s2.setRange(-200, 200);
s2.setValue(0);
// create slider to control the scaler of coordinate system
Slider s3 = gui.addSlider("scaler");
s3.moveTo(ajustmentBox);
s3.setPosition(20, 80);
s3.setSize(100, 20);
s3.setRange(0.1, 2);
s3.setValue(1);
// create slider to control the rotate of x-axis
Slider s4 = gui.addSlider("rotateX");
s4.moveTo(ajustmentBox);
s4.setPosition(20, 110);
s4.setSize(100, 20);
s4.setRange(-PI, PI);
s4.setValue(1.19);
// create slider to control the rotate of y-axis
Slider s5 = gui.addSlider("rotateY");
s5.moveTo(ajustmentBox);
s5.setPosition(20, 140);
s5.setSize(100, 20);
s5.setRange(-PI, PI);
s5.setValue(0);
// create slider to control the rotate of z-axis
Slider s6 = gui.addSlider("rotateZ");
s6.moveTo(ajustmentBox);
s6.setPosition(20, 170);
s6.setSize(100, 20);
s6.setRange(-PI, PI);
s6.setValue(1.19);
// create slider to control the value of R-color
Slider s7 = gui.addSlider("R-Value");
s7.moveTo(ajustmentBox);
s7.setPosition(20, 200);
s7.setSize(100, 20);
s7.setRange(0, 255);
s7.setValue(rColor);
// create slider to control the value of B-color
Slider s8 = gui.addSlider("B-Value");
s8.moveTo(ajustmentBox);
s8.setPosition(20, 230);
s8.setSize(100, 20);
s8.setRange(0, 255);
s8.setValue(bColor);
// create slider to control the value of G-color
Slider s9 = gui.addSlider("G-Value");
s9.moveTo(ajustmentBox);
s9.setPosition(20, 260);
s9.setSize(100, 20);
s9.setRange(0, 255);
s9.setValue(gColor);
/*
Button b5 = gui.addButton("controlRevertA",0,65,290,80,24);
b5.moveTo(ajustmentBox);
b5.setColorBackground(color(40));
b5.setColorActive(color(20));
b5.setBroadcast(false);
b5.setValue(1);
b5.setBroadcast(true);
b5.setCaptionLabel("Revert");
*/
// group functionBox
// create a group to store the messageBox elements
functionBox = gui.addGroup("functionBox");
functionBox.setBackgroundHeight(200);
functionBox.setBackgroundColor(color(0,100));
functionBox.setBarHeight(14);
Button b3 = gui.addButton("controlFixed", 0, 65, 20, 80, 24);
b3.moveTo(functionBox);
b3.setColorBackground(color(40));
b3.setColorActive(color(20));
b3.setColorActive(color(20));
b3.setBroadcast(false);
b3.setValue(0);
b3.setBroadcast(true);
b3.setCaptionLabel("FixedCoords");
Button b4 = gui.addButton("controlFree", 0, 155, 20, 80, 24);
b4.moveTo(functionBox);
b4.setColorBackground(color(40));
b4.setColorActive(color(20));
b4.setColorActive(color(20));
b4.setBroadcast(false);
b4.setValue(-1);
b4.setBroadcast(true);
b4.setCaptionLabel("FreeCoords");
xv = gui.addTextarea("outputX");
xv.setPosition(20, 60);
xv.setSize(200, 20);
xv.moveTo(functionBox);
xv.setFont(createFont("arial",12));
xv.setLineHeight(14);
xv.setColor(color(128));
xv.setColorBackground(color(255, 100));
xv.setColorForeground(color(255, 100));
yv = gui.addTextarea("outputY");
yv.setPosition(20, 100);
yv.setSize(200, 20);
yv.moveTo(functionBox);
yv.setFont(createFont("arial",12));
yv.setLineHeight(14);
yv.setColor(color(128));
yv.setColorBackground(color(255, 100));
yv.setColorForeground(color(255, 100));
zv = gui.addTextarea("outputZ");
zv.setPosition(20, 140);
zv.setSize(200, 20);
zv.moveTo(functionBox);
zv.setFont(createFont("arial",12));
zv.setLineHeight(14);
zv.setColor(color(128));
zv.setColorBackground(color(255, 100));
zv.setColorForeground(color(255, 100));
// create a new accordion
// add g1, g2, and g3 to the accordion.
accordion = gui.addAccordion("acc")
.setPosition(20,60)
.setWidth(300)
.addItem(messageBox)
.addItem(ajustmentBox)
.addItem(functionBox)
;
// use Accordion.MULTI to allow multiple group
// to be open at a time.
accordion.setCollapseMode(Accordion.MULTI);
// when in SINGLE mode, only 1 accordion
// group can be open at a time.
accordion.setCollapseMode(Accordion.SINGLE);
}
void inputX(String theString) {
messageBox.hide();
}
void inputY(String theString) {
messageBox.hide();
}
void inputZ(String theString) {
messageBox.hide();
}
void buttonConfirm(int theValue) {
xString = ((Textfield)gui.getController("inputX")).getText();
yString = ((Textfield)gui.getController("inputY")).getText();
zString = ((Textfield)gui.getController("inputZ")).getText();
xValue.append(float(xString));
yValue.append(float(yString));
zValue.append(float(zString));
pointSet += '\n' + " " + xString + " " + yString + " " + zString;
String temp = '\n' + " " + xString + " " + yString + " " + zString;
txt.setText(pointSet);
messageBoxResult = theValue;
((Textfield)gui.getController("inputX")).clear();
((Textfield)gui.getController("inputY")).clear();
((Textfield)gui.getController("inputZ")).clear();
//messageBox.hide();
//functionBox.hide();
//ajustmentBox.hide();
}
void buttonCancel(int theValue) {
xValue.clear();
yValue.clear();
zValue.clear();
txt.setText("X-Value Y-Value Z-Value");
messageBoxResult = theValue;
//messageBox.hide();
//functionBox.hide();
//ajustmentBox.hide();
}
//Drawing 3D points by point sets
void pointSets(float x, float y, float z){
pushMatrix();
translate(x, y, z);
noFill();
stroke(255);
sphere(2.5);
popMatrix();
stroke(100);
line(x, y, z, x, y, 0);
line(x, y, z, x, 0, z);
line(x, y, z, 0, y, z);
}
//Drawing arrows of coordinate system
void arrow(int x, int y, int z){
smooth();
strokeCap(ROUND);
strokeJoin(ROUND);
if(x!=0&&y==0&&z==0){
line(x, 0, 0, x-10, 10, 10);
line(x, 0, 0, x-10, -10, 10);
line(x, 0, 0, x-10, -10, -10);
line(x, 0, 0, x-10, 10, -10);
}
if(y!=0&&x==0&&z==0){
line(0, y, 0, 10, y-10, 10);
line(0, y, 0, -10, y-10, -10);
line(0, y, 0, -10, y-10, 10);
line(0, y, 0, -10, y-10, 10);
}
if(z!=0&&y==0&&x==0){
line(0, 0, z, 10, 10, z-10);
line(0, 0, z, -10, 10, z-10);
line(0, 0, z, -10, -10, z-10);
line(0, 0, z, -10, 10, z-10);
}
}
// move the coordinate system by sidesway slider and shift slider
void moveCoords() {
float s1 = gui.getController("sidesway").getValue();
float s2 = gui.getController("shift").getValue();
if (s1!=0||s2!=0)
{
translate(width/2+s1, height/2+s2, 0);
}
else
{
translate(width/2, height/2, 0);
}
}
// scaler the coordinate system by scaler slider
void scalerCoords() {
float s = gui.getController("scaler").getValue();
float a = s/2;
scale(a);
}
void rotateCoords() {
float x = gui.getController("rotateX").getValue();
float y = gui.getController("rotateY").getValue();
float z = gui.getController("rotateZ").getValue();
rotateX(x);
rotateY(y);
rotateZ(z);
//rotateX(0);
//rotateY(phi);
//rotateZ(the);
}
void controlBox(int theValue) {
if(messageBox.isVisible()) {
messageBox.hide();
functionBox.hide();
ajustmentBox.hide();
control.setLabel("SHOW");
} else {
messageBox.show();
functionBox.show();
ajustmentBox.show();
control.setLabel("Hide");
}
}
void controlRevertA(int theValue) {
aRevert = theValue;
}
void revertAjustment() {
if(aRevert==1)
{
vSidesway = 0.0;
aRevert = -1;
}
}
void controlBackgroundColor() {
float r = gui.getController("R-Value").getValue();
float b = gui.getController("B-Value").getValue();
float g = gui.getController("G-Value").getValue();
rColor = int(r);
bColor = int(b);
gColor = int(g);
background(rColor, bColor, gColor);
}
void pressedEnter() {
if(mousePressed==true)
{
xVariable = mouseX;
yVariable = mouseY;
pointSets(xVariable, yVariable, zVariable);
xValue.append(xVariable);
yValue.append(yVariable);
zValue.append(zVariable);
}
}
void controlFixed(int theValue) {
functionBoxResult = theValue;
}
void controlFree(int theValue) {
functionBoxResult = theValue;
}
void controlCamera() {
if (functionBoxResult == -1)
{
camera(mouseX, mouseY, (height/2) / tan(PI/5), width/2, height/2, 0, 0, 1, 0);
}
}
// main function to realize transforming the mouse position to 3D coordinates
void getPressedPoints(float x1, float y1, float z1, float x2, float y2, float z2, color strokeColour)
{
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);
getPoints(v1.x, v1.y, v1.z);
xVariable = v1.x;
yVariable = v1.y;
zVariable = v1.z;
output();
popMatrix();
}
// get points by mouse press
void getPoints(float x, float y, float z) {
if(mousePressed) {
functionBoxResult = 0;
noCursor();
stroke(255);
mousePointSets(x, y, z);
println(xVariable);
}
else
{
functionBoxResult = -1;
cursor();
}
}
// output the x, y, z value in the textfield of functionbox
void output() {
xv.setText(str(xVariable));
yv.setText(str(yVariable));
zv.setText(str(-zVariable));
}
// draw the points by mouse pressed
void mousePointSets(float x, float y, float z) {
pushMatrix();
translate(x, y, z);
noFill();
stroke(255);
sphere(2.5);
popMatrix();
stroke(100);
}
/*
void drawCoordsSystem() {
for (int i=0; i<500; i += 20){
stroke(96);
line(i, 0, 0, i, 0, 500);
line(0, i, 0, 0, i, 500);
line(0, 0, i, 0, 500, i);
line(i, 0, 0, i, 500, 0);
line(0, 0, i, 500, 0, i);
line(0, i, 0, 500, i, 0);
}
strokeWeight(1);
//z-coordinate: red
stroke(255, 0, 0);
line(0, 0, -500, 0, 0, 500);
arrow(0, 0, 500);
//y-coordinate: green
stroke(0, 102, 0);
line(0, -500, 0, 0, 500, 0);
arrow(0, 500, 0);
//x-coordinate: blue
stroke(0, 76, 153);
line(-500, 0, 0, 500, 0, 0);
arrow(500, 0, 0);
}
*/
You can’t perform that action at this time.