Skip to content

Commit

Permalink
Update teaser_cpp_ply.cc
Browse files Browse the repository at this point in the history
  • Loading branch information
Enzo-MiMan authored Jul 23, 2020
1 parent 2c50d35 commit 9aabf0d
Showing 1 changed file with 15 additions and 56 deletions.
71 changes: 15 additions & 56 deletions examples/teaser_cpp_ply/teaser_cpp_ply.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,63 +18,35 @@ inline double getAngularError(Eigen::Matrix3d R_exp, Eigen::Matrix3d R_est) {
return std::abs(std::acos(fmin(fmax(((R_exp.transpose() * R_est).trace() - 1) / 2, -1.0), 1.0)));
}

void addNoiseAndOutliers(Eigen::Matrix<double, 3, Eigen::Dynamic>& tgt) {
// Add uniform noise
Eigen::Matrix<double, 3, Eigen::Dynamic> noise =
Eigen::Matrix<double, 3, Eigen::Dynamic>::Random(3, tgt.cols()) * NOISE_BOUND;
NOISE_BOUND / 2;
tgt = tgt + noise;

// Add outliers
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis2(0, tgt.cols() - 1); // pos of outliers
std::uniform_int_distribution<> dis3(OUTLIER_TRANSLATION_LB,
OUTLIER_TRANSLATION_UB); // random translation
std::vector<bool> expected_outlier_mask(tgt.cols(), false);
for (int i = 0; i < N_OUTLIERS; ++i) {
int c_outlier_idx = dis2(gen);
assert(c_outlier_idx < expected_outlier_mask.size());
expected_outlier_mask[c_outlier_idx] = true;
tgt.col(c_outlier_idx).array() += dis3(gen); // random translation
}
}

int main() {
// Load the .ply file
teaser::PLYReader reader;
teaser::PointCloud src_cloud;
auto status = reader.read("./example_data/bun_zipper_res3.ply", src_cloud);
auto status = reader.read("/Users/manmi/Desktop/mm/1574955838821870584.ply", src_cloud);
int N = src_cloud.size();

teaser::PointCloud tgt_cloud;
reader.read("/Users/manmi/Desktop/mm/1574955838971913886.ply", tgt_cloud);
int M = tgt_cloud.size();



// Convert the point cloud to Eigen
Eigen::Matrix<double, 3, Eigen::Dynamic> src(3, N);
for (size_t i = 0; i < N; ++i) {
src.col(i) << src_cloud[i].x, src_cloud[i].y, src_cloud[i].z;
}

// Homogeneous coordinates
Eigen::Matrix<double, 4, Eigen::Dynamic> src_h;
src_h.resize(4, src.cols());
src_h.topRows(3) = src;
src_h.bottomRows(1) = Eigen::Matrix<double, 1, Eigen::Dynamic>::Ones(N);

// Apply an arbitrary SE(3) transformation
Eigen::Matrix4d T;
// clang-format off
T << 9.96926560e-01, 6.68735757e-02, -4.06664421e-02, -1.15576939e-01,
-6.61289946e-02, 9.97617877e-01, 1.94008687e-02, -3.87705398e-02,
4.18675510e-02, -1.66517807e-02, 9.98977765e-01, 1.14874890e-01,
0, 0, 0, 1;
// clang-format on

// Apply transformation
Eigen::Matrix<double, 4, Eigen::Dynamic> tgt_h = T * src_h;
Eigen::Matrix<double, 3, Eigen::Dynamic> tgt = tgt_h.topRows(3);
Eigen::Matrix<double, 3, Eigen::Dynamic> tgt(3, M);
for (size_t i = 0; i < M; ++i) {
tgt.col(i) << tgt_cloud[i].x, tgt_cloud[i].y, tgt_cloud[i].z;
}

// Add some noise & outliers
addNoiseAndOutliers(tgt);

std::cout << "src_cloud.size()" << std::endl;

// Run TEASER++ registration
// Prepare solver parameters
teaser::RobustRegistrationSolver::Params params;
Expand All @@ -99,23 +71,10 @@ int main() {
std::cout << "=====================================" << std::endl;
std::cout << " TEASER++ Results " << std::endl;
std::cout << "=====================================" << std::endl;
std::cout << "Expected rotation: " << std::endl;
std::cout << T.topLeftCorner(3, 3) << std::endl;

std::cout << "Estimated rotation: " << std::endl;
std::cout << solution.rotation << std::endl;
std::cout << "Error (deg): " << getAngularError(T.topLeftCorner(3, 3), solution.rotation)
<< std::endl;
std::cout << std::endl;
std::cout << "Expected translation: " << std::endl;
std::cout << T.topRightCorner(3, 1) << std::endl;

std::cout << "Estimated translation: " << std::endl;
std::cout << solution.translation << std::endl;
std::cout << "Error (m): " << (T.topRightCorner(3, 1) - solution.translation).norm() << std::endl;
std::cout << std::endl;
std::cout << "Number of correspondences: " << N << std::endl;
std::cout << "Number of outliers: " << N_OUTLIERS << std::endl;
std::cout << "Time taken (s): "
<< std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() /
1000000.0
<< std::endl;
}

0 comments on commit 9aabf0d

Please sign in to comment.