orient3d.cpp 3.5 KB

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