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