#include "iostream" #include "fstream" #include "sstream" #include "iomanip" #include "string.h" #include "cmath" #include "vector" #define ZERO 1e-20 #define Pi (4.0*atan(1.0)) using namespace std; struct point{ double x,y,z; }; typedef vector pointArray; struct spoint { double lon,lat,rad; }; typedef vector spointArray; struct vertex : public point{ int id; double attri; }; typedef vector vertexArray; struct face{ int id; int vec[3] = {-1,-1,-1}; point outNor; }; typedef vector faceArray; point normal(point a){ point out = a; double module = sqrt(a.x*a.x+a.y*a.y+a.z*a.z); out.x /= module; out.y /= module; out.z /= module; return out; } point operator -(point a,point b){ point out; out.x = a.x - b.x; out.y = a.y - b.y; out.z = a.z - b.z; return out; } point operator -(vertex a,vertex b){ point out; out.x = a.x - b.x; out.y = a.y - b.y; out.z = a.z - b.z; return out; } point cross(point a,point b){ point out; out.x = a.y*b.z-a.z*b.y; out.y = a.z*b.x-a.x*b.z; out.z = a.x*b.y-a.y*b.x; return out; } spoint c2s(vertex c) { spoint s; s.rad = sqrt(c.x*c.x+c.y*c.y+c.z*c.z); if (fabs(s.rad)> temp_int; for (int i = 0; i < temp_int; i++){ getline(mshin,temp_str); temp_ss.str(""); temp_ss.clear(); temp_ss.str(temp_str); temp_ss >> node.id >> node.x >> node.y >> node.z; nodes.push_back(node); } } else if (!strcmp(temp_str.c_str(),"$Elements")){ getline(mshin,temp_str); temp_ss.str(""); temp_ss.clear(); temp_ss.str(temp_str); temp_ss >> temp_int; for (int i = 0; i < temp_int; i++){ getline(mshin,temp_str); temp_ss.str(""); temp_ss.clear(); temp_ss.str(temp_str); temp_ss >> triangle.id >> temp_int2 >> temp_int2 >> temp_int2 >> triangle.vec[0] >> triangle.vec[1] >> triangle.vec[2]; //triangle.vec[0] += 1; triangle.vec[1] += 1; triangle.vec[2] += 1; triangle.id += 1; triangles.push_back(triangle); } } else if (!strcmp(temp_str.c_str(),"$NodeData")){ for (int i = 0; i < 8; i++) getline(mshin,temp_str); temp_ss.str(""); temp_ss.clear(); temp_ss.str(temp_str); temp_ss >> temp_int; for (int i = 0; i < temp_int; i++){ getline(mshin,temp_str); temp_ss.str(""); temp_ss.clear(); temp_ss.str(temp_str); temp_ss >> temp_int2 >> nodes[i].attri; if(nodes[i].attri > attriMax) attriMax = nodes[i].attri; if(nodes[i].attri < attriMin) attriMin = nodes[i].attri; } } else continue; } mshin.close(); for (int i = 0; i < nodes.size(); i++) nodes[i].attri = (nodes[i].attri - attriMin)/(attriMax - attriMin); for (int i = 0; i < triangles.size(); i++){ triangles[i].outNor = normal(cross(nodes[triangles[i].vec[1]-1]-nodes[triangles[i].vec[0]-1], nodes[triangles[i].vec[2]-1]-nodes[triangles[i].vec[0]-1])); } ofstream objout("sphere.obj"); objout << "mtllib global_topography.mtl" << endl << "o sphere" << endl; for (int i = 0; i < nodes.size(); i++){ objout << "v " << setprecision(16) << nodes[i].x << " " << nodes[i].y << " " << nodes[i].z << endl; } for (int i = 0; i < nodes.size(); i++){ objout << "vt " << setprecision(16) << nodes[i].attri << " 0.5" << endl; } for (int i = 0; i < triangles.size(); i++){ objout << "vn " << setprecision(16) << triangles[i].outNor.x << " " << triangles[i].outNor.y << " " << triangles[i].outNor.z << endl; } objout << "usemtl earth-topo" << endl << "g model1" << endl << "s 1" << endl; for (int i = 0; i < triangles.size(); i++){ objout << "f " << triangles[i].vec[0] << "/" << triangles[i].vec[0] << "/" << i+1 << " " << triangles[i].vec[1] << "/" << triangles[i].vec[1] << "/" << i+1 << " " << triangles[i].vec[2] << "/" << triangles[i].vec[2] << "/" << i+1 << endl; } objout.close(); return 0; }