00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include <sofa/helper/gl/Cylinder.h>
00028
00029 #include <assert.h>
00030 #include <algorithm>
00031 #include <iostream>
00032 using std::cout;
00033 using std::cerr;
00034 using std::endl;
00035
00036
00037 namespace sofa
00038 {
00039
00040 namespace helper
00041 {
00042
00043 namespace gl
00044 {
00045
00046 static const int quadricDiscretisation = 16;
00047
00048
00049 std::map < std::pair<std::pair<float,float>,float>, Cylinder* > Cylinder::CylinderMap;
00050
00051 void Cylinder::initDraw()
00052 {
00053 if (quadratic!=NULL) return;
00054
00055 quadratic=gluNewQuadric();
00056 gluQuadricNormals(quadratic, GLU_SMOOTH);
00057 gluQuadricTexture(quadratic, GL_TRUE);
00058
00059 displayList=glGenLists(1);
00060
00061 glNewList(displayList, GL_COMPILE);
00062
00063 glColor3f(1,1,0);
00064
00065
00066 if (length[0] > 0.0)
00067 {
00068 glRotatef(90,0,1,0);
00069 glTranslated(0,0,-length[0]/2);
00070 gluSphere(quadratic,length[0]/15,quadricDiscretisation,quadricDiscretisation/2);
00071 gluCylinder(quadratic,length[0]/15,length[0]/15,length[0],quadricDiscretisation,quadricDiscretisation);
00072 glTranslated(0,0,length[0]);
00073 gluSphere(quadratic,length[0]/15,quadricDiscretisation,quadricDiscretisation/2);
00074 glTranslated(0,0,-length[0]/2);
00075 glRotatef(-90,0,1,0);
00076 }
00077 if (length[1] > 0.0)
00078 {
00079 glRotatef(90,1,0,0);
00080 glTranslated(0,0,-length[1]/2);
00081 gluSphere(quadratic,length[1]/15,quadricDiscretisation,quadricDiscretisation/2);
00082 gluCylinder(quadratic,length[1]/15,length[1]/15,length[1],quadricDiscretisation,quadricDiscretisation);
00083 glTranslated(0,0,length[1]);
00084 gluSphere(quadratic,length[1]/15,quadricDiscretisation,quadricDiscretisation/2);
00085 glTranslated(0,0,-length[1]/2);
00086 glRotatef(-90,1,0,0);
00087
00088 }
00089 if (length[2] > 0.0)
00090 {
00091 glTranslated(0,0,-length[2]/2);
00092 gluSphere(quadratic,length[2]/15,quadricDiscretisation,quadricDiscretisation/2);
00093 gluCylinder(quadratic,length[2]/15,length[2]/15,length[2],quadricDiscretisation,quadricDiscretisation);
00094 glTranslated(0,0,length[2]);
00095 gluSphere(quadratic,length[2]/15,quadricDiscretisation,quadricDiscretisation/2);
00096 glTranslated(0,0,-length[2]/2);
00097 }
00098 glEndList();
00099 }
00100
00101 void Cylinder::draw()
00102 {
00103 initDraw();
00104
00105 glPushMatrix();
00106 glPushAttrib(GL_ENABLE_BIT);
00107
00108 glEnable(GL_LIGHTING);
00109 glEnable(GL_COLOR_MATERIAL);
00110 glMultMatrixd(matTransOpenGL);
00111 glCallList(displayList);
00112
00113 glPopAttrib();
00114 glPopMatrix();
00115 }
00116
00117 void Cylinder::update(const double *mat)
00118 {
00119 std::copy(mat,mat+16, matTransOpenGL);
00120 }
00121
00122 void Cylinder::update(const Vector3& center, const double orient[4][4])
00123 {
00124 matTransOpenGL[0] = orient[0][0];
00125 matTransOpenGL[1] = orient[0][1];
00126 matTransOpenGL[2] = orient[0][2];
00127 matTransOpenGL[3] = 0;
00128
00129 matTransOpenGL[4] = orient[1][0];
00130 matTransOpenGL[5] = orient[1][1];
00131 matTransOpenGL[6] = orient[1][2];
00132 matTransOpenGL[7] = 0;
00133
00134 matTransOpenGL[8] = orient[2][0];
00135 matTransOpenGL[9] = orient[2][1];
00136 matTransOpenGL[10]= orient[2][2];
00137 matTransOpenGL[11] = 0;
00138
00139 matTransOpenGL[12] = center[0];
00140 matTransOpenGL[13] = center[1];
00141 matTransOpenGL[14] = center[2];
00142 matTransOpenGL[15] = 1;
00143 }
00144
00145 void Cylinder::update(const Vector3& center, const Quaternion& orient)
00146 {
00147 orient.writeOpenGlMatrix(matTransOpenGL);
00148 matTransOpenGL[12] = center[0];
00149 matTransOpenGL[13] = center[1];
00150 matTransOpenGL[14] = center[2];
00151 }
00152
00153 Cylinder::Cylinder(SReal len)
00154 {
00155 quadratic = NULL;
00156 length = Vector3(len,len,len);
00157 update(Vector3(0,0,0), Quaternion(1,0,0,0));
00158 }
00159
00160 Cylinder::Cylinder(const Vector3& len)
00161 {
00162 quadratic = NULL;
00163 length = len;
00164 update(Vector3(0,0,0), Quaternion(1,0,0,0));
00165 }
00166
00167 Cylinder::Cylinder(const Vector3& center, const Quaternion& orient, const Vector3& len)
00168 {
00169 quadratic = NULL;
00170 length = len;
00171 update(center, orient);
00172 }
00173
00174 Cylinder::Cylinder(const Vector3& center, const double orient[4][4], const Vector3& len)
00175 {
00176 quadratic = NULL;
00177 length = len;
00178 update(center, orient);
00179 }
00180
00181 Cylinder::Cylinder(const double *mat, const Vector3& len)
00182 {
00183 quadratic = NULL;
00184 length = len;
00185 update(mat);
00186 }
00187
00188 Cylinder::Cylinder(const Vector3& center, const Quaternion& orient, SReal len)
00189 {
00190 quadratic = NULL;
00191 length = Vector3(len,len,len);
00192 update(center, orient);
00193 }
00194 Cylinder::Cylinder(const Vector3& center, const double orient[4][4], SReal len)
00195 {
00196 quadratic = NULL;
00197 length = Vector3(len,len,len);
00198 update(center, orient);
00199 }
00200
00201 Cylinder::Cylinder(const double *mat, SReal len)
00202 {
00203 quadratic = NULL;
00204 length = Vector3(len,len,len);
00205 update(mat);
00206 }
00207
00208 Cylinder::~Cylinder()
00209 {
00210 if (quadratic != NULL)
00211 gluDeleteQuadric(quadratic);
00212 }
00213
00214 Cylinder* Cylinder::get(const Vector3& len)
00215 {
00216 Cylinder*& a = CylinderMap[std::make_pair(std::make_pair((float)len[0],(float)len[1]),(float)len[2])];
00217 if (a==NULL)
00218 a = new Cylinder(len);
00219 return a;
00220 }
00221
00222 void Cylinder::draw(const Vector3& center, const Quaternion& orient, const Vector3& len)
00223 {
00224 Cylinder* a = get(len);
00225 a->update(center, orient);
00226 a->draw();
00227 }
00228
00229 void Cylinder::draw(const Vector3& center, const double orient[4][4], const Vector3& len)
00230 {
00231 Cylinder* a = get(len);
00232 a->update(center, orient);
00233 a->draw();
00234 }
00235
00236 void Cylinder::draw(const double *mat, const Vector3& len)
00237 {
00238 Cylinder* a = get(len);
00239 a->update(mat);
00240 a->draw();
00241 }
00242
00243 void Cylinder::draw(const Vector3& center, const Quaternion& orient, SReal len)
00244 {
00245 Cylinder* a = get(Vector3(len,len,len));
00246 a->update(center, orient);
00247 a->draw();
00248 }
00249
00250 void Cylinder::draw(const Vector3& center, const double orient[4][4], SReal len)
00251 {
00252 Cylinder* a = get(Vector3(len,len,len));
00253 a->update(center, orient);
00254 a->draw();
00255 }
00256
00257 void Cylinder::draw(const double *mat, SReal len)
00258 {
00259 Cylinder* a = get(Vector3(len,len,len));
00260 a->update(mat);
00261 a->draw();
00262 }
00263
00264 }
00265
00266 }
00267
00268 }
00269