Skip to content

Commit

Permalink
update covexation
Browse files Browse the repository at this point in the history
  • Loading branch information
hankstag committed Apr 18, 2019
1 parent 5953771 commit 3c1ce4d
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 26 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Expand Up @@ -62,6 +62,8 @@ set(MAPTOCONVEX_FILES
map_to_convex.cpp
plot.cpp
loader.cpp
target_polygon.cpp
path_tracing.cpp
)

add_executable(matchmaker_bin ${MATCHMAKER_FILES})
Expand Down
131 changes: 105 additions & 26 deletions map_to_convex.cpp
Expand Up @@ -7,9 +7,12 @@
#include "validity_check.h"
#include "plot.h"
#include "loader.h"
#include "target_polygon.h"
#include "argh.h"
#include <igl/segment_segment_intersect.h>
#include <igl/copyleft/cgal/point_inside_polygon.h>
#include <igl/copyleft/cgal/segment_segment_intersect.h>
#include <igl/opengl/glfw/imgui/ImGuiMenu.h>
// auto strech = [](
// Eigen::RowVector2d p0,
// Eigen::RowVector2d v0,
Expand Down Expand Up @@ -56,8 +59,13 @@ void convexation(
Eigen::MatrixXd& uv,
const Eigen::MatrixXi& F
){
Eigen::MatrixXd uv0 = uv;
Eigen::VectorXi bd;
igl::boundary_loop(F,bd);
Eigen::VectorXi B(uv.rows());
B.setZero();
for(int i=0;i<bd.rows();i++)
B(bd(i)) = 1;
Eigen::MatrixXd polygon(bd.rows(),3);
auto rotation_matrix = [](
double angle,
Expand All @@ -67,7 +75,7 @@ void convexation(
-std::sin(angle),std::cos(angle),0,
0,0,1;
};

std::vector<Eigen::MatrixXd> polylines;
auto strech = [&](
Eigen::RowVector2d p0_2d,
Eigen::RowVector2d v0_2d,
Expand Down Expand Up @@ -115,25 +123,25 @@ void convexation(
polyline.row(0) << v0;
if(N > 2)
polyline.row(1) << vp;
igl::opengl::glfw::Viewer viewer;
viewer.data().set_mesh(uv,F);
viewer.data().add_points(p0,Eigen::RowVector3d(1,0,0));
viewer.data().add_points(p1,Eigen::RowVector3d(1,0,0));
viewer.data().add_points(v0,Eigen::RowVector3d(1,1,0));
viewer.data().add_points(v1,Eigen::RowVector3d(1,1,0));
viewer.data().add_points(v0+t0*h0,Eigen::RowVector3d(0,1,0));
// igl::opengl::glfw::Viewer viewer;
// viewer.data().set_mesh(uv,F);
// viewer.data().add_points(p0,Eigen::RowVector3d(1,0,0));
// viewer.data().add_points(p1,Eigen::RowVector3d(1,0,0));
// viewer.data().add_points(v0,Eigen::RowVector3d(1,1,0));
// viewer.data().add_points(v1,Eigen::RowVector3d(1,1,0));
// viewer.data().add_points(v0+t0*h0,Eigen::RowVector3d(0,1,0));
std::cout<<"N = "<<N<<std::endl;
for(int i=2;i<N-1;i++){
Eigen::RowVector3d hi = (Rd * hp.transpose()).transpose().eval();
double len = v0v1.norm()*(l/(N-1))/hi.dot(v0v1);
Eigen::RowVector3d vi = vp + hi * len;
polyline.row(i) << vi;
viewer.data().add_points(vi,Eigen::RowVector3d(0,0,1));
//viewer.data().add_points(vi,Eigen::RowVector3d(0,0,1));
hp = hi;
vp = vi;
}
polyline.bottomRows(1) << v1;

polylines.push_back(polyline);
// for(int i=1;i<N;i++){
// Eigen::RowVector3d h_i;
// Eigen::Matrix3d rotation;
Expand All @@ -147,7 +155,7 @@ void convexation(
// viewer.data().add_points(joint,Eigen::RowVector3d(0,0,1));
// // for(int i=1;i<polyline.rows()-1;i++)
// // viewer.data().add_points(polyline.row(i),Eigen::RowVector3d(1,0,0));
viewer.launch();
// viewer.launch();
};
auto is_corner = [](
const Eigen::RowVector2d& vi,
Expand All @@ -172,12 +180,15 @@ void convexation(
Eigen::RowVector2d center;
center.setZero();
int count_corner = 0;
for(int i=0;i<bd.rows();i++){
std::vector<int> C;
for(int i=0;i<bd.rows()+1;i++){
int n = bd.rows();
if(is_corner(uv.row(bd(i)),uv.row(bd((i-1+n)%n)),uv.row(bd((i+1)%n)))){
if(is_corner(uv.row(bd(i%n)),uv.row(bd((i%n-1+n)%n)),uv.row(bd((i%n+1)%n)))){
begin = i;
count_corner++;
center += uv.row(bd(i));
C.push_back(i%n);
if(i<bd.rows())
center += uv.row(bd(i%n));
}
}
center /= count_corner;
Expand Down Expand Up @@ -205,6 +216,7 @@ void convexation(
}
}
igl::harmonic(F,bd,polygon,1,uv);
uv.conservativeResize(uv.rows(),2);
Eigen::VectorXi T;
flipped_elements(uv,F,T);
std::cout<<"flipped "<<T.sum()<<std::endl;
Expand All @@ -213,21 +225,78 @@ void convexation(
};
plots(Os);
// reverse scaling
igl::opengl::glfw::Viewer viewer;
igl::opengl::glfw::imgui::ImGuiMenu menu;
viewer.data().set_mesh(uv,F);
viewer.plugins.push_back(&menu);
for(int i=0;i<uv.rows();i++){
if(B(i))
viewer.data().add_points(uv.row(i),Eigen::RowVector3d(0,1,0));
}
viewer.data().add_points(center,Eigen::RowVector3d(0,0,1));
viewer.launch();
Eigen::MatrixXd uv2 = uv0;
for(int i=0;i<uv.rows();i++){
// find which sector does vertex vi belong to
for(int j=0;j<bd.rows();j++){
Eigen::MatrixXd T(3,2);
T<< uv.row(bd(j)), uv.row(bd((j+1)%bd.rows())), center;
if(igl::copyleft::cgal::point_inside_polygon(uv.row(i),T)){
double u1,u2,t1,t2;
bool cross1 = igl::segment_segment_intersect(p,r,q,s,u,t);
bool cross2 = igl::segment_segment_intersect(p,r,q,s,u,t);
double scaling = u2 / u1;
uv.row(i) << scaling * (uv.row(i) - center) + center;
break;
if(B(i)==0){
// find which sector does vertex vi belong to
bool found = false;
int cid = 0;
for(int j=0;j<C.size();j++){
int c0 = C[j];
int c1 = C[(j+1)%C.size()];
Eigen::MatrixXd P((c1-c0+2+bd.rows())%bd.rows(),2);
for(int p=0;p<P.rows()-1;p++){
int ci = (c0+p) % bd.rows();
P.row(p) << uv.row(bd(ci));
}
P.bottomRows(1) << center;
Eigen::RowVector2d q = uv.row(i);
if(igl::copyleft::cgal::point_inside_polygon(P,q)){
double u1,u2,t1,t2;
Eigen::MatrixXd polyline = polylines[cid];
for(int k=0;k<polyline.rows();k++){
int k_1 = (k+1) % polyline.rows();
Eigen::RowVector3d dir1 = polyline.row(k_1)-polyline.row(k);
Eigen::RowVector3d dir2;
dir2 << 1e5*(uv.row(i)-center),0;
Eigen::RowVector3d p = polyline.row(k);
Eigen::RowVector3d q; q << center,0;
bool cross1 = igl::segment_segment_intersect(p,dir1,q,dir2,u1,t1);
if(cross1){
Eigen::RowVector3d dir3 = polyline.row(polyline.rows()-1)-polyline.row(0);
Eigen::RowVector3d p0 = polyline.row(0);
igl::segment_segment_intersect(p0,dir3,q,dir2,u2,t2);
double scaling = t2 / t1;
uv2.row(i) << scaling * (uv.row(i) - center) + center;
if(i == 1519){
igl::opengl::glfw::Viewer vr;
vr.data().set_mesh(uv,F);
vr.data().add_points(center,Eigen::RowVector3d(1,0,0));
vr.data().add_points(uv.row(i),Eigen::RowVector3d(0,1,0));
vr.data().add_points((t1*dir2+q).eval(),Eigen::RowVector3d(1,0,1));
vr.data().add_points((t2*dir2+q).eval(),Eigen::RowVector3d(1,1,0));
vr.launch();
}
j = C.size(); // break outer loop
break;
}
}
// bool cross1 = igl::segment_segment_intersect(p,r,q,s,u,t);
// bool cross2 = igl::segment_segment_intersect(p,r,q,s,u,t);
// double scaling = u2 / u1;
// uv.row(i) << scaling * (uv.row(i) - center) + center;
found = true;
break;
}
}
assert(found == true);
}
}
for(int i=0;i<uv.rows();i++){
if(B(i)==1)
uv2.row(i) << uv0.row(i);
}
uv = uv2;
}

int main(int argc, char *argv[])
Expand Down Expand Up @@ -262,16 +331,26 @@ int main(int argc, char *argv[])
igl::harmonic(F,bd,circle,1,H);
progressive_embedding(V,F,H,bd,circle,1e20);
#else

Eigen::VectorXi b;
Eigen::MatrixXd bc;
std::map<int,int> mm;
background_mesh(50,bc,b,mm,uv,F);
std::vector<Object> O = {Object(uv,F,OTYPE::MESH)};
plots(O);


igl::boundary_loop(F,b);
igl::slice(uv,b,1,bc);
Eigen::MatrixXd H;
igl::harmonic(F,b,bc,1,H);
convexation(uv,F);
Eigen::VectorXi I;
flipped_elements(H,F,I);
convexation(uv,F);
std::cout<<"count flipped: "<<I.sum()<<std::endl;
std::vector<Object> Os = {Object(uv,F,OTYPE::MESH)};
plots(Os);

bool x = progressive_embedding(V,F,uv,b,bc,1e20);
#endif

Expand Down
2 changes: 2 additions & 0 deletions plot.cpp
Expand Up @@ -64,6 +64,8 @@ void plots(
std::vector<Object>& data
){
igl::opengl::glfw::Viewer vr;
igl::opengl::glfw::imgui::ImGuiMenu menu;
vr.plugins.push_back(&menu);
int item_num = data.size();
auto key_down = [&](
igl::opengl::glfw::Viewer& viewer, unsigned char key, int modifier
Expand Down

0 comments on commit 3c1ce4d

Please sign in to comment.