#include #include "SingularityJudger.h" #include "fstream" #include "QString" #include "QList" #include "QRegularExpression" #include "gauss_map.h" #include "bvh.h" #include "utils.h" SrfMesh getMesh(const RationalSurface &s, int sampleLevel) { SrfMesh res; res.sampleLevel = sampleLevel; res.sampleCntOnSingleDir = int(pow(2, sampleLevel - 1) + 1); res.evaluation = vector>(res.sampleCntOnSingleDir, vector(res.sampleCntOnSingleDir)); res.tangent_u = vector>(res.sampleCntOnSingleDir, vector(res.sampleCntOnSingleDir)); res.tangent_v = vector>(res.sampleCntOnSingleDir, vector(res.sampleCntOnSingleDir)); res.normal = vector>(res.sampleCntOnSingleDir, vector(res.sampleCntOnSingleDir)); auto s_first_u = *(s.knots_u.begin()); auto s_first_v = *(s.knots_v.begin()); auto s_step_u = (*(s.knots_u.end() - 1) - s_first_u) / float(res.sampleCntOnSingleDir - 1); auto s_step_v = (*(s.knots_v.end() - 1) - s_first_v) / float(res.sampleCntOnSingleDir - 1); for (int i = 0; i < res.sampleCntOnSingleDir; i++) { auto u = s_first_u + s_step_u * float(i); for (int j = 0; j < res.sampleCntOnSingleDir; j++) { auto v = s_first_v + s_step_v * float(j); res.evaluation[i][j] = tinynurbs::surfacePoint(s, u, v); auto der = tinynurbs::surfaceDerivatives(s, 1, u, v); res.tangent_u[i][j] = der(1, 0); res.tangent_v[i][j] = der(0, 1); res.normal[i][j] = glm::normalize(glm::cross(res.tangent_u[i][j], res.tangent_v[i][j])); } } return res; } array2 getPtsFromStr(QString srfData); int main() { RationalSurface s; RationalSurface f; int level = 6; printf("level: %d, sample cnt: %d * %d\n", level, int(pow(2, level - 1)), int(pow(2, level - 1))); ifstream fin; fin.open(R"(intersectTest\case2\surfaces.txt)"); string str; string tmp; while (fin.good()) { getline(fin, tmp); str += tmp + "\n"; } // cout << str << endl; auto infos = QString(str.c_str()).split(";"); auto wData = infos[0]; auto srf1Data = infos[1]; auto srf2Data = infos[2]; auto points1 = getPtsFromStr(srf1Data); auto points2 = getPtsFromStr(srf2Data); wData = wData.remove(0, wData.indexOf("Matrix") + 6).trimmed(); wData.remove(0, 3); wData.remove(wData.size() - 3, 3); auto wInfos = wData.split(QRegularExpression("\\]( )*,( )*\\[")); array2 weights; vector wArray(points1.cols() * points1.rows()); for (int i = 0; i < points1.cols(); i++) { auto wsInV = wInfos[i].split(QRegularExpression("( )*,( )*")); for (int j = 0; j < points1.rows(); j++) { wArray[i * points1.rows() + j] = wsInV[j].toFloat(); } } weights = {points1.cols(), points2.rows(), wArray}; // knot std::vector knot_u(2 * points1.cols(), 1.); for (int i = 0; i < knot_u.size() / 2; i++)knot_u[i] = 0; std::vector knot_v(2 * points1.rows(), 1.); for (int i = 0; i < knot_v.size() / 2; i++)knot_v[i] = 0; s.degree_u = knot_u.size() / 2 - 1; s.degree_v = knot_v.size() / 2 - 1; s.knots_u = knot_u; s.knots_v = knot_v; s.control_points = points1; s.weights = weights; f.degree_u = knot_u.size() / 2 - 1; f.degree_v = knot_v.size() / 2 - 1; f.knots_u = knot_u; f.knots_v = knot_v; f.control_points = points2; f.weights = weights; fin.close(); // ====================== 测试 ======================= vector> s_evaluation; vector> f_evaluation; // 曲面s和f的切向量。zd*-sznmj vector> s_tangent_v; vector> f_tangent_u; const vector> f_tangent_v; // 曲面s和f的法向量 const vector> s_normal; const vector> f_normal; auto mesh1 = getMesh(s, level); auto mesh2 = getMesh(f, level); BVH bvh1(mesh1.evaluation); BVH bvh2(mesh2.evaluation); bvh1.build(); bvh2.build(); auto intersectBoxPairs = getOverlapLeafNodes(bvh1, bvh2); // [{{u1, v1}, {u2, v2}}] printf("box pairs size: %lld\n", intersectBoxPairs.size()); double time_cost = utils::get_time(); SingularityJudger singularityJudger(s, f, mesh1, mesh2); pair cellIdxFullRange = {0, pow(2, level - 1) - 1}; singularityJudger.judge(intersectBoxPairs, cellIdxFullRange, cellIdxFullRange, cellIdxFullRange, cellIdxFullRange); time_cost = utils::get_time() - time_cost; printf("time cost of singularityJudger: %lf\n", time_cost); // ================== 测试对整个曲面,gauss能排除多少(或保留多少)================== // GaussMap gaussMap1(mesh1.normal); // GaussMap gaussMap2(mesh2.normal); // gaussMap1.build(); // gaussMap2.build(); // auto pairs = getOverlapLeafNodes(gaussMap1, gaussMap2); // printf("Gauss Map: keep %lld samples in totally %lld boxes\n", pairs.size(), // mesh1.normal.size() * mesh1.normal[0].size() * mesh2.normal.size() * mesh2.normal[0].size()); return 0; } array2 getPtsFromStr(QString srfData) { srfData = srfData.remove(0, srfData.indexOf("Matrix") + 6).trimmed(); // 去掉首尾的括号 srfData.remove(0, 1); srfData.remove(srfData.size() - 1, 1); auto srfInfos = srfData.split("{"); auto srfDimInfos = srfInfos[0].split(QRegularExpression(",( )*")); auto dimU = srfDimInfos[0].toInt(); auto dimV = srfDimInfos[1].toInt(); auto srfPtInfo = srfInfos[1].remove(srfInfos[1].indexOf("}"), 1); auto srfPtsInfos = srfPtInfo.split(QRegularExpression(",( )*\\(")); std::vector points(dimU * dimV, glm::vec3()); for (int i = 0; i < srfPtsInfos.size(); i++) { auto pt = srfPtsInfos[i].split("=")[1].trimmed(); // 去掉首尾的方括号 pt.remove(0, 1); pt.remove(pt.size() - 1, 1); // int iu = i / dimU; // int iv = i % dimU; auto coords = pt.split(QRegularExpression(",( )*")); for (int k = 0; k < 3; k++) { float num; if (coords[k].contains("/")) { auto nums = coords[k].split("/"); num = nums[0].trimmed().toFloat() / nums[1].trimmed().toFloat(); } else { num = coords[k].toFloat(); } if (k == 0) points[i].x = num; else if (k == 1) points[i].y = num; else points[i].z = num; } } array2 res = {(size_t) dimU, (size_t) dimV, points}; return res; }