00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "core/uniones/articulacion.h"
00023
00024 #include <ctype.h>
00025 #include <string.h>
00026 #include <stdlib.h>
00027
00028 #include "lista.cc"
00029
00030 #define ESPERO(tipo, msg) \
00031 if ( tipo_token != (tipo) ) { \
00032 fprintf(stderr, "<COSA> ERROR: Esperaba %s y recibi [%s].\n", \
00033 (msg), cad); fflush(stderr); return FALSE; \
00034 }
00035
00036
00037
00038
00039
00040 ARTICULACION::ARTICULACION()
00041 {
00042 Extremo1 = NULL;
00043 Extremo2 = NULL;
00044 num_extremos = 0;
00045
00046 ks = 1;
00047 kd = 0;
00048 }
00049
00050 ARTICULACION::~ARTICULACION()
00051 {
00052 ;
00053 }
00054
00055 BOOLEAN
00056 ARTICULACION::insertar_extremo(char *nombre, LISTA<COSA*> *Lista_cosas, VECTOR pos)
00057 {
00058 int i;
00059 COSA *Cosa = NULL;
00060
00061 if ( num_extremos >= 2 ) {
00062 fprintf(stderr,
00063 "<ARTICULACION> - ERROR: No puedo tener mas de dos extremos! \n"
00064 "No acepto a \"%s\".\n", nombre);
00065 fflush(stderr);
00066 return FALSE;
00067 }
00068
00069 for ( i = 0; i < Lista_cosas->tam(); i++ ) {
00070 if ( strcmp(nombre, (*Lista_cosas)[i]->nombre()) == 0 ) {
00071 Cosa = (*Lista_cosas)[i];
00072 break;
00073 }
00074 }
00075
00076 if ( !Cosa ) {
00077 fprintf(stderr,
00078 "<ARTICULACION> - ERROR: La COSA \"%s\" no esta definida! \n", nombre);
00079 fflush(stderr);
00080 return FALSE;
00081 }
00082
00083 if ( num_extremos == 0 ) {
00084 Extremo1 = Cosa;
00085 posicion1 = pos;
00086 }
00087 else {
00088 Extremo2 = Cosa;
00089 posicion2 = pos;
00090 }
00091 num_extremos++;
00092 return TRUE;
00093 }
00094
00095 #ifdef GL_ENABLED
00096
00097 void
00098 ARTICULACION::pintar_gl(CALIDAD_VISUAL * )
00099 {
00100
00101 MATERIAL m;
00102 COLOR c(0, 1, 0);
00103
00104 m.set_difusa(c);
00105 m.activar_gl();
00106 glShadeModel(GL_SMOOTH);
00107 glEnable(GL_LIGHTING);
00108
00109 if ( !TEXTURA_glu_global ) TEXTURA_glu_global = gluNewQuadric();
00110
00111
00112 VECTOR p1 = Extremo1->posicion_absoluta(posicion1);
00113 VECTOR p2 = Extremo2->posicion_absoluta(posicion2);
00114 VECTOR v = p2 - p1;
00115 double d = v.norma();
00116 MATRIZ_4x4 R;
00117 VECTOR pt;
00118
00119 v.normalizar();
00120 R.rotacion_punto_punto(p1, p2);
00121
00122
00123 glPushMatrix();
00124 glTranslatef((float)p1.x, (float)p1.y, (float)p1.z);
00125 R.cargar_gl();
00126 gluCylinder(TEXTURA_glu_global, 0.02, 0.02, d, 6, 1);
00127 gluDisk(TEXTURA_glu_global, 0.02, 0.05, 6, 1);
00128 glPopMatrix();
00129
00130
00131 glPushMatrix();
00132 pt = p1 - v * 0.02;
00133 glTranslatef((float)pt.x, (float)pt.y, (float)pt.z);
00134 R.cargar_gl();
00135 gluCylinder(TEXTURA_glu_global, 0.05, 0.05, 0.02, 6, 1);
00136 glRotated(180, 1, 0, 0);
00137 gluDisk(TEXTURA_glu_global, 0, 0.05, 6, 1);
00138 glPopMatrix();
00139
00140
00141 glPushMatrix();
00142 glTranslatef((float)p2.x, (float)p2.y, (float)p2.z);
00143 R.cargar_gl();
00144 gluCylinder(TEXTURA_glu_global, 0.05, 0.05, 0.02, 6, 1);
00145 glRotated(180, 1, 0, 0);
00146 gluDisk(TEXTURA_glu_global, 0.02, 0.05, 6, 1);
00147 glPopMatrix();
00148
00149
00150 glPushMatrix();
00151 pt = p2 + v * 0.02;
00152 glTranslatef((float)pt.x, (float)pt.y, (float)pt.z);
00153 R.cargar_gl();
00154 gluDisk(TEXTURA_glu_global, 0, 0.05, 6, 1);
00155 glPopMatrix();
00156 }
00157 #endif // GL_ENABLED
00158
00159 void
00160 ARTICULACION::pintar_povray(FILE *fd)
00161 {
00162 VECTOR p1 = Extremo1->posicion_absoluta(posicion1);
00163 VECTOR p2 = Extremo2->posicion_absoluta(posicion2);
00164
00165 fprintf(fd,
00166 "cylinder {\n"
00167 " <%f, %f, %f>\n"
00168 " <%f, %f, %f>\n"
00169 " 0.02\n"
00170 " texture { pigment { color rgb <0, 1, 0> } }\n"
00171 "}\n",
00172 p1.x, p1.y, p1.z, p2.x, p2.y, p2.z
00173 );
00174
00175 VECTOR v = p2 - p1, pt;
00176
00177 v.normalizar();
00178 pt = p1 - v * 0.02;
00179
00180 fprintf(fd,
00181 "cylinder {\n"
00182 " <%f, %f, %f>\n"
00183 " <%f, %f, %f>\n"
00184 " 0.05\n"
00185 " texture { pigment { color rgb <0, 1, 0> } }\n"
00186 "}\n",
00187 p1.x, p1.y, p1.z, pt.x, pt.y, pt.z
00188 );
00189
00190 pt = p2 + v * 0.02;
00191 fprintf(fd,
00192 "cylinder {\n"
00193 " <%f, %f, %f>\n"
00194 " <%f, %f, %f>\n"
00195 " 0.05\n"
00196 " texture { pigment { color rgb <0, 1, 0> } }\n"
00197 "}\n",
00198 p2.x, p2.y, p2.z, pt.x, pt.y, pt.z
00199 );
00200 }
00201
00202 BOOLEAN
00203 ARTICULACION::leer(TOKENIZADOR *Sabiondo, LISTA<COSA *> *Lista_cosas)
00204 {
00205 char cad[1000], nombre[1000];
00206 int tipo_token = TK_DESCONOCIDO, pos;
00207 VECTOR posicion;
00208
00209 pos = 1;
00210 while ( tipo_token != TK_CERRAR ) {
00211 tipo_token = Sabiondo->siguiente_token(cad);
00212 switch ( pos ) {
00213 case 1:
00214 ESPERO(TK_CADENA, "una cadena");
00215 if ( strlen(cad) > MAX_CAD-1 ) cad[MAX_CAD-1] = '\0';
00216 des_comille(cad);
00217 set_nombre(cad);
00218 pos++;
00219 break;
00220 case 2: ESPERO(TK_ABRIR, "\"{\""); pos++; break;
00221 default:
00222 if ( tipo_token == TK_CERRAR ) break;
00223 ESPERO(TK_IDENTIFICADOR, "un identificador (1)");
00224 if ( strcmp(cad, "ks") == 0 ) {
00225 tipo_token = Sabiondo->siguiente_token(cad);
00226 ESPERO(TK_NUMERO, "el valor de ks");
00227 ks = atof(cad);
00228 }
00229 else if ( strcmp(cad, "kd") == 0 ) {
00230 tipo_token = Sabiondo->siguiente_token(cad);
00231 ESPERO(TK_NUMERO, "el valor de kd");
00232 kd = atof(cad);
00233 }
00234 else if ( strcmp(cad, "extremo") == 0 ) {
00235 tipo_token = Sabiondo->siguiente_token(cad);
00236 ESPERO(TK_ABRIR, "\"{\"");
00237 tipo_token = Sabiondo->siguiente_token(cad);
00238 ESPERO(TK_CADENA, "una cadena con un nombre de cosa");
00239 des_comille(cad);
00240 strcpy(nombre, cad);
00241 tipo_token = Sabiondo->siguiente_token(cad);
00242 ESPERO(TK_VECTOR_INICIO, "el inicio de un VECTOR");
00243 posicion.x = atof(&cad[1]);
00244 tipo_token = Sabiondo->siguiente_token(cad);
00245 ESPERO(TK_NUMERO, "un numero (dato 2 de un VECTOR)");
00246 posicion.y = atof(cad);
00247 tipo_token = Sabiondo->siguiente_token(cad);
00248 ESPERO(TK_VECTOR_FIN, "el final de un VECTOR");
00249 cad[strlen(cad) - 1] = '\0';
00250 posicion.z = atof(cad);
00251 tipo_token = Sabiondo->siguiente_token(cad);
00252 ESPERO(TK_CERRAR, "\"}\"");
00253
00254 if ( !insertar_extremo(nombre, Lista_cosas, posicion) ) {
00255
00256 }
00257
00258 tipo_token = TK_DESCONOCIDO;
00259 }
00260 ;
00261 break;
00262 }
00263 }
00264
00265 return TRUE;
00266 }
00267
00268
00269
00270
00271