Principal | Gráficos 3D | Gráficos 2D | Fractales | Math | Códigos | Tutoriales | Links

Visualizacion del Campo Magnetico

El siguiente programa fue hecho por Herling Gonzalez Alvarez, que visualiza un Campo Magnetico utilizando OpenGL.

Email: gherling@lycos.com



Pantalla de opciones del programa



Campo Magnetico



Perfil del Campo


Archivo de Datos

Esta es una porcion del archivo de datos a graficar, si te interesa el archivo completo para probarlo, dame tu direccion de mail y te lo envio, no lo pongo en la pagina por ser algo pesadito.

0 0 0 -5188 -5207 -97.88
0 0 0.3158 -5081 -5099 339.5
0 0 0.6316 -4871 -4893 660.3
0 0 0.9474 -4596 -4626 872.6
0 0 1.2631 -4286 -4331 995.9
0 0 1.5789 -3963 -4025 1054
0 0 1.8947 -3645 -3723 1066
0 0 2.2107 -3342 -3429 1045
0 0 2.5267 -3061 -3149 1001
0 0 2.8417 -2803 -2887 940.3
0 0 3.1577 -2569 -2644 869.8
0 0 3.4737 -2358 -2420 794.4
0 0 3.7897 -2169 -2215 719.1
0 0 4.1057 -2001 -2031 646.4
0 0 4.4207 -1850 -1870 576.4
0 0 4.7367 -1717 -1729 508.4
0 0 5.0527 -1600 -1608 442.3
0 0 5.3687 -1499 -1504 377.9
0 0 5.6837 -1414 -1417 315.4
0 0 5.9997 -1344 -1347 254.4
0 0 6.3157 -1289 -1291 194.8
0 0 6.6317 -1248 -1249 136.1
0 0 6.9477 -1221 -1222 77.53
0 0 7.2627 -1207 -1208 18.32
0 0 7.5787 -1207 -1208 -42.78

Codigo Fuente

Codigo fuente: aqui

//
// field2.cpp
// Visulizacion del Campo Magnetico 
// desde los archivos mds.dat y cbs.dat (minino y cero B)
// Herling Gonzalez Alvarez
// marzo de 2002
// gherling@lycos.com
//

#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <GL/glut.h>

#ifndef M_PI
#define M_PI 3.141592653589793238E0
#endif

int frame=0, time=0, timebase=0;
float fps;
int w,h;

//numero de nodos por dimension para el campo magnetico
const int N=30625,nx=25,ny=25,nz=49;

int m=0,fm=ny*nz,set=0;

const char *stick[]={"2.0","4.0","6.0","8.0","10.0","12.0","14.0",
	"16.0","18.0","20.0"};
static char label[100];
char *name="ninguna";

double dt=0.01;
double theta=45.0,rho=45.0,t=0.0,distancia=100.0,xc=0.0,zc=0.0,yc=0.0;
double x[1000],y[1000],z[1000];
double pos[30625][6]; 

void setFieldDat(int caso)
{
    ifstream infield;
    switch (caso) {
	    case 1: infield.open("mbs.dat");break;
	    case 2: infield.open("cbs.dat");break;
	    case 3: infield.open("mirrors.dat");break;
	    case 4: infield.open("cupss.dat");break;
    }		
	for(register int i=0;i<N;i++)
	{
		infield >> pos[i][0] >> pos[i][1] >> pos[i][2] \
	               >> pos[i][3] >> pos[i][4] >> pos[i][5];		
	}
    infield.close();
}

/*void arrow(double ax, double ay, double az)
{
	glPushMatrix();
	glTranslatef(ax,ay,az);
	glBegin(GL_LINES);
		glVertex3d(  0.0, 0.1, 0.0);	
		glVertex3d(  0.1, 0.0, 0.0);	
		glVertex3d(  0.0, 0.1, 0.0);
		glVertex3d( -0.1, 0.0, 0.0);
	glEnd();
	glPopMatrix();
	
}
*/

void allField()
{
	double f = 15000.0 ;
	glBegin(GL_LINES);	
	for(register int i=0;i<N;i++)
	{
	  glColor3f(0.0f, 0.0f, 1.0f);
	  glVertex3d(pos[i][0],pos[i][1],pos[i][2]);
	  glColor3f(1.f, 0.f, 0.0f);
	  glVertex3d(pos[i][0]+pos[i][3]/f,pos[i][1]+pos[i][4]/f,
			  pos[i][2]+pos[i][5]/f);
	} 
	glEnd();	
}

void zyField()
{
	register int mm=m,fmm=fm; 
	float f = 15000.0 ;
	glBegin(GL_LINES);		
	for(mm;mm<fmm;mm++)
	{
		glColor3f(0.0,0.0,1.0);
		glVertex3d(pos[mm][0],pos[mm][1],pos[mm][2]);
		glColor3f(1.0,0.0,0.0);
		glVertex3d(pos[mm][0]+pos[mm][3]/f,pos[mm][1]+pos[mm][4]/f,
		pos[mm][2]+pos[mm][5]/f);
		
	/*	arrow(pos[mm][0]+pos[mm][3]/f,pos[mm][1]+pos[mm][4]/f,
			pos[mm][2]+pos[mm][5]/f);*/
	} 
	glEnd();
}

void pointField()
{
	glPointSize(1.0);
	glBegin(GL_POINTS);
	for(int p=0;p<N;p++)
	{
	  glColor3f(1.0, 1.0, 0.0);
	  glVertex3d(pos[p][0],pos[p][1],pos[p][2]);
	} 
	glEnd();
}

void setViewField(int caso)
{
	switch (caso){
		case 8: allField();break;
		case 9: zyField();break;
		case 0: pointField();break;	
	}
}

void inline drawString (char *s)
{
  unsigned int i;
  for (i = 0; i < strlen (s); i++)
    glutBitmapCharacter (GLUT_BITMAP_HELVETICA_10, s[i]);
}

void init() 
{
   glClearColor (0.5,0.5,0.5,1.0);
 //glClearColor (0.,0.,0.,0.0);
}

void changeSize(int w1, int h1) 
{
	if(h1 == 0) h1 = 1;
	w = w1;
	h = h1;
        glViewport(0, 0, w, h); // Se va a usar toda la ventana para 
	                            // mostrar gráficos.
	glMatrixMode(GL_PROJECTION);// Activamos la matriz de proyeccion.
	glLoadIdentity();           // reseteamos la matrix de 
	                            // projection (stack=1).
	

	gluPerspective( 15,	     // Ángulo de visión.
	    (float)w/(float)h,   // Razón entre el largo y el ancho,
	                         // para calcular la perspectiva.
	     0.0001,		     // Cuan cerca se puede ver.
	     1000);              // Cuan lejos se puede ver.
	
	glMatrixMode(GL_MODELVIEW); // Escogemos la matriz de vista
	glLoadIdentity();
	
	gluLookAt(xc,yc,zc,      // Desde donde miramos.
		  4.25,  4.25, 7.5,  // Hacia donde miramos.
		  0.0,  1.0, 0.0);   // Que eje es el que esta hacia arriba
}


void setOrthographicProjection() {

    // glClearColor (0.5,0.5,0.5,1.0);
	// cambiamos a modo de proyecion 
	glMatrixMode(GL_PROJECTION);
	// guardamos la matriz anterior que contiene la 
	// configuracion de la perspectiva de proyecion.
	glPushMatrix();
	// reseteamos la matriz
	glLoadIdentity();
	// Se crea una projection ortografica 2D
	gluOrtho2D(0, w, 0, h);
	// se invierte el eje y, es decir, hacia abajo es positivo
	glScalef(1, -1, 1);
	// se mueve el origen de la esquina inferior izq.
	// hacia la esquina superior del mismo lado.
	glTranslatef(0, -h, 0);
	glMatrixMode(GL_MODELVIEW);
}

void resetPerspectiveProjection() {
	//Configura la matriz actual ha GL_PROJECTION
	glMatrixMode(GL_PROJECTION);
	//restaura la configuracion anterior
	glPopMatrix();
	// retorna ha la matrix  GL_MODELVIEW 
	glMatrixMode(GL_MODELVIEW);
}

long double Bfield(double x)
{
	long double b;
	return b;	
}


void renderScene(void)	
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	// Guarda la matriz actual en un stack
	glPushMatrix();   
	// Activar lineas con Antialiasing 
    glEnable(GL_LINE_SMOOTH);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glHint(GL_LINE_SMOOTH_HINT,GL_DONT_CARE);
	glLineWidth(0.5);

//----------------------------------------- View Magnetic Field ---->
	setViewField(set);
//----------------------------------------- View Magnetic Field ----<

//----------------------------------------- Moving Particles - LeapFrog -->
//	setViewMoving();
//----------------------------------------- Moving Particles - LeapFrog --<
 
	
//-------------------------------AXIS-XYZ--------> 
	glLineWidth(1.5);
	glBegin(GL_LINE_STRIP);
	glColor3f(1.0,0.0,0.0);
	glVertex3f(0.0,0.0,0.0);
	glVertex3f(14.0,0.0,0.0);
	glEnd();

	glBegin(GL_LINE_STRIP);
	glColor3f (0.0,1.0,0.0);
	glVertex3f(0.0,0.0,0.0);
	glVertex3f(0.0,14.0,0.0);
	glEnd();
	
	glBegin(GL_LINE_STRIP);
	glColor3f(0.0,0.0,1.0);
	glVertex3f(0.0,0.0,0.0);
	glVertex3f(0.0,0.0,22.0);
	glEnd();
//-------------------------------AXIS-XYZ--------<
		
//---------- STICKS DE ESCALA PARA LOS EJES------>
	glColor3f (1.0,1.0,1.0);

	for(int j=1;j <= 6;j++)
		{
		sprintf(label,stick[j-1]);
		glRasterPos3f ( float(j*2.0),0.0,0.0);
		drawString (label);
		}
    for(int k=1;k<=6;k++)
		{
		sprintf(label,stick[k-1]);
		glRasterPos3f (0.0,float(k*2.0),0.0);
		drawString (label);
		}
    for(int l=1;l<=10;l++)
		{
		sprintf(label,stick[l-1]);
		glRasterPos3f (0.0,0.0,float(l*2.0));
		drawString (label);
		}
	
	glColor3f (1.0,0.0,0.0);
	sprintf(label,"X");
	glRasterPos3f(14.0,-0.5,-0.5);
	drawString (label);

	glColor3f (0.0,1.0,0.0);
	sprintf(label,"Y");
	glRasterPos3f(-0.5,14.0,-0.5);
	drawString (label);

	glColor3f (0.0,0.0,1.0);
	sprintf(label,"Z");
	glRasterPos3f(-0.5,-0.5,22.0);
	drawString (label);

//---------- STICKS DE ESCALA PARA LOS EJES------<
	glPopMatrix(); // Recupera la matriz guardada en el stack
//------------------------------------------- INTERFACE ORTOGRAFICA ---->
	setOrthographicProjection();
	glPushMatrix();
	glLoadIdentity();

	glColor3f(10.0f,1.0f,1.0f);	
	sprintf(label,"FPS:%5.2f",fps);
	glRasterPos2f (30.0,90.0);
	drawString (label);
	
	sprintf (label, "AZIMUT = %4.1f",theta);
    glRasterPos2f (30,30.0);
    drawString (label);
	
    sprintf (label, "ALTURA = %4.1f",rho);
    glRasterPos2f (30,45.0);
    drawString (label);
	
    sprintf (label, "m = %5d,fm = %5d ",m,fm);
    glRasterPos2f (30,60.0);
    drawString (label);
	
    sprintf (label, "Tipo de Trampa :%10s",name);
    glRasterPos2f (30,75.0);
    drawString (label);
	
    glPopMatrix();
	resetPerspectiveProjection();
//------------------------------------------- INTERFACE ORTOGRAFICA ----
     glutSwapBuffers();
}


void keyboard_1(unsigned char key, int x, int y)
{
   int nodos;
   nodos = nx*nz;
   switch (key) {
      case 27: exit(0); break;
      case '-': distancia += 1.0; break;
      case '+': distancia -= 1.0; break;

      case '1':	setFieldDat(1);name=" Minimo B"; break;
      case '2':	setFieldDat(2);name=" Cero B";   break;
      case '3':	setFieldDat(3);name=" Espejo";   break;
      case '4':	setFieldDat(4);name=" Cusp";   break;
		
      case '0':	set=0;   break;
      case '9':	set=9;   break;
      case '8':	set=8;   break;
		
      case 'l':case 'L': m+=nodos; fm+=nodos; 
			 if(m>(N-nodos))
			 {
				 m=0;
				 fm=nodos;
			 } 
			 
			 break;
      case 'k':case 'K': m-=nodos; fm-=nodos; 
			 if(m<0)
			 {
				 m=N-nodos;
				 fm=N;
			 }
			 break;
   }
}


void keyboard_2(int key, int x, int y)
{
	switch (key) {
		case GLUT_KEY_LEFT  : theta += 1.0;break;
		case GLUT_KEY_RIGHT : theta -= 1.0;break;
		case GLUT_KEY_UP    : rho  -= 1.0;break;
		case GLUT_KEY_DOWN  : rho  += 1.0;break;
	}
}

void viewCam(double theta, double rho)
{
	xc = distancia*cos(theta*M_PI/180.0)*sin(rho*M_PI/180.0);
	zc = distancia*sin(theta*M_PI/180.0)*sin(rho*M_PI/180.0);
	yc = distancia*cos(rho*M_PI/180.0);
	glLoadIdentity();
	gluLookAt(xc,yc,zc,  
		  4.25,  4.25, 7.5,
		  0.0,  1.0, 0.0); 
}

void idle()
{
	t += 0.25;
	if(theta > 360 || theta < -360) theta=0.0;
	if(rho >   360 || rho  < -360) rho=0.0;
	viewCam(theta,rho);
	frame++;
	time=glutGet(GLUT_ELAPSED_TIME);
	if (time - timebase > 1000) 
		{
		fps=frame*1000.0/(time-timebase);
		timebase = time;		
		frame = 0;
		}
	glutPostRedisplay();
}

int main(int argc, char **argv) 
{
	x[0] =  4.5;
	y[0] =  4.5;
	z[0] =  7.0;
	cout<< "-------------------------------"<<endl;
	cout<< " Trayectoria de Particulas      "<<endl;
	cout<< " Cagadas un Campo Magnetico    "<<endl;
	cout<< "-------------------------------"<<endl;
	cout<< "TECLAS:"<<endl;
	cout<< "-------------------------------"<<endl;
	cout<< "1 :carga el archivo mbs.dat -Trampa minimoB-"<<endl;
	cout<< "2 :carga el archivo cbs.dat -Trampa CeroB-"<<endl;
	cout<< "3 :carga el archivo mirror.dat -Trapa Espejo-"<<endl;
	cout<< "4 :carga el archivo cups.dat -Trampa Cusp-"<<endl;
	cout<< "                                   "<<endl;
	cout<< "8 :campo vectorial (opcion 1,2,3 o 4 anteriores)."<<endl;
	cout<< "9 :perfil YZ del campo."<<endl;
	cout<< "l :mueviniento en una direccion del perfil ZY del campo.-"<<endl;
	cout<< "k :movimiento del perfil en la direccion contraria."<<endl;
	cout<< "                                   "<<endl;
	cout<< "UP,Left,Right,Down para mover la vista"<<endl;
	cout<< "+ y - se aleja o se acerca."<<endl;
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
	glutInitWindowPosition(320,320);
	glutInitWindowSize(480,400);
	glutCreateWindow("VECTORIAL FIELD 3D");
	init();
	glutReshapeFunc(changeSize);
    // glutFullScreen();
	glutKeyboardFunc(keyboard_1);
	glutSpecialFunc(keyboard_2);
	glutIdleFunc(idle);
	glutDisplayFunc(renderScene);
	glutMainLoop();
	return 0;
}


valcoey@hotmail.com

Ramiro Alcocer, 2001

Principal | Gráficos 3D | Gráficos 2D | Fractales | Math | Códigos | Tutoriales | Links