bone_visible.cpp 5.1 KB

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