orient2d.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2019 Qingnan Zhou <[email protected]>
  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 "orient2d.h"
  9. #include "exactinit.h"
  10. #include "../parallel_for.h"
  11. #include <predicates.h>
  12. namespace igl {
  13. namespace predicates {
  14. using REAL = IGL_PREDICATES_REAL;
  15. #include "IGL_PREDICATES_ASSERT_SCALAR.h"
  16. template <
  17. typename Derivedpa,
  18. typename Derivedpb,
  19. typename Derivedpc>
  20. IGL_INLINE Orientation orient2d(
  21. const Eigen::MatrixBase<Derivedpa>& pa,
  22. const Eigen::MatrixBase<Derivedpb>& pb,
  23. const Eigen::MatrixBase<Derivedpc>& pc)
  24. {
  25. static_assert(
  26. (Derivedpa::RowsAtCompileTime == 2 && Derivedpa::ColsAtCompileTime == 1) ||
  27. (Derivedpa::RowsAtCompileTime == 1 && Derivedpa::ColsAtCompileTime == 2) ||
  28. (Derivedpa::RowsAtCompileTime == Eigen::Dynamic && Derivedpa::ColsAtCompileTime == 1 ) ||
  29. (Derivedpa::RowsAtCompileTime == 1 && Derivedpa::ColsAtCompileTime == Eigen::Dynamic ),
  30. "pa must be a 2D point");
  31. assert(pa.size() == 2 && "pa must be a 2D point");
  32. static_assert(
  33. (Derivedpb::RowsAtCompileTime == 2 && Derivedpb::ColsAtCompileTime == 1) ||
  34. (Derivedpb::RowsAtCompileTime == 1 && Derivedpb::ColsAtCompileTime == 2) ||
  35. (Derivedpb::RowsAtCompileTime == Eigen::Dynamic && Derivedpb::ColsAtCompileTime == 1 ) ||
  36. (Derivedpb::RowsAtCompileTime == 1 && Derivedpb::ColsAtCompileTime == Eigen::Dynamic ),
  37. "pb must be a 2D point");
  38. assert(pb.size() == 2 && "pb must be a 2D point");
  39. static_assert(
  40. (Derivedpc::RowsAtCompileTime == 2 && Derivedpc::ColsAtCompileTime == 1) ||
  41. (Derivedpc::RowsAtCompileTime == 1 && Derivedpc::ColsAtCompileTime == 2) ||
  42. (Derivedpc::RowsAtCompileTime == Eigen::Dynamic && Derivedpc::ColsAtCompileTime == 1 ) ||
  43. (Derivedpc::RowsAtCompileTime == 1 && Derivedpc::ColsAtCompileTime == Eigen::Dynamic ),
  44. "pc must be a 2D point");
  45. assert(pc.size() == 2 && "pc must be a 2D point");
  46. using Point = Eigen::Matrix<REAL, 2, 1>;
  47. Point a{pa[0], pa[1]};
  48. Point b{pb[0], pb[1]};
  49. Point c{pc[0], pc[1]};
  50. const auto r = ::orient2d(a.data(), b.data(), c.data());
  51. if (r > 0) return Orientation::POSITIVE;
  52. else if (r < 0) return Orientation::NEGATIVE;
  53. else return Orientation::COLLINEAR;
  54. }
  55. template
  56. <typename DerivedA,
  57. typename DerivedB,
  58. typename DerivedC,
  59. typename DerivedR>
  60. IGL_INLINE void orient2d(
  61. const Eigen::MatrixBase<DerivedA>& A,
  62. const Eigen::MatrixBase<DerivedB>& B,
  63. const Eigen::MatrixBase<DerivedC>& C,
  64. Eigen::PlainObjectBase<DerivedR>& R)
  65. {
  66. igl::predicates::exactinit();
  67. typedef typename DerivedR::Scalar RScalar;
  68. typedef typename DerivedA::Scalar Scalar;
  69. typedef Eigen::Matrix<Scalar, 1, 2> RowVector2S;
  70. // max(A.rows(),B.rows(),C.rows()) is the number of points
  71. const int np = std::max(
  72. std::max(A.rows(), B.rows()),C.rows());
  73. R.resize(np, 1);
  74. igl::parallel_for(np, [&](const int p)
  75. {
  76. // Not sure if these copies are needed
  77. const RowVector2S a = A.row(p % A.rows());
  78. const RowVector2S b = B.row(p % B.rows());
  79. const RowVector2S c = C.row(p % C.rows());
  80. // Compute the orientation
  81. R(p) = static_cast<RScalar>(igl::predicates::orient2d(a, b, c));
  82. },1000);
  83. }
  84. }
  85. }
  86. #ifdef IGL_STATIC_LIBRARY
  87. // Explicit template instantiation
  88. template igl::Orientation igl::predicates::orient2d<Eigen::Block<Eigen::Matrix<double, 4, -1, 1, 4, -1> const, 1, -1, true>, Eigen::Block<Eigen::Matrix<double, 4, -1, 1, 4, -1> const, 1, -1, true>, Eigen::Block<Eigen::Matrix<double, 4, -1, 1, 4, -1> const, 1, -1, true>>(Eigen::MatrixBase<Eigen::Block<Eigen::Matrix<double, 4, -1, 1, 4, -1> const, 1, -1, true>> const&, Eigen::MatrixBase<Eigen::Block<Eigen::Matrix<double, 4, -1, 1, 4, -1> const, 1, -1, true>> const&, Eigen::MatrixBase<Eigen::Block<Eigen::Matrix<double, 4, -1, 1, 4, -1> const, 1, -1, true>> const&);
  89. template igl::Orientation igl::predicates::orient2d<Eigen::Block<Eigen::Matrix<double, 4, 2, 0, 4, 2> const, 1, 2, false>, Eigen::Block<Eigen::Matrix<double, 4, 2, 0, 4, 2> const, 1, 2, false>, Eigen::Block<Eigen::Matrix<double, 4, 2, 0, 4, 2> const, 1, 2, false>>(Eigen::MatrixBase<Eigen::Block<Eigen::Matrix<double, 4, 2, 0, 4, 2> const, 1, 2, false>> const&, Eigen::MatrixBase<Eigen::Block<Eigen::Matrix<double, 4, 2, 0, 4, 2> const, 1, 2, false>> const&, Eigen::MatrixBase<Eigen::Block<Eigen::Matrix<double, 4, 2, 0, 4, 2> const, 1, 2, false>> const&);
  90. template igl::Orientation igl::predicates::orient2d<Eigen::Matrix<double, 1, -1, 1, 1, -1>, Eigen::Matrix<double, 1, -1, 1, 1, -1>, Eigen::Matrix<double, 1, -1, 1, 1, -1>>(Eigen::MatrixBase<Eigen::Matrix<double, 1, -1, 1, 1, -1>> const&, Eigen::MatrixBase<Eigen::Matrix<double, 1, -1, 1, 1, -1>> const&, Eigen::MatrixBase<Eigen::Matrix<double, 1, -1, 1, 1, -1>> const&);
  91. template igl::Orientation igl::predicates::orient2d<Eigen::Matrix<double, 1, 2, 1, 1, 2>, Eigen::Matrix<double, 1, 2, 1, 1, 2>, Eigen::Matrix<double, 1, -1, 1, 1, -1>>(Eigen::MatrixBase<Eigen::Matrix<double, 1, 2, 1, 1, 2>> const&, Eigen::MatrixBase<Eigen::Matrix<double, 1, 2, 1, 1, 2>> const&, Eigen::MatrixBase<Eigen::Matrix<double, 1, -1, 1, 1, -1>> const&);
  92. template igl::Orientation igl::predicates::orient2d<Eigen::Matrix<double, 1, 2, 1, 1, 2>, Eigen::Matrix<double, 1, 2, 1, 1, 2>, Eigen::Matrix<double, 1, 2, 1, 1, 2>>(Eigen::MatrixBase<Eigen::Matrix<double, 1, 2, 1, 1, 2>> const&, Eigen::MatrixBase<Eigen::Matrix<double, 1, 2, 1, 1, 2>> const&, Eigen::MatrixBase<Eigen::Matrix<double, 1, 2, 1, 1, 2>> const&);
  93. template igl::Orientation igl::predicates::orient2d<Eigen::Matrix<double, 2, 1, 0, 2, 1>, Eigen::Matrix<double, 2, 1, 0, 2, 1>, Eigen::Matrix<double, 2, 1, 0, 2, 1>>(Eigen::MatrixBase<Eigen::Matrix<double, 2, 1, 0, 2, 1>> const&, Eigen::MatrixBase<Eigen::Matrix<double, 2, 1, 0, 2, 1>> const&, Eigen::MatrixBase<Eigen::Matrix<double, 2, 1, 0, 2, 1>> const&);
  94. #endif