map_vertices_to_circle.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Stefan Brugger <stefanbrugger@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 "map_vertices_to_circle.h"
  9. #include <Eigen/Sparse>
  10. #include "igl/cotmatrix.h"
  11. #include "igl/boundary_vertices_sorted.h"
  12. IGL_INLINE void igl::map_vertices_to_circle(
  13. const Eigen::MatrixXd& V,
  14. const Eigen::MatrixXi& F,
  15. const Eigen::VectorXi& bnd,
  16. Eigen::MatrixXd& UV)
  17. {
  18. // Get sorted list of boundary vertices
  19. std::vector<int> interior,map_ij;
  20. map_ij.resize(V.rows());
  21. std::vector<bool> isOnBnd(V.rows(),false);
  22. for (int i = 0; i < bnd.size(); i++)
  23. {
  24. isOnBnd[bnd[i]] = true;
  25. map_ij[bnd[i]] = i;
  26. }
  27. for (int i = 0; i < isOnBnd.size(); i++)
  28. {
  29. if (!isOnBnd[i])
  30. {
  31. map_ij[i] = interior.size();
  32. interior.push_back(i);
  33. }
  34. }
  35. // Map boundary to unit circle
  36. std::vector<double> len(bnd.size());
  37. len[0] = 0.;
  38. for (int i = 1; i < bnd.size(); i++)
  39. {
  40. len[i] = len[i-1] + (V.row(bnd[i-1]) - V.row(bnd[i])).norm();
  41. }
  42. double total_len = len[len.size()-1] + (V.row(bnd[0]) - V.row(bnd[bnd.size()-1])).norm();
  43. UV.resize(bnd.size(),2);
  44. for (int i = 0; i < bnd.size(); i++)
  45. {
  46. double frac = len[i] * 2. * M_PI / total_len;
  47. UV.row(map_ij[bnd[i]]) << cos(frac), sin(frac);
  48. }
  49. }