Página principal | Jerarquía de la clase | Lista alfabética | Lista de componentes | Lista de archivos | Miembros de las clases | Archivos de los miembros | Páginas relacionadas

vs_vector.C

Ir a la documentación de este archivo.
00001 //===========================================================================
00002 //= vs_vector.cc                              Julio de 1998, enero del 2000 =
00003 //=-------------------------------------------------------------------------=
00004 //= Definicion de clases de visores de sensores vector                      =
00005 //=-------------------------------------------------------------------------=
00006 //= ADVERTENCIA: ESTE SOFTWARE NO ESTA CONCEBIDO NI DISENNADO PARA EL USO   =
00007 //= EN EQUIPO DE CONTROL EN LINEA EN ENTORNOS PELIGROSOS QUE REQUIERAN UN   =
00008 //= DESEMPENNO LIBRE DE FALLAS, COMO LA OPERACION DE PLANTAS NUCLEARES,     = 
00009 //= SISTEMAS DE NAVEGACION O COMUNICACION EN AVIONES, TRAFICO AEREO,        =
00010 //= EQUIPO MEDICO DEL CUAL DEPENDAN VIDAS HUMANAS O SISTEMAS DE ARMAMENTO,  =
00011 //= EN LOS CUALES UNA FALLA EN EL SOFTWARE PUEDA IMPLICAR DIRECTAMENTE LA   =
00012 //= MUERTE, DANNOS PERSONALES O DANNOS FISICOS Y/O AMBIENTALES GRAVES       =
00013 //= ("ACTIVIDADES DE ALGO RIESGO").                                         =
00014 //=-------------------------------------------------------------------------=
00015 //= Autor original: Oscar J. Chavarro G.  A.K.A. JEDILINK. Copyright (c),   =
00016 //= 1997 - 2003, oscarchavarro@hotmail.com                                  =
00017 //= AQUYNZA es software libre, y se rige bajo los terminos de la licencia   =
00018 //= LGPL de GNU (http://www.gnu.org). Para mayor informacion respecto a la  =
00019 //= licencia de uso, consulte el archivo ./doc/LICENCIA en la distribucion. =
00020 //===========================================================================
00021 
00022 #include "jed_defs.h"
00023 #include "jed_gl.h"
00024 
00025 #include "framework/visual/visor.h"
00026 
00027 #ifdef VEL_ROSITA
00028     #include "framework/visual/v_sensor.h"
00029 #endif
00030 
00031 #include <stdio.h>
00032 #include <string.h>
00033 #include <stdlib.h>
00034 
00035 #ifdef GLTT_ENABLED
00036     #include "toolkits/util/gl_fonts.h"
00037 #endif
00038 
00039 //===========================================================================
00040 //= Constantes y macros                                                     =
00041 //===========================================================================
00042 
00043 #define ESPERO(tipo, msg) \
00044     if ( tipo_token != (tipo) ) { \
00045         fprintf(stderr, "<VISOR_SENSOR_VECTOR> " \
00046           "ERROR: Esperaba %s y recibi [%s].\n", \
00047           (msg), cad); fflush(stderr);  return FALSE; \
00048     }
00049 
00050 #define VERIFICAR(c) \
00051     if ( !(c) ) {\
00052         fprintf(stderr, \
00053             "<VISOR_SENSOR_VECTOR> " \
00054             "ERROR: No se pudo crear un objeto dinamico!\n");\
00055         fflush(stderr);\
00056         exit(1);\
00057     }
00058 
00059 #define DATA_PERCENT_X(V) ( ((V).x-min_val.x) / (max_val.x-min_val.x) )
00060 #define DATA_PERCENT_Y(V) ( ((V).y-min_val.y) / (max_val.y-min_val.y) )
00061 #define DATA_PERCENT_Z(V) ( ((V).z-min_val.z) / (max_val.z-min_val.z) )
00062 
00063 //===========================================================================
00064 //= Clase VISOR_SENSOR_VECTOR                                          =
00065 //===========================================================================
00066 
00067 VISOR_SENSOR_VECTOR::VISOR_SENSOR_VECTOR() : VISOR_SENSOR()
00068 {
00069     Sensor = NULL;
00070     min_pos = -1;
00071     pos = -1;
00072     _tipo = 2;
00073     tam_data = 50;
00074     data = new VECTOR[tam_data];
00075     if ( !data ) {
00076         _tipo = 0;
00077         tam_data = 0;
00078     }
00079 
00080     min_val.x = -1;
00081     min_val.y = -1;
00082     min_val.z = -1;
00083     max_val.x = 1;
00084     max_val.y = 1;
00085     max_val.z = 1;
00086 }
00087 
00088 VISOR_SENSOR_VECTOR::VISOR_SENSOR_VECTOR(int nuevo_tipo) : VISOR_SENSOR()
00089 {
00090     Sensor = NULL;
00091     min_pos = -1;
00092 
00093     pos = -1;
00094     _tipo = nuevo_tipo;
00095     if ( _tipo == 2 ) tam_data = 50; else tam_data = 1;
00096     data = new VECTOR[tam_data];
00097     if ( !data ) {
00098         _tipo = 0;
00099         tam_data = 0;
00100     }
00101 
00102 }
00103 
00104 VISOR_SENSOR_VECTOR::~VISOR_SENSOR_VECTOR()
00105 {
00106 
00107 }
00108 
00109 #ifdef GL_ENABLED
00110 void
00111 VISOR_SENSOR_VECTOR::pintar_vector_gl(CAMARA *Camara)
00112 {
00113     int i;
00114     double factor;
00115 
00116     //- Prepare la camara --------------------------------------------------
00117     glClear(GL_DEPTH_BUFFER_BIT);
00118 
00119     if ( Camara ) {
00120         Camara->activar_gl();
00121       }
00122       else {
00123         glMatrixMode(GL_PROJECTION);
00124         glLoadIdentity();
00125     }
00126 
00127     glMatrixMode(GL_MODELVIEW);
00128     glLoadIdentity();
00129     glEnable(GL_DEPTH_TEST);
00130     glDisable(GL_LIGHTING);
00131     glShadeModel(GL_FLAT);
00132 
00133     //- Do the trick --------------------------------------------------------
00134     //glLineWidth(2.0);
00135     if ( tam_grilla > 0 ) {
00136         glColor4f(1, 1, 1, (float)_transparencia); // Ejes
00137         glBegin(GL_LINES);
00138             glVertex3f(0, 0, 0);  glVertex3f(1, 0, 0);
00139             glVertex3f(0, 0, 0);  glVertex3f(0, 1, 0);
00140             glVertex3f(0, 0, 0);  glVertex3f(0, 0, 1);
00141         glEnd();
00142     }
00143 
00144     //glLineWidth(1.0);
00145     glColor4f(0, 1, 0, (float)_transparencia);
00146     glBegin(GL_LINE_STRIP);
00147     factor = 1.0/tam_data * (tam_data - this->pos);
00148     for ( i = 0; min_pos < 0 && i <= this->pos; i++ ) { // Primera vez
00149         glColor4f(0, (float)factor, 0, (float)_transparencia);
00150         glVertex3f((float)data[i].x, (float)data[i].y, (float)data[i].z);
00151         factor += 1.0/tam_data;
00152     }
00153     factor = 0;
00154     for ( i = min_pos; min_pos >= 0 && i < tam_data; i++ ) { // Buffer circular
00155         glColor4f(0, (float)factor, 0, (float)_transparencia);
00156         glVertex3f((float)data[i].x, (float)data[i].y, (float)data[i].z);
00157         factor += 1.0/tam_data;
00158     }
00159     for ( i = 0; min_pos >= 0 && i <= this->pos; i++ ) {
00160         glColor4f(0, (float)factor, 0, (float)_transparencia);
00161         glVertex3f((float)data[i].x, (float)data[i].y, (float)data[i].z);
00162         factor += 1.0/tam_data;
00163     }
00164     glEnd();
00165 
00166     //glPointSize(4.0);
00167     glColor4f(1, 0, 0, (float)_transparencia);
00168     glBegin(GL_POINTS);
00169         glVertex3f((float)data[this->pos].x, (float)data[this->pos].y, (float)data[this->pos].z);
00170     glEnd();
00171 }
00172 
00173 void
00174 VISOR_SENSOR_VECTOR::init_fonts(void)
00175 {
00176 
00177 }
00178 
00179 void
00180 VISOR_SENSOR_VECTOR::pintar_vistavion1_gl(CAMARA *Camara)
00185 {
00186     //-----------------------------------------------------------------------
00187     glMatrixMode(GL_PROJECTION);   glLoadIdentity();
00188     glMatrixMode(GL_MODELVIEW);    glLoadIdentity();
00189     glDisable(GL_DEPTH_TEST);      glDisable(GL_LIGHTING);
00190     glShadeModel(GL_FLAT);
00191 
00192     //-----------------------------------------------------------------------
00193     double dx = 0.15, dy = 0.15;
00194     glColor4d(0, 1, 0, _transparencia);
00195     glBegin(GL_LINES);
00196         glVertex2d(1-dx, 1-dy);
00197         glVertex2d(-1+dx, 1-dy);
00198     glEnd();
00199     glBegin(GL_LINES);
00200         glVertex2d(0, 1-dy-EPSILON);
00201         glVertex2d(0, 1-dy-3*dy/6);
00202     glEnd();
00203 
00204     //-----------------------------------------------------------------------
00205   #ifdef GLTT_ENABLED
00206     static FTFace *font = NULL;
00207     static GLTTFont* vector_font = NULL;
00208     int llx, lly, urx, ury;
00209     double factor = 2*Camara->factor_font();
00210 
00211     //- Inicializacion del raster de letras -
00212     font = chequear_fuente("./etc/fonts/crystal.ttf");
00213     if ( !font ) {
00214         fprintf(stderr,
00215             "<CAMARA> - Error fatal: No encuentro una truetype font!\n");
00216         fflush(stderr);
00217         exit(1);
00218     }
00219     if ( !vector_font ) {
00220         vector_font = new GLTTFont(font);
00221     }
00222     // OJO: Esto deberia hacerce una vez por vista?
00223     static int num_vistas = 2;
00224     if ( num_vistas > 0 ) {
00225         if ( !vector_font->create(150) ) {
00226             fprintf(stderr,"<VISOR_SENSOR_VECTOR> ERROR fatal: "
00227                 "no puedo crear una fuente GLTT");
00228             fflush(stderr);
00229             exit(1);
00230         }
00231         num_vistas--;
00232     }
00233     ACTIVAR_TRANSPARENCIA(_transparencia);
00234 
00235     //-----------------------------------------------------------------------
00236     double vref = RAD2DEG(data[pos].x)-5;
00237     int val = (int)((floor(vref))) % 360;
00238     int i, v2;
00239     char cad[10];
00240     double x, dyf;
00241     double offset, inc = (2-2*dx)/10;
00242 
00243     while ( val < 0 ) val += 360;    while ( val >= 360 ) val -= 360;
00244     while ( vref < 0 ) vref += 360;  while ( vref >= 360 ) vref -= 360;
00245     offset = inc * (double)(( (val%10) + (vref-val) ))/5;
00246     val -= val % 10;
00247 
00248     //- Generacion de primitivas para la letra y el cuadro -
00249     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00250     for ( x = -1+dx+offset, i = 0; x <= 1-dx+EPSILON; x += inc, i++ ) {
00251         glPushMatrix();
00252         glTranslated(x, 0, 0);
00253         glBegin(GL_LINES);
00254            glVertex2d(0, 1-dy+2*EPSILON);
00255            if ( i % 2 ) glVertex2d(0, 1-2.7*dy/4);
00256            else glVertex2d(0, 1-2.3*dy/4);
00257         glEnd();
00258         if ( i % 2 ) { glPopMatrix(); continue; }
00259         v2 = val - 10 * (-3+i/2);
00260         while ( v2 < 0 ) v2 += 360;
00261         while ( v2 >= 360 ) v2 -= 360;
00262         sprintf(cad, "%d", v2);
00263         vector_font->getBBox(cad, llx, lly, urx, ury);
00264         dyf = ((double)ury) * factor;
00265         glTranslated(-((double)urx/2)*factor, 1.0 - (double)dyf, 0.0f);
00266         glScaled(factor, factor, factor);
00267         vector_font->output(cad);
00268         glPopMatrix();
00269     }
00270 
00271     //-----------------------------------------------------------------------
00272     glDisable(GL_BLEND);
00273   #endif
00274 }
00275 
00276 void
00277 VISOR_SENSOR_VECTOR::pintar_vistavion2_gl(CAMARA *Camara)
00282 {
00283     //-----------------------------------------------------------------------
00284     glMatrixMode(GL_PROJECTION);   glLoadIdentity();
00285     glMatrixMode(GL_MODELVIEW);    glLoadIdentity();
00286     glDisable(GL_DEPTH_TEST);      glDisable(GL_LIGHTING);
00287     glShadeModel(GL_FLAT);
00288 
00289     //-----------------------------------------------------------------------
00290     glColor4d(0, 1, 0, _transparencia);
00291 
00292     //-----------------------------------------------------------------------
00293   #ifdef GLTT_ENABLED
00294     static FTFace *font = NULL;
00295     static GLTTFont* vector_font = NULL;
00296     int llx, lly, urx, ury;
00297     double factor = 2*Camara->factor_font();
00298     double /*dx = 0.15,*/ dy = 0.15;
00299 
00300     //- Inicializacion del raster de letras -
00301     font = chequear_fuente("./etc/fonts/crystal.ttf");
00302     if ( !font ) {
00303         fprintf(stderr,
00304             "<CAMARA> - Error fatal: No encuentro una truetype font!\n");
00305         fflush(stderr);
00306         exit(1);
00307     }
00308     if ( !vector_font ) {
00309         vector_font = new GLTTFont(font);
00310     }
00311     // OJO: Esto deberia hacerce una vez por vista?
00312     static int num_vistas = 2;
00313     if ( num_vistas > 0 ) {
00314         if ( !vector_font->create(150) ) {
00315             fprintf(stderr,"<VISOR_SENSOR_VECTOR> ERROR fatal: "
00316                 "no puedo crear una fuente GLTT");
00317             fflush(stderr);
00318             exit(1);
00319         }
00320         num_vistas--;
00321     }
00322     ACTIVAR_TRANSPARENCIA(_transparencia);
00323 
00324     //-----------------------------------------------------------------------
00325     double a = data[pos].y;
00326     while ( a < -90 ) a += 360;
00327     while ( a >= 90 ) a -= 360;
00328 
00329     double vref = RAD2DEG(a) + 2.5;
00330     int val = (int)((floor(vref)));
00331     int i, v2;
00332     char cad[10];
00333     double y, dyf;
00334     double offset, inc = 4.0/10.0;
00335 
00336 
00337     offset = inc * (double)(( (val%10) + (vref-val) ))/5;
00338     val -= val % 10;
00339 
00340     //- Generacion de primitivas para la letra y el cuadro -
00341     glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
00342     glPushMatrix();
00343     glRotated(RAD2DEG(data[pos].z), 0, 0, 1);
00344     glBegin(GL_LINES);
00345         glVertex2d(-0.1, 0);
00346         glVertex2d(0.1, 0);
00347     glEnd();
00348     for ( y = -1-offset, i = 0; y <= 1+EPSILON; y += inc, i++ ) {
00349         glPushMatrix();
00350             glTranslated(0.5, y, 0);
00351             glBegin(GL_LINES);
00352                glVertex2d(-dy, 0);  glVertex2d(dy, 0);
00353             glEnd();
00354             v2 = val + 5 * (-3+i);
00355             sprintf(cad, "%d", v2);
00356             vector_font->getBBox(cad, llx, lly, urx, ury);
00357             dyf = ((double)ury) * factor;
00358             glTranslated(1.8*dy-((double)urx/2)*factor, -(double)dyf/2, 0.0f);
00359             glScaled(factor, factor, factor);
00360             vector_font->output(cad);
00361         glPopMatrix();
00362         glPushMatrix();
00363             glTranslated(-0.5, y, 0);
00364             glBegin(GL_LINES);
00365                glVertex2d(-dy, 0);  glVertex2d(dy, 0);
00366             glEnd();
00367             v2 = val + 5 * (-3+i);
00368             sprintf(cad, "%d", v2);
00369             vector_font->getBBox(cad, llx, lly, urx, ury);
00370             dyf = ((double)ury) * factor;
00371             glTranslated(-2*dy-((double)urx/2)*factor, -(double)dyf/2, 0.0f);
00372             glScaled(factor, factor, factor);
00373             vector_font->output(cad);
00374         glPopMatrix();
00375     }
00376     glPopMatrix();
00377 
00378     //-----------------------------------------------------------------------
00379     glDisable(GL_BLEND);
00380   #endif
00381 }
00382 
00383 void
00384 VISOR_SENSOR_VECTOR::pintar_gl(void)
00389 {
00390     //-----------------------------------------------------------------------
00391     // Calcule la nueva posicion a manejar
00392     pos++;
00393     if ( min_pos >= 0 ) min_pos++;
00394     if ( pos >= tam_data ) {
00395         pos = 0;
00396         min_pos = 1;
00397     }
00398 
00399     // Actualice el valor de la posicion actual
00400     if ( Sensor ) data[pos] = Sensor->evaluar();
00401     else { data[pos].x = data[pos].y = data[pos].z = 0; };
00402 
00403     //-----------------------------------------------------------------------
00404     if ( tam_grilla > 0 ) {
00405         camara.subbillboard_gl(tam_grilla, x_grilla, y_grilla, 
00406                                dx_grilla, dy_grilla, cuadre_grilla);
00407       }
00408       else {
00409         camara.billboard_gl(CAM_NO_CUADRAR);
00410     }
00411     ACTIVAR_TRANSPARENCIA(_transparencia);
00412     glDisable(GL_DEPTH_TEST);
00413     glDisable(GL_LIGHTING);
00414     glShadeModel(GL_FLAT);
00415 
00416 switch ( _tipo ) {
00417   //---------------------------------------------------------------------------
00418   case 1: // Tres alturas
00419     glColor4f(1, 0, 0, (float)_transparencia);
00420     jvs_cuadro_gl(-1, -1, 2.0f/3.0f, 2,
00421         (float)DATA_PERCENT_X(data[pos]), _transparencia);
00422     glColor4f(0, 1, 0, (float)_transparencia);
00423     jvs_cuadro_gl(-1+2.0f/3.0f, -1, 2.0f/3.0f, 2, 
00424         (float)DATA_PERCENT_Y(data[pos]), _transparencia);
00425     glColor4f(0, 0, 1, (float)_transparencia);
00426     jvs_cuadro_gl(-1+4.0f/3.0f, -1, 2.0f/3.0f, 2,
00427         (float)DATA_PERCENT_Z(data[pos]), _transparencia);
00428     break;
00429 
00430   //---------------------------------------------------------------------------
00431   case 2: // Trayectoria 3D
00432     pintar_vector_gl(&camara);
00433     break;
00434 
00435   //---------------------------------------------------------------------------
00436   case 3: // Vista de avion1
00437     pintar_vistavion1_gl(&camara);
00438     break;
00439 
00440   //---------------------------------------------------------------------------
00441   case 4: // Vista de avion2
00442     pintar_vistavion2_gl(&camara);
00443     break;
00444 
00445   //---------------------------------------------------------------------------
00446   default: // Traza una X para indicar que algo no esta bien...
00447     glColor4f(1.0, 1.0, 1.0, (float)_transparencia);
00448     glBegin(GL_LINES);
00449         glVertex2f(-1, -1);        glVertex2f(1, 1);
00450         glVertex2f(-1, 1);      glVertex2f(1, -1);
00451     glEnd();
00452     break;
00453   //---------------------------------------------------------------------------
00454 }
00455 
00456 }
00457 #endif // GL_ENABLED
00458 
00459 int
00460 VISOR_SENSOR_VECTOR::tipo(void)
00461 {
00462     return T_VECTOR;
00463 }
00464 
00465 //= SERVICIOS DE PERSISTENCIA ===============================================
00466 
00467 BOOLEAN
00468 VISOR_SENSOR_VECTOR::leer(TOKENIZADOR *Sabiondo)
00469 {
00470     char cad[1000];
00471     int tipo_token = TK_DESCONOCIDO, pos;
00472 
00473     //- Ejecute el parser especifico de la COSA_RIGIDA -----------------
00474     pos = 1;
00475     while ( tipo_token != TK_CERRAR) {
00476         tipo_token = Sabiondo->siguiente_token(cad);
00477         switch ( pos ) {
00478             case 1:
00479               ESPERO(TK_CADENA, "un nombre de variable de estado");
00480               des_comille(cad);
00481               _nombre_variable = new char [strlen(cad) + 1];
00482               VERIFICAR(_nombre_variable);
00483               strcpy(_nombre_variable, cad);
00484               pos++;
00485               break;
00486             case 2:  ESPERO(TK_ABRIR, "\"{\"");  pos++; break;
00487             default:
00488               if ( tipo_token == TK_CERRAR ) break;
00489               ESPERO(TK_IDENTIFICADOR, "un identificador (3)");
00490               if ( strcmp(cad, "tipo") == 0 ) {
00491                   tipo_token = Sabiondo->siguiente_token(cad);
00492                   ESPERO(TK_NUMERO, "un numero");
00493                   _tipo = atoi(cad);
00494                   //if ( _tipo == 3 || _tipo == 4 ) {
00495                       //configurar_vector_de_estado(100);
00496                   //}
00497                 }
00498                 else if ( strcmp(cad, "min") == 0 ) {
00499                   tipo_token = Sabiondo->siguiente_token(cad);
00500                   ESPERO(TK_VECTOR_INICIO, "el inicio de un VECTOR");
00501                   min_val.x = atof(&cad[1]);
00502                   tipo_token = Sabiondo->siguiente_token(cad);
00503                   ESPERO(TK_NUMERO, "un numero (dato 2 de un VECTOR)");
00504                   min_val.y = atof(cad);
00505                   tipo_token = Sabiondo->siguiente_token(cad);
00506                   ESPERO(TK_VECTOR_FIN, "el final de un VECTOR");
00507                   cad[strlen(cad) - 1] = '\0';
00508                   min_val.z = atof(cad);
00509                 }
00510                 else if ( strcmp(cad, "max") == 0 ) {
00511                   tipo_token = Sabiondo->siguiente_token(cad);
00512                   ESPERO(TK_VECTOR_INICIO, "el inicio de un VECTOR");
00513                   max_val.x = atof(&cad[1]);
00514                   tipo_token = Sabiondo->siguiente_token(cad);
00515                   ESPERO(TK_NUMERO, "un numero (dato 2 de un VECTOR)");
00516                   max_val.y = atof(cad);
00517                   tipo_token = Sabiondo->siguiente_token(cad);
00518                   ESPERO(TK_VECTOR_FIN, "el final de un VECTOR");
00519                   cad[strlen(cad) - 1] = '\0';
00520                   max_val.z = atof(cad);
00521                 }
00522                 if ( !leer_sensor(Sabiondo, cad) ) {
00523                   return FALSE;
00524                 }
00525               ;
00526               break;
00527         }
00528     }
00529 
00530     return TRUE;
00531 }
00532 
00533 
00534 BOOLEAN
00535 VISOR_SENSOR_VECTOR::asociar_sensor(SENSOR *S)
00536 {
00537     if ( S->tipo() != T_VECTOR ) return FALSE;
00538     Sensor = (SENSOR_VECTOR *)S;
00539     Sensor->sensar();
00540     return TRUE;
00541 }
00542 
00543 //===========================================================================
00544 //= EOF                                                                     =
00545 //===========================================================================
00546 

Este archivo HTML ha sido generado automáticamente a partir del código fuente AQUYNZA. NO LO EDITE. Para mayor información contacte al autor.