几种mesh 数据文件格式
几种mesh 数据文件格式
1、Ply格式
PLY - Polygon File Format (paulbourke.net)
2、Obj格式
http://paulbourke.net/dataformats/obj/
https://people.cs.clemson.edu/~dhouse/courses/405/docs/brief-obj-file-format.html
Read obj
1 /** 2 * @file read_obj.cpp 3 * @author your name (you@domain.com) 4 * @brief 5 * @version 0.1 6 * @date 2022-08-22 7 * 8 * @copyright Copyright (c) 2022 9 * 10 */ 11 12 #include <iostream> 13 #include <fstream> 14 #include <string> 15 #include <sstream> 16 #include <vector> 17 #include <unordered_map> 18 19 int stringSplit(std::string &str, std::string &str1, std::string &str2) 20 { 21 std::string::size_type pos; 22 pos = str.find("/"); 23 if (pos != str.npos) 24 { 25 str1 = str.substr(0, pos); 26 str2 = str.substr(pos + 1); 27 } 28 else 29 { 30 str1 = str; 31 str2 = ""; 32 } 33 34 return 0; 35 } 36 37 template <class PointType, class PointType2D> 38 int readObj( 39 const std::string &in_path, 40 const std::string &obj_file, 41 std::vector<PointType> &out_vertces, 42 std::vector<PointType2D> &out_uvs, 43 std::vector<uint32_t> &out_tria, 44 std::vector<uint32_t> &out_tria_uv, 45 std::vector<int> &out_triangle_texture_ids, 46 std::vector<std::string> &out_texture_names) 47 { 48 // 0. read obj 49 std::ifstream infile(in_path + "/" + obj_file); 50 if (!infile.is_open()) 51 { 52 std::cout << "open obj file failed ...\n"; 53 return -1; 54 } 55 56 std::vector<std::string> tex_names; 57 std::string mtl_file; 58 std::string Fline; 59 while (getline(infile, Fline)) // Fline中不包括每行的换行符 60 { 61 std::string buf; 62 std::stringstream ss(Fline); // 字符流ss 63 std::vector<std::string> tokens; // vector 64 while (ss >> buf) // stringstream 按空格分开字符输入 65 { 66 tokens.push_back(buf); 67 } 68 69 // mtl 70 if (tokens.size() == 2 && tokens[0] == "mtllib") 71 { 72 mtl_file = tokens[1]; 73 } 74 75 // vertex 76 if (tokens.size() == 4 && tokens[0] == "v") 77 { 78 PointType pt; 79 pt[0] = atof(tokens[1].c_str()); 80 pt[1] = atof(tokens[2].c_str()); 81 pt[2] = atof(tokens[3].c_str()); 82 out_vertces.emplace_back(pt); 83 } 84 85 // vt 86 if (tokens.size() == 3 && tokens[0] == "vt") 87 { 88 PointType2D uv; 89 uv[0] = atof(tokens[1].c_str()); 90 uv[1] = atof(tokens[2].c_str()); 91 out_uvs.emplace_back(uv); 92 } 93 94 // usemtl 95 if (tokens.size() == 2 && tokens[0] == "usemtl") 96 { 97 tex_names.emplace_back(tokens[1]); 98 } 99 100 // face 101 if (tokens.size() == 4 && tokens[0] == "f") 102 { 103 std::string str1, str2; 104 105 stringSplit(tokens[1], str1, str2); 106 out_tria.emplace_back(atoi(str1.c_str())); 107 out_tria_uv.emplace_back(atoi(str2.c_str())); 108 109 stringSplit(tokens[2], str1, str2); 110 out_tria.emplace_back(atoi(str1.c_str())); 111 out_tria_uv.emplace_back(atoi(str2.c_str())); 112 113 stringSplit(tokens[3], str1, str2); 114 out_tria.emplace_back(atoi(str1.c_str())); 115 out_tria_uv.emplace_back(atoi(str2.c_str())); 116 117 out_triangle_texture_ids.emplace_back(tex_names.size() - 1); 118 } 119 } 120 infile.close(); 121 122 // 1. read stl 123 std::ifstream stl_file(in_path + "/" + mtl_file); 124 if (!stl_file.is_open()) 125 { 126 std::cout << "open stl file failed ...\n"; 127 return -1; 128 } 129 130 std::unordered_map<std::string, std::string> stl_map; 131 std::string tt; 132 while (getline(stl_file, Fline)) // Fline中不包括每行的换行符 133 { 134 std::string buf; 135 std::stringstream ss(Fline); // 字符流ss 136 std::vector<std::string> tokens; // vector 137 while (ss >> buf) // stringstream 按空格分开字符输入 138 { 139 tokens.push_back(buf); 140 } 141 142 if (tokens.size() == 2 && tokens[0] == "newmtl") 143 { 144 tt = tokens[1]; 145 } 146 147 if (tokens.size() == 2 && tokens[0] == "map_Kd") 148 { 149 stl_map[tt] = tokens[1]; 150 } 151 } 152 stl_file.close(); 153 154 out_texture_names.resize(tex_names.size()); 155 for (int i = 0; i < tex_names.size(); i++) 156 { 157 /* code */ 158 if (stl_map.find(tex_names[i]) != stl_map.end()) 159 { 160 out_texture_names[i] = stl_map[tex_names[i]]; 161 } 162 } 163 164 return 0; 165 } 166 167 ///////////////////////////////////////////////////////////////////////// 168 class Point3D 169 { 170 public: 171 /* data */ 172 double x, y, z; 173 174 Point3D() 175 { 176 } 177 178 double &operator[](uint32_t id) 179 { 180 if (id == 0) 181 { 182 return this->x; 183 } 184 else if (id == 1) 185 { 186 return this->y; 187 } 188 else if (id == 2) 189 { 190 return this->z; 191 } 192 } 193 }; 194 195 class Point2D 196 { 197 public: 198 /* data */ 199 double x, y; 200 Point2D() 201 { 202 } 203 204 double &operator[](uint32_t id) 205 { 206 if (id == 0) 207 { 208 return this->x; 209 } 210 else if (id == 1) 211 { 212 return this->y; 213 } 214 } 215 }; 216 217 int main() 218 { 219 std::string path = "C:/Users/86186/Desktop/test/LOD-0"; 220 std::string obj_file = "Mesh-XL-YL-XL-YL.obj"; 221 std::vector<Point3D> out_vertces; 222 std::vector<Point2D> out_uvs; 223 std::vector<uint32_t> out_tria; 224 std::vector<uint32_t> out_tria_uv; 225 std::vector<int> out_triangle_texture_ids; 226 std::vector<std::string> out_texture_names; 227 228 int ret = readObj<Point3D, Point2D>( 229 path, 230 obj_file, 231 out_vertces, 232 out_uvs, 233 out_tria, 234 out_tria_uv, 235 out_triangle_texture_ids, 236 out_texture_names); 237 238 system("pause"); 239 return 0; 240 }