Browse Source

格式化

divide_struct_def_imp
郑敬润 10 months ago
parent
commit
b53fcc82ff
  1. 625
      src/xmlsql.cpp

625
src/xmlsql.cpp

@ -5,424 +5,449 @@
#include "Astar.h"
#include "Geometry.h"
#include "Intersection.h"
#include<string>
#include<map>
#include <string>
#include <map>
#include <iomanip>
#include<io.h>
#include<vector>
#include<fstream>
#include<sstream>
#include<time.h>
#include <io.h>
#include <vector>
#include <fstream>
#include <sstream>
#include <time.h>
using namespace std;
map<P, string> pmp, clipName;
map<P,string> pmp,clipName;
struct Readexcel{
vector<string>slist;
TiXmlElement* GetNodePointerByName(TiXmlElement* pRootEle, const char* strNodeName)
struct Readexcel
{
vector<string> slist;
TiXmlElement *GetNodePointerByName(TiXmlElement *pRootEle, const char *strNodeName)
{
// if equal root node then return
if (0 == strcmp(strNodeName, pRootEle->Value()))
{
return pRootEle;
}
TiXmlElement* pEle = pRootEle;
for (pEle = pRootEle->FirstChildElement(); pEle; pEle = pEle->NextSiblingElement())
{
// recursive find sub node return node pointer
TiXmlElement *pEle = pRootEle;
for (pEle = pRootEle->FirstChildElement(); pEle; pEle = pEle->NextSiblingElement())
{
// recursive find sub node return node pointer
if (0 != strcmp(pEle->Value(), strNodeName))
{
TiXmlElement* res=GetNodePointerByName(pEle,strNodeName);
if(res!=NULL)return res;
TiXmlElement *res = GetNodePointerByName(pEle, strNodeName);
if (res != NULL)
return res;
}
else return pEle;
}
else
return pEle;
}
return NULL;
}
vector<string> read(string xml){
vector<string> read(string xml)
{
slist.clear();
TiXmlDocument* pDoc = new TiXmlDocument();
TiXmlDocument *pDoc = new TiXmlDocument();
pDoc->LoadFile(xml.c_str());
TiXmlElement *RootElement = pDoc->RootElement();
TiXmlElement * row=GetNodePointerByName(RootElement, "Row");
TiXmlElement* pEle;
for (pEle =row; pEle; pEle = pEle->NextSiblingElement())
{
// recursive find sub node return node pointer
TiXmlElement* cell;
TiXmlElement *row = GetNodePointerByName(RootElement, "Row");
TiXmlElement *pEle;
for (pEle = row; pEle; pEle = pEle->NextSiblingElement())
{
// recursive find sub node return node pointer
TiXmlElement *cell;
string s[7];
int i=0;
for (cell =pEle->FirstChildElement(); cell; cell = cell->NextSiblingElement()){
//cout<<(cell->FirstChildElement()->GetText())<<endl;
s[i]=cell->FirstChildElement()->GetText();
int i = 0;
for (cell = pEle->FirstChildElement(); cell; cell = cell->NextSiblingElement())
{
// cout<<(cell->FirstChildElement()->GetText())<<endl;
s[i] = cell->FirstChildElement()->GetText();
i++;
}
slist.push_back(s[0]);
slist.push_back(s[1]+" "+s[2]+" "+s[3]);
slist.push_back(s[4]+" "+s[5]+" "+s[6]);
}
slist.push_back(s[1] + " " + s[2] + " " + s[3]);
slist.push_back(s[4] + " " + s[5] + " " + s[6]);
}
return slist;
}
}rexcel;
//读取卡箍点
void read_points(string filename,Astar * pastar){
//cout<<"read "<<filename<<endl;
vector<string>sl=rexcel.read(filename);
//cout<<"------"<<endl;
} rexcel;
// 读取卡箍点
void read_points(string filename, Astar *pastar)
{
// cout<<"read "<<filename<<endl;
vector<string> sl = rexcel.read(filename);
// 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];
//cout<<s0<<" "<<s1<<endl;//test
istringstream sin(s1),sin2(s2); //将整行字符串line读入到字符串流istringstream中
P p1,p2;
sin>>p1.x>>p1.y>>p1.z;
sin2>>p2.x>>p2.y>>p2.z;
//p1.print("p");
pmp[p1]=s1;
pmp[p2]=s2;
clipName[p1]=s0;
clipSet.addclip(p1,p2,s0,2);
string s1 = sl[i + 1], s2 = sl[i + 2], s0 = sl[i];
// cout<<s0<<" "<<s1<<endl;//test
istringstream sin(s1), sin2(s2); // 将整行字符串line读入到字符串流istringstream中
P p1, p2;
sin >> p1.x >> p1.y >> p1.z;
sin2 >> p2.x >> p2.y >> p2.z;
// p1.print("p");
pmp[p1] = s1;
pmp[p2] = s2;
clipName[p1] = s0;
clipSet.addclip(p1, p2, s0, 2);
pastar->add_clip(clipSet.c[clipSet.clipnum]);
//cout<<"--"<<endl;
// cout<<"--"<<endl;
kdtree.add(p1);
}
}
unsigned int extractAndConvert(const string &str)
{
size_t pos = str.find("/"); // 查找 "//" 的位置
if (pos == std::string::npos)
{
throw invalid_argument("No '/' found in the string.");
}
string numberStr = str.substr(0, pos); // 截取 "//" 之前的部分
char *endptr;
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)
{
throw invalid_argument("Invalid input string for conversion to unsigned int.");
} // 检查转换是否成功,以及值是否在 unsigned int 的范围内
return static_cast<unsigned int>(number); // 转换为 unsigned int 并返回
}
unsigned int extractAndConvert(const string& str) {
size_t pos = str.find("/"); // 查找 "//" 的位置
if (pos == std::string::npos) {
throw invalid_argument("No '/' found in the string.");
}
string numberStr = str.substr(0, pos); // 截取 "//" 之前的部分
char* endptr;
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) {
throw invalid_argument("Invalid input string for conversion to unsigned int.");
} // 检查转换是否成功,以及值是否在 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());
if (!file.is_open()) {
return false;
}
string line;
while (getline(file, line)) {
// 忽略空行和注释行
if (line.empty() || line.at(0) == '#') {
continue;
}
//std::cout<<line<<std::endl;
istringstream iss(line);
string token;
if (iss >> token && token == "v") {// 解析顶点
float x, y, z;
iss >> x >> y >> z;
//std::cout<<"v:"<<x<<" "<<y<<" "<<z<<std::endl;
//std::cout<<"vertices_length"<<vertices.size()<<std::endl;
vertices.push_back(Vec3f(x,y,z));
}
else if (token == "f") {// 解析面(这里只提取顶点索引)
//std::cout<<"enter_f"<<std::endl;
string v1_s,v2_s,v3_s;
unsigned int v1, v2, v3; // 通常一个面由三个顶点定义
if (!(iss >> v1_s >> v2_s >> v3_s)) {
// 处理非三角形面或其他错误情况
continue;
}
try {
v1=extractAndConvert(v1_s)-1;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
try {
v2=extractAndConvert(v2_s)-1;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
try {
v3=extractAndConvert(v3_s)-1;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
//std::cout<<"f:"<<v1<<" "<<v2<<" "<<v3<<std::endl;
//std::cout<<"indices_length:"<<indices.size()<<std::endl;
indices.push_back(Vec3u(v1,v2,v3));
}
//else std::cout<<token<<std::endl;
// 忽略其他类型的行(如法线、纹理坐标等)
}
file.close();
return true;
ifstream file(filename.c_str());
if (!file.is_open())
{
return false;
}
string line;
while (getline(file, line))
{
// 忽略空行和注释行
if (line.empty() || line.at(0) == '#')
{
continue;
}
// std::cout<<line<<std::endl;
istringstream iss(line);
string token;
if (iss >> token && token == "v")
{ // 解析顶点
float x, y, z;
iss >> x >> y >> z;
// std::cout<<"v:"<<x<<" "<<y<<" "<<z<<std::endl;
// std::cout<<"vertices_length"<<vertices.size()<<std::endl;
vertices.push_back(Vec3f(x, y, z));
}
else if (token == "f")
{ // 解析面(这里只提取顶点索引)
// std::cout<<"enter_f"<<std::endl;
string v1_s, v2_s, v3_s;
unsigned int v1, v2, v3; // 通常一个面由三个顶点定义
if (!(iss >> v1_s >> v2_s >> v3_s))
{
// 处理非三角形面或其他错误情况
continue;
}
try
{
v1 = extractAndConvert(v1_s) - 1;
}
catch (const std::exception &e)
{
std::cerr << "Error: " << e.what() << std::endl;
}
try
{
v2 = extractAndConvert(v2_s) - 1;
}
catch (const std::exception &e)
{
std::cerr << "Error: " << e.what() << std::endl;
}
try
{
v3 = extractAndConvert(v3_s) - 1;
}
catch (const std::exception &e)
{
std::cerr << "Error: " << e.what() << std::endl;
}
// std::cout<<"f:"<<v1<<" "<<v2<<" "<<v3<<std::endl;
// std::cout<<"indices_length:"<<indices.size()<<std::endl;
indices.push_back(Vec3u(v1, v2, v3));
}
// else std::cout<<token<<std::endl;
// 忽略其他类型的行(如法线、纹理坐标等)
}
file.close();
return true;
}
void produceXML(const char * xml,const char * connectorFile,string resultfile){
ofstream ofs,oendpoint;
void produceXML(const char *xml, const char *connectorFile, string resultfile)
{
ofstream ofs, oendpoint;
ofs.open("result.txt", ios::out);
string endfilename="end"+resultfile;
//oendpoint.open(endfilename.c_str(),ios::out);
readxml(xml,connectorFile);
cout<<"wire_pairs num: "<<wire_pairs.size()<<endl;
for(int i=0;i<wire_pairs.size();i++){
//if(i!=6)continue;
Bund &bd=wire_pairs[i];
//oendpoint<<bd.start.x<<" "<<bd.start.y<<" "<<bd.start.z<<endl;
//oendpoint<<bd.goal.x<<" "<<bd.goal.y<<" "<<bd.goal.z<<endl;
bd.start.isend=1;
bd.goal.isend=1;
if(inbox(bd.start)&&inbox(bd.goal)){
cout<<"yes"<<endl;
Path path=astar.search_pair(bd);
for(int j=0;j<path.points.size();j++){
P pp=path.points[j];
ofs<<pp.x<<" "<<pp.y<<" "<<pp.z<<endl;
cout<<setprecision(10)<<"("<<pp.x<<","<<pp.y<<","<<pp.z<<")";
if(j!=path.points.size()-1)cout<<"->";
string endfilename = "end" + resultfile;
// oendpoint.open(endfilename.c_str(),ios::out);
readxml(xml, connectorFile);
cout << "wire_pairs num: " << wire_pairs.size() << endl;
for (int i = 0; i < wire_pairs.size(); i++)
{
// if(i!=6)continue;
Bund &bd = wire_pairs[i];
// oendpoint<<bd.start.x<<" "<<bd.start.y<<" "<<bd.start.z<<endl;
// oendpoint<<bd.goal.x<<" "<<bd.goal.y<<" "<<bd.goal.z<<endl;
bd.start.isend = 1;
bd.goal.isend = 1;
if (inbox(bd.start) && inbox(bd.goal))
{
cout << "yes" << endl;
Path path = astar.search_pair(bd);
for (int j = 0; j < path.points.size(); j++)
{
P pp = path.points[j];
ofs << pp.x << " " << pp.y << " " << pp.z << endl;
cout << setprecision(10) << "(" << pp.x << "," << pp.y << "," << pp.z << ")";
if (j != path.points.size() - 1)
cout << "->";
}
cout<<endl;
cout<<endl;
ofs<<"#"<<endl;
ofs<<endl;
cout << endl;
cout << endl;
ofs << "#" << endl;
ofs << endl;
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;
//文件信息
struct _finddata_t fileinfo;
string p;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1) {
do {
if ((fileinfo.attrib & _A_SUBDIR)) { //比较文件类型是否是文件夹
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0) {
// 文件信息
struct _finddata_t fileinfo;
string p;
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
{
do
{
if ((fileinfo.attrib & _A_SUBDIR))
{ // 比较文件类型是否是文件夹
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
//递归搜索
// 递归搜索
getAllFiles(p.assign(path).append("\\").append(fileinfo.name), files);
}
}
else {
else
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
}
} while (_findnext(hFile, &fileinfo) == 0); //寻找下一个,成功返回0,否则-1
} while (_findnext(hFile, &fileinfo) == 0); // 寻找下一个,成功返回0,否则-1
_findclose(hFile);
}
}
//初始化各个结构体
inline void init(){
// 初始化各个结构体
inline void init()
{
kdtree.build();
cout<<"kdtree build Finish"<<endl;
cout << "kdtree build Finish" << endl;
clipSet.computeCenter();
cout<<"compute center Finish Y:"<<Ycenter<<" Z:"<<Zcenter<<endl;
cout << "compute center Finish Y:" << Ycenter << " Z:" << Zcenter << endl;
clipSet.buildChannel();
basicChannel.createChannel();
cout<<"basicChannel build Finish"<<endl;
cout << "basicChannel build Finish" << endl;
basicChannel.createBranchPoint();
cout<<"branchPoint create Finish"<<endl;
for(int i=1;i<=branchPointSet.branchPointNum;i++){
cout << "branchPoint create Finish" << endl;
for (int i = 1; i <= branchPointSet.branchPointNum; i++)
{
astar.add_branchPoint(branchPointSet.b[i]);
}
astar.init();
cout<<"astar init Finish clipNum:"<<astar.pnum-2<<" Toatal Point Num:"<<astar.tnum<<endl;
cout << "astar init Finish clipNum:" << astar.pnum - 2 << " Toatal Point Num:" << astar.tnum << endl;
basicEdge.buildEdgeBetweenClipAndBranchPoint();
cout<<"edge build Finish"<<endl;
cout<<"INIT Finish"<<endl;
cout << "edge build Finish" << endl;
cout << "INIT Finish" << endl;
}
int _tmain(int argc, char * argv[])
int _tmain(int argc, char *argv[])
{
//test();
//return 0;
cout<<"Start"<<endl;
Astar *pastar=&astar;
// test();
// return 0;
cout << "Start" << endl;
Astar *pastar = &astar;
string mode;
// cin>>mode;
mode = argv[1];
if(mode=="generateCATIA"){
//该模式为基本模式,使用它来生成通道模型
string clipDoc,xmlDoc,connectorFile,stlFileName,outputFileName;
cin>>clipDoc>>xmlDoc>>connectorFile>>stlFileName>>outputFileName;
if (mode == "generateCATIA")
{
// 该模式为基本模式,使用它来生成通道模型
string clipDoc, xmlDoc, connectorFile, stlFileName, outputFileName;
cin >> clipDoc >> xmlDoc >> connectorFile >> stlFileName >> outputFileName;
vector<string> clipFileNames;
getAllFiles(clipDoc,clipFileNames);
for(int i=0;i<clipFileNames.size();i++)
read_points(clipFileNames[i].c_str(),pastar);
cout<<"read points finish"<<endl;
//bvh.read_stl(stlFileName.c_str());
time_t r1,r2;
getAllFiles(clipDoc, clipFileNames);
for (int i = 0; i < clipFileNames.size(); i++)
read_points(clipFileNames[i].c_str(), pastar);
cout << "read points finish" << endl;
// bvh.read_stl(stlFileName.c_str());
time_t r1, r2;
time(&r1);
init();
vector<string> filenames;
getAllFiles(xmlDoc,filenames);
for(int i=0;i<filenames.size();i++){
//if(i!=0)break;
getAllFiles(xmlDoc, filenames);
for (int i = 0; i < filenames.size(); i++)
{
// if(i!=0)break;
string wirebh;
stringstream ss;
ss<<i;
wirebh=ss.str();
string filename=filenames[i];
cout<<filename<<endl;
cout<<endl;
string resultname=outputFileName+wirebh+".txt";
produceXML(filename.c_str(),connectorFile.c_str(),resultname);
ss << i;
wirebh = ss.str();
string filename = filenames[i];
cout << filename << endl;
cout << endl;
string resultname = outputFileName + wirebh + ".txt";
produceXML(filename.c_str(), connectorFile.c_str(), resultname);
}
string resultname=outputFileName+"ALL.txt";
string resultname = outputFileName + "ALL.txt";
basicChannel.printBundle(resultname);
time(&r2);
cout<<"Astar time "<<(r2-r1)<<endl;
cout<<"ALL FINISH"<<endl;
cout << "Astar time " << (r2 - r1) << endl;
cout << "ALL FINISH" << endl;
}
else if(mode=="createChannel"){
//该模式用于根据卡箍相对位置生成近似通道
string clipDoc,stlFileName,outputFileName;
cin>>clipDoc>>stlFileName>>outputFileName;
cout<<"mode = createChannel"<<endl;
else if (mode == "createChannel")
{
// 该模式用于根据卡箍相对位置生成近似通道
string clipDoc, stlFileName, outputFileName;
cin >> clipDoc >> stlFileName >> outputFileName;
cout << "mode = createChannel" << endl;
vector<string> clipFileNames;
getAllFiles(clipDoc,clipFileNames);
for(int i=0;i<clipFileNames.size();i++)
read_points(clipFileNames[i].c_str(),pastar);
cout<<"read points finish"<<endl;
getAllFiles(clipDoc, clipFileNames);
for (int i = 0; i < clipFileNames.size(); i++)
read_points(clipFileNames[i].c_str(), pastar);
cout << "read points finish" << endl;
bvh.read_stl(stlFileName.c_str());
time_t r1,r2;
time_t r1, r2;
time(&r1);
init();
basicChannel.printBundle(outputFileName,true);
basicChannel.printBundle(outputFileName, true);
time(&r2);
cout<<"Astar time "<<(r2-r1)<<endl;
cout<<"ALL FINISH"<<endl;
cout << "Astar time " << (r2 - r1) << endl;
cout << "ALL FINISH" << endl;
}
//----------------------mark----------------------
else if(mode=="createTinyroute"){
//该模式为狭小空间模式,使用它来生成狭小空间的路径
string clipDoc,xmlDoc,connectorFile,OBJDoc,outputFileName;
else if (mode == "createTinyroute")
{
// 该模式为狭小空间模式,使用它来生成狭小空间的路径
string clipDoc, xmlDoc, connectorFile, OBJDoc, outputFileName;
// cin>>clipDoc>>xmlDoc>>connectorFile>>OBJDoc>>outputFileName;
clipDoc = argv[2];
xmlDoc = argv[3];
connectorFile = argv[4];
OBJDoc = argv[5];
outputFileName = argv[6];
vector<string> OBJFilenames;
getAllFiles(OBJDoc,OBJFilenames);
cout<<"OBJfilename.size()"<<OBJFilenames.size()<<endl;
for(int i=0;i<OBJFilenames.size();i++){
if(read_OBJ(OBJFilenames[i],vertices,indices))
getAllFiles(OBJDoc, OBJFilenames);
cout << "OBJfilename.size()" << OBJFilenames.size() << endl;
for (int i = 0; i < OBJFilenames.size(); i++)
{
if (read_OBJ(OBJFilenames[i], vertices, indices))
{
cout<<"read OBJ File succeed"<<endl;
mesh.indices=indices;
mesh.vertices=vertices;
cout<<"vertices_length:"<<vertices.size()<<endl;
cout<<"indices_length:"<<indices.size()<<endl;
cout << "read OBJ File succeed" << endl;
mesh.indices = indices;
mesh.vertices = vertices;
cout << "vertices_length:" << vertices.size() << endl;
cout << "indices_length:" << indices.size() << endl;
// Line segment
intersection_model =1;
LineSegment lineSegment(Vec3f(3.46, 0.87, 1.57),Vec3f(1.7, 2.04, 1.46));//VS2008
intersection_model = 1;
LineSegment lineSegment(Vec3f(3.46, 0.87, 1.57), Vec3f(1.7, 2.04, 1.46)); // VS2008
static BVH_intersection bvh(mesh);
bool hit = bvh.intersectWithLineSegment(lineSegment);
cout<<"Line segment intersects with Mesh:"<< hit<<endl;
cout << "Line segment intersects with Mesh:" << hit << endl;
}
else
{
cerr << "Failed to parse OBJ file." << endl;
}
else {
cerr << "Failed to parse OBJ file." << endl;
}
}
vector<string> clipFileNames;
getAllFiles(clipDoc,clipFileNames);
for(int i=0;i<clipFileNames.size();i++)
read_points(clipFileNames[i].c_str(),pastar);
cout<<"read points finish"<<endl;
//bvh.read_stl(stlFileName.c_str());
time_t r1,r2;
getAllFiles(clipDoc, clipFileNames);
for (int i = 0; i < clipFileNames.size(); i++)
read_points(clipFileNames[i].c_str(), pastar);
cout << "read points finish" << endl;
// bvh.read_stl(stlFileName.c_str());
time_t r1, r2;
time(&r1);
init();
vector<string> filenames;
getAllFiles(xmlDoc,filenames);
getAllFiles(xmlDoc, filenames);
cout<<"filename.size()"<<filenames.size()<<endl;
for(int i=0;i<filenames.size();i++){
cout<<"i:"<<i<<" "<<filenames[i]<<endl;
//if(i!=0)break;
cout << "filename.size()" << filenames.size() << endl;
for (int i = 0; i < filenames.size(); i++)
{
cout << "i:" << i << " " << filenames[i] << endl;
// if(i!=0)break;
string wirebh;
stringstream ss;
ss<<i;
wirebh=ss.str();
string filename=filenames[i];
cout<<filename<<endl;
cout<<endl;
string resultname=outputFileName+wirebh+".txt";
produceXML(filename.c_str(),connectorFile.c_str(),resultname);
ss << i;
wirebh = ss.str();
string filename = filenames[i];
cout << filename << endl;
cout << endl;
string resultname = outputFileName + wirebh + ".txt";
produceXML(filename.c_str(), connectorFile.c_str(), resultname);
}
string resultname=outputFileName+"ALL.txt";
string resultname = outputFileName + "ALL.txt";
basicChannel.printBundle(resultname);
time(&r2);
cout<<"Astar time "<<(r2-r1)<<endl;
cout<<"ALL FINISH"<<endl;
cout << "Astar time " << (r2 - r1) << endl;
cout << "ALL FINISH" << endl;
}
system("pause");
return 0;
}

Loading…
Cancel
Save