bone_visible.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2013 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 "bone_visible.h"
  9. #include <igl/project_to_line.h>
  10. #include <igl/EPS.h>
  11. #include <igl/Timer.h>
  12. #include <iostream>
  13. template <
  14. typename DerivedV,
  15. typename DerivedF,
  16. typename DerivedSD,
  17. typename Derivedflag>
  18. IGL_INLINE void igl::embree::bone_visible(
  19. const Eigen::PlainObjectBase<DerivedV> & V,
  20. const Eigen::PlainObjectBase<DerivedF> & F,
  21. const Eigen::PlainObjectBase<DerivedSD> & s,
  22. const Eigen::PlainObjectBase<DerivedSD> & d,
  23. Eigen::PlainObjectBase<Derivedflag> & flag)
  24. {
  25. // "double sided lighting"
  26. Eigen::PlainObjectBase<DerivedF> FF;
  27. FF.resize(F.rows()*2,F.cols());
  28. FF << F, F.rowwise().reverse();
  29. // Initialize intersector
  30. EmbreeIntersector ei;
  31. ei.init(V.template cast<float>(),FF.template cast<int>());
  32. return bone_visible(V,F,ei,s,d,flag);
  33. }
  34. template <
  35. typename DerivedV,
  36. typename DerivedF,
  37. typename DerivedSD,
  38. typename Derivedflag>
  39. IGL_INLINE void igl::embree::bone_visible(
  40. const Eigen::PlainObjectBase<DerivedV> & V,
  41. const Eigen::PlainObjectBase<DerivedF> & F,
  42. const EmbreeIntersector & ei,
  43. const Eigen::PlainObjectBase<DerivedSD> & s,
  44. const Eigen::PlainObjectBase<DerivedSD> & d,
  45. Eigen::PlainObjectBase<Derivedflag> & flag)
  46. {
  47. using namespace igl;
  48. using namespace std;
  49. using namespace Eigen;
  50. flag.resize(V.rows());
  51. const double sd_norm = (s-d).norm();
  52. // Embree seems to be parallel when constructing but not when tracing rays
  53. #pragma omp parallel for
  54. // loop over mesh vertices
  55. for(int v = 0;v<V.rows();v++)
  56. {
  57. const Vector3d Vv = V.row(v);
  58. // Project vertex v onto line segment sd
  59. //embree.intersectSegment
  60. double t,sqrd;
  61. Vector3d projv;
  62. // degenerate bone, just snap to s
  63. if(sd_norm < DOUBLE_EPS)
  64. {
  65. t = 0;
  66. sqrd = (Vv-s).array().pow(2).sum();
  67. projv = s;
  68. }else
  69. {
  70. // project onto (infinite) line
  71. project_to_line(
  72. Vv(0),Vv(1),Vv(2),s(0),s(1),s(2),d(0),d(1),d(2),
  73. projv(0),projv(1),projv(2),t,sqrd);
  74. // handle projections past endpoints
  75. if(t<0)
  76. {
  77. t = 0;
  78. sqrd = (Vv-s).array().pow(2).sum();
  79. projv = s;
  80. } else if(t>1)
  81. {
  82. t = 1;
  83. sqrd = (Vv-d).array().pow(2).sum();
  84. projv = d;
  85. }
  86. }
  87. igl::embree::Hit 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 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. // cout<<(fi+1)<<endl;
  106. // cout<<bc.transpose()<<endl;
  107. // cout<<P.transpose()<<endl;
  108. // cout<<hit.t<<endl;
  109. // cout<<(projv + dir*hit.t).transpose()<<endl;
  110. // cout<<Vv.transpose()<<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. }
  135. }
  136. #ifdef IGL_STATIC_LIBRARY
  137. // Explicit template specialization
  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::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, 3, 1, 0, 3, 1> > const&, Eigen::PlainObjectBase<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::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 1, 0, -1, 1> > const&, Eigen::PlainObjectBase<Eigen::Matrix<bool, -1, 1, 0, -1, 1> >&);
  140. #endif