#define _CRT_SECURE_NO_DEPRECATE
#include #include #include #include #include #include #include #include using namespace std; //配置:修改a以修改保存路径,将待转化的文件放入文件夹下命名为input.txt即可开始转化 //或者将c修改为绝对路径 char a[] = "F:\实验室\output"; char c[] = "D:\QQfile\最大的挑战.txt";
class point { public:
int x, y, z; bool exist; point(); ~point(); void set(float x, float y, float z,bool exist) { this->x = x, this->y = y, this->z = z; this->exist = exist; } void setexist(int i) { this->exist = i; }
private:
};
point::point() { }
point::~point() { }
class CSTL { private:
vector<float> vx; vector<float> vy; vector<float> vz; vector<int> triaV1; vector<int> triaV2; vector<int> triaV3; int m_TriaNum;
public:
CSTL(); ~CSTL();
bool SetStlInf(vector<float> px, vector<float> py, vector<float> pz, vector<int> TriaNum1, vector<int> TriaNum2, vector<int> TriaNum3, int TriaTotalNum); bool SaveStlBinary(char* pathname, char* filename); bool SaveStlASCII(char* pathname, char* filename);
}; CSTL::CSTL() { } CSTL::~CSTL() { } bool CSTL::SetStlInf(vector px, vector py, vector pz,
vector<int> TriaNum1, vector<int> TriaNum2, vector<int> TriaNum3, int TriaTotalNum)
{
bool success = false; vx = px; vy = py; vz = pz; triaV1 = TriaNum1; triaV2 = TriaNum2; triaV3 = TriaNum3; m_TriaNum = TriaTotalNum; success = true; return success;
} bool CSTL::SaveStlBinary(char* pathname, char* filename) {
bool suc = true; char* savename = new char[100]; sprintf(savename, "%s%s.stl", pathname, filename); char* fileInf = new char[200]; sprintf(fileInf, "solid %s.stl %s", filename, "by master"); FILE* fp = fopen(savename, "wb"); delete[]savename; float* dat = new float[12]; fwrite(fileInf, sizeof(char), 80, fp); fwrite(&m_TriaNum, sizeof(int), 1, fp); for (int i = 0; i < m_TriaNum; i++) { int id = triaV1[i]; float v1x = vx[id]; float v1y = vy[id]; float v1z = vz[id]; id = triaV2[i]; float v2x = vx[id]; float v2y = vy[id]; float v2z = vz[id]; id = triaV3[i]; float v3x = vx[id]; float v3y = vy[id]; float v3z = vz[id]; float nx = (v1y - v3y) * (v2z - v3z) - (v1z - v3z) * (v2y - v3y); float ny = (v1z - v3z) * (v2x - v3x) - (v2z - v3z) * (v1x - v3x); float nz = (v1x - v3x) * (v2y - v3y) - (v2x - v3x) * (v1y - v3y); float nxyz = sqrt(nx * nx + ny * ny + nz * nz); dat[0] = nx / nxyz; dat[1] = ny / nxyz; dat[2] = nz / nxyz; dat[3] = v1x; dat[4] = v1y; dat[5] = v1z; dat[6] = v2x; dat[7] = v2y; dat[8] = v2z; dat[9] = v3x; dat[10] = v3y; dat[11] = v3z; fwrite(dat, sizeof(float), 12, fp); fwrite("wl", sizeof(char), 2, fp); } fclose(fp); delete[]dat; delete[]fileInf; Sleep(1); return suc;
}
void generate(point*cube,int xmax,int ymax,int zmax); void STLstorage(); void grant(); void cubestorage();
enum forward{up,down,left,right,front,back}; vector xin,yin,zin; vectorranx, rany, ranz; int max_pointnum = 0; vectorx_statue, y_statue, z_statue;//存储xyz坐标 vectorexist; int xmax, ymax, zmax;
int main() {
STLstorage(); //grant(); cubestorage(); for (long i = 0; i < max_pointnum/3; i++) { ranx.push_back(3 * i); rany.push_back(3 * i + 1); ranz.push_back(3 * i + 2); } CSTL stl; char c[] = "bin"; stl.SetStlInf(xin, yin, zin, ranx, rany, ranz, max_pointnum/3); stl.SaveStlBinary(a, c); cout << "max face: " << max_pointnum / 6 << endl; return 0;
void STLstorage() {
string storage = ""; ifstream inFile(c, ios::in); string lineStr; vector<vector<string>> strArray; while (getline(inFile, lineStr)) { storage += lineStr; storage += '\n'; } int i = 0; while (storage[i]) { if (storage[i] == ' ')storage[i] = '\n'; i++; } ofstream outfile; outfile.open("storage.txt"); outfile << storage; outfile.close();
void cubestorage() {
ifstream afile; afile.open("storage.txt", ios::in); string pin; int i = 0; while (getline(afile, pin)) { int a=i % 4; if (a == 0)x_statue.push_back(atof(pin.c_str())); if (a == 1)y_statue.push_back(atof(pin.c_str())); if (a == 2)z_statue.push_back(atof(pin.c_str())); if (a == 3) { if (pin == "1") { exist.push_back(1); } else { exist.push_back(0); } } i++; } cout << "总点数为:" << i / 4 <<"开始创建点类" << endl; xmax = *max_element(x_statue.begin(), x_statue.end()); ymax = *max_element(y_statue.begin(), y_statue.end()); zmax = *max_element(z_statue.begin(), z_statue.end()); xmax++; ymax++; zmax++; cout << "最大跨度分别为\t" << xmax <<'\t' << ymax <<"\t" << zmax << endl; point* cube = new point[xmax*ymax*zmax]; for (int j = 0; j < xmax * ymax * zmax; j++) { cube[j].set(x_statue[j], y_statue[j], z_statue[j], exist[j]); //cout << cube[j].x << '\t' << cube[j].y << '\t' << cube[j].z <<'\t'<<"bool:"<<cube[j].exist << endl; }//现将所有点及状态存入cube for (int i = 0; i < xmax * ymax * zmax; i++) { //cout << "第" << i + 1 << "次尝试" << endl; generate(cube+i, xmax - 1, ymax - 1, zmax - 1); } //顺序:上下左右前后
} void generate(point*cube,int xmax,int ymax,int zmax) {
if (cube->exist) { //前方无路可走时 if ((cube->x != xmax && cube->exist != (cube +(ymax+1)* (zmax+1))->exist) || (cube->exist && cube->x == xmax)) {//&& cube->x == xmax //cout << "添加片" << endl; max_pointnum += 6; xin.push_back(cube->x + 0.5); yin.push_back(cube->y + 0.5); zin.push_back(cube->z + 0.5); xin.push_back(cube->x + 0.5); yin.push_back(cube->y - 0.5); zin.push_back(cube->z + 0.5); xin.push_back(cube->x + 0.5); yin.push_back(cube->y - 0.5); zin.push_back(cube->z - 0.5); xin.push_back(cube->x + 0.5); yin.push_back(cube->y + 0.5); zin.push_back(cube->z + 0.5); xin.push_back(cube->x + 0.5); yin.push_back(cube->y + 0.5); zin.push_back(cube->z - 0.5); xin.push_back(cube->x + 0.5); yin.push_back(cube->y - 0.5); zin.push_back(cube->z - 0.5); } //后方无路可走时 if ((cube->x != 0 && cube->exist != (cube - (ymax + 1) * (zmax + 1))->exist) || (cube->exist && cube->x == 0)) {// && cube->x == xmax //cout << "添加片" << endl; max_pointnum += 6; xin.push_back(cube->x - 0.5); yin.push_back(cube->y + 0.5); zin.push_back(cube->z + 0.5); xin.push_back(cube->x - 0.5); yin.push_back(cube->y + 0.5); zin.push_back(cube->z - 0.5); xin.push_back(cube->x - 0.5); yin.push_back(cube->y - 0.5); zin.push_back(cube->z - 0.5); xin.push_back(cube->x - 0.5); yin.push_back(cube->y + 0.5); zin.push_back(cube->z + 0.5); xin.push_back(cube->x - 0.5); yin.push_back(cube->y - 0.5); zin.push_back(cube->z + 0.5); xin.push_back(cube->x - 0.5); yin.push_back(cube->y - 0.5); zin.push_back(cube->z - 0.5); } //左边无路可走时 if ((cube->y != 0 && cube->exist != (cube - (zmax+1))->exist) || (cube->exist && cube->y == 0)) {// && cube->y == 0 //cout << "添加片" << endl; max_pointnum += 6; xin.push_back(cube->x + 0.5); yin.push_back(cube->y - 0.5); zin.push_back(cube->z + 0.5); xin.push_back(cube->x + 0.5); yin.push_back(cube->y - 0.5); zin.push_back(cube->z - 0.5); xin.push_back(cube->x - 0.5); yin.push_back(cube->y - 0.5); zin.push_back(cube->z - 0.5); xin.push_back(cube->x + 0.5); yin.push_back(cube->y - 0.5); zin.push_back(cube->z + 0.5); xin.push_back(cube->x - 0.5); yin.push_back(cube->y - 0.5); zin.push_back(cube->z + 0.5); xin.push_back(cube->x - 0.5); yin.push_back(cube->y - 0.5); zin.push_back(cube->z - 0.5); } //右边无路可走时 if ((cube->y != ymax && cube->exist != (cube + (zmax+1))->exist) || (cube->exist && cube->y == ymax)) {// && cube->y == ymax //cout << "添加片" << endl; max_pointnum += 6; xin.push_back(cube->x + 0.5); yin.push_back(cube->y + 0.5); zin.push_back(cube->z + 0.5); xin.push_back(cube->x + 0.5); yin.push_back(cube->y + 0.5); zin.push_back(cube->z - 0.5); xin.push_back(cube->x - 0.5); yin.push_back(cube->y + 0.5); zin.push_back(cube->z - 0.5); xin.push_back(cube->x + 0.5); yin.push_back(cube->y + 0.5); zin.push_back(cube->z + 0.5); xin.push_back(cube->x - 0.5); yin.push_back(cube->y + 0.5); zin.push_back(cube->z + 0.5); xin.push_back(cube->x - 0.5); yin.push_back(cube->y + 0.5); zin.push_back(cube->z - 0.5); } //上边无路可走时 if ((cube->z != zmax && cube->exist != (cube + 1)->exist) || (cube->exist && cube->z == zmax)) {// && cube->z == zmax //cout << "添加片" << endl; max_pointnum += 6; xin.push_back(cube->x - 0.5); yin.push_back(cube->y - 0.5); zin.push_back(cube->z + 0.5); xin.push_back(cube->x + 0.5); yin.push_back(cube->y - 0.5); zin.push_back(cube->z + 0.5); xin.push_back(cube->x - 0.5); yin.push_back(cube->y + 0.5); zin.push_back(cube->z + 0.5); xin.push_back(cube->x + 0.5); yin.push_back(cube->y + 0.5); zin.push_back(cube->z + 0.5); xin.push_back(cube->x + 0.5); yin.push_back(cube->y - 0.5); zin.push_back(cube->z + 0.5); xin.push_back(cube->x - 0.5); yin.push_back(cube->y + 0.5); zin.push_back(cube->z + 0.5); } //下边无路可走时 if ((cube->z != 0 && cube->exist != (cube - 1)->exist) || (cube->exist && cube->z == 0)) {// && cube->z == 0 //cout << "添加片" << endl; max_pointnum += 6; xin.push_back(cube->x - 0.5); yin.push_back(cube->y - 0.5); zin.push_back(cube->z - 0.5); xin.push_back(cube->x + 0.5); yin.push_back(cube->y - 0.5); zin.push_back(cube->z - 0.5); xin.push_back(cube->x - 0.5); yin.push_back(cube->y + 0.5); zin.push_back(cube->z - 0.5); xin.push_back(cube->x + 0.5); yin.push_back(cube->y + 0.5); zin.push_back(cube->z - 0.5); xin.push_back(cube->x + 0.5); yin.push_back(cube->y - 0.5); zin.push_back(cube->z - 0.5); xin.push_back(cube->x - 0.5); yin.push_back(cube->y + 0.5); zin.push_back(cube->z - 0.5); } }
#define _CRT_SECURE_NO_DEPRECATE
#include #include #include #include #include #include #include #include using namespace std; //配置:修改a以修改保存路径,将待转化的文件放入文件夹下命名为input.txt即可开始转化 //或者将c修改为绝对路径 char a[] = "F:\实验室\output"; char c[] = "D:\QQfile\最大的挑战.txt";
class point { public:
private:
};
point::point() { }
point::~point() { }
class CSTL { private:
public:
public:
}; CSTL::CSTL() { } CSTL::~CSTL() { } bool CSTL::SetStlInf(vector px, vector py, vector pz,
{
} bool CSTL::SaveStlBinary(char* pathname, char* filename) {
}
void generate(point*cube,int xmax,int ymax,int zmax); void STLstorage(); void grant(); void cubestorage();
enum forward{up,down,left,right,front,back}; vector xin,yin,zin; vectorranx, rany, ranz; int max_pointnum = 0; vectorx_statue, y_statue, z_statue;//存储xyz坐标 vectorexist; int xmax, ymax, zmax;
int main() {
}
void STLstorage() {
}
void cubestorage() {
} void generate(point*cube,int xmax,int ymax,int zmax) {
}