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/resorte.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 RESORTE::RESORTE()
00041 {
00042 Extremo1 = NULL;
00043 Extremo2 = NULL;
00044 num_extremos = 0;
00045
00046 l = 1;
00047 ks = 1;
00048 kd = 0;
00049 }
00050
00051 RESORTE::~RESORTE()
00052 {
00053 ;
00054 }
00055
00056 BOOLEAN
00057 RESORTE::insertar_extremo(char *nombre, LISTA<COSA*> *Lista_cosas, VECTOR pos)
00058 {
00059 int i;
00060 COSA *Cosa = NULL;
00061
00062 if ( num_extremos >= 2 ) {
00063 fprintf(stderr,
00064 "<RESORTE> - ERROR: No puedo tener mas de dos extremos! \n"
00065 "No acepto a \"%s\".\n", nombre);
00066 fflush(stderr);
00067 return FALSE;
00068 }
00069
00070 for ( i = 0; i < Lista_cosas->tam(); i++ ) {
00071 if ( strcmp(nombre, (*Lista_cosas)[i]->nombre()) == 0 ) {
00072 Cosa = (*Lista_cosas)[i];
00073 break;
00074 }
00075 }
00076
00077 if ( !Cosa ) {
00078 fprintf(stderr,
00079 "<RESORTE> - ERROR: La COSA \"%s\" no esta definida! \n", nombre);
00080 fflush(stderr);
00081 return FALSE;
00082 }
00083
00084 if ( num_extremos == 0 ) {
00085 Extremo1 = Cosa;
00086 posicion1 = pos;
00087 }
00088 else {
00089 Extremo2 = Cosa;
00090 posicion2 = pos;
00091 }
00092 num_extremos++;
00093 return TRUE;
00094 }
00095
00096 #ifdef GL_ENABLED
00097 void
00098 RESORTE::pintar_gl(CALIDAD_VISUAL * )
00099 {
00100 VECTOR p;
00101
00102 glPushMatrix();
00103
00104
00105 glDisable(GL_LIGHTING);
00106 glColor3f(1, 0, 0);
00107 glBegin(GL_LINES);
00108 p = Extremo1->posicion_absoluta(posicion1);
00109 glVertex3d(p.x, p.y, p.z);
00110 p = Extremo2->posicion_absoluta(posicion2);
00111 glVertex3d(p.x, p.y, p.z);
00112 glEnd();
00113
00114 glPopMatrix();
00115 }
00116 #endif // GL_ENABLED
00117
00118 void
00119 RESORTE::pintar_povray(FILE *fd)
00120 {
00121 VECTOR p1 = Extremo1->posicion_absoluta(posicion1);
00122 VECTOR p2 = Extremo2->posicion_absoluta(posicion2);
00123
00124 fprintf(fd,
00125 "cylinder {\n"
00126 " <%f, %f, %f>\n"
00127 " <%f, %f, %f>\n"
00128 " 0.01\n"
00129 " texture { pigment { color rgb <1, 0, 0> } }\n"
00130 "}\n",
00131 p1.x, p1.y, p1.z, p2.x, p2.y, p2.z
00132 );
00133 }
00134
00135 BOOLEAN
00136 RESORTE::leer(TOKENIZADOR *Sabiondo, LISTA<COSA *> *Lista_cosas)
00137 {
00138 char cad[1000], nombre[1000];
00139 int tipo_token = TK_DESCONOCIDO, pos;
00140 VECTOR posicion;
00141
00142 pos = 1;
00143 while ( tipo_token != TK_CERRAR ) {
00144 tipo_token = Sabiondo->siguiente_token(cad);
00145 switch ( pos ) {
00146 case 1:
00147 ESPERO(TK_CADENA, "una cadena");
00148 if ( strlen(cad) > MAX_CAD-1 ) cad[MAX_CAD-1] = '\0';
00149 des_comille(cad);
00150 set_nombre(cad);
00151 pos++;
00152 break;
00153 case 2: ESPERO(TK_ABRIR, "\"{\""); pos++; break;
00154 default:
00155 if ( tipo_token == TK_CERRAR ) break;
00156 ESPERO(TK_IDENTIFICADOR, "un identificador (1)");
00157 if ( strcmp(cad, "ks") == 0 ) {
00158 tipo_token = Sabiondo->siguiente_token(cad);
00159 ESPERO(TK_NUMERO, "el valor de ks");
00160 ks = atof(cad);
00161 }
00162 else if ( strcmp(cad, "kd") == 0 ) {
00163 tipo_token = Sabiondo->siguiente_token(cad);
00164 ESPERO(TK_NUMERO, "el valor de kd");
00165 kd = atof(cad);
00166 }
00167 else if ( strcmp(cad, "l") == 0 ) {
00168 tipo_token = Sabiondo->siguiente_token(cad);
00169 ESPERO(TK_NUMERO, "el valor de l");
00170 l = atof(cad);
00171 }
00172 else if ( strcmp(cad, "extremo") == 0 ) {
00173 tipo_token = Sabiondo->siguiente_token(cad);
00174 ESPERO(TK_ABRIR, "\"{\"");
00175 tipo_token = Sabiondo->siguiente_token(cad);
00176 ESPERO(TK_CADENA, "una cadena con un nombre de cosa");
00177 des_comille(cad);
00178 strcpy(nombre, cad);
00179 tipo_token = Sabiondo->siguiente_token(cad);
00180 ESPERO(TK_VECTOR_INICIO, "el inicio de un VECTOR");
00181 posicion.x = atof(&cad[1]);
00182 tipo_token = Sabiondo->siguiente_token(cad);
00183 ESPERO(TK_NUMERO, "un numero (dato 2 de un VECTOR)");
00184 posicion.y = atof(cad);
00185 tipo_token = Sabiondo->siguiente_token(cad);
00186 ESPERO(TK_VECTOR_FIN, "el final de un VECTOR");
00187 cad[strlen(cad) - 1] = '\0';
00188 posicion.z = atof(cad);
00189 tipo_token = Sabiondo->siguiente_token(cad);
00190 ESPERO(TK_CERRAR, "\"}\"");
00191
00192 if ( !insertar_extremo(nombre, Lista_cosas, posicion) ) {
00193
00194 }
00195
00196 tipo_token = TK_DESCONOCIDO;
00197 }
00198 ;
00199 break;
00200 }
00201 }
00202
00203 return TRUE;
00204 }
00205
00206
00207
00208
00209