fast_winding_number.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. #include "fast_winding_number.h"
  2. #include "octree.h"
  3. #include "knn.h"
  4. #include "parallel_for.h"
  5. #include "PI.h"
  6. #include <vector>
  7. namespace igl {
  8. template <typename DerivedP, typename DerivedA, typename DerivedN,
  9. typename Index, typename DerivedCH, typename DerivedCM, typename DerivedR,
  10. typename DerivedEC>
  11. IGL_INLINE void fast_winding_number(const Eigen::MatrixBase<DerivedP>& P,
  12. const Eigen::MatrixBase<DerivedN>& N,
  13. const Eigen::MatrixBase<DerivedA>& A,
  14. const std::vector<std::vector<Index> > & point_indices,
  15. const Eigen::MatrixBase<DerivedCH>& CH,
  16. const int expansion_order,
  17. Eigen::PlainObjectBase<DerivedCM>& CM,
  18. Eigen::PlainObjectBase<DerivedR>& R,
  19. Eigen::PlainObjectBase<DerivedEC>& EC)
  20. {
  21. typedef typename DerivedP::Scalar real_p;
  22. typedef typename DerivedN::Scalar real_n;
  23. typedef typename DerivedA::Scalar real_a;
  24. typedef typename DerivedCM::Scalar real_cm;
  25. typedef typename DerivedR::Scalar real_r;
  26. typedef typename DerivedEC::Scalar real_ec;
  27. typedef Eigen::Matrix<real_p,1,3> RowVec3p;
  28. int m = CH.size();
  29. int num_terms;
  30. assert(expansion_order < 3 && expansion_order >= 0 && "m must be less than n");
  31. if(expansion_order == 0){
  32. num_terms = 3;
  33. } else if(expansion_order ==1){
  34. num_terms = 3 + 9;
  35. } else if(expansion_order == 2){
  36. num_terms = 3 + 9 + 27;
  37. }
  38. R.resize(m);
  39. CM.resize(m,3);
  40. EC.resize(m,num_terms);
  41. EC.setZero(m,num_terms);
  42. std::function< void(const int) > helper;
  43. helper = [&helper,
  44. &P,&N,&A,&expansion_order,&point_indices,&CH,&EC,&R,&CM]
  45. (const int index)-> void
  46. {
  47. Eigen::Matrix<real_cm,1,3> masscenter;
  48. masscenter << 0,0,0;
  49. Eigen::Matrix<real_ec,1,3> zeroth_expansion;
  50. zeroth_expansion << 0,0,0;
  51. real_p areatotal = 0.0;
  52. for(int j = 0; j < point_indices.at(index).size(); j++){
  53. int curr_point_index = point_indices.at(index).at(j);
  54. areatotal += A(curr_point_index);
  55. masscenter += A(curr_point_index)*P.row(curr_point_index);
  56. zeroth_expansion += A(curr_point_index)*N.row(curr_point_index);
  57. }
  58. masscenter = masscenter/areatotal;
  59. CM.row(index) = masscenter;
  60. EC.block(index,0,1,3) = zeroth_expansion;
  61. real_r max_norm = 0;
  62. real_r curr_norm;
  63. for(int i = 0; i < point_indices.at(index).size(); i++){
  64. //Get max distance from center of mass:
  65. int curr_point_index = point_indices.at(index).at(i);
  66. Eigen::Matrix<real_r,1,3> point =
  67. P.row(curr_point_index)-masscenter;
  68. curr_norm = point.norm();
  69. if(curr_norm > max_norm){
  70. max_norm = curr_norm;
  71. }
  72. //Calculate higher order terms if necessary
  73. Eigen::Matrix<real_ec,3,3> TempCoeffs;
  74. if(EC.cols() >= (3+9)){
  75. TempCoeffs = A(curr_point_index)*point.transpose()*
  76. N.row(curr_point_index);
  77. EC.block(index,3,1,9) +=
  78. Eigen::Map<Eigen::Matrix<real_ec,1,9> >(TempCoeffs.data(),
  79. TempCoeffs.size());
  80. }
  81. if(EC.cols() == (3+9+27)){
  82. for(int k = 0; k < 3; k++){
  83. TempCoeffs = 0.5 * point(k) * (A(curr_point_index)*
  84. point.transpose()*N.row(curr_point_index));
  85. EC.block(index,12+9*k,1,9) += Eigen::Map<
  86. Eigen::Matrix<real_ec,1,9> >(TempCoeffs.data(),
  87. TempCoeffs.size());
  88. }
  89. }
  90. }
  91. R(index) = max_norm;
  92. if(CH(index,0) != -1)
  93. {
  94. for(int i = 0; i < 8; i++){
  95. int child = CH(index,i);
  96. helper(child);
  97. }
  98. }
  99. };
  100. helper(0);
  101. }
  102. template <typename DerivedP, typename DerivedA, typename DerivedN,
  103. typename Index, typename DerivedCH, typename DerivedCM, typename DerivedR,
  104. typename DerivedEC, typename DerivedQ, typename BetaType,
  105. typename DerivedWN>
  106. IGL_INLINE void fast_winding_number(const Eigen::MatrixBase<DerivedP>& P,
  107. const Eigen::MatrixBase<DerivedN>& N,
  108. const Eigen::MatrixBase<DerivedA>& A,
  109. const std::vector<std::vector<Index> > & point_indices,
  110. const Eigen::MatrixBase<DerivedCH>& CH,
  111. const Eigen::MatrixBase<DerivedCM>& CM,
  112. const Eigen::MatrixBase<DerivedR>& R,
  113. const Eigen::MatrixBase<DerivedEC>& EC,
  114. const Eigen::MatrixBase<DerivedQ>& Q,
  115. const BetaType beta,
  116. Eigen::PlainObjectBase<DerivedWN>& WN){
  117. typedef typename DerivedP::Scalar real_p;
  118. typedef typename DerivedN::Scalar real_n;
  119. typedef typename DerivedA::Scalar real_a;
  120. typedef typename DerivedCM::Scalar real_cm;
  121. typedef typename DerivedR::Scalar real_r;
  122. typedef typename DerivedEC::Scalar real_ec;
  123. typedef typename DerivedQ::Scalar real_q;
  124. typedef typename DerivedWN::Scalar real_wn;
  125. typedef Eigen::Matrix<real_q,1,3> RowVec;
  126. typedef Eigen::Matrix<real_ec,3,3> EC_3by3;
  127. auto direct_eval = [](const RowVec & loc,
  128. const Eigen::Matrix<real_ec,1,3> & anorm){
  129. real_wn wn = (loc(0)*anorm(0)+loc(1)*anorm(1)+loc(2)*anorm(2))
  130. /(4.0*igl::PI*std::pow(loc.norm(),3));
  131. if(std::isnan(wn)){
  132. return 0.5;
  133. }else{
  134. return wn;
  135. }
  136. };
  137. auto expansion_eval = [&direct_eval](const RowVec & loc,
  138. const Eigen::RowVectorXd & EC){
  139. real_wn wn = direct_eval(loc,EC.head<3>());
  140. double r = loc.norm();
  141. if(EC.size()>3){
  142. Eigen::Matrix<real_ec,3,3> SecondDerivative =
  143. Eigen::Matrix<real_ec,3,3>::Identity()/(4.0*igl::PI*std::pow(r,3));
  144. SecondDerivative += -3.0*loc.transpose()*loc/(4.0*igl::PI*std::pow(r,5));
  145. Eigen::Matrix<real_ec,1,9> derivative_vector =
  146. Eigen::Map<Eigen::Matrix<real_ec,1,9> >(SecondDerivative.data(),
  147. SecondDerivative.size());
  148. wn += derivative_vector.cwiseProduct(EC.segment<9>(3)).sum();
  149. }
  150. if(EC.size()>3+9){
  151. Eigen::Matrix<real_ec,3,3> ThirdDerivative;
  152. for(int i = 0; i < 3; i++){
  153. ThirdDerivative =
  154. 15.0*loc(i)*loc.transpose()*loc/(4.0*igl::PI*std::pow(r,7));
  155. Eigen::Matrix<real_ec,3,3> Diagonal;
  156. Diagonal << loc(i), 0, 0,
  157. 0, loc(i), 0,
  158. 0, 0, loc(i);
  159. Eigen::Matrix<real_ec,3,3> RowCol;
  160. RowCol.setZero(3,3);
  161. RowCol.row(i) = loc;
  162. Eigen::Matrix<real_ec,3,3> RowColT = RowCol.transpose();
  163. RowCol = RowCol + RowColT;
  164. ThirdDerivative +=
  165. -3.0/(4.0*igl::PI*std::pow(r,5))*(RowCol+Diagonal);
  166. Eigen::Matrix<real_ec,1,9> derivative_vector =
  167. Eigen::Map<Eigen::Matrix<real_ec,1,9> >(ThirdDerivative.data(),
  168. ThirdDerivative.size());
  169. wn += derivative_vector.cwiseProduct(
  170. EC.segment<9>(12 + i*9)).sum();
  171. }
  172. }
  173. return wn;
  174. };
  175. int m = Q.rows();
  176. WN.resize(m,1);
  177. std::function< real_wn(const RowVec, const std::vector<int>) > helper;
  178. helper = [&helper,
  179. &P,&N,&A,
  180. &point_indices,&CH,
  181. &CM,&R,&EC,&beta,
  182. &direct_eval,&expansion_eval]
  183. (const RowVec query, const std::vector<int> near_indices)-> real_wn
  184. {
  185. std::vector<int> new_near_indices;
  186. real_wn wn = 0;
  187. for(int i = 0; i < near_indices.size(); i++){
  188. int index = near_indices.at(i);
  189. //Leaf Case, Brute force
  190. if(CH(index,0) == -1){
  191. for(int j = 0; j < point_indices.at(index).size(); j++){
  192. int curr_row = point_indices.at(index).at(j);
  193. wn += direct_eval(P.row(curr_row)-query,
  194. N.row(curr_row)*A(curr_row));
  195. }
  196. }
  197. //Non-Leaf Case
  198. else {
  199. for(int child = 0; child < 8; child++){
  200. int child_index = CH(index,child);
  201. if(point_indices.at(child_index).size() > 0){
  202. if((CM.row(child_index)-query).norm() > beta*R(child_index)){
  203. if(CH(child_index,0) == -1){
  204. for(int j=0;j<point_indices.at(child_index).size();j++){
  205. int curr_row = point_indices.at(child_index).at(j);
  206. wn += direct_eval(P.row(curr_row)-query,
  207. N.row(curr_row)*A(curr_row));
  208. }
  209. }else{
  210. wn += expansion_eval(CM.row(child_index)-query,
  211. EC.row(child_index));
  212. }
  213. }else {
  214. new_near_indices.emplace_back(child_index);
  215. }
  216. }
  217. }
  218. }
  219. }
  220. if(new_near_indices.size() > 0){
  221. wn += helper(query,new_near_indices);
  222. }
  223. return wn;
  224. };
  225. if(beta > 0){
  226. std::vector<int> near_indices_start = {0};
  227. igl::parallel_for(m,[&](int iter){
  228. WN(iter) = helper(Q.row(iter),near_indices_start);
  229. },1000);
  230. } else {
  231. igl::parallel_for(m,[&](int iter){
  232. double wn = 0;
  233. for(int j = 0; j <P.rows(); j++){
  234. wn += direct_eval(P.row(j)-Q.row(iter),N.row(j)*A(j));
  235. }
  236. WN(iter) = wn;
  237. },1000);
  238. }
  239. }
  240. template <typename DerivedP, typename DerivedA, typename DerivedN,
  241. typename DerivedQ, typename BetaType, typename DerivedWN>
  242. IGL_INLINE void fast_winding_number(const Eigen::MatrixBase<DerivedP>& P,
  243. const Eigen::MatrixBase<DerivedN>& N,
  244. const Eigen::MatrixBase<DerivedA>& A,
  245. const Eigen::MatrixBase<DerivedQ>& Q,
  246. const int expansion_order,
  247. const BetaType beta,
  248. Eigen::PlainObjectBase<DerivedWN>& WN
  249. ){
  250. typedef typename DerivedWN::Scalar real;
  251. std::vector<std::vector<int> > point_indices;
  252. Eigen::Matrix<int,Eigen::Dynamic,8> CH;
  253. Eigen::Matrix<real,Eigen::Dynamic,3> CN;
  254. Eigen::Matrix<real,Eigen::Dynamic,1> W;
  255. octree(P,point_indices,CH,CN,W);
  256. Eigen::Matrix<real,Eigen::Dynamic,Eigen::Dynamic> EC;
  257. Eigen::Matrix<real,Eigen::Dynamic,3> CM;
  258. Eigen::Matrix<real,Eigen::Dynamic,1> R;
  259. fast_winding_number(P,N,A,point_indices,CH,expansion_order,CM,R,EC);
  260. fast_winding_number(P,N,A,point_indices,CH,CM,R,EC,Q,beta,WN);
  261. }
  262. template <typename DerivedP, typename DerivedA, typename DerivedN,
  263. typename DerivedQ, typename DerivedWN>
  264. IGL_INLINE void fast_winding_number(const Eigen::MatrixBase<DerivedP>& P,
  265. const Eigen::MatrixBase<DerivedN>& N,
  266. const Eigen::MatrixBase<DerivedA>& A,
  267. const Eigen::MatrixBase<DerivedQ>& Q,
  268. Eigen::PlainObjectBase<DerivedWN>& WN
  269. ){
  270. fast_winding_number(P,N,A,Q,2,2.0,WN);
  271. }
  272. }