fast_winding_number.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. #include "fast_winding_number.h"
  2. #include "build_octree.h"
  3. #include "knn_octree.h"
  4. #include "parallel_for.h"
  5. #include "copyleft/cgal/point_areas_and_normals.h"
  6. #include <vector>
  7. namespace igl {
  8. template <typename DerivedP, typename DerivedA, typename DerivedN,
  9. typename Index, typename DerivedCM, typename DerivedR, typename DerivedEC>
  10. IGL_INLINE void fast_winding_number(const Eigen::MatrixBase<DerivedP>& P,
  11. const Eigen::MatrixBase<DerivedN>& N,
  12. const Eigen::MatrixBase<DerivedA>& A,
  13. const std::vector<std::vector<Index> > & point_indices,
  14. const std::vector<Eigen::Matrix<Index,8,1>,
  15. Eigen::aligned_allocator<Eigen::Matrix<Index,8,1> > > & children,
  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 = children.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,&children,&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(children.at(index)(0) != -1)
  93. {
  94. for(int i = 0; i < 8; i++){
  95. int child = children.at(index)(i);
  96. helper(child);
  97. }
  98. }
  99. };
  100. helper(0);
  101. }
  102. template <typename DerivedP, typename DerivedA, typename DerivedN,
  103. typename Index, typename DerivedCM, typename DerivedR, typename DerivedEC,
  104. typename DerivedQ, typename BetaType, typename DerivedWN>
  105. IGL_INLINE void fast_winding_number(const Eigen::MatrixBase<DerivedP>& P,
  106. const Eigen::MatrixBase<DerivedN>& N,
  107. const Eigen::MatrixBase<DerivedA>& A,
  108. const std::vector<std::vector<Index> > & point_indices,
  109. const std::vector<Eigen::Matrix<Index,8,1>,
  110. Eigen::aligned_allocator<Eigen::Matrix<Index,8,1> > > & children,
  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. ){
  118. typedef typename DerivedP::Scalar real_p;
  119. typedef typename DerivedN::Scalar real_n;
  120. typedef typename DerivedA::Scalar real_a;
  121. typedef typename DerivedCM::Scalar real_cm;
  122. typedef typename DerivedR::Scalar real_r;
  123. typedef typename DerivedEC::Scalar real_ec;
  124. typedef typename DerivedQ::Scalar real_q;
  125. typedef typename DerivedWN::Scalar real_wn;
  126. typedef Eigen::Matrix<real_q,1,3> RowVec;
  127. typedef Eigen::Matrix<real_ec,3,3> EC_3by3;
  128. auto direct_eval = [](const RowVec & loc,
  129. const Eigen::Matrix<real_ec,1,3> & anorm){
  130. real_wn wn = (loc(0)*anorm(0)+loc(1)*anorm(1)+loc(2)*anorm(2))
  131. /(4.0*M_PI*std::pow(loc.norm(),3));
  132. if(std::isnan(wn)){
  133. return 0.5;
  134. }else{
  135. return wn;
  136. }
  137. };
  138. auto expansion_eval = [&direct_eval](const RowVec & loc,
  139. const Eigen::RowVectorXd & EC){
  140. real_wn wn = direct_eval(loc,EC.head<3>());
  141. double r = loc.norm();
  142. if(EC.size()>3){
  143. Eigen::Matrix<real_ec,3,3> SecondDerivative =
  144. Eigen::Matrix<real_ec,3,3>::Identity()/(4.0*M_PI*std::pow(r,3));
  145. SecondDerivative += -3.0*loc.transpose()*loc/(4.0*M_PI*std::pow(r,5));
  146. Eigen::Matrix<real_ec,1,9> derivative_vector =
  147. Eigen::Map<Eigen::Matrix<real_ec,1,9> >(SecondDerivative.data(),
  148. SecondDerivative.size());
  149. wn += derivative_vector.cwiseProduct(EC.segment<9>(3)).sum();
  150. }
  151. if(EC.size()>3+9){
  152. Eigen::Matrix<real_ec,3,3> ThirdDerivative;
  153. for(int i = 0; i < 3; i++){
  154. ThirdDerivative =
  155. 15.0*loc(i)*loc.transpose()*loc/(4.0*M_PI*std::pow(r,7));
  156. Eigen::Matrix<real_ec,3,3> Diagonal;
  157. Diagonal << loc(i), 0, 0,
  158. 0, loc(i), 0,
  159. 0, 0, loc(i);
  160. Eigen::Matrix<real_ec,3,3> RowCol;
  161. RowCol.setZero(3,3);
  162. RowCol.row(i) = loc;
  163. RowCol = RowCol + RowCol.transpose();
  164. ThirdDerivative +=
  165. -3.0/(4.0*M_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,&children,
  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(children.at(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 = children.at(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(children.at(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. typedef typename Eigen::Matrix<real,1,3> RowVec;
  252. std::vector<std::vector<int> > point_indices;
  253. std::vector<Eigen::Matrix<int,8,1>,
  254. Eigen::aligned_allocator<Eigen::Matrix<int,8,1> > > children;
  255. std::vector<RowVec, Eigen::aligned_allocator<RowVec> > centers;
  256. std::vector<real> widths;
  257. Eigen::MatrixXi I;
  258. Eigen::MatrixXd NotUsed;
  259. build_octree(P,point_indices,children,centers,widths);
  260. Eigen::MatrixXd EC, CM;
  261. Eigen::VectorXd R;
  262. fast_winding_number(P,N,A,point_indices,children,
  263. expansion_order,CM,R,EC);
  264. fast_winding_number(P,N,A,point_indices,children,CM,R,EC,Q,beta,WN);
  265. }
  266. template <typename DerivedP, typename DerivedA, typename DerivedN,
  267. typename DerivedQ, typename DerivedWN>
  268. IGL_INLINE void fast_winding_number(const Eigen::MatrixBase<DerivedP>& P,
  269. const Eigen::MatrixBase<DerivedN>& N,
  270. const Eigen::MatrixBase<DerivedA>& A,
  271. const Eigen::MatrixBase<DerivedQ>& Q,
  272. Eigen::PlainObjectBase<DerivedWN>& WN
  273. ){
  274. fast_winding_number(P,N,A,Q,2,2.0,WN);
  275. }
  276. template <typename DerivedP, typename DerivedN, typename DerivedQ,
  277. typename BetaType, typename DerivedWN>
  278. IGL_INLINE void fast_winding_number(const Eigen::MatrixBase<DerivedP>& P,
  279. const Eigen::MatrixBase<DerivedN>& N,
  280. const Eigen::MatrixBase<DerivedQ>& Q,
  281. const int expansion_order,
  282. const BetaType beta,
  283. Eigen::PlainObjectBase<DerivedWN>& WN
  284. ){
  285. typedef typename DerivedWN::Scalar real;
  286. typedef typename Eigen::Matrix<real,1,3> RowVec;
  287. std::vector<std::vector<int> > point_indices;
  288. std::vector<Eigen::Matrix<int,8,1>,
  289. Eigen::aligned_allocator<Eigen::Matrix<int,8,1> > > children;
  290. std::vector<RowVec, Eigen::aligned_allocator<RowVec> > centers;
  291. std::vector<real> widths;
  292. Eigen::MatrixXi I;
  293. Eigen::Matrix<real,Eigen::Dynamic,Eigen::Dynamic> NotUsed;
  294. Eigen::Matrix<real,Eigen::Dynamic,1> A;
  295. build_octree(P,point_indices,children,centers,widths);
  296. knn_octree(P,21,point_indices,children,centers,widths,I);
  297. copyleft::cgal::point_areas_and_normals(P,I,N,A,NotUsed);
  298. Eigen::MatrixXd EC, CM;
  299. Eigen::VectorXd R;
  300. fast_winding_number(P,N,A,point_indices,children,expansion_order,CM,R,EC);
  301. fast_winding_number(P,N,A,point_indices,children,CM,R,EC,Q,beta,WN);
  302. }
  303. template <typename DerivedP, typename DerivedN,
  304. typename DerivedQ, typename DerivedWN>
  305. IGL_INLINE void fast_winding_number(
  306. const Eigen::MatrixBase<DerivedP>& P,
  307. const Eigen::MatrixBase<DerivedN>& N,
  308. const Eigen::MatrixBase<DerivedQ>& Q,
  309. Eigen::PlainObjectBase<DerivedWN>& WN
  310. ){
  311. fast_winding_number(P,N,Q,2,2.0,WN);
  312. }
  313. }