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

Converción de un archivo ASC

Convierte un archivo ASC, en uno de texto en algo mas simple, para su posterior lectura. El archivo de ASC, es creado por 3D Studio MAX o cualquier programa de modelado que exporte este tipo de archivos, la unica restricción para su converciónes es que tenga un solo objeto y sin las coordenadas del mapeo de textura.

Archivo de entrada (.asc)

cubo.asc

Named Object: "Box01"
Tri-mesh, Vertices: 8     Faces: 12
Vertex list:
Vertex 0: X: -9.997291     Y: -9.995248     Z: -10.003587
Vertex 1: X: 10.002709     Y: -9.995248     Z: -10.003587
Vertex 2: X: -9.997291     Y: 10.004752     Z: -10.003587
Vertex 3: X: 10.002709     Y: 10.004752     Z: -10.003587
Vertex 4: X: -9.997291     Y: -9.995248     Z: 9.996413
Vertex 5: X: 10.002709     Y: -9.995248     Z: 9.996413
Vertex 6: X: -9.997291     Y: 10.004752     Z: 9.996413
Vertex 7: X: 10.002709     Y: 10.004752     Z: 9.996413
Face list:
Face 0:    A:0 B:2 C:3 AB:1 BC:1 CA:0
Smoothing: 2 
Face 1:    A:3 B:1 C:0 AB:1 BC:1 CA:0
Smoothing: 2 
Face 2:    A:4 B:5 C:7 AB:1 BC:1 CA:0
Smoothing: 3 
Face 3:    A:7 B:6 C:4 AB:1 BC:1 CA:0
Smoothing: 3 
Face 4:    A:0 B:1 C:5 AB:1 BC:1 CA:0
Smoothing: 6 
Face 5:    A:5 B:4 C:0 AB:1 BC:1 CA:0
Smoothing: 6 
Face 6:    A:1 B:3 C:7 AB:1 BC:1 CA:0
Smoothing: 5 
Face 7:    A:7 B:5 C:1 AB:1 BC:1 CA:0
Smoothing: 5 
Face 8:    A:3 B:2 C:6 AB:1 BC:1 CA:0
Smoothing: 6 
Face 9:    A:6 B:7 C:3 AB:1 BC:1 CA:0
Smoothing: 6 
Face 10:    A:2 B:0 C:4 AB:1 BC:1 CA:0
Smoothing: 7 
Face 11:    A:4 B:6 C:2 AB:1 BC:1 CA:0
Smoothing: 7 

Archivo de salida (.dat)

Cubo.dat

8
-9.997291 -9.995248 -10.003587
10.002709 -9.995248 -10.003587
-9.997291 10.004752 -10.003587
10.002709 10.004752 -10.003587
-9.997291 -9.995248 9.996413
10.002709 -9.995248 9.996413
-9.997291 10.004752 9.996413
10.002709 10.004752 9.996413
12
0 2 3
3 1 0
4 5 7
7 6 4
0 1 5
5 4 0
1 3 7
7 5 1
3 2 6
6 7 3
2 0 4
4 6 2

Código Fuente

Código en un zip
Archivos con los objetos

#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>

//numero de vertices y caras del objeto
int vertices, faces;

void LoadObject(FILE *fin, FILE *fout);
void LoadScene(FILE *fin, FILE *fout);
void DisplayUsage(void);

void LoadObject(FILE *fin, FILE *fout)
{
 int count, vertexnum, tempA, tempB, tempC;
 float tempX, tempY, tempZ;
 char *tempString = new char [80];

 while(strncmp(tempString, "Vertices",8))
	{
	fscanf(fin, "%s", tempString);
	if (feof(fin))
		{
		printf("String \"Vertices\" not found in file.Exitting...\n");
		exit(1);
		}
	}

 fgetc(fin);				//Get ':'
 fscanf(fin, "%d", &vertices);
 printf("Object has %d vertices\n", vertices);
 fprintf(fout, "%d\n", vertices);
 while(strncmp(tempString, "Faces", 5))
	{
	fscanf(fin, "%s", tempString);
	if (feof(fin))
		{
		printf("String \"Faces\" not found in file.Exitting...\n");
		exit(1);
		}
	}

 fgetc(fin);				//Get ':'
 fscanf(fin, "%d", &faces);
 printf("Object has %d faces\n", faces);
 while(strncmp(tempString, "Vertex",6))
	{
	fscanf(fin, "%s", tempString);
	if (feof(fin))
		{
		printf("String \"Vertex\" not found in file.Exitting...\n");
		exit(1);
		}
	}

 while(strncmp(tempString,"list:",5))
	{
	fscanf(fin, "%s", tempString);
	if (feof(fin))
		{
		printf("String \"list\" not found in file.Exitting...\n");
		exit(1);
		}
	}
 printf("\nVertex data:\n");

//********************  LOAD VERTEX DATA  ********************************

 for (count = 0; count < vertices; count++)
	{
	while(strncmp(tempString,"Vertex",6))
		{
		fscanf(fin, "%s", tempString);
		if(feof(fin))
			{
			printf("String \"Vertex\" not found in file.Exitting...\n");
			exit(1);
			}
		}
	fscanf(fin, "%d", &vertexnum);
	fscanf(fin, "%s", tempString);      //get ':   '
	fscanf(fin, "%[^:]", tempString);   //get '     X'
	fgetc(fin);                      	//get ':'
	fscanf(fin, "%f", &tempX);
	fscanf(fin, "%[^:]", tempString);
	fgetc(fin);
	fscanf(fin, "%f", &tempY);
	fscanf(fin, "%[^:]", tempString);
	fgetc(fin);
	fscanf(fin, "%f", &tempZ);
	/*- aqui es donde cargo la estructura con los datos de cada vertice --*/
	printf("Vertex %d:  X:%4.2f Y:%4.2f Z:%4.2f\n", vertexnum, tempX, tempY, tempZ);
	fprintf(fout, "%f %f %f\n", tempX, tempY, tempZ);
	}

 while(strncmp(tempString,"Face",4))
	{
	fscanf(fin, "%s", tempString);
	if (feof(fin))
		{
		printf("String \"Face\" not found in file.Exitting...\n");
		exit(1);
		}
	}

 while(strncmp(tempString,"list",4))
	{
	fscanf(fin, "%s", tempString);
	if (feof(fin))
		{
		printf("String \"list\" not found in file.Exitting...\n");
		exit(1);
		}
	}
 printf("\nFace data:\n");

//**********************  LOAD FACE DATA  ********************************
 fprintf(fout, "%d\n", faces);
 for (count = 0; count < faces; count++)
	{
	while(strncmp(tempString,"Face",4))
		{
		fscanf(fin, "%s", tempString);
		if (feof(fin))
			{
			printf("String \"Face\" not found in file.Exitting...\n");
			exit(1);
			}
		}
	fscanf(fin, "%d", &vertexnum);
	fscanf(fin, "%s", tempString);

	while (fgetc(fin) != 'A');
		fgetc(fin);                  // get the ':' character
	fscanf(fin, "%d", &tempA);   // get value for vertex A
	while (fgetc(fin) != 'B');
		fgetc(fin);
	fscanf(fin, "%d", &tempB);
	while (fgetc(fin) != 'C');
		fgetc(fin);
	fscanf(fin, "%d", &tempC);
	/***** me cargo las caras del objeto ********/
	printf("Face %d:  %8d %8d %8d\n", vertexnum, tempA, tempB, tempC);
	fprintf(fout,"%d %d %d\n", tempA, tempB, tempC);
	}
}

void LoadScene(FILE *fin, FILE *fout)
{
 char *tempString = new char[80];
 //Parse the input file
 while(!feof(fin))
	{
	fscanf(fin,"%s",tempString);
	if(strcmp("Tri-mesh,",tempString)==0)  //Found Tri-mesh
		LoadObject(fin, fout);
	}
}

void DisplayUsage()
{
 printf("A L P H A  v1.0  - 3D Studio object viewer (July 1996)\n");
 printf("by Ramiro Alcocer (c) 2000\n\n");
 printf("USAGE: asc <filename.asc>\n");
}

void main(int argc, char *argv[])
{
 FILE *file_asc, *file_text;

 clrscr();
 if(argc < 2)
	{
	DisplayUsage();
	exit(0);
	}
 if((file_asc = fopen(argv[1], "rt"))==NULL)
	{
	printf("Error opening file %s\n", argv[1]);
	DisplayUsage();
	exit(1);
	}

 if((file_text = fopen(argv[2], "wt"))==NULL)
	{
	printf("Error opening file %s\n", argv[2]);
	DisplayUsage();
	exit(1);
	}

 LoadScene(file_asc, file_text);

 fclose(file_text);
 fclose(file_asc);
 getch();
}

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