shape_diameter_function.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2017 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 "shape_diameter_function.h"
  9. #include "random_dir.h"
  10. #include "ray_mesh_intersect.h"
  11. #include "EPS.h"
  12. #include "Hit.h"
  13. #include "parallel_for.h"
  14. #include <functional>
  15. #include <vector>
  16. #include <algorithm>
  17. template <
  18. typename DerivedP,
  19. typename DerivedN,
  20. typename DerivedS >
  21. IGL_INLINE void igl::shape_diameter_function(
  22. const std::function<
  23. double(
  24. const Eigen::Vector3f&,
  25. const Eigen::Vector3f&)
  26. > & shoot_ray,
  27. const Eigen::PlainObjectBase<DerivedP> & P,
  28. const Eigen::PlainObjectBase<DerivedN> & N,
  29. const int num_samples,
  30. Eigen::PlainObjectBase<DerivedS> & S)
  31. {
  32. using namespace Eigen;
  33. const int n = P.rows();
  34. // Resize output
  35. S.resize(n,1);
  36. // Embree seems to be parallel when constructing but not when tracing rays
  37. const MatrixXf D = random_dir_stratified(num_samples).cast<float>();
  38. const auto & inner = [&P,&N,&num_samples,&D,&S,&shoot_ray](const int p)
  39. {
  40. const Vector3f origin = P.row(p).template cast<float>();
  41. const Vector3f normal = N.row(p).template cast<float>();
  42. int num_hits = 0;
  43. double total_distance = 0;
  44. for(int s = 0;s<num_samples;s++)
  45. {
  46. Vector3f d = D.row(s);
  47. // Shoot _inward_
  48. if(d.dot(normal) > 0)
  49. {
  50. // reverse ray
  51. d *= -1;
  52. }
  53. const double dist = shoot_ray(origin,d);
  54. if(std::isfinite(dist))
  55. {
  56. total_distance += dist;
  57. num_hits++;
  58. }
  59. }
  60. S(p) = total_distance/(double)num_hits;
  61. };
  62. parallel_for(n,inner,1000);
  63. }
  64. template <
  65. typename DerivedV,
  66. int DIM,
  67. typename DerivedF,
  68. typename DerivedP,
  69. typename DerivedN,
  70. typename DerivedS >
  71. IGL_INLINE void igl::shape_diameter_function(
  72. const igl::AABB<DerivedV,DIM> & aabb,
  73. const Eigen::PlainObjectBase<DerivedV> & V,
  74. const Eigen::PlainObjectBase<DerivedF> & F,
  75. const Eigen::PlainObjectBase<DerivedP> & P,
  76. const Eigen::PlainObjectBase<DerivedN> & N,
  77. const int num_samples,
  78. Eigen::PlainObjectBase<DerivedS> & S)
  79. {
  80. const auto & shoot_ray = [&aabb,&V,&F](
  81. const Eigen::Vector3f& _s,
  82. const Eigen::Vector3f& dir)->double
  83. {
  84. Eigen::Vector3f s = _s+1e-4*dir;
  85. igl::Hit hit;
  86. if(aabb.intersect_ray(
  87. V,
  88. F,
  89. s .cast<typename DerivedV::Scalar>().eval(),
  90. dir.cast<typename DerivedV::Scalar>().eval(),
  91. hit))
  92. {
  93. return hit.t;
  94. }else
  95. {
  96. return std::numeric_limits<double>::infinity();
  97. }
  98. };
  99. return shape_diameter_function(shoot_ray,P,N,num_samples,S);
  100. }
  101. template <
  102. typename DerivedV,
  103. typename DerivedF,
  104. typename DerivedP,
  105. typename DerivedN,
  106. typename DerivedS >
  107. IGL_INLINE void igl::shape_diameter_function(
  108. const Eigen::PlainObjectBase<DerivedV> & V,
  109. const Eigen::PlainObjectBase<DerivedF> & F,
  110. const Eigen::PlainObjectBase<DerivedP> & P,
  111. const Eigen::PlainObjectBase<DerivedN> & N,
  112. const int num_samples,
  113. Eigen::PlainObjectBase<DerivedS> & S)
  114. {
  115. if(F.rows() < 100)
  116. {
  117. // Super naive
  118. const auto & shoot_ray = [&V,&F](
  119. const Eigen::Vector3f& _s,
  120. const Eigen::Vector3f& dir)->double
  121. {
  122. Eigen::Vector3f s = _s+1e-4*dir;
  123. igl::Hit hit;
  124. if(ray_mesh_intersect(s,dir,V,F,hit))
  125. {
  126. return hit.t;
  127. }else
  128. {
  129. return std::numeric_limits<double>::infinity();
  130. }
  131. };
  132. return shape_diameter_function(shoot_ray,P,N,num_samples,S);
  133. }
  134. AABB<DerivedV,3> aabb;
  135. aabb.init(V,F);
  136. return shape_diameter_function(aabb,V,F,P,N,num_samples,S);
  137. }
  138. #ifdef IGL_STATIC_LIBRARY
  139. // Explicit template instantiation
  140. template void igl::shape_diameter_function<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(std::function<double (Eigen::Matrix<float, 3, 1, 0, 3, 1> const&, Eigen::Matrix<float, 3, 1, 0, 3, 1> const&)> const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  141. template void igl::shape_diameter_function<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(std::function<double (Eigen::Matrix<float, 3, 1, 0, 3, 1> const&, Eigen::Matrix<float, 3, 1, 0, 3, 1> const&)> const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> > const&, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  142. template void igl::shape_diameter_function<Eigen::Matrix<double, 1, 3, 1, 1, 3>, Eigen::Matrix<double, 1, 3, 1, 1, 3>, Eigen::Matrix<double, -1, 1, 0, -1, 1> >(std::function<double (Eigen::Matrix<float, 3, 1, 0, 3, 1> const&, Eigen::Matrix<float, 3, 1, 0, 3, 1> const&)> const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 1, 3, 1, 1, 3> > const&, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> >&);
  143. template void igl::shape_diameter_function<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(std::function<double (Eigen::Matrix<float, 3, 1, 0, 3, 1> const&, Eigen::Matrix<float, 3, 1, 0, 3, 1> const&)> const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, int, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
  144. #endif