00001 package edu.tum.cs.vis.items;
00002
00003 import edu.tum.cs.util.math.Vector3f;
00004 import edu.tum.cs.vis.Canvas;
00005 import edu.tum.cs.vis.Drawable;
00006
00007
00008 public class Cylinder implements Drawable {
00009
00010 protected int cylinder_detail=0;
00011 protected float[] cylinderX,cylinderZ;
00012 protected float h, r, r2;
00013 protected Vector3f v1, v2;
00014
00015 public Cylinder(Vector3f v1, Vector3f v2, float r){
00016 this(v1,v2,r,r);
00017 }
00018
00019 public Cylinder(Vector3f v1, Vector3f v2, float r, float r2){
00020 h = (float)v1.distance(v2);
00021 this.v1 = v1;
00022 this.v2 = v2;
00023 this.r = r;
00024 this.r2 = r2;
00025 cylinderDetail(30);
00026 }
00027
00028 public void draw(Canvas c) {
00029 c.pushMatrix();
00030 c.noStroke();
00031 c.translate(v1.x, v1.y, v1.z);
00032 Vector3f dir = new Vector3f(v2);
00033 dir.subtract(v1);
00034 Vector3f cp = new Vector3f();
00035 Vector3f yAxis = new Vector3f(0,1,0);
00036 cp.cross(dir, yAxis);
00037 if(cp.distance(new Vector3f(0,0,0)) != 0){
00038 double theta = dir.angle(yAxis);
00039 c.rotateAxis(theta,cp);
00040 }
00041 c.translate(0,h/2,0);
00042 cylinder(c,r,r2,h,true,true);
00043 c.popMatrix();
00044 }
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083 protected void cylinderDetail(int res) {
00084 if(res < 3)
00085 res = 3;
00086 if(res != cylinder_detail) {
00087 float delta = (float) Math.PI * 2 / res;
00088 cylinderX = new float[res];
00089 cylinderZ = new float[res];
00090
00091 for(int i = 0; i < res; i++) {
00092 cylinderX[i] = (float) Math.cos(i * delta);
00093 cylinderZ[i] = (float) Math.sin(i * delta);
00094 }
00095 cylinder_detail = res;
00096 }
00097 }
00098
00099 protected void cylinder(Canvas c, float r1, float r2, float h, boolean topCap, boolean bottomCap) {
00100 if(cylinder_detail == 0) {
00101 cylinderDetail(30);
00102 }
00103 h *= 0.5;
00104 if(topCap) {
00105 c.beginShape(Canvas.TRIANGLE_STRIP);
00106 for(int i = 0; i < cylinder_detail; i++) {
00107 c.vertex(0, -h, 0);
00108 c.vertex(cylinderX[i] * r1, -h, cylinderZ[i] * r1);
00109 }
00110 c.vertex(0, -h, 0);
00111 c.vertex(cylinderX[0] * r1, -h, cylinderZ[0] * r1);
00112 c.endShape();
00113 }
00114 c.beginShape(Canvas.TRIANGLE_STRIP);
00115 for(int i = 0; i < cylinder_detail; i++) {
00116 c.vertex(cylinderX[i] * r1, -h, cylinderZ[i] * r1);
00117 c.vertex(cylinderX[i] * r2, h, cylinderZ[i] * r2);
00118 }
00119 c.vertex(cylinderX[0] * r1, -h, cylinderZ[0] * r1);
00120 c.vertex(cylinderX[0] * r2, h, cylinderZ[0] * r2);
00121 c.endShape();
00122 if(bottomCap) {
00123 c.beginShape(Canvas.TRIANGLE_STRIP);
00124 for(int i = 0; i < cylinder_detail; i++) {
00125 c.vertex(0, h, 0);
00126 c.vertex(cylinderX[i] * r2, h, cylinderZ[i] * r2);
00127 }
00128 c.vertex(0, h, 0);
00129 c.vertex(cylinderX[0] * r2, h, cylinderZ[0] * r2);
00130 c.endShape();
00131 }
00132 }
00133
00134 }