resolve_intersections.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2016 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 "resolve_intersections.h"
  9. #include "subdivide_segments.h"
  10. #include "row_to_point.h"
  11. #include "../../unique.h"
  12. #include "../../list_to_matrix.h"
  13. #include <CGAL/Exact_predicates_exact_constructions_kernel.h>
  14. #include <CGAL/Segment_2.h>
  15. #include <CGAL/Point_2.h>
  16. #include <algorithm>
  17. #include <vector>
  18. template <
  19. typename DerivedV,
  20. typename DerivedE,
  21. typename DerivedVI,
  22. typename DerivedEI,
  23. typename DerivedJ,
  24. typename DerivedIM>
  25. IGL_INLINE void igl::copyleft::cgal::resolve_intersections(
  26. const Eigen::PlainObjectBase<DerivedV> & V,
  27. const Eigen::PlainObjectBase<DerivedE> & E,
  28. Eigen::PlainObjectBase<DerivedVI> & VI,
  29. Eigen::PlainObjectBase<DerivedEI> & EI,
  30. Eigen::PlainObjectBase<DerivedJ> & J,
  31. Eigen::PlainObjectBase<DerivedIM> & IM)
  32. {
  33. using namespace Eigen;
  34. using namespace igl;
  35. using namespace std;
  36. // Exact scalar type
  37. typedef CGAL::Epeck K;
  38. typedef K::FT EScalar;
  39. typedef CGAL::Segment_2<K> Segment_2;
  40. typedef CGAL::Point_2<K> Point_2;
  41. typedef Matrix<EScalar,Dynamic,Dynamic> MatrixXE;
  42. // Convert vertex positions to exact kernel
  43. MatrixXE VE(V.rows(),V.cols());
  44. for(int i = 0;i<V.rows();i++)
  45. {
  46. for(int j = 0;j<V.cols();j++)
  47. {
  48. VE(i,j) = V(i,j);
  49. }
  50. }
  51. const int m = E.rows();
  52. // resolve all intersections: silly O(n²) implementation
  53. std::vector<std::vector<Point_2> > steiner(m);
  54. for(int i = 0;i<m;i++)
  55. {
  56. Segment_2 si(row_to_point<K>(VE,E(i,0)),row_to_point<K>(VE,E(i,1)));
  57. steiner[i].push_back(si.vertex(0));
  58. steiner[i].push_back(si.vertex(1));
  59. for(int j = i+1;j<m;j++)
  60. {
  61. Segment_2 sj(row_to_point<K>(VE,E(j,0)),row_to_point<K>(VE,E(j,1)));
  62. // do they intersect?
  63. if(CGAL::do_intersect(si,sj))
  64. {
  65. CGAL::Object result = CGAL::intersection(si,sj);
  66. if(const Point_2 * p = CGAL::object_cast<Point_2 >(&result))
  67. {
  68. steiner[i].push_back(*p);
  69. steiner[j].push_back(*p);
  70. // add intersection point
  71. }else if(const Segment_2 * s = CGAL::object_cast<Segment_2 >(&result))
  72. {
  73. // add both endpoints
  74. steiner[i].push_back(s->vertex(0));
  75. steiner[j].push_back(s->vertex(0));
  76. steiner[i].push_back(s->vertex(1));
  77. steiner[j].push_back(s->vertex(1));
  78. }else
  79. {
  80. assert(false && "Unknown intersection type");
  81. }
  82. }
  83. }
  84. }
  85. subdivide_segments(V,E,steiner,VI,EI,J,IM);
  86. }