bone_visible.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 Alec Jacobson <[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 "bone_visible.h"
  9. #include "../project_to_line.h"
  10. #include "../EPS.h"
  11. #include "../Hit.h"
  12. #include "../parallel_for.h"
  13. #include "../Timer.h"
  14. #include <iostream>
  15. template <
  16. typename DerivedV,
  17. typename DerivedF,
  18. typename DerivedSD,
  19. typename Derivedflag>
  20. IGL_INLINE void igl::embree::bone_visible(
  21. const Eigen::MatrixBase<DerivedV> & V,
  22. const Eigen::MatrixBase<DerivedF> & F,
  23. const Eigen::MatrixBase<DerivedSD> & s,
  24. const Eigen::MatrixBase<DerivedSD> & d,
  25. Eigen::PlainObjectBase<Derivedflag> & flag)
  26. {
  27. // "double sided lighting"
  28. Eigen::Matrix<typename DerivedF::Scalar,Eigen::Dynamic,Eigen::Dynamic> FF;
  29. FF.resize(F.rows()*2,F.cols());
  30. FF << F, F.rowwise().reverse();
  31. // Initialize intersector
  32. EmbreeIntersector ei;
  33. ei.init(V.template cast<float>(),FF.template cast<int>());
  34. return bone_visible(V,F,ei,s,d,flag);
  35. }
  36. template <
  37. typename DerivedV,
  38. typename DerivedF,
  39. typename DerivedSD,
  40. typename Derivedflag>
  41. IGL_INLINE void igl::embree::bone_visible(
  42. const Eigen::MatrixBase<DerivedV> & V,
  43. const Eigen::MatrixBase<DerivedF> & F,
  44. const EmbreeIntersector & ei,
  45. const Eigen::MatrixBase<DerivedSD> & s,
  46. const Eigen::MatrixBase<DerivedSD> & d,
  47. Eigen::PlainObjectBase<Derivedflag> & flag)
  48. {
  49. flag.resize(V.rows());
  50. const double sd_norm = (s-d).norm();
  51. // Embree seems to be parallel when constructing but not when tracing rays
  52. // loop over mesh vertices
  53. parallel_for(V.rows(),[&](const int v)
  54. {
  55. const Eigen::Vector3d Vv = V.row(v);
  56. // Project vertex v onto line segment sd
  57. //embree.intersectSegment
  58. double t,sqrd;
  59. Eigen::Vector3d projv;
  60. // degenerate bone, just snap to s
  61. if(sd_norm < DOUBLE_EPS)
  62. {
  63. t = 0;
  64. sqrd = (Vv-s).array().pow(2).sum();
  65. projv = s;
  66. }else
  67. {
  68. // project onto (infinite) line
  69. project_to_line(
  70. Vv(0),Vv(1),Vv(2),s(0),s(1),s(2),d(0),d(1),d(2),
  71. projv(0),projv(1),projv(2),t,sqrd);
  72. // handle projections past endpoints
  73. if(t<0)
  74. {
  75. t = 0;
  76. sqrd = (Vv-s).array().pow(2).sum();
  77. projv = s;
  78. } else if(t>1)
  79. {
  80. t = 1;
  81. sqrd = (Vv-d).array().pow(2).sum();
  82. projv = d;
  83. }
  84. }
  85. // This could use double, but I'm not sure this function is popular enough
  86. // to spend time fixing.
  87. igl::Hit<float> hit;
  88. // perhaps 1.0 should be 1.0-epsilon, or actually since we checking the
  89. // incident face, perhaps 1.0 should be 1.0+eps
  90. const Eigen::Vector3d dir = (Vv-projv)*1.0;
  91. if(ei.intersectSegment(
  92. projv.template cast<float>(),
  93. dir.template cast<float>(),
  94. hit))
  95. {
  96. // mod for double sided lighting
  97. const int fi = hit.id % F.rows();
  98. //if(v == 1228-1)
  99. //{
  100. // Vector3d bc,P;
  101. // bc << 1 - hit.u - hit.v, hit.u, hit.v; // barycentric
  102. // P = V.row(F(fi,0))*bc(0) +
  103. // V.row(F(fi,1))*bc(1) +
  104. // V.row(F(fi,2))*bc(2);
  105. // std::cout<<(fi+1)<<std::endl;
  106. // std::cout<<bc.transpose()<<std::endl;
  107. // std::cout<<P.transpose()<<std::endl;
  108. // std::cout<<hit.t<<std::endl;
  109. // std::cout<<(projv + dir*hit.t).transpose()<<std::endl;
  110. // std::cout<<Vv.transpose()<<std::endl;
  111. //}
  112. // Assume hit is valid, so not visible
  113. flag(v) = false;
  114. // loop around corners of triangle
  115. for(int c = 0;c<F.cols();c++)
  116. {
  117. if(F(fi,c) == v)
  118. {
  119. // hit self, so no hits before, so vertex v is visible
  120. flag(v) = true;
  121. break;
  122. }
  123. }
  124. // Hit is actually past v
  125. if(!flag(v) && (hit.t*hit.t*dir.squaredNorm())>sqrd)
  126. {
  127. flag(v) = true;
  128. }
  129. }else
  130. {
  131. // no hit so vectex v is visible
  132. flag(v) = true;
  133. }
  134. },10000);
  135. }
  136. #ifdef IGL_STATIC_LIBRARY
  137. // Explicit template instantiation
  138. template void igl::embree::bone_visible<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, 3, 1, 0, 3, 1>, Eigen::Matrix<bool, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> > const&, Eigen::MatrixBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<bool, -1, 1, 0, -1, 1> >&);
  139. template void igl::embree::bone_visible<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, 1, 0, -1, 1>, Eigen::Matrix<bool, -1, 1, 0, -1, 1> >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -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<bool, -1, 1, 0, -1, 1> >&);
  140. #endif