blue_noise.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2020 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 "blue_noise.h"
  9. #include "doublearea.h"
  10. #include "random_points_on_mesh.h"
  11. #include "sortrows.h"
  12. #include "placeholders.h"
  13. #include "PI.h"
  14. #include "get_seconds.h"
  15. #include <unordered_map>
  16. #include <algorithm>
  17. #include <vector>
  18. #include <random>
  19. #include <cstdint>
  20. namespace igl
  21. {
  22. // It is very important that we use 64bit keys to avoid out of bounds (easy to
  23. // get to happen with dense samplings (e.g., r = 0.0005*bbd)
  24. typedef std::int64_t BlueNoiseKeyType;
  25. }
  26. // Helper functions
  27. namespace igl
  28. {
  29. // Should probably find and replace with less generic name
  30. //
  31. // Map 3D subscripts (x,y,z) to unique index (return value)
  32. //
  33. // Inputs:
  34. // w side length of w×w×w integer cube lattice
  35. // x subscript along x direction
  36. // y subscript along y direction
  37. // z subscript along z direction
  38. // Returns index value
  39. //
  40. inline BlueNoiseKeyType blue_noise_key(
  41. const BlueNoiseKeyType w, // pass by copy --> int64_t so that multiplication is OK
  42. const BlueNoiseKeyType x, // pass by copy --> int64_t so that multiplication is OK
  43. const BlueNoiseKeyType y, // pass by copy --> int64_t so that multiplication is OK
  44. const BlueNoiseKeyType z) // pass by copy --> int64_t so that multiplication is OK
  45. {
  46. return x+w*(y+w*z);
  47. }
  48. // Determine if a query candidate at position X.row(i) is too close to already
  49. // selected sites (stored in S).
  50. //
  51. // Inputs:
  52. // X #X by 3 list of raw candidate positions
  53. // Xs #Xs by 3 list of corresponding integer cell subscripts
  54. // i index of candidate in question
  55. // S map from cell index to index into X of selected candidate (or -1 if
  56. // cell is currently empty)
  57. // rr Poisson disk radius squared
  58. // w side length of w×w×w integer cube lattice (into which Xs subscripts)
  59. template <
  60. typename DerivedX,
  61. typename DerivedXs>
  62. inline bool blue_noise_far_enough(
  63. const Eigen::MatrixBase<DerivedX> & X,
  64. const Eigen::MatrixBase<DerivedXs> & Xs,
  65. const std::unordered_map<BlueNoiseKeyType,int> & S,
  66. const double & rr,
  67. const int & w,
  68. const int i)
  69. {
  70. const int xi = Xs(i,0);
  71. const int yi = Xs(i,1);
  72. const int zi = Xs(i,2);
  73. int g = 2; // ceil(r/s)
  74. for(int x = std::max(xi-g,0);x<=std::min(xi+g,w-1);x++)
  75. for(int y = std::max(yi-g,0);y<=std::min(yi+g,w-1);y++)
  76. for(int z = std::max(zi-g,0);z<=std::min(zi+g,w-1);z++)
  77. {
  78. if(x!=xi || y!=yi || z!=zi)
  79. {
  80. const BlueNoiseKeyType nk = blue_noise_key(w,x,y,z);
  81. // have already selected from this cell
  82. const auto Siter = S.find(nk);
  83. if(Siter !=S.end() && Siter->second >= 0)
  84. {
  85. const int ni = Siter->second;
  86. // too close
  87. if( (X.row(i)-X.row(ni)).squaredNorm() < rr)
  88. {
  89. return false;
  90. }
  91. }
  92. }
  93. }
  94. return true;
  95. }
  96. // Try to activate a candidate in a given cell
  97. //
  98. // Inputs:
  99. // X #X by 3 list of raw candidate positions
  100. // Xs #Xs by 3 list of corresponding integer cell subscripts
  101. // rr Poisson disk radius squared
  102. // w side length of w×w×w integer cube lattice (into which Xs subscripts)
  103. // nk index of cell in which we'd like to activate a candidate
  104. // M map from cell index to list of candidates
  105. // S map from cell index to index into X of selected candidate (or -1 if
  106. // cell is currently empty)
  107. // active list of indices into X of active candidates
  108. // Outputs:
  109. // M visited candidates deemed too close to already selected points are
  110. // removed
  111. // S updated to reflect activated point (if successful)
  112. // active updated to reflect activated point (if successful)
  113. // Returns true iff activation was successful
  114. template <
  115. typename DerivedX,
  116. typename DerivedXs>
  117. inline bool activate(
  118. const Eigen::MatrixBase<DerivedX> & X,
  119. const Eigen::MatrixBase<DerivedXs> & Xs,
  120. const double & rr,
  121. const int & i,
  122. const int & w,
  123. const BlueNoiseKeyType & nk,
  124. std::unordered_map<BlueNoiseKeyType,std::vector<int> > & M,
  125. std::unordered_map<BlueNoiseKeyType,int> & S,
  126. std::vector<int> & active)
  127. {
  128. assert(M.count(nk));
  129. auto & Mvec = M.find(nk)->second;
  130. auto miter = Mvec.begin();
  131. while(miter != Mvec.end())
  132. {
  133. const int mi = *miter;
  134. // mi is our candidate sample. Is it far enough from all existing
  135. // samples?
  136. if(i>=0 && (X.row(i)-X.row(mi)).squaredNorm() > 4.*rr)
  137. {
  138. // too far skip (reject)
  139. miter++;
  140. } else if(blue_noise_far_enough(X,Xs,S,rr,w,mi))
  141. {
  142. active.push_back(mi);
  143. S.find(nk)->second = mi;
  144. //printf(" found %d\n",mi);
  145. return true;
  146. }else
  147. {
  148. // remove forever (instead of incrementing we swap and eat from the
  149. // back)
  150. //std::swap(*miter,Mvec.back());
  151. *miter = Mvec.back();
  152. bool was_last = (std::next(miter) == Mvec.end());
  153. Mvec.pop_back();
  154. if (was_last) {
  155. // popping from the vector can invalidate the iterator, if it was
  156. // pointing to the last element that was popped. Alternatively,
  157. // one could use indices directly...
  158. miter = Mvec.end();
  159. }
  160. }
  161. }
  162. return false;
  163. }
  164. template <
  165. typename DerivedX,
  166. typename DerivedXs,
  167. typename URBG>
  168. inline bool step(
  169. const Eigen::MatrixBase<DerivedX> & X,
  170. const Eigen::MatrixBase<DerivedXs> & Xs,
  171. const double & rr,
  172. const int & w,
  173. URBG && urbg,
  174. std::unordered_map<BlueNoiseKeyType,std::vector<int> > & M,
  175. std::unordered_map<BlueNoiseKeyType,int> & S,
  176. std::vector<int> & active,
  177. std::vector<int> & collected
  178. )
  179. {
  180. //considered.clear();
  181. if(active.size() == 0) return false;
  182. // random entry
  183. std::uniform_int_distribution<> dis(0, active.size()-1);
  184. const int e = dis(urbg);
  185. const int i = active[e];
  186. //printf("%d\n",i);
  187. const int xi = Xs(i,0);
  188. const int yi = Xs(i,1);
  189. const int zi = Xs(i,2);
  190. //printf("%d %d %d - %g %g %g\n",xi,yi,zi,X(i,0),X(i,1),X(i,2));
  191. // cell indices of neighbors
  192. int g = 4;
  193. std::vector<BlueNoiseKeyType> N;N.reserve((1+g*1)^3-1);
  194. for(int x = std::max(xi-g,0);x<=std::min(xi+g,w-1);x++)
  195. for(int y = std::max(yi-g,0);y<=std::min(yi+g,w-1);y++)
  196. for(int z = std::max(zi-g,0);z<=std::min(zi+g,w-1);z++)
  197. {
  198. if(x!=xi || y!=yi || z!=zi)
  199. {
  200. //printf(" %d %d %d\n",x,y,z);
  201. const BlueNoiseKeyType nk = blue_noise_key(w,x,y,z);
  202. // haven't yet selected from this cell?
  203. const auto Siter = S.find(nk);
  204. if(Siter !=S.end() && Siter->second < 0)
  205. {
  206. assert(M.find(nk) != M.end());
  207. N.emplace_back(nk);
  208. }
  209. }
  210. }
  211. //printf(" --------\n");
  212. // randomize order: this might be a little paranoid...
  213. std::shuffle(std::begin(N), std::end(N), urbg);
  214. bool found = false;
  215. for(const BlueNoiseKeyType & nk : N)
  216. {
  217. assert(M.find(nk) != M.end());
  218. if(activate(X,Xs,rr,i,w,nk,M,S,active))
  219. {
  220. found = true;
  221. break;
  222. }
  223. }
  224. if(!found)
  225. {
  226. // remove i from active list
  227. // https://stackoverflow.com/a/60765833/148668
  228. collected.push_back(i);
  229. //printf(" before: "); for(const int j : active) { printf("%d ",j); } printf("\n");
  230. std::swap(active[e], active.back());
  231. //printf(" after : "); for(const int j : active) { printf("%d ",j); } printf("\n");
  232. active.pop_back();
  233. //printf(" removed %d\n",i);
  234. }
  235. //printf(" active: "); for(const int j : active) { printf("%d ",j); } printf("\n");
  236. return true;
  237. }
  238. }
  239. template <
  240. typename DerivedV,
  241. typename DerivedF,
  242. typename DerivedB,
  243. typename DerivedFI,
  244. typename DerivedP,
  245. typename URBG>
  246. IGL_INLINE void igl::blue_noise(
  247. const Eigen::MatrixBase<DerivedV> & V,
  248. const Eigen::MatrixBase<DerivedF> & F,
  249. const typename DerivedV::Scalar r,
  250. Eigen::PlainObjectBase<DerivedB> & B,
  251. Eigen::PlainObjectBase<DerivedFI> & FI,
  252. Eigen::PlainObjectBase<DerivedP> & P,
  253. URBG && urbg)
  254. {
  255. typedef typename DerivedV::Scalar Scalar;
  256. // float+RowMajor is faster...
  257. typedef Eigen::Matrix<Scalar,Eigen::Dynamic,3,Eigen::RowMajor> MatrixX3S;
  258. assert(V.cols() == 3 && "Only 3D embeddings allowed");
  259. // minimum radius
  260. const Scalar min_r = r;
  261. // cell size based on 3D distance
  262. // It works reasonably well (but is probably biased to use s=2*r/√3 here and
  263. // g=1 in the outer loop below.
  264. //
  265. // One thing to try would be to store a list in S (rather than a single point)
  266. // or equivalently a mask over M and just use M as a generic spatial hash
  267. // (with arbitrary size) and then tune its size (being careful to make g a
  268. // function of r and s; and removing the `if S=-1 checks`)
  269. const Scalar s = r/sqrt(3.0);
  270. const double area =
  271. [&](){Eigen::VectorXd A;igl::doublearea(V,F,A);return A.array().sum()/2;}();
  272. // Circle packing in the plane has igl::PI*sqrt(3)/6 efficiency
  273. const double expected_number_of_points =
  274. area * (igl::PI * sqrt(3.0) / 6.0) / (igl::PI * min_r * min_r / 4.0);
  275. // Make a uniform random sampling with 30*expected_number_of_points.
  276. const int nx = 30.0*expected_number_of_points;
  277. MatrixX3S X,XB;
  278. Eigen::VectorXi XFI;
  279. igl::random_points_on_mesh(nx,V,F,XB,XFI,X,urbg);
  280. // Rescale so that s = 1
  281. Eigen::Matrix<int,Eigen::Dynamic,3,Eigen::RowMajor> Xs =
  282. ((X.rowwise()-X.colwise().minCoeff())/s).template cast<int>();
  283. const int w = Xs.maxCoeff()+1;
  284. {
  285. Eigen::VectorXi I;
  286. igl::sortrows(decltype(Xs)(Xs),true,Xs,I);
  287. X = X(I,igl::placeholders::all).eval();
  288. // These two could be spun off in their own thread.
  289. XB = XB(I,igl::placeholders::all).eval();
  290. XFI = XFI(I,igl::placeholders::all).eval();
  291. }
  292. // Initialization
  293. std::unordered_map<BlueNoiseKeyType,std::vector<int> > M;
  294. std::unordered_map<BlueNoiseKeyType, int > S;
  295. // attempted to seed
  296. std::unordered_map<BlueNoiseKeyType, int > A;
  297. // Q: Too many?
  298. // A: Seems to help though.
  299. M.reserve(Xs.rows());
  300. S.reserve(Xs.rows());
  301. for(int i = 0;i<Xs.rows();i++)
  302. {
  303. BlueNoiseKeyType k = blue_noise_key(w,Xs(i,0),Xs(i,1),Xs(i,2));
  304. const auto Miter = M.find(k);
  305. if(Miter == M.end())
  306. {
  307. M.insert({k,{i}});
  308. }else
  309. {
  310. Miter->second.push_back(i);
  311. }
  312. S.emplace(k,-1);
  313. A.emplace(k,false);
  314. }
  315. std::vector<int> active;
  316. // precompute r²
  317. // Q: is this necessary?
  318. const double rr = r*r;
  319. std::vector<int> collected;
  320. collected.reserve(2.0*expected_number_of_points);
  321. auto Mouter = M.begin();
  322. // Just take the first point as the initial seed
  323. const auto initialize = [&]()->bool
  324. {
  325. while(true)
  326. {
  327. if(Mouter == M.end())
  328. {
  329. return false;
  330. }
  331. const BlueNoiseKeyType k = Mouter->first;
  332. // Haven't placed in this cell yet
  333. if(S[k]<0)
  334. {
  335. if(activate(X,Xs,rr,-1,w,k,M,S,active)) return true;
  336. }
  337. Mouter++;
  338. }
  339. assert(false && "should not be reachable.");
  340. };
  341. // important if mesh contains many connected components
  342. while(initialize())
  343. {
  344. while(active.size()>0)
  345. {
  346. step(X,Xs,rr,w,urbg,M,S,active,collected);
  347. }
  348. }
  349. {
  350. const int n = collected.size();
  351. P.resize(n,3);
  352. B.resize(n,3);
  353. FI.resize(n);
  354. for(int i = 0;i<n;i++)
  355. {
  356. const int c = collected[i];
  357. P.row(i) = X.row(c).template cast<typename DerivedP::Scalar>();
  358. B.row(i) = XB.row(c).template cast<typename DerivedB::Scalar>();
  359. FI(i) = XFI(c);
  360. }
  361. }
  362. }
  363. #ifdef IGL_STATIC_LIBRARY
  364. // Explicit template instantiation
  365. template void igl::blue_noise<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<int, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, std::mt19937_64 >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::Matrix<double, -1, -1, 0, -1, -1>::Scalar, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, std::mt19937_64&&);
  366. template void igl::blue_noise<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<int, -1, 1, 0, -1, 1>, Eigen::Matrix<double, -1, -1, 0, -1, -1>, std::mt19937 >(Eigen::MatrixBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> > const&, Eigen::MatrixBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> > const&, Eigen::Matrix<double, -1, -1, 0, -1, -1>::Scalar, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 1, 0, -1, 1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, std::mt19937&&);
  367. #endif