00001 #include <iostream>
00002 #include <string.h>
00003 #include <math.h>
00004 #include <stdio.h>
00005
00006 #include "vector.hh"
00007 #include "asc.hh"
00008
00009
00010
00011
00012 ASC::~ASC() {
00013 std::vector<Vertex *>::iterator i;
00014 for(i = vertices.begin(); i != vertices.end(); ++i) {
00015 delete (*i);
00016 }
00017 }
00018
00019 ASC::ASC(std::string& filename){
00020 char name[1000];
00021 double x,y,z;
00022 double maxx,maxy,maxz;
00023 double minx,miny,minz;
00024 int a,b,c,ab,bc,ca;
00025 int numVerts = 0;
00026 int numFaces = 0;
00027 int number;
00028
00029 FILE *f = fopen(filename.c_str(), "r");
00030
00031 fscanf(f, " Named object: %s", name);
00032 fscanf(f, " Tri-mesh, Vertices: %d Faces: %d", &numVerts, &numFaces);
00033 fscanf(f, " Vertex List:");
00034
00035 for (int i = 0; i < numVerts; ++i) {
00036 fscanf(f, " Vertex %d: X: %lf Y: %lf Z: %lf", &number, &x, &y, &z);
00037 if(i == 0) {
00038 minx = maxx = x;
00039 miny = maxy = y;
00040 minz = maxz = z;
00041 } else {
00042 if(x > maxx) maxx = x;
00043 if(y > maxy) maxy = y;
00044 if(z > maxz) maxz = z;
00045 if(x < minx) minx = x;
00046 if(y < miny) miny = y;
00047 if(z < minz) minz = z;
00048 }
00049 vertices.push_back(new Vertex(x,y,z));
00050 }
00051
00052 double dx,dy,dz,maxd;
00053 dx = (maxx - minx) / 2.0;
00054 dy = (maxy - miny) / 2.0;
00055 dz = (maxz - minz) / 2.0;
00056 maxd = dx;
00057 if (dy > maxd) maxd = dy;
00058 if (dz > maxd) maxd = dz;
00059 maxd *= 2.0;
00060 if(maxd != 1.0) {
00061 for (int i = 0; i < numVerts; ++i) {
00062 Vertex *v = vertices[i];
00063 v->setX((v->getX()-minx-dx) / maxd);
00064 v->setY((v->getY()-miny-dy) / maxd);
00065 v->setZ((v->getZ()-minz-dz) / maxd);
00066 }
00067 }
00068 fscanf(f, " Face list:");
00069
00070 for (int i = 0; i < numFaces; ++i) {
00071 fscanf(f, " Face %d: A: %d B: %d C: %d AB: %d BC: %d CA: %d",
00072 &number, &a, &b, &c, &ab, &bc, &ca);
00073 triangles.push_back(Triangle(a, b, c));
00074
00075 triangles.back().normal = (*vertices[c]-*vertices[a]) % (*vertices[c]-*vertices[b]);
00076 triangles.back().normal.setNorm(1.0);
00077
00078 vertices[a]->normal += triangles.back().normal;
00079 vertices[b]->normal += triangles.back().normal;
00080 vertices[c]->normal += triangles.back().normal;
00081
00082 fscanf(f, " Smoothing: %d", &number);
00083 }
00084
00085 fclose(f);
00086
00087 for (int i = 0; i < numVerts; ++i) {
00088 vertices[i]->normal.setNorm(1.0);
00089
00090 }
00091
00092 }