fit_plane.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. IGL_INLINE 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. {
  24. double diffX=V(i,0)-center(0);
  25. double diffY=V(i,1)-center(1);
  26. double diffZ=V(i,2)-center(2);
  27. sumXX+=diffX*diffX;
  28. sumXY+=diffX*diffY;
  29. sumXZ+=diffX*diffZ;
  30. sumYY+=diffY*diffY;
  31. sumYZ+=diffY*diffZ;
  32. sumZZ+=diffZ*diffZ;
  33. }
  34. Eigen::MatrixXd m(3,3);
  35. m << sumXX,sumXY,sumXZ,
  36. sumXY,sumYY,sumYZ,
  37. sumXZ,sumYZ,sumZZ;
  38. Eigen::SelfAdjointEigenSolver<Eigen::MatrixXd> es(m);
  39. N = es.eigenvectors().col(0);
  40. }
  41. #ifdef IGL_STATIC_LIBRARY
  42. #endif