| 482 | } |
| 483 | |
| 484 | NormalResult findNormal(const PointView& view, PointIdList neighbors) |
| 485 | { |
| 486 | using namespace Eigen; |
| 487 | |
| 488 | NormalResult result; |
| 489 | if (neighbors.size() < 3) |
| 490 | { |
| 491 | result.msg = "Not enough neighbors to compute normal."; |
| 492 | return result; |
| 493 | } |
| 494 | |
| 495 | // Check if the covariance matrix is all zeros |
| 496 | auto B = math::computeCovariance(view, neighbors); |
| 497 | if (B.isZero()) |
| 498 | { |
| 499 | result.msg = "Covariance matrix is all zeros. This suggests a large " |
| 500 | "number of redundant points."; |
| 501 | return result; |
| 502 | } |
| 503 | |
| 504 | SelfAdjointEigenSolver<Matrix3d> solver(B); |
| 505 | if (solver.info() != Success) |
| 506 | { |
| 507 | result.msg = "Cannot perform eigen decomposition during normal calculation."; |
| 508 | return result; |
| 509 | } |
| 510 | |
| 511 | // The curvature is computed as the ratio of the first (smallest) |
| 512 | // eigenvalue to the sum of all eigenvalues. |
| 513 | auto eval = solver.eigenvalues(); |
| 514 | double sum = eval[0] + eval[1] + eval[2]; |
| 515 | |
| 516 | result.curvature = sum ? std::fabs(eval[0] / sum) : 0; |
| 517 | |
| 518 | // The normal is defined by the eigenvector corresponding to the |
| 519 | // smallest eigenvalue. |
| 520 | result.normal = solver.eigenvectors().col(0); |
| 521 | |
| 522 | return result; |
| 523 | } |
| 524 | |
| 525 | NormalResult findNormal(double x, double y, double z, PointView& v, double radius) |
| 526 | { |
no test coverage detected