fit_plane.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Daniele Panozzo <daniele.panozzo@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "fit_plane.h"
  9. #include <iostream>
  10. void igl::fit_plane(
  11. const Eigen::MatrixXd & V,
  12. Eigen::RowVector3d & N,
  13. Eigen::RowVector3d & C)
  14. {
  15. assert(V.rows()>0);
  16. Eigen::Vector3d sum = V.colwise().sum();
  17. Eigen::Vector3d center = sum.array()/(double(V.rows()));
  18. C = center;
  19. double sumXX=0.0f,sumXY=0.0f,sumXZ=0.0f;
  20. double sumYY=0.0f,sumYZ=0.0f;
  21. double sumZZ=0.0f;
  22. for(int i=0;i<V.rows();i++){
  23. double diffX=V(i,0)-center(0);
  24. double diffY=V(i,1)-center(1);
  25. double diffZ=V(i,2)-center(2);
  26. sumXX+=diffX*diffX;
  27. sumXY+=diffX*diffY;
  28. sumXZ+=diffX*diffZ;
  29. sumYY+=diffY*diffY;
  30. sumYZ+=diffY*diffZ;
  31. sumZZ+=diffZ*diffZ;
  32. }
  33. Eigen::MatrixXd m(3,3);
  34. m << sumXX,sumXY,sumXZ,
  35. sumXY,sumYY,sumYZ,
  36. sumXZ,sumYZ,sumZZ;
  37. Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> es(m);
  38. N = es.eigenvectors().col(0);
  39. }
  40. #ifndef IGL_HEADER_ONLY
  41. #endif