diff --git a/include/icp_single_iteration.h b/include/icp_single_iteration.h index 03e4040..4ab74ba 100644 --- a/include/icp_single_iteration.h +++ b/include/icp_single_iteration.h @@ -37,5 +37,4 @@ void icp_single_iteration( const ICPMethod method, Eigen::Matrix3d & R, Eigen::RowVector3d & t); - #endif diff --git a/include/point_mesh_distance.h b/include/point_mesh_distance.h index f542c54..2c14999 100644 --- a/include/point_mesh_distance.h +++ b/include/point_mesh_distance.h @@ -1,6 +1,8 @@ #ifndef POINT_MESH_DISTANCE_H #define POINT_MESH_DISTANCE_H #include +#include +#include "point_triangle_distance.h" // Compute the distances `D` between a set of given points `X` and their // closest points `P` on a given mesh with vertex positions `VY` and face // indices `FY`. diff --git a/include/point_triangle_distance.h b/include/point_triangle_distance.h index ecdd14e..27a1400 100644 --- a/include/point_triangle_distance.h +++ b/include/point_triangle_distance.h @@ -19,4 +19,11 @@ void point_triangle_distance( const Eigen::RowVector3d & c, double & d, Eigen::RowVector3d & p); + +void dist_helper(const Eigen::RowVector3d & x, + const Eigen::RowVector3d & px, + const Eigen::RowVector3d & a, + const Eigen::RowVector3d & b, + double & d, + Eigen::RowVector3d & p); #endif diff --git a/include/random_points_on_mesh.h b/include/random_points_on_mesh.h index 4fe2497..21e8d60 100644 --- a/include/random_points_on_mesh.h +++ b/include/random_points_on_mesh.h @@ -1,6 +1,11 @@ #ifndef RANDOM_POINTS_ON_MESH_H #define RANDOM_POINTS_ON_MESH_H #include +#include +#include +#include +#include + // RANDOM_POINTS_ON_MESH Randomly sample a mesh (V,F) n times. // // Inputs: diff --git a/main.cpp b/main.cpp index 3115cc9..4fde073 100644 --- a/main.cpp +++ b/main.cpp @@ -21,6 +21,7 @@ int main(int argc, char *argv[]) int num_samples = 100; bool show_samples = true; ICPMethod method = ICP_METHOD_POINT_TO_POINT; + // ICPMethod method = ICP_METHOD_POINT_TO_PLANE; igl::viewer::Viewer viewer; std::cout< +#include void closest_rotation( const Eigen::Matrix3d & M, @@ -6,4 +8,10 @@ void closest_rotation( { // Replace with your code R = Eigen::Matrix3d::Identity(); + Eigen::JacobiSVD svd(M, Eigen::ComputeThinU | Eigen::ComputeThinV); + Eigen::Matrix3d omega; + omega << 1, 0, 0, + 0, 1, 0, + 0, 0, (svd.matrixU() * svd.matrixV().transpose()).determinant(); + R = svd.matrixV() * omega * svd.matrixU().transpose(); } diff --git a/src/hausdorff_lower_bound.cpp b/src/hausdorff_lower_bound.cpp index 8608964..c532994 100644 --- a/src/hausdorff_lower_bound.cpp +++ b/src/hausdorff_lower_bound.cpp @@ -1,4 +1,6 @@ #include "hausdorff_lower_bound.h" +#include "random_points_on_mesh.h" +#include "point_mesh_distance.h" double hausdorff_lower_bound( const Eigen::MatrixXd & VX, @@ -8,5 +10,11 @@ double hausdorff_lower_bound( const int n) { // Replace with your code - return 0; + Eigen::MatrixXd sample; + random_points_on_mesh(n, VX, FX, sample); + Eigen::VectorXd D; + Eigen::MatrixXd P; + Eigen::MatrixXd N; + point_mesh_distance(sample, VY, FY, D, P, N); + return D.maxCoeff(); } diff --git a/src/icp_single_iteration.cpp b/src/icp_single_iteration.cpp index 1e8fda9..b3e64af 100644 --- a/src/icp_single_iteration.cpp +++ b/src/icp_single_iteration.cpp @@ -1,4 +1,9 @@ #include "icp_single_iteration.h" +#include "random_points_on_mesh.h" +#include "point_mesh_distance.h" +#include "point_to_point_rigid_matching.h" +#include "point_to_plane_rigid_matching.h" +#include void icp_single_iteration( const Eigen::MatrixXd & VX, @@ -13,4 +18,15 @@ void icp_single_iteration( // Replace with your code R = Eigen::Matrix3d::Identity(); t = Eigen::RowVector3d::Zero(); + Eigen::MatrixXd X; + random_points_on_mesh(num_samples, VX, FX, X); + Eigen::VectorXd D; + Eigen::MatrixXd P; + Eigen::MatrixXd N; + point_mesh_distance(X, VY, FY, D, P, N); + switch(method) + { + case ICP_METHOD_POINT_TO_POINT: point_to_point_rigid_matching(X, P, R, t); break; + case ICP_METHOD_POINT_TO_PLANE: point_to_plane_rigid_matching(X, P, N, R, t); break; + } } diff --git a/src/point_mesh_distance.cpp b/src/point_mesh_distance.cpp index 2e6a070..d8ac445 100644 --- a/src/point_mesh_distance.cpp +++ b/src/point_mesh_distance.cpp @@ -1,5 +1,6 @@ #include "point_mesh_distance.h" - +#include +#include void point_mesh_distance( const Eigen::MatrixXd & X, const Eigen::MatrixXd & VY, @@ -9,8 +10,26 @@ void point_mesh_distance( Eigen::MatrixXd & N) { // Replace with your code - P.resizeLike(X); + D = Eigen::VectorXd::Zero(X.rows()); + P = Eigen::MatrixXd::Zero(X.rows(), 3); + N = Eigen::MatrixXd::Zero(X.rows(), 3); + N = Eigen::MatrixXd::Zero(X.rows(),X.cols()); - for(int i = 0;i::max(); + for(int j = 0;j d) { + D(i) = d; + P.row(i) = p; + N.row(i) = pfn.row(j); + } + } + } + } diff --git a/src/point_to_plane_rigid_matching.cpp b/src/point_to_plane_rigid_matching.cpp index 1978510..eb5d039 100644 --- a/src/point_to_plane_rigid_matching.cpp +++ b/src/point_to_plane_rigid_matching.cpp @@ -1,4 +1,7 @@ #include "point_to_plane_rigid_matching.h" +#include "closest_rotation.h" +#include +#include void point_to_plane_rigid_matching( const Eigen::MatrixXd & X, @@ -8,6 +11,40 @@ void point_to_plane_rigid_matching( Eigen::RowVector3d & t) { // Replace with your code - R = Eigen::Matrix3d::Identity(); - t = Eigen::RowVector3d::Zero(); + Eigen::MatrixXd dig = Eigen::MatrixXd::Zero(X.rows(), X.rows() *3); + dig.block(0,0,X.rows(),X.rows()) = N.col(0).asDiagonal(); + dig.block(0,X.rows(),X.rows(),X.rows()) = N.col(1).asDiagonal(); + dig.block(0,X.rows()*2,X.rows(),X.rows()) = N.col(2).asDiagonal(); + + Eigen::MatrixXd A = Eigen::MatrixXd::Zero(X.rows() * 3, 6); + for (int i = 0; i +#include "closest_rotation.h" +#include void point_to_point_rigid_matching( const Eigen::MatrixXd & X, @@ -10,5 +12,30 @@ void point_to_point_rigid_matching( // Replace with your code R = Eigen::Matrix3d::Identity(); t = Eigen::RowVector3d::Zero(); + Eigen::MatrixXd A = Eigen::MatrixXd::Zero(X.rows() * 3, 6); + Eigen::VectorXd B = Eigen::VectorXd::Zero(X.rows() * 3); + Eigen::MatrixXd XP = X-P; + for (int i = 0; i +#include +#include void point_triangle_distance( const Eigen::RowVector3d & x, @@ -8,7 +11,63 @@ void point_triangle_distance( double & d, Eigen::RowVector3d & p) { - // Replace with your code - d = 0; - p = a; + // following algorithm from below link + // http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.104.4264&rep=rep1&type=pdf + Eigen::RowVector3d ab = a-b; + Eigen::RowVector3d ba = b-a; + Eigen::RowVector3d ac = a-c; + Eigen::RowVector3d ca = c-a; + Eigen::RowVector3d bc = b-c; + Eigen::RowVector3d cb = c-b; + Eigen::RowVector3d ax = a-x; + Eigen::RowVector3d n = ba.cross(cb); + double area = n.norm(); + double angle = ax.dot(n) / n.norm(); + Eigen::RowVector3d projectx = x - angle * n / n.norm(); + + Eigen::RowVector3d pb = projectx - b; + Eigen::RowVector3d pc = projectx - c; + Eigen::RowVector3d pa = projectx - a; + + double alpha = (pb.cross(pc)).norm() / area; + double beta = (pc.cross(pa)).norm() / area; + double gamma = 1 - alpha - beta; + + if(0 <= alpha && alpha <= 1 && + 0 <= beta && beta <= 1 && + 0 <= gamma && gamma <= 1) { + p = projectx; + d = (x - p).norm(); + return ; + } + d = std::numeric_limits::max(); + dist_helper(x, projectx, a, b, d, p); + dist_helper(x, projectx, b, c, d, p); + dist_helper(x, projectx, c, a, d, p); +} + +void dist_helper(const Eigen::RowVector3d & x, + const Eigen::RowVector3d & px, + const Eigen::RowVector3d & a, + const Eigen::RowVector3d & b, + double & d, + Eigen::RowVector3d & p) { + Eigen::RowVector3d pa = a + ((px-a).dot(b-a)) / ((b-a).dot(b-a)) * (b-a); + double distance; + if (abs((pa-a).norm() + (a-b).norm() - (pa-b).norm()) < 1e-10) { + distance = (x-a).norm(); + p = a; + } + else if (abs((pa-b).norm() + (b-a).norm() - (pa-a).norm()) <1e-10) { + distance = (x-b).norm(); + p = b; + } + else { + distance = (x-pa).norm(); + p = pa; + } + + if (d > distance) { + d = distance; + } } diff --git a/src/random_points_on_mesh.cpp b/src/random_points_on_mesh.cpp index 6e85d75..6ac074c 100644 --- a/src/random_points_on_mesh.cpp +++ b/src/random_points_on_mesh.cpp @@ -1,4 +1,6 @@ #include "random_points_on_mesh.h" +#include +#include void random_points_on_mesh( const int n, @@ -8,6 +10,55 @@ void random_points_on_mesh( { // REPLACE WITH YOUR CODE: X.resize(n,3); - for(int i = 0;i distribution(0.,ttl); + double randx = distribution(generator); + + int idx = cumsumA.rows() / 2; + int gap = cumsumA.rows() / 4; + while(idx >= 1 && idx <= cumsumA.rows() - 1 && gap != 0) { + if (cumsumA(idx - 1, 0) < randx && cumsumA(idx, 0) > randx) { + break; + } + if (randx > cumsumA(idx)) { + idx += gap; + } + else { + idx -= gap; + } + gap = gap / 2; + if (gap == 0) { + gap = 1; + } + } + std::uniform_real_distribution distribution_alpha(0.,1.); + std::uniform_real_distribution distribution_beta(0.,1.); + double alpha = distribution_alpha(generator); + double beta = distribution_beta(generator); + + if (alpha + beta > 1) { + alpha = 1 - alpha; + beta = 1 - beta; + } + + if (idx < 0) { + idx = 0; + } + if (idx > cumsumA.rows() - 1) { + idx = cumsumA.rows() - 1; + } + + X.row(i) = V.row(F(idx, 0)) + + alpha * (V.row(F(idx, 1)) - V.row(F(idx, 0))) + + beta * (V.row(F(idx, 2)) - V.row(F(idx, 0))); + } }