void readVTK() { ifstream vtkFile; //Input vtk file string temp; //String to absorb file contents point currentPoint; //Temporary vertex struct to work with polygon currentPolygon; //Temporary polygon struct to work with int polygonPoints; //Number of vertices a polygon has int pointNumber; //Var to extract vertex number with cout << "Fetching geometry" << endl; //Open vtk file vtkFile.open("../data/face.vtk"); //Absort header vtkFile >> temp; while (temp != "POINTS"){ vtkFile >> temp; } //Number of vertices vtkFile >> pointCount; //Absorb unwanted information vtkFile >> temp; //Save all vertices from file for (int i=0; i < pointCount; i++) { vtkFile >> currentPoint.x; vtkFile >> currentPoint.y; vtkFile >> currentPoint.z; points.push_back(currentPoint); }; //Absorb unwanted information vtkFile >> temp; //Number of polygons vtkFile >> polygonCount; //Absort unwanted information vtkFile >> temp; //Save all polygons to file for (int i=0; i < polygonCount; i++) { //Number of vertices polygon has vtkFile >> polygonPoints; //Clear temporary polygon struct currentPolygon.pointlist.clear(); //Capture all associated vertices and save in vertex list for (int j=0; j < polygonPoints; j++) { vtkFile >> pointNumber; points[pointNumber].polygonlist.push_back(i); currentPolygon.pointlist.push_back(pointNumber); }; //Calculate polygon normals currentPolygon = processNormals(currentPolygon); //Update associated vector normals for efficiency accumulateVNormals(currentPolygon.pointlist, currentPolygon.xNormal, currentPolygon.yNormal, currentPolygon.zNormal); //Push polygon template to polygon list to store polygons.push_back(currentPolygon); }; //Absorb unwanted information while (temp != "float") { vtkFile >> temp; }; //Read in x,y texture reference for each point for (int k = 0; k < pointCount; k++) { vtkFile >> points[k].xTexture; vtkFile >> points[k].yTexture; }; //Close input file vtkFile.close(); }
/** * Processes all <Mesh> elements from the XML file. * * @param n: <Mesh> element * @param outfile: Binary file, opened as "wb", for output. * * @return: TRUE if successful, FALSE otherwise. */ int processMesh(xmlNode *n, FILE *outfile) { xmlChar name[] = "name"; xmlChar frameCount[] = "frameCount"; xmlChar vertexCount[] = "vertexCount"; xmlChar indexCount[] = "indexCount"; xmlChar specularPower[] = "specularPower"; xmlChar opacity[] = "opacity"; xmlChar twoSided[] = "twoSided"; xmlChar customColor[] = "customColor"; xmlChar diffuseTexture[] = "diffuseTexture"; float32 color[3]; struct MeshHeader mh; uint8 texname[NAMESIZE+1]; int foundFlag = FALSE; xmlNode *texn = NULL; xmlNode *curNode = NULL; /* make sure we're dealing with a <Mesh> element */ assert(strcmp((char*)n->name, "Mesh") == 0); /* populate the MeshHeader structure appropriately */ memset(&mh, 0, sizeof(struct MeshHeader)); strncpy((char*)mh.name, (char*)xmlGetProp(n, name), NAMESIZE-1); mh.frameCount = (uint32)atoi((char*)xmlGetProp(n, frameCount)); mh.vertexCount = (uint32)atoi((char*)xmlGetProp(n, vertexCount)); mh.indexCount = (uint32)atoi((char*)xmlGetProp(n, indexCount)); if (readColorChild(n, "Diffuse", color) == FALSE) return FALSE; mh.diffuseColor[0] = color[0]; mh.diffuseColor[1] = color[1]; mh.diffuseColor[2] = color[2]; if (readColorChild(n, "Specular", color) == FALSE) return FALSE; mh.specularColor[0] = color[0]; mh.specularColor[1] = color[1]; mh.specularColor[2] = color[2]; mh.specularPower = (float32)atof((char*)xmlGetProp(n, specularPower)); mh.opacity = (float32)atof((char*)xmlGetProp(n, opacity)); mh.properties = 0; if (strcmp((char*)xmlGetProp(n, twoSided), "true") == 0) mh.properties += (0x1 << mpfTwoSided); if (strcmp((char*)xmlGetProp(n, customColor), "true") == 0) mh.properties += (0x1 << mpfCustomColor); mh.textures = 0; if (strcmp((char*)xmlGetProp(n, diffuseTexture), "true") == 0) mh.textures = 1; /* write the MeshHeader */ fwrite(&mh, sizeof(struct MeshHeader), 1, outfile); /* if we have a texture, then also write its name */ foundFlag = FALSE; if (mh.textures) { for (texn=n->children; texn; texn = texn->next) { if (texn->type == XML_ELEMENT_NODE) { if (strcmp((char*)texn->name, "Texture") == 0) { foundFlag = TRUE; break; } } } if (foundFlag == FALSE) { printf("Could not find <Texture> element!\n"); return FALSE; } memset(texname, 0, NAMESIZE+1); strncpy((char*)texname, (char*)xmlGetProp(texn, (xmlChar*)"name"), NAMESIZE); fwrite(texname, NAMESIZE, 1, outfile); } /* write out vertices */ foundFlag = FALSE; for (curNode=n->children; curNode; curNode = curNode->next) { if (curNode->type == XML_ELEMENT_NODE) { if (strcmp((char*)curNode->name, "Vertices") == 0) { foundFlag = TRUE; if (processVertices(curNode, outfile, mh.vertexCount) == FALSE) { return FALSE; } } } } if (foundFlag == FALSE) { printf("No <Vertices> found!\n"); return FALSE; } /* write out normals */ foundFlag = FALSE; for (curNode=n->children; curNode; curNode = curNode->next) { if (curNode->type == XML_ELEMENT_NODE) { if (strcmp((char*)curNode->name, "Normals") == 0) { foundFlag = TRUE; if (processNormals(curNode, outfile, mh.vertexCount) == FALSE) { return FALSE; } } } } if (foundFlag == FALSE) { printf("No <Normals> found!\n"); return FALSE; } /* write out texture coordinates */ if (mh.textures) { foundFlag = FALSE; for (curNode=n->children; curNode; curNode = curNode->next) { if (curNode->type == XML_ELEMENT_NODE) { if (strcmp((char*)curNode->name, "TexCoords") == 0) { foundFlag = TRUE; if (processTexcoords(curNode, outfile, mh.vertexCount) == FALSE) { return FALSE; } } } } if (foundFlag == FALSE) { printf("No <TexCoords> found!\n"); return FALSE; } } /* write out indices */ foundFlag = FALSE; for (curNode=n->children; curNode; curNode = curNode->next) { if (curNode->type == XML_ELEMENT_NODE) { if (strcmp((char*)curNode->name, "Indices") == 0) { foundFlag = TRUE; if (processIndices(curNode, outfile, mh.indexCount) == FALSE) { return FALSE; } } } } if (foundFlag == FALSE) { printf("No <Indices> found!\n"); } return TRUE; }