From f03d2d63db23cbb282737aab8420e58956e21cee Mon Sep 17 00:00:00 2001 From: lo Date: Mon, 26 Feb 2018 20:07:33 +0100 Subject: [PATCH] add edge relaxing method --- src/Mod/MeshPart/App/MeshFlattening.h | 11 +-- .../MeshPart/App/MeshFlatteningLscmRelax.cpp | 93 ++++++++++++++++++- .../MeshPart/App/MeshFlatteningLscmRelax.h | 2 + 3 files changed, 98 insertions(+), 8 deletions(-) diff --git a/src/Mod/MeshPart/App/MeshFlattening.h b/src/Mod/MeshPart/App/MeshFlattening.h index d1f4671dfe..4984ae64b6 100644 --- a/src/Mod/MeshPart/App/MeshFlattening.h +++ b/src/Mod/MeshPart/App/MeshFlattening.h @@ -20,14 +20,15 @@ * * ***************************************************************************/ -// idea: -// - unwrap any meshed shells and output a 2d face (meshing is done externally) -// - unwrap faces which are nurbs and return nurbs (no cuts, meshing internally) - #ifndef MESHFLATTENING #define MESHFLATTENING +// idea: +// - unwrap any meshed shells and output a 2d face (meshing is done externally) +// - unwrap faces which are nurbs and return nurbs (no cuts, meshing internally) +// - TODO: map any curves from origin to flattened faces + #include "MeshFlatteningNurbs.h" #include #include @@ -78,8 +79,6 @@ public: ColMat ze_poles; // compute spMat A; // mapping between nurbs(poles) and mesh(vertices) computed with nurbs-basis-functions and uv_mesh - // - }; #endif // MESHFLATTENING diff --git a/src/Mod/MeshPart/App/MeshFlatteningLscmRelax.cpp b/src/Mod/MeshPart/App/MeshFlatteningLscmRelax.cpp index 7b7f7b138b..9261267aa1 100644 --- a/src/Mod/MeshPart/App/MeshFlatteningLscmRelax.cpp +++ b/src/Mod/MeshPart/App/MeshFlatteningLscmRelax.cpp @@ -29,13 +29,16 @@ #include #include +#include +#include +#include +#include + #ifndef M_PI #define M_PI 3.14159265358979323846f #endif // TODO: -// vertices, flat vertices = EIgen::MatrixXd (better python conversation, parallell) -// make it a own library // area constrained (scale the final unwrapped mesh to the original area) // FEM approach @@ -289,6 +292,92 @@ void LscmRelax::relax(double weight) } +void LscmRelax::area_relax(double weight) +{ +// 1. create system of equation +// 2. minimize lsq error + +} + +void LscmRelax::edge_relax(double weight) +{ +// 1. get all edges + std::set> edges; + std::array edge; + for(long i=0; itriangles.cols(); i++) + { + for(int j=0; j<3; j++) + { + long k = j+1; + if (k==3) + k = 0; + if (this->triangles(j, i) < this->triangles(k, i)) + edge = std::array{this->triangles(j, i), this->triangles(k, i)}; + else + edge = std::array{this->triangles(k, i), this->triangles(j, i)}; + edges.insert(edge); + } + } +// 2. create system + + std::vector K_g_triplets; + spMat K_g(this->vertices.cols() * 2 + 3, this->vertices.cols() * 2 + 3); + Eigen::VectorXd rhs(this->vertices.cols() * 2 + 3); + rhs.setZero(); + for(auto edge : edges) + { +// this goes to the right side + Vector3 v1_g = this->vertices.col(edge[0]); + Vector3 v2_g = this->vertices.col(edge[1]); + Vector2 v1_l = this->flat_vertices.col(edge[0]); + Vector2 v2_l = this->flat_vertices.col(edge[1]); + std::vector range_4 {0, 1, 2, 3}; + std::vector indices {edge[0] * 2, edge[0] * 2 + 1, edge[1] * 2, edge[1] * 2 + 1}; + double l_g = (v2_g - v1_g).norm(); + double l_l = (v2_l - v1_l).norm(); + Vector2 t = (v2_l - v1_l); // direction + t.normalize(); + + Eigen::Matrix B; + Eigen::Matrix K; + Eigen::Matrix rhs_m; + + B << -t.x(), -t.y(), t.x(), t.y(); + K = 1. / l_g * B.transpose() * B; + rhs_m = - B.transpose() * (l_g - l_l); + + + for(auto row: range_4) + { + for (auto col: range_4) + { + K_g_triplets.push_back(trip(indices[row], indices[col], (double) K(row, col))); + } + rhs(indices[row]) += rhs_m[row]; + } + } + for (long i=0; i < this->flat_vertices.cols() ; i++) + { + // fixing total ux + K_g_triplets.push_back(trip(i * 2, this->flat_vertices.cols() * 2, 1)); + K_g_triplets.push_back(trip(this->flat_vertices.cols() * 2, i * 2, 1)); + // fixing total uy + K_g_triplets.push_back(trip(i * 2 + 1, this->flat_vertices.cols() * 2 + 1, 1)); + K_g_triplets.push_back(trip(this->flat_vertices.cols() * 2 + 1, i * 2 + 1, 1)); + // fixing ux*y-uy*x + K_g_triplets.push_back(trip(i * 2, this->flat_vertices.cols() * 2 + 2, - this->flat_vertices(1, i))); + K_g_triplets.push_back(trip(this->flat_vertices.cols() * 2 + 2, i * 2, - this->flat_vertices(1, i))); + K_g_triplets.push_back(trip(i * 2 + 1, this->flat_vertices.cols() * 2 + 2, this->flat_vertices(0, i))); + K_g_triplets.push_back(trip(this->flat_vertices.cols() * 2 + 2, i * 2 + 1, this->flat_vertices(0, i))); + } + K_g.setFromTriplets(K_g_triplets.begin(), K_g_triplets.end()); + Eigen::SimplicialLDLT solver; + solver.compute(K_g); + this->sol = solver.solve(-rhs); + this->set_shift(this->sol.head(this->vertices.cols() * 2) * weight); +} + + ////////////////////////////////////////////////////////////////////////// ///////////////// L.S.C.M ///////////// ////////////////////////////////////////////////////////////////////////// diff --git a/src/Mod/MeshPart/App/MeshFlatteningLscmRelax.h b/src/Mod/MeshPart/App/MeshFlatteningLscmRelax.h index cd967240cf..87b7c31735 100644 --- a/src/Mod/MeshPart/App/MeshFlatteningLscmRelax.h +++ b/src/Mod/MeshPart/App/MeshFlatteningLscmRelax.h @@ -89,6 +89,8 @@ public: void lscm(); void relax(double); + void area_relax(double); + void edge_relax(double); ColMat get_flat_vertices_3D();