triangle_fan.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. template <typename DerivedE, typename Derivedcap>
  12. IGL_INLINE void igl::triangle_fan(
  13. const Eigen::MatrixBase<DerivedE> & E,
  14. Eigen::PlainObjectBase<Derivedcap> & cap)
  15. {
  16. using namespace std;
  17. using namespace Eigen;
  18. // Handle lame base case
  19. if(E.size() == 0)
  20. {
  21. cap.resize(0,E.cols()+1);
  22. return;
  23. }
  24. // "Triangulate" aka "close" the E trivially with facets
  25. // Note: in 2D we need to know if E endpoints are incoming or
  26. // outgoing (left or right). Thus this will not work.
  27. assert(E.cols() == 2);
  28. // Arbitrary starting vertex
  29. //int s = E(int(((double)rand() / RAND_MAX)*E.rows()),0);
  30. int s = E(rand()%E.rows(),0);
  31. vector<vector<int> > lcap;
  32. for(int i = 0;i<E.rows();i++)
  33. {
  34. // Skip edges incident on s (they would be zero-area)
  35. if(E(i,0) == s || E(i,1) == s)
  36. {
  37. continue;
  38. }
  39. vector<int> e(3);
  40. e[0] = s;
  41. e[1] = E(i,0);
  42. e[2] = E(i,1);
  43. lcap.push_back(e);
  44. }
  45. list_to_matrix(lcap,cap);
  46. }
  47. IGL_INLINE Eigen::MatrixXi igl::triangle_fan( const Eigen::MatrixXi & E)
  48. {
  49. Eigen::MatrixXi cap;
  50. triangle_fan(E,cap);
  51. return cap;
  52. }
  53. #if IGL_STATIC_LIBRARY
  54. #endif