triangle_fan.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2015 Alec Jacobson <alecjacobson@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 "triangle_fan.h"
  9. #include "exterior_edges.h"
  10. #include "list_to_matrix.h"
  11. IGL_INLINE void igl::triangle_fan(
  12. const Eigen::MatrixXi & E,
  13. Eigen::MatrixXi & cap)
  14. {
  15. using namespace std;
  16. using namespace Eigen;
  17. // Handle lame base case
  18. if(E.size() == 0)
  19. {
  20. cap.resize(0,E.cols()+1);
  21. return;
  22. }
  23. // "Triangulate" aka "close" the E trivially with facets
  24. // Note: in 2D we need to know if E endpoints are incoming or
  25. // outgoing (left or right). Thus this will not work.
  26. assert(E.cols() == 2);
  27. // Arbitrary starting vertex
  28. //int s = E(int(((double)rand() / RAND_MAX)*E.rows()),0);
  29. int s = E(rand()%E.rows(),0);
  30. vector<vector<int> > lcap;
  31. for(int i = 0;i<E.rows();i++)
  32. {
  33. // Skip edges incident on s (they would be zero-area)
  34. if(E(i,0) == s || E(i,1) == s)
  35. {
  36. continue;
  37. }
  38. vector<int> e(3);
  39. e[0] = s;
  40. e[1] = E(i,0);
  41. e[2] = E(i,1);
  42. lcap.push_back(e);
  43. }
  44. list_to_matrix(lcap,cap);
  45. }
  46. IGL_INLINE Eigen::MatrixXi igl::triangle_fan( const Eigen::MatrixXi & E)
  47. {
  48. Eigen::MatrixXi cap;
  49. triangle_fan(E,cap);
  50. return cap;
  51. }