Browse Source

格式化

divide_struct_def_imp
郑敬润 1 year ago
parent
commit
b53fcc82ff
  1. 149
      src/xmlsql.cpp

149
src/xmlsql.cpp

@ -15,10 +15,10 @@
#include <time.h> #include <time.h>
using namespace std; using namespace std;
map<P, string> pmp, clipName; map<P, string> pmp, clipName;
struct Readexcel{ struct Readexcel
{
vector<string> slist; vector<string> slist;
TiXmlElement *GetNodePointerByName(TiXmlElement *pRootEle, const char *strNodeName) TiXmlElement *GetNodePointerByName(TiXmlElement *pRootEle, const char *strNodeName)
{ {
@ -34,13 +34,16 @@ struct Readexcel{
if (0 != strcmp(pEle->Value(), strNodeName)) if (0 != strcmp(pEle->Value(), strNodeName))
{ {
TiXmlElement *res = GetNodePointerByName(pEle, strNodeName); TiXmlElement *res = GetNodePointerByName(pEle, strNodeName);
if(res!=NULL)return res; if (res != NULL)
return res;
} }
else return pEle; else
return pEle;
} }
return NULL; return NULL;
} }
vector<string> read(string xml){ vector<string> read(string xml)
{
slist.clear(); slist.clear();
TiXmlDocument *pDoc = new TiXmlDocument(); TiXmlDocument *pDoc = new TiXmlDocument();
@ -59,7 +62,8 @@ struct Readexcel{
TiXmlElement *cell; TiXmlElement *cell;
string s[7]; string s[7];
int i = 0; int i = 0;
for (cell =pEle->FirstChildElement(); cell; cell = cell->NextSiblingElement()){ for (cell = pEle->FirstChildElement(); cell; cell = cell->NextSiblingElement())
{
// cout<<(cell->FirstChildElement()->GetText())<<endl; // cout<<(cell->FirstChildElement()->GetText())<<endl;
@ -71,18 +75,17 @@ struct Readexcel{
slist.push_back(s[4] + " " + s[5] + " " + s[6]); slist.push_back(s[4] + " " + s[5] + " " + s[6]);
} }
return slist; return slist;
} }
} rexcel; } rexcel;
// 读取卡箍点 // 读取卡箍点
void read_points(string filename,Astar * pastar){ void read_points(string filename, Astar *pastar)
{
// cout<<"read "<<filename<<endl; // cout<<"read "<<filename<<endl;
vector<string> sl = rexcel.read(filename); vector<string> sl = rexcel.read(filename);
// cout<<"------"<<endl; // cout<<"------"<<endl;
for (int i = 0; i < sl.size(); i += 3) for (int i = 0; i < sl.size(); i += 3)
{ {
string s1 = sl[i + 1], s2 = sl[i + 2], s0 = sl[i]; string s1 = sl[i + 1], s2 = sl[i + 2], s0 = sl[i];
@ -101,69 +104,84 @@ void read_points(string filename,Astar * pastar){
pastar->add_clip(clipSet.c[clipSet.clipnum]); pastar->add_clip(clipSet.c[clipSet.clipnum]);
// cout<<"--"<<endl; // cout<<"--"<<endl;
kdtree.add(p1); kdtree.add(p1);
} }
} }
unsigned int extractAndConvert(const string& str) { unsigned int extractAndConvert(const string &str)
{
size_t pos = str.find("/"); // 查找 "//" 的位置 size_t pos = str.find("/"); // 查找 "//" 的位置
if (pos == std::string::npos) { if (pos == std::string::npos)
{
throw invalid_argument("No '/' found in the string."); throw invalid_argument("No '/' found in the string.");
} }
string numberStr = str.substr(0, pos); // 截取 "//" 之前的部分 string numberStr = str.substr(0, pos); // 截取 "//" 之前的部分
char *endptr; char *endptr;
long number = strtol(numberStr.c_str(), &endptr, 10); // 使用 strtol 将字符串转换为 long,并检查转换是否成功 long number = strtol(numberStr.c_str(), &endptr, 10); // 使用 strtol 将字符串转换为 long,并检查转换是否成功
if (endptr == numberStr.c_str() || *endptr != '\0' || number < 0 || static_cast<unsigned int>(number) != number) { if (endptr == numberStr.c_str() || *endptr != '\0' || number < 0 || static_cast<unsigned int>(number) != number)
{
throw invalid_argument("Invalid input string for conversion to unsigned int."); throw invalid_argument("Invalid input string for conversion to unsigned int.");
} // 检查转换是否成功,以及值是否在 unsigned int 的范围内 } // 检查转换是否成功,以及值是否在 unsigned int 的范围内
return static_cast<unsigned int>(number); // 转换为 unsigned int 并返回 return static_cast<unsigned int>(number); // 转换为 unsigned int 并返回
} }
bool read_OBJ(const string &filename, vector<Vec3f> &vertices, vector<Vec3u> &indices) bool read_OBJ(const string &filename, vector<Vec3f> &vertices, vector<Vec3u> &indices)
{ {
ifstream file(filename.c_str()); ifstream file(filename.c_str());
if (!file.is_open()) { if (!file.is_open())
{
return false; return false;
} }
string line; string line;
while (getline(file, line)) { while (getline(file, line))
{
// 忽略空行和注释行 // 忽略空行和注释行
if (line.empty() || line.at(0) == '#') { if (line.empty() || line.at(0) == '#')
{
continue; continue;
} }
// std::cout<<line<<std::endl; // std::cout<<line<<std::endl;
istringstream iss(line); istringstream iss(line);
string token; string token;
if (iss >> token && token == "v") {// 解析顶点 if (iss >> token && token == "v")
{ // 解析顶点
float x, y, z; float x, y, z;
iss >> x >> y >> z; iss >> x >> y >> z;
// std::cout<<"v:"<<x<<" "<<y<<" "<<z<<std::endl; // std::cout<<"v:"<<x<<" "<<y<<" "<<z<<std::endl;
// std::cout<<"vertices_length"<<vertices.size()<<std::endl; // std::cout<<"vertices_length"<<vertices.size()<<std::endl;
vertices.push_back(Vec3f(x, y, z)); vertices.push_back(Vec3f(x, y, z));
} }
else if (token == "f") {// 解析面(这里只提取顶点索引) else if (token == "f")
{ // 解析面(这里只提取顶点索引)
// std::cout<<"enter_f"<<std::endl; // std::cout<<"enter_f"<<std::endl;
string v1_s, v2_s, v3_s; string v1_s, v2_s, v3_s;
unsigned int v1, v2, v3; // 通常一个面由三个顶点定义 unsigned int v1, v2, v3; // 通常一个面由三个顶点定义
if (!(iss >> v1_s >> v2_s >> v3_s)) { if (!(iss >> v1_s >> v2_s >> v3_s))
{
// 处理非三角形面或其他错误情况 // 处理非三角形面或其他错误情况
continue; continue;
} }
try { try
{
v1 = extractAndConvert(v1_s) - 1; v1 = extractAndConvert(v1_s) - 1;
} catch (const std::exception& e) { }
catch (const std::exception &e)
{
std::cerr << "Error: " << e.what() << std::endl; std::cerr << "Error: " << e.what() << std::endl;
} }
try { try
{
v2 = extractAndConvert(v2_s) - 1; v2 = extractAndConvert(v2_s) - 1;
} catch (const std::exception& e) { }
catch (const std::exception &e)
{
std::cerr << "Error: " << e.what() << std::endl; std::cerr << "Error: " << e.what() << std::endl;
} }
try { try
{
v3 = extractAndConvert(v3_s) - 1; v3 = extractAndConvert(v3_s) - 1;
} catch (const std::exception& e) { }
catch (const std::exception &e)
{
std::cerr << "Error: " << e.what() << std::endl; std::cerr << "Error: " << e.what() << std::endl;
} }
// std::cout<<"f:"<<v1<<" "<<v2<<" "<<v3<<std::endl; // std::cout<<"f:"<<v1<<" "<<v2<<" "<<v3<<std::endl;
@ -177,18 +195,19 @@ bool read_OBJ(const string& filename,vector<Vec3f>& vertices,vector<Vec3u>& indi
return true; return true;
} }
void produceXML(const char * xml,const char * connectorFile,string resultfile){ void produceXML(const char *xml, const char *connectorFile, string resultfile)
{
ofstream ofs, oendpoint; ofstream ofs, oendpoint;
ofs.open("result.txt", ios::out); ofs.open("result.txt", ios::out);
string endfilename = "end" + resultfile; string endfilename = "end" + resultfile;
// oendpoint.open(endfilename.c_str(),ios::out); // oendpoint.open(endfilename.c_str(),ios::out);
readxml(xml, connectorFile); readxml(xml, connectorFile);
cout << "wire_pairs num: " << wire_pairs.size() << endl; cout << "wire_pairs num: " << wire_pairs.size() << endl;
for(int i=0;i<wire_pairs.size();i++){ for (int i = 0; i < wire_pairs.size(); i++)
{
// if(i!=6)continue; // if(i!=6)continue;
Bund &bd = wire_pairs[i]; Bund &bd = wire_pairs[i];
// oendpoint<<bd.start.x<<" "<<bd.start.y<<" "<<bd.start.z<<endl; // oendpoint<<bd.start.x<<" "<<bd.start.y<<" "<<bd.start.z<<endl;
@ -197,49 +216,55 @@ void produceXML(const char * xml,const char * connectorFile,string resultfile){
bd.start.isend = 1; bd.start.isend = 1;
bd.goal.isend = 1; bd.goal.isend = 1;
if(inbox(bd.start)&&inbox(bd.goal)){ if (inbox(bd.start) && inbox(bd.goal))
{
cout << "yes" << endl; cout << "yes" << endl;
Path path = astar.search_pair(bd); Path path = astar.search_pair(bd);
for(int j=0;j<path.points.size();j++){ for (int j = 0; j < path.points.size(); j++)
{
P pp = path.points[j]; P pp = path.points[j];
ofs << pp.x << " " << pp.y << " " << pp.z << endl; ofs << pp.x << " " << pp.y << " " << pp.z << endl;
cout << setprecision(10) << "(" << pp.x << "," << pp.y << "," << pp.z << ")"; cout << setprecision(10) << "(" << pp.x << "," << pp.y << "," << pp.z << ")";
if(j!=path.points.size()-1)cout<<"->"; if (j != path.points.size() - 1)
cout << "->";
} }
cout << endl; cout << endl;
cout << endl; cout << endl;
ofs << "#" << endl; ofs << "#" << endl;
ofs << endl; ofs << endl;
branchTree.addPath(path); branchTree.addPath(path);
} }
else cout<<"no "<<i<<endl; else
cout << "no " << i << endl;
} }
branchTree.init(); branchTree.init();
} }
void getAllFiles(string path, vector<string>& files) { void getAllFiles(string path, vector<string> &files)
{
// 文件句柄 // 文件句柄
long long hFile = 0; long long hFile = 0;
// 文件信息 // 文件信息
struct _finddata_t fileinfo; struct _finddata_t fileinfo;
string p; string p;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1) { if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
do { {
if ((fileinfo.attrib & _A_SUBDIR)) { //比较文件类型是否是文件夹 do
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0) { {
if ((fileinfo.attrib & _A_SUBDIR))
{ // 比较文件类型是否是文件夹
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name)); files.push_back(p.assign(path).append("\\").append(fileinfo.name));
// 递归搜索 // 递归搜索
getAllFiles(p.assign(path).append("\\").append(fileinfo.name), files); getAllFiles(p.assign(path).append("\\").append(fileinfo.name), files);
} }
} }
else { else
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name)); files.push_back(p.assign(path).append("\\").append(fileinfo.name));
} }
} while (_findnext(hFile, &fileinfo) == 0); // 寻找下一个,成功返回0,否则-1 } while (_findnext(hFile, &fileinfo) == 0); // 寻找下一个,成功返回0,否则-1
@ -248,7 +273,8 @@ void getAllFiles(string path, vector<string>& files) {
} }
// 初始化各个结构体 // 初始化各个结构体
inline void init(){ inline void init()
{
kdtree.build(); kdtree.build();
cout << "kdtree build Finish" << endl; cout << "kdtree build Finish" << endl;
@ -259,7 +285,8 @@ inline void init(){
cout << "basicChannel build Finish" << endl; cout << "basicChannel build Finish" << endl;
basicChannel.createBranchPoint(); basicChannel.createBranchPoint();
cout << "branchPoint create Finish" << endl; cout << "branchPoint create Finish" << endl;
for(int i=1;i<=branchPointSet.branchPointNum;i++){ for (int i = 1; i <= branchPointSet.branchPointNum; i++)
{
astar.add_branchPoint(branchPointSet.b[i]); astar.add_branchPoint(branchPointSet.b[i]);
} }
astar.init(); astar.init();
@ -267,13 +294,11 @@ inline void init(){
basicEdge.buildEdgeBetweenClipAndBranchPoint(); basicEdge.buildEdgeBetweenClipAndBranchPoint();
cout << "edge build Finish" << endl; cout << "edge build Finish" << endl;
cout << "INIT Finish" << endl; cout << "INIT Finish" << endl;
} }
int _tmain(int argc, char *argv[]) int _tmain(int argc, char *argv[])
{ {
// test(); // test();
// return 0; // return 0;
cout << "Start" << endl; cout << "Start" << endl;
@ -283,7 +308,8 @@ int _tmain(int argc, char * argv[])
// cin>>mode; // cin>>mode;
mode = argv[1]; mode = argv[1];
if(mode=="generateCATIA"){ if (mode == "generateCATIA")
{
// 该模式为基本模式,使用它来生成通道模型 // 该模式为基本模式,使用它来生成通道模型
string clipDoc, xmlDoc, connectorFile, stlFileName, outputFileName; string clipDoc, xmlDoc, connectorFile, stlFileName, outputFileName;
cin >> clipDoc >> xmlDoc >> connectorFile >> stlFileName >> outputFileName; cin >> clipDoc >> xmlDoc >> connectorFile >> stlFileName >> outputFileName;
@ -303,7 +329,8 @@ int _tmain(int argc, char * argv[])
vector<string> filenames; vector<string> filenames;
getAllFiles(xmlDoc, filenames); getAllFiles(xmlDoc, filenames);
for(int i=0;i<filenames.size();i++){ for (int i = 0; i < filenames.size(); i++)
{
// if(i!=0)break; // if(i!=0)break;
string wirebh; string wirebh;
stringstream ss; stringstream ss;
@ -318,12 +345,12 @@ int _tmain(int argc, char * argv[])
string resultname = outputFileName + "ALL.txt"; string resultname = outputFileName + "ALL.txt";
basicChannel.printBundle(resultname); basicChannel.printBundle(resultname);
time(&r2); time(&r2);
cout << "Astar time " << (r2 - r1) << endl; cout << "Astar time " << (r2 - r1) << endl;
cout << "ALL FINISH" << endl; cout << "ALL FINISH" << endl;
} }
else if(mode=="createChannel"){ else if (mode == "createChannel")
{
// 该模式用于根据卡箍相对位置生成近似通道 // 该模式用于根据卡箍相对位置生成近似通道
string clipDoc, stlFileName, outputFileName; string clipDoc, stlFileName, outputFileName;
cin >> clipDoc >> stlFileName >> outputFileName; cin >> clipDoc >> stlFileName >> outputFileName;
@ -347,7 +374,8 @@ int _tmain(int argc, char * argv[])
cout << "ALL FINISH" << endl; cout << "ALL FINISH" << endl;
} }
//----------------------mark---------------------- //----------------------mark----------------------
else if(mode=="createTinyroute"){ else if (mode == "createTinyroute")
{
// 该模式为狭小空间模式,使用它来生成狭小空间的路径 // 该模式为狭小空间模式,使用它来生成狭小空间的路径
string clipDoc, xmlDoc, connectorFile, OBJDoc, outputFileName; string clipDoc, xmlDoc, connectorFile, OBJDoc, outputFileName;
// cin>>clipDoc>>xmlDoc>>connectorFile>>OBJDoc>>outputFileName; // cin>>clipDoc>>xmlDoc>>connectorFile>>OBJDoc>>outputFileName;
@ -360,7 +388,8 @@ int _tmain(int argc, char * argv[])
vector<string> OBJFilenames; vector<string> OBJFilenames;
getAllFiles(OBJDoc, OBJFilenames); getAllFiles(OBJDoc, OBJFilenames);
cout << "OBJfilename.size()" << OBJFilenames.size() << endl; cout << "OBJfilename.size()" << OBJFilenames.size() << endl;
for(int i=0;i<OBJFilenames.size();i++){ for (int i = 0; i < OBJFilenames.size(); i++)
{
if (read_OBJ(OBJFilenames[i], vertices, indices)) if (read_OBJ(OBJFilenames[i], vertices, indices))
{ {
cout << "read OBJ File succeed" << endl; cout << "read OBJ File succeed" << endl;
@ -374,9 +403,9 @@ int _tmain(int argc, char * argv[])
static BVH_intersection bvh(mesh); static BVH_intersection bvh(mesh);
bool hit = bvh.intersectWithLineSegment(lineSegment); bool hit = bvh.intersectWithLineSegment(lineSegment);
cout << "Line segment intersects with Mesh:" << hit << endl; cout << "Line segment intersects with Mesh:" << hit << endl;
} }
else { else
{
cerr << "Failed to parse OBJ file." << endl; cerr << "Failed to parse OBJ file." << endl;
} }
} }
@ -397,7 +426,8 @@ int _tmain(int argc, char * argv[])
getAllFiles(xmlDoc, filenames); getAllFiles(xmlDoc, filenames);
cout << "filename.size()" << filenames.size() << endl; cout << "filename.size()" << filenames.size() << endl;
for(int i=0;i<filenames.size();i++){ for (int i = 0; i < filenames.size(); i++)
{
cout << "i:" << i << " " << filenames[i] << endl; cout << "i:" << i << " " << filenames[i] << endl;
// if(i!=0)break; // if(i!=0)break;
string wirebh; string wirebh;
@ -413,16 +443,11 @@ int _tmain(int argc, char * argv[])
string resultname = outputFileName + "ALL.txt"; string resultname = outputFileName + "ALL.txt";
basicChannel.printBundle(resultname); basicChannel.printBundle(resultname);
time(&r2); time(&r2);
cout << "Astar time " << (r2 - r1) << endl; cout << "Astar time " << (r2 - r1) << endl;
cout << "ALL FINISH" << endl; cout << "ALL FINISH" << endl;
} }
system("pause"); system("pause");
return 0; return 0;
} }

Loading…
Cancel
Save