orient2d.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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<typename Vector2D>
  17. IGL_INLINE Orientation orient2d(
  18. const Eigen::MatrixBase<Vector2D>& pa,
  19. const Eigen::MatrixBase<Vector2D>& pb,
  20. const Eigen::MatrixBase<Vector2D>& pc)
  21. {
  22. EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Vector2D, 2);
  23. IGL_PREDICATES_ASSERT_SCALAR(Vector2D);
  24. using Point = Eigen::Matrix<REAL, 2, 1>;
  25. Point a{pa[0], pa[1]};
  26. Point b{pb[0], pb[1]};
  27. Point c{pc[0], pc[1]};
  28. const auto r = ::orient2d(a.data(), b.data(), c.data());
  29. if (r > 0) return Orientation::POSITIVE;
  30. else if (r < 0) return Orientation::NEGATIVE;
  31. else return Orientation::COLLINEAR;
  32. }
  33. template
  34. <typename DerivedA,
  35. typename DerivedB,
  36. typename DerivedC,
  37. typename DerivedR>
  38. IGL_INLINE void orient2d(
  39. const Eigen::MatrixBase<DerivedA>& A,
  40. const Eigen::MatrixBase<DerivedB>& B,
  41. const Eigen::MatrixBase<DerivedC>& C,
  42. Eigen::PlainObjectBase<DerivedR>& R)
  43. {
  44. igl::predicates::exactinit();
  45. typedef typename DerivedR::Scalar RScalar;
  46. typedef typename DerivedA::Scalar Scalar;
  47. typedef Eigen::Matrix<Scalar, 1, 2> RowVector2S;
  48. // max(A.rows(),B.rows(),C.rows()) is the number of points
  49. const int np = std::max(
  50. std::max(A.rows(), B.rows()),C.rows());
  51. R.resize(np, 1);
  52. igl::parallel_for(np, [&](const int p)
  53. {
  54. // Not sure if these copies are needed
  55. const RowVector2S a = A.row(p % A.rows());
  56. const RowVector2S b = B.row(p % B.rows());
  57. const RowVector2S c = C.row(p % C.rows());
  58. // Compute the orientation
  59. R(p) = static_cast<RScalar>(igl::predicates::orient2d(a, b, c));
  60. },1000);
  61. }
  62. }
  63. }
  64. #ifdef IGL_STATIC_LIBRARY
  65. #define IGL_ORIENT2D(Vector) template igl::predicates::Orientation igl::predicates::orient2d<Vector>(const Eigen::MatrixBase<Vector>&, const Eigen::MatrixBase<Vector>&, const Eigen::MatrixBase<Vector>&)
  66. #define IGL_MATRIX(T, R, C) Eigen::Matrix<T, R, C>
  67. IGL_ORIENT2D(IGL_MATRIX(float, 1, 2));
  68. IGL_ORIENT2D(IGL_MATRIX(float, 2, 1));
  69. #ifndef LIBIGL_PREDICATES_USE_FLOAT
  70. IGL_ORIENT2D(IGL_MATRIX(double, 1, 2));
  71. IGL_ORIENT2D(IGL_MATRIX(double, 2, 1));
  72. #endif
  73. #undef IGL_MATRIX
  74. #undef IGL_ORIENT2D
  75. #endif